hcd.c 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703
  1. /*
  2. * hcd.c - DesignWare HS OTG Controller host-mode routines
  3. *
  4. * Copyright (C) 2004-2013 Synopsys, Inc.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions, and the following disclaimer,
  11. * without modification.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The names of the above-listed copyright holders may not be used
  16. * to endorse or promote products derived from this software without
  17. * specific prior written permission.
  18. *
  19. * ALTERNATIVELY, this software may be distributed under the terms of the
  20. * GNU General Public License ("GPL") as published by the Free Software
  21. * Foundation; either version 2 of the License, or (at your option) any
  22. * later version.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  25. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  26. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  27. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  30. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  31. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. /*
  37. * This file contains the core HCD code, and implements the Linux hc_driver
  38. * API
  39. */
  40. #include "usb_os_adapter.h"
  41. #include "trace.h"
  42. #include <asm/dma-mapping.h>
  43. #include <linux/usb/ch9.h>
  44. #include <linux/usb/gadget.h>
  45. #include "cp15.h"
  46. #include "core.h"
  47. #include "hcd.h"
  48. int usb_urb_dir_in(struct urb *urb)
  49. {
  50. return (urb->transfer_flags & URB_DIR_MASK) == URB_DIR_IN;
  51. }
  52. static void dwc2_port_resume(struct dwc2_hsotg *hsotg);
  53. /*
  54. * =========================================================================
  55. * Host Core Layer Functions
  56. * =========================================================================
  57. */
  58. /**
  59. * dwc2_enable_common_interrupts() - Initializes the commmon interrupts,
  60. * used in both device and host modes
  61. *
  62. * @hsotg: Programming view of the DWC_otg controller
  63. */
  64. static void dwc2_enable_common_interrupts(struct dwc2_hsotg *hsotg)
  65. {
  66. u32 intmsk;
  67. /* Clear any pending OTG Interrupts */
  68. dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
  69. /* Clear any pending interrupts */
  70. dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
  71. /* Enable the interrupts in the GINTMSK */
  72. intmsk = GINTSTS_MODEMIS | GINTSTS_OTGINT;
  73. intmsk |= GINTSTS_RXFLVL;
  74. intmsk |= GINTSTS_CONIDSTSCHNG;
  75. intmsk |= GINTSTS_WKUPINT | GINTSTS_USBSUSP |
  76. GINTSTS_SESSREQINT;
  77. dwc2_writel(intmsk, hsotg->regs + GINTMSK);
  78. }
  79. /*
  80. * Initializes the FSLSPClkSel field of the HCFG register depending on the
  81. * PHY type
  82. */
  83. static void dwc2_init_fs_ls_pclk_sel(struct dwc2_hsotg *hsotg)
  84. {
  85. u32 hcfg, val;
  86. /* High speed PHY running at full speed or high speed */
  87. val = HCFG_FSLSPCLKSEL_30_60_MHZ;
  88. dev_dbg(hsotg->dev, "Initializing HCFG.FSLSPClkSel to %08x\n", val);
  89. hcfg = dwc2_readl(hsotg->regs + HCFG);
  90. hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
  91. hcfg |= val << HCFG_FSLSPCLKSEL_SHIFT;
  92. dwc2_writel(hcfg, hsotg->regs + HCFG);
  93. }
  94. static int dwc2_fs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
  95. {
  96. u32 usbcfg, ggpio;
  97. int retval = 0;
  98. /*
  99. * core_init() is now called on every switch so only call the
  100. * following for the first time through
  101. */
  102. if (select_phy) {
  103. dev_dbg(hsotg->dev, "FS PHY selected\n");
  104. usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  105. if (!(usbcfg & GUSBCFG_PHYSEL)) {
  106. usbcfg |= GUSBCFG_PHYSEL;
  107. dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
  108. /* Reset after a PHY select */
  109. retval = dwc2_core_reset_and_force_dr_mode(hsotg);
  110. if (retval) {
  111. dev_err(hsotg->dev,
  112. "%s: Reset failed, aborting", __func__);
  113. return retval;
  114. }
  115. }
  116. if (hsotg->params.activate_stm_fs_transceiver) {
  117. ggpio = dwc2_readl(hsotg->regs + GGPIO);
  118. if (!(ggpio & GGPIO_STM32_OTG_GCCFG_PWRDWN)) {
  119. dev_dbg(hsotg->dev, "Activating transceiver\n");
  120. /*
  121. * STM32F4x9 uses the GGPIO register as general
  122. * core configuration register.
  123. */
  124. ggpio |= GGPIO_STM32_OTG_GCCFG_PWRDWN;
  125. dwc2_writel(ggpio, hsotg->regs + GGPIO);
  126. }
  127. }
  128. }
  129. /*
  130. * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
  131. * do this on HNP Dev/Host mode switches (done in dev_init and
  132. * host_init).
  133. */
  134. if (dwc2_is_host_mode(hsotg))
  135. dwc2_init_fs_ls_pclk_sel(hsotg);
  136. return retval;
  137. }
  138. static int dwc2_hs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
  139. {
  140. u32 usbcfg, usbcfg_old;
  141. int retval = 0;
  142. if (!select_phy)
  143. return 0;
  144. usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  145. usbcfg_old = usbcfg;
  146. /*
  147. * HS PHY parameters. These parameters are preserved during soft reset
  148. * so only program the first time. Do a soft reset immediately after
  149. * setting phyif.
  150. */
  151. switch (hsotg->params.phy_type) {
  152. case DWC2_PHY_TYPE_PARAM_ULPI:
  153. /* ULPI interface */
  154. dev_dbg(hsotg->dev, "HS ULPI PHY selected\n");
  155. usbcfg |= GUSBCFG_ULPI_UTMI_SEL;
  156. usbcfg &= ~(GUSBCFG_PHYIF16 | GUSBCFG_DDRSEL);
  157. break;
  158. case DWC2_PHY_TYPE_PARAM_UTMI:
  159. /* UTMI+ interface */
  160. dev_dbg(hsotg->dev, "HS UTMI+ PHY selected\n");
  161. usbcfg &= ~(GUSBCFG_ULPI_UTMI_SEL | GUSBCFG_PHYIF16);
  162. if (hsotg->params.phy_utmi_width == 16)
  163. usbcfg |= GUSBCFG_PHYIF16;
  164. break;
  165. default:
  166. dev_err(hsotg->dev, "FS PHY selected at HS!\n");
  167. break;
  168. }
  169. if (usbcfg != usbcfg_old) {
  170. dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
  171. /* Reset after setting the PHY parameters */
  172. retval = dwc2_core_reset_and_force_dr_mode(hsotg);
  173. if (retval) {
  174. dev_err(hsotg->dev,
  175. "%s: Reset failed, aborting", __func__);
  176. return retval;
  177. }
  178. }
  179. return retval;
  180. }
  181. static int dwc2_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
  182. {
  183. u32 usbcfg;
  184. int retval = 0;
  185. if ((hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
  186. hsotg->params.speed == DWC2_SPEED_PARAM_LOW) &&
  187. hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
  188. /* If FS/LS mode with FS/LS PHY */
  189. retval = dwc2_fs_phy_init(hsotg, select_phy);
  190. if (retval)
  191. return retval;
  192. } else {
  193. /* High speed PHY */
  194. retval = dwc2_hs_phy_init(hsotg, select_phy);
  195. if (retval)
  196. return retval;
  197. }
  198. usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  199. usbcfg &= ~GUSBCFG_ULPI_FS_LS;
  200. usbcfg &= ~GUSBCFG_ULPI_CLK_SUSP_M;
  201. dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
  202. return retval;
  203. }
  204. static int dwc2_gahbcfg_init(struct dwc2_hsotg *hsotg)
  205. {
  206. u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
  207. if (hsotg->params.ahbcfg != -1) {
  208. ahbcfg &= GAHBCFG_CTRL_MASK;
  209. ahbcfg |= hsotg->params.ahbcfg &
  210. ~GAHBCFG_CTRL_MASK;
  211. }
  212. dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
  213. return 0;
  214. }
  215. static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
  216. {
  217. u32 usbcfg;
  218. usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  219. usbcfg &= ~(GUSBCFG_HNPCAP | GUSBCFG_SRPCAP);
  220. switch (hsotg->hw_params.op_mode) {
  221. case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
  222. if (hsotg->params.otg_cap ==
  223. DWC2_CAP_PARAM_HNP_SRP_CAPABLE)
  224. usbcfg |= GUSBCFG_HNPCAP;
  225. if (hsotg->params.otg_cap !=
  226. DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
  227. usbcfg |= GUSBCFG_SRPCAP;
  228. break;
  229. case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
  230. case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
  231. case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
  232. if (hsotg->params.otg_cap !=
  233. DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
  234. usbcfg |= GUSBCFG_SRPCAP;
  235. break;
  236. case GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE:
  237. case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE:
  238. case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST:
  239. default:
  240. break;
  241. }
  242. dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
  243. }
  244. /**
  245. * dwc2_enable_host_interrupts() - Enables the Host mode interrupts
  246. *
  247. * @hsotg: Programming view of DWC_otg controller
  248. */
  249. static void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg)
  250. {
  251. u32 intmsk;
  252. dev_dbg(hsotg->dev, "%s()\n", __func__);
  253. /* Disable all interrupts */
  254. dwc2_writel(0, hsotg->regs + GINTMSK);
  255. dwc2_writel(0, hsotg->regs + HAINTMSK);
  256. /* Enable the common interrupts */
  257. dwc2_enable_common_interrupts(hsotg);
  258. /* Enable host mode interrupts without disturbing common interrupts */
  259. intmsk = dwc2_readl(hsotg->regs + GINTMSK);
  260. intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT;
  261. dwc2_writel(intmsk, hsotg->regs + GINTMSK);
  262. }
  263. /**
  264. * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts
  265. *
  266. * @hsotg: Programming view of DWC_otg controller
  267. */
  268. static void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg)
  269. {
  270. u32 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
  271. /* Disable host mode interrupts without disturbing common interrupts */
  272. intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT |
  273. GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP | GINTSTS_DISCONNINT);
  274. dwc2_writel(intmsk, hsotg->regs + GINTMSK);
  275. }
  276. /*
  277. * dwc2_calculate_dynamic_fifo() - Calculates the default fifo size
  278. * For system that have a total fifo depth that is smaller than the default
  279. * RX + TX fifo size.
  280. *
  281. * @hsotg: Programming view of DWC_otg controller
  282. */
  283. static void dwc2_calculate_dynamic_fifo(struct dwc2_hsotg *hsotg)
  284. {
  285. struct dwc2_core_params *params = &hsotg->params;
  286. struct dwc2_hw_params *hw = &hsotg->hw_params;
  287. u32 rxfsiz, nptxfsiz, ptxfsiz, total_fifo_size;
  288. total_fifo_size = hw->total_fifo_size;
  289. rxfsiz = params->host_rx_fifo_size;
  290. nptxfsiz = params->host_nperio_tx_fifo_size;
  291. ptxfsiz = params->host_perio_tx_fifo_size;
  292. /*
  293. * Will use Method 2 defined in the DWC2 spec: minimum FIFO depth
  294. * allocation with support for high bandwidth endpoints. Synopsys
  295. * defines MPS(Max Packet size) for a periodic EP=1024, and for
  296. * non-periodic as 512.
  297. */
  298. if (total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)) {
  299. /*
  300. * For Buffer DMA mode/Scatter Gather DMA mode
  301. * 2 * ((Largest Packet size / 4) + 1 + 1) + n
  302. * with n = number of host channel.
  303. * 2 * ((1024/4) + 2) = 516
  304. */
  305. rxfsiz = 516 + hw->host_channels;
  306. /*
  307. * min non-periodic tx fifo depth
  308. * 2 * (largest non-periodic USB packet used / 4)
  309. * 2 * (512/4) = 256
  310. */
  311. nptxfsiz = 256;
  312. /*
  313. * min periodic tx fifo depth
  314. * (largest packet size*MC)/4
  315. * (1024 * 3)/4 = 768
  316. */
  317. ptxfsiz = 768;
  318. params->host_rx_fifo_size = rxfsiz;
  319. params->host_nperio_tx_fifo_size = nptxfsiz;
  320. params->host_perio_tx_fifo_size = ptxfsiz;
  321. }
  322. /*
  323. * If the summation of RX, NPTX and PTX fifo sizes is still
  324. * bigger than the total_fifo_size, then we have a problem.
  325. *
  326. * We won't be able to allocate as many endpoints. Right now,
  327. * we're just printing an error message, but ideally this FIFO
  328. * allocation algorithm would be improved in the future.
  329. *
  330. * FIXME improve this FIFO allocation algorithm.
  331. */
  332. if (unlikely(total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)))
  333. dev_err(hsotg->dev, "invalid fifo sizes\n");
  334. }
  335. static void dwc2_config_fifos(struct dwc2_hsotg *hsotg)
  336. {
  337. struct dwc2_core_params *params = &hsotg->params;
  338. u32 nptxfsiz, hptxfsiz, dfifocfg, grxfsiz;
  339. if (!params->enable_dynamic_fifo)
  340. return;
  341. dwc2_calculate_dynamic_fifo(hsotg);
  342. /* Rx FIFO */
  343. grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
  344. dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n", grxfsiz);
  345. grxfsiz &= ~GRXFSIZ_DEPTH_MASK;
  346. grxfsiz |= params->host_rx_fifo_size <<
  347. GRXFSIZ_DEPTH_SHIFT & GRXFSIZ_DEPTH_MASK;
  348. dwc2_writel(grxfsiz, hsotg->regs + GRXFSIZ);
  349. dev_dbg(hsotg->dev, "new grxfsiz=%08x\n",
  350. dwc2_readl(hsotg->regs + GRXFSIZ));
  351. /* Non-periodic Tx FIFO */
  352. dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n",
  353. dwc2_readl(hsotg->regs + GNPTXFSIZ));
  354. nptxfsiz = params->host_nperio_tx_fifo_size <<
  355. FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
  356. nptxfsiz |= params->host_rx_fifo_size <<
  357. FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
  358. dwc2_writel(nptxfsiz, hsotg->regs + GNPTXFSIZ);
  359. dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n",
  360. dwc2_readl(hsotg->regs + GNPTXFSIZ));
  361. /* Periodic Tx FIFO */
  362. dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n",
  363. dwc2_readl(hsotg->regs + HPTXFSIZ));
  364. hptxfsiz = params->host_perio_tx_fifo_size <<
  365. FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
  366. hptxfsiz |= (params->host_rx_fifo_size +
  367. params->host_nperio_tx_fifo_size) <<
  368. FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
  369. dwc2_writel(hptxfsiz, hsotg->regs + HPTXFSIZ);
  370. dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n",
  371. dwc2_readl(hsotg->regs + HPTXFSIZ));
  372. if (hsotg->params.en_multiple_tx_fifo &&
  373. hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_91a) {
  374. /*
  375. * This feature was implemented in 2.91a version
  376. * Global DFIFOCFG calculation for Host mode -
  377. * include RxFIFO, NPTXFIFO and HPTXFIFO
  378. */
  379. dfifocfg = dwc2_readl(hsotg->regs + GDFIFOCFG);
  380. dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK;
  381. dfifocfg |= (params->host_rx_fifo_size +
  382. params->host_nperio_tx_fifo_size +
  383. params->host_perio_tx_fifo_size) <<
  384. GDFIFOCFG_EPINFOBASE_SHIFT &
  385. GDFIFOCFG_EPINFOBASE_MASK;
  386. dwc2_writel(dfifocfg, hsotg->regs + GDFIFOCFG);
  387. }
  388. }
  389. /**
  390. * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
  391. * the HFIR register according to PHY type and speed
  392. *
  393. * @hsotg: Programming view of DWC_otg controller
  394. *
  395. * NOTE: The caller can modify the value of the HFIR register only after the
  396. * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
  397. * has been set
  398. */
  399. u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
  400. {
  401. u32 usbcfg;
  402. u32 hprt0;
  403. int clock = 60; /* default value */
  404. usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  405. hprt0 = dwc2_readl(hsotg->regs + HPRT0);
  406. if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) &&
  407. !(usbcfg & GUSBCFG_PHYIF16))
  408. clock = 60;
  409. if ((usbcfg & GUSBCFG_PHYSEL) && hsotg->hw_params.fs_phy_type ==
  410. GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
  411. clock = 48;
  412. if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
  413. !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
  414. clock = 30;
  415. if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
  416. !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16))
  417. clock = 60;
  418. if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
  419. !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
  420. clock = 48;
  421. if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) &&
  422. hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
  423. clock = 48;
  424. if ((usbcfg & GUSBCFG_PHYSEL) &&
  425. hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
  426. clock = 48;
  427. if ((hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT == HPRT0_SPD_HIGH_SPEED)
  428. /* High speed case */
  429. return 125 * clock - 1;
  430. /* FS/LS case */
  431. return 1000 * clock - 1;
  432. }
  433. /**
  434. * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination
  435. * buffer
  436. *
  437. * @core_if: Programming view of DWC_otg controller
  438. * @dest: Destination buffer for the packet
  439. * @bytes: Number of bytes to copy to the destination
  440. */
  441. void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
  442. {
  443. u32 __iomem *fifo = (u32 __iomem *)(hsotg->regs + HCFIFO(0));
  444. u32 *data_buf = (u32 *)dest;
  445. int word_count = (bytes + 3) / 4;
  446. int i;
  447. /*
  448. * Todo: Account for the case where dest is not dword aligned. This
  449. * requires reading data from the FIFO into a u32 temp buffer, then
  450. * moving it into the data buffer.
  451. */
  452. dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
  453. for (i = 0; i < word_count; i++, data_buf++)
  454. *data_buf = dwc2_readl((u32)fifo);
  455. }
  456. static int _dwc2_hcd_start(struct usb_hcd *hcd);
  457. void dwc2_host_start(struct dwc2_hsotg *hsotg)
  458. {
  459. struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
  460. hcd->self.is_b_host = dwc2_hcd_is_b_host(hsotg);
  461. _dwc2_hcd_start(hcd);
  462. }
  463. static void dwc2_host_disconnect(struct dwc2_hsotg *hsotg)
  464. {
  465. struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
  466. hcd->self.is_b_host = 0;
  467. }
  468. static void dwc2_host_hub_info(struct dwc2_hsotg *hsotg, void *context,
  469. int *hub_addr, int *hub_port)
  470. {
  471. }
  472. /*
  473. * =========================================================================
  474. * Low Level Host Channel Access Functions
  475. * =========================================================================
  476. */
  477. static void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg,
  478. struct dwc2_host_chan *chan)
  479. {
  480. u32 hcintmsk = HCINTMSK_CHHLTD;
  481. switch (chan->ep_type) {
  482. case USB_ENDPOINT_XFER_CONTROL:
  483. case USB_ENDPOINT_XFER_BULK:
  484. dev_vdbg(hsotg->dev, "control/bulk\n");
  485. hcintmsk |= HCINTMSK_XFERCOMPL;
  486. hcintmsk |= HCINTMSK_STALL;
  487. hcintmsk |= HCINTMSK_XACTERR;
  488. hcintmsk |= HCINTMSK_DATATGLERR;
  489. if (chan->ep_is_in) {
  490. hcintmsk |= HCINTMSK_BBLERR;
  491. } else {
  492. hcintmsk |= HCINTMSK_NAK;
  493. hcintmsk |= HCINTMSK_NYET;
  494. if (chan->do_ping)
  495. hcintmsk |= HCINTMSK_ACK;
  496. }
  497. if (chan->do_split) {
  498. hcintmsk |= HCINTMSK_NAK;
  499. if (chan->complete_split)
  500. hcintmsk |= HCINTMSK_NYET;
  501. else
  502. hcintmsk |= HCINTMSK_ACK;
  503. }
  504. if (chan->error_state)
  505. hcintmsk |= HCINTMSK_ACK;
  506. break;
  507. default:
  508. dev_err(hsotg->dev, "## Unknown EP type ##\n");
  509. break;
  510. }
  511. dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
  512. if (dbg_hc(chan))
  513. dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
  514. }
  515. static void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg,
  516. struct dwc2_host_chan *chan)
  517. {
  518. u32 intmsk;
  519. if (dbg_hc(chan))
  520. dev_vdbg(hsotg->dev, "DMA disabled\n");
  521. dwc2_hc_enable_slave_ints(hsotg, chan);
  522. /* Enable the top level host channel interrupt */
  523. intmsk = dwc2_readl(hsotg->regs + HAINTMSK);
  524. intmsk |= 1 << chan->hc_num;
  525. dwc2_writel(intmsk, hsotg->regs + HAINTMSK);
  526. if (dbg_hc(chan))
  527. dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk);
  528. /* Make sure host channel interrupts are enabled */
  529. intmsk = dwc2_readl(hsotg->regs + GINTMSK);
  530. intmsk |= GINTSTS_HCHINT;
  531. dwc2_writel(intmsk, hsotg->regs + GINTMSK);
  532. if (dbg_hc(chan))
  533. dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk);
  534. }
  535. /**
  536. * dwc2_hc_init() - Prepares a host channel for transferring packets to/from
  537. * a specific endpoint
  538. *
  539. * @hsotg: Programming view of DWC_otg controller
  540. * @chan: Information needed to initialize the host channel
  541. *
  542. * The HCCHARn register is set up with the characteristics specified in chan.
  543. * Host channel interrupts that may need to be serviced while this transfer is
  544. * in progress are enabled.
  545. */
  546. static void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
  547. {
  548. u8 hc_num = chan->hc_num;
  549. u32 hcintmsk;
  550. u32 hcchar;
  551. u32 hcsplt = 0;
  552. if (dbg_hc(chan))
  553. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  554. /* Clear old interrupt conditions for this host channel */
  555. hcintmsk = 0xffffffff;
  556. hcintmsk &= ~HCINTMSK_RESERVED14_31;
  557. dwc2_writel(hcintmsk, hsotg->regs + HCINT(hc_num));
  558. /* Enable channel interrupts required for this transfer */
  559. dwc2_hc_enable_ints(hsotg, chan);
  560. /*
  561. * Program the HCCHARn register with the endpoint characteristics for
  562. * the current transfer
  563. */
  564. hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK;
  565. hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK;
  566. if (chan->ep_is_in)
  567. hcchar |= HCCHAR_EPDIR;
  568. if (chan->speed == USB_SPEED_LOW)
  569. hcchar |= HCCHAR_LSPDDEV;
  570. hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK;
  571. hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK;
  572. dwc2_writel(hcchar, hsotg->regs + HCCHAR(hc_num));
  573. if (dbg_hc(chan)) {
  574. dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n",
  575. hc_num, hcchar);
  576. dev_vdbg(hsotg->dev, "%s: Channel %d\n",
  577. __func__, hc_num);
  578. dev_vdbg(hsotg->dev, " Dev Addr: %d\n",
  579. chan->dev_addr);
  580. dev_vdbg(hsotg->dev, " Ep Num: %d\n",
  581. chan->ep_num);
  582. dev_vdbg(hsotg->dev, " Is In: %d\n",
  583. chan->ep_is_in);
  584. dev_vdbg(hsotg->dev, " Is Low Speed: %d\n",
  585. chan->speed == USB_SPEED_LOW);
  586. dev_vdbg(hsotg->dev, " Ep Type: %d\n",
  587. chan->ep_type);
  588. dev_vdbg(hsotg->dev, " Max Pkt: %d\n",
  589. chan->max_packet);
  590. }
  591. /* Program the HCSPLT register for SPLITs */
  592. if (chan->do_split) {
  593. if (dbg_hc(chan))
  594. dev_vdbg(hsotg->dev,
  595. "Programming HC %d with split --> %s\n",
  596. hc_num,
  597. chan->complete_split ? "CSPLIT" : "SSPLIT");
  598. if (chan->complete_split)
  599. hcsplt |= HCSPLT_COMPSPLT;
  600. hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT &
  601. HCSPLT_XACTPOS_MASK;
  602. hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT &
  603. HCSPLT_HUBADDR_MASK;
  604. hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT &
  605. HCSPLT_PRTADDR_MASK;
  606. if (dbg_hc(chan)) {
  607. dev_vdbg(hsotg->dev, " comp split %d\n",
  608. chan->complete_split);
  609. dev_vdbg(hsotg->dev, " xact pos %d\n",
  610. chan->xact_pos);
  611. dev_vdbg(hsotg->dev, " hub addr %d\n",
  612. chan->hub_addr);
  613. dev_vdbg(hsotg->dev, " hub port %d\n",
  614. chan->hub_port);
  615. dev_vdbg(hsotg->dev, " is_in %d\n",
  616. chan->ep_is_in);
  617. dev_vdbg(hsotg->dev, " Max Pkt %d\n",
  618. chan->max_packet);
  619. dev_vdbg(hsotg->dev, " xferlen %d\n",
  620. chan->xfer_len);
  621. }
  622. }
  623. dwc2_writel(hcsplt, hsotg->regs + HCSPLT(hc_num));
  624. }
  625. /**
  626. * dwc2_hc_halt() - Attempts to halt a host channel
  627. *
  628. * @hsotg: Controller register interface
  629. * @chan: Host channel to halt
  630. * @halt_status: Reason for halting the channel
  631. *
  632. * This function should only be called in Slave mode or to abort a transfer in
  633. * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the
  634. * controller halts the channel when the transfer is complete or a condition
  635. * occurs that requires application intervention.
  636. *
  637. * In slave mode, checks for a free request queue entry, then sets the Channel
  638. * Enable and Channel Disable bits of the Host Channel Characteristics
  639. * register of the specified channel to intiate the halt. If there is no free
  640. * request queue entry, sets only the Channel Disable bit of the HCCHARn
  641. * register to flush requests for this channel. In the latter case, sets a
  642. * flag to indicate that the host channel needs to be halted when a request
  643. * queue slot is open.
  644. *
  645. * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
  646. * HCCHARn register. The controller ensures there is space in the request
  647. * queue before submitting the halt request.
  648. *
  649. * Some time may elapse before the core flushes any posted requests for this
  650. * host channel and halts. The Channel Halted interrupt handler completes the
  651. * deactivation of the host channel.
  652. */
  653. void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
  654. enum dwc2_halt_status halt_status)
  655. {
  656. u32 nptxsts, hcchar;
  657. if (dbg_hc(chan))
  658. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  659. if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
  660. dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
  661. if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
  662. halt_status == DWC2_HC_XFER_AHB_ERR) {
  663. /*
  664. * Disable all channel interrupts except Ch Halted. The QTD
  665. * and QH state associated with this transfer has been cleared
  666. * (in the case of URB_DEQUEUE), so the channel needs to be
  667. * shut down carefully to prevent crashes.
  668. */
  669. u32 hcintmsk = HCINTMSK_CHHLTD;
  670. dev_vdbg(hsotg->dev, "dequeue/error\n");
  671. dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
  672. /*
  673. * Make sure no other interrupts besides halt are currently
  674. * pending. Handling another interrupt could cause a crash due
  675. * to the QTD and QH state.
  676. */
  677. dwc2_writel(~hcintmsk, hsotg->regs + HCINT(chan->hc_num));
  678. /*
  679. * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
  680. * even if the channel was already halted for some other
  681. * reason
  682. */
  683. chan->halt_status = halt_status;
  684. hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
  685. if (!(hcchar & HCCHAR_CHENA)) {
  686. /*
  687. * The channel is either already halted or it hasn't
  688. * started yet. In DMA mode, the transfer may halt if
  689. * it finishes normally or a condition occurs that
  690. * requires driver intervention. Don't want to halt
  691. * the channel again. In either Slave or DMA mode,
  692. * it's possible that the transfer has been assigned
  693. * to a channel, but not started yet when an URB is
  694. * dequeued. Don't want to halt a channel that hasn't
  695. * started yet.
  696. */
  697. return;
  698. }
  699. }
  700. if (chan->halt_pending) {
  701. /*
  702. * A halt has already been issued for this channel. This might
  703. * happen when a transfer is aborted by a higher level in
  704. * the stack.
  705. */
  706. dev_vdbg(hsotg->dev,
  707. "*** %s: Channel %d, chan->halt_pending already set ***\n",
  708. __func__, chan->hc_num);
  709. return;
  710. }
  711. hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
  712. /* No need to set the bit in DDMA for disabling the channel */
  713. /* TODO check it everywhere channel is disabled */
  714. if (dbg_hc(chan))
  715. dev_vdbg(hsotg->dev, "desc DMA disabled\n");
  716. hcchar |= HCCHAR_CHENA;
  717. hcchar |= HCCHAR_CHDIS;
  718. if (dbg_hc(chan))
  719. dev_vdbg(hsotg->dev, "DMA not enabled\n");
  720. hcchar |= HCCHAR_CHENA;
  721. /* Check for space in the request queue to issue the halt */
  722. if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
  723. chan->ep_type == USB_ENDPOINT_XFER_BULK) {
  724. dev_vdbg(hsotg->dev, "control/bulk\n");
  725. nptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
  726. if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
  727. dev_vdbg(hsotg->dev, "Disabling channel\n");
  728. hcchar &= ~HCCHAR_CHENA;
  729. }
  730. }
  731. dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
  732. chan->halt_status = halt_status;
  733. if (hcchar & HCCHAR_CHENA) {
  734. if (dbg_hc(chan))
  735. dev_vdbg(hsotg->dev, "Channel enabled\n");
  736. chan->halt_pending = 1;
  737. chan->halt_on_queue = 0;
  738. } else {
  739. if (dbg_hc(chan))
  740. dev_vdbg(hsotg->dev, "Channel disabled\n");
  741. chan->halt_on_queue = 1;
  742. }
  743. if (dbg_hc(chan)) {
  744. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  745. chan->hc_num);
  746. dev_vdbg(hsotg->dev, " hcchar: 0x%08x\n",
  747. hcchar);
  748. dev_vdbg(hsotg->dev, " halt_pending: %d\n",
  749. chan->halt_pending);
  750. dev_vdbg(hsotg->dev, " halt_on_queue: %d\n",
  751. chan->halt_on_queue);
  752. dev_vdbg(hsotg->dev, " halt_status: %d\n",
  753. chan->halt_status);
  754. }
  755. }
  756. /**
  757. * dwc2_hc_cleanup() - Clears the transfer state for a host channel
  758. *
  759. * @hsotg: Programming view of DWC_otg controller
  760. * @chan: Identifies the host channel to clean up
  761. *
  762. * This function is normally called after a transfer is done and the host
  763. * channel is being released
  764. */
  765. void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
  766. {
  767. u32 hcintmsk;
  768. chan->xfer_started = 0;
  769. list_del_init(&chan->split_order_list_entry);
  770. /*
  771. * Clear channel interrupt enables and any unhandled channel interrupt
  772. * conditions
  773. */
  774. dwc2_writel(0, hsotg->regs + HCINTMSK(chan->hc_num));
  775. hcintmsk = 0xffffffff;
  776. hcintmsk &= ~HCINTMSK_RESERVED14_31;
  777. dwc2_writel(hcintmsk, hsotg->regs + HCINT(chan->hc_num));
  778. }
  779. /**
  780. * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
  781. * which frame a periodic transfer should occur
  782. *
  783. * @hsotg: Programming view of DWC_otg controller
  784. * @chan: Identifies the host channel to set up and its properties
  785. * @hcchar: Current value of the HCCHAR register for the specified host channel
  786. *
  787. * This function has no effect on non-periodic transfers
  788. */
  789. static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
  790. struct dwc2_host_chan *chan, u32 *hcchar)
  791. {
  792. }
  793. /**
  794. * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
  795. * the Host Channel
  796. *
  797. * @hsotg: Programming view of DWC_otg controller
  798. * @chan: Information needed to initialize the host channel
  799. *
  800. * This function should only be called in Slave mode. For a channel associated
  801. * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
  802. * associated with a periodic EP, the periodic Tx FIFO is written.
  803. *
  804. * Upon return the xfer_buf and xfer_count fields in chan are incremented by
  805. * the number of bytes written to the Tx FIFO.
  806. */
  807. static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
  808. struct dwc2_host_chan *chan)
  809. {
  810. u32 i;
  811. u32 remaining_count;
  812. u32 byte_count;
  813. u32 dword_count;
  814. u32 __iomem *data_fifo;
  815. u32 *data_buf = (u32 *)chan->xfer_buf;
  816. if (dbg_hc(chan))
  817. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  818. data_fifo = (u32 __iomem *)(hsotg->regs + HCFIFO(chan->hc_num));
  819. remaining_count = chan->xfer_len - chan->xfer_count;
  820. if (remaining_count > chan->max_packet)
  821. byte_count = chan->max_packet;
  822. else
  823. byte_count = remaining_count;
  824. dword_count = (byte_count + 3) / 4;
  825. if (((unsigned long)data_buf & 0x3) == 0) {
  826. /* xfer_buf is DWORD aligned */
  827. for (i = 0; i < dword_count; i++, data_buf++)
  828. dwc2_writel(*data_buf, (u32)data_fifo);
  829. } else {
  830. /* xfer_buf is not DWORD aligned */
  831. for (i = 0; i < dword_count; i++, data_buf++) {
  832. u32 data = data_buf[0] | data_buf[1] << 8 |
  833. data_buf[2] << 16 | data_buf[3] << 24;
  834. dwc2_writel(data, (u32)data_fifo);
  835. }
  836. }
  837. chan->xfer_count += byte_count;
  838. chan->xfer_buf += byte_count;
  839. }
  840. /**
  841. * dwc2_hc_do_ping() - Starts a PING transfer
  842. *
  843. * @hsotg: Programming view of DWC_otg controller
  844. * @chan: Information needed to initialize the host channel
  845. *
  846. * This function should only be called in Slave mode. The Do Ping bit is set in
  847. * the HCTSIZ register, then the channel is enabled.
  848. */
  849. static void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg,
  850. struct dwc2_host_chan *chan)
  851. {
  852. u32 hcchar;
  853. u32 hctsiz;
  854. if (dbg_hc(chan))
  855. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  856. chan->hc_num);
  857. hctsiz = TSIZ_DOPNG;
  858. hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
  859. dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
  860. hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
  861. hcchar |= HCCHAR_CHENA;
  862. hcchar &= ~HCCHAR_CHDIS;
  863. dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
  864. }
  865. /**
  866. * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
  867. * channel and starts the transfer
  868. *
  869. * @hsotg: Programming view of DWC_otg controller
  870. * @chan: Information needed to initialize the host channel. The xfer_len value
  871. * may be reduced to accommodate the max widths of the XferSize and
  872. * PktCnt fields in the HCTSIZn register. The multi_count value may be
  873. * changed to reflect the final xfer_len value.
  874. *
  875. * This function may be called in either Slave mode or DMA mode. In Slave mode,
  876. * the caller must ensure that there is sufficient space in the request queue
  877. * and Tx Data FIFO.
  878. *
  879. * For an OUT transfer in Slave mode, it loads a data packet into the
  880. * appropriate FIFO. If necessary, additional data packets are loaded in the
  881. * Host ISR.
  882. *
  883. * For an IN transfer in Slave mode, a data packet is requested. The data
  884. * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
  885. * additional data packets are requested in the Host ISR.
  886. *
  887. * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
  888. * register along with a packet count of 1 and the channel is enabled. This
  889. * causes a single PING transaction to occur. Other fields in HCTSIZ are
  890. * simply set to 0 since no data transfer occurs in this case.
  891. *
  892. * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
  893. * all the information required to perform the subsequent data transfer. In
  894. * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
  895. * controller performs the entire PING protocol, then starts the data
  896. * transfer.
  897. */
  898. static void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
  899. struct dwc2_host_chan *chan)
  900. {
  901. u32 max_hc_xfer_size = hsotg->params.max_transfer_size;
  902. u16 max_hc_pkt_count = hsotg->params.max_packet_count;
  903. u32 hcchar;
  904. u32 hctsiz = 0;
  905. u16 num_packets;
  906. u32 ec_mc;
  907. if (dbg_hc(chan))
  908. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  909. if (chan->do_ping) {
  910. if (dbg_hc(chan))
  911. dev_vdbg(hsotg->dev, "ping, no DMA\n");
  912. dwc2_hc_do_ping(hsotg, chan);
  913. chan->xfer_started = 1;
  914. return;
  915. }
  916. if (chan->do_split) {
  917. if (dbg_hc(chan))
  918. dev_vdbg(hsotg->dev, "split\n");
  919. num_packets = 1;
  920. if (chan->complete_split && !chan->ep_is_in)
  921. /*
  922. * For CSPLIT OUT Transfer, set the size to 0 so the
  923. * core doesn't expect any data written to the FIFO
  924. */
  925. chan->xfer_len = 0;
  926. else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
  927. chan->xfer_len = chan->max_packet;
  928. else if (!chan->ep_is_in && chan->xfer_len > 188)
  929. chan->xfer_len = 188;
  930. hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
  931. TSIZ_XFERSIZE_MASK;
  932. /* For split set ec_mc for immediate retries */
  933. ec_mc = 1;
  934. } else {
  935. if (dbg_hc(chan))
  936. dev_vdbg(hsotg->dev, "no split\n");
  937. /*
  938. * Ensure that the transfer length and packet count will fit
  939. * in the widths allocated for them in the HCTSIZn register
  940. */
  941. if (chan->xfer_len > max_hc_xfer_size) {
  942. /*
  943. * Make sure that xfer_len is a multiple of max packet
  944. * size
  945. */
  946. chan->xfer_len =
  947. max_hc_xfer_size - chan->max_packet + 1;
  948. }
  949. if (chan->xfer_len > 0) {
  950. num_packets = (chan->xfer_len + chan->max_packet - 1) /
  951. chan->max_packet;
  952. if (num_packets > max_hc_pkt_count) {
  953. num_packets = max_hc_pkt_count;
  954. chan->xfer_len = num_packets * chan->max_packet;
  955. }
  956. } else {
  957. /* Need 1 packet for transfer length of 0 */
  958. num_packets = 1;
  959. }
  960. if (chan->ep_is_in)
  961. /*
  962. * Always program an integral # of max packets for IN
  963. * transfers
  964. */
  965. chan->xfer_len = num_packets * chan->max_packet;
  966. hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
  967. TSIZ_XFERSIZE_MASK;
  968. /* The ec_mc gets the multi_count for non-split */
  969. ec_mc = chan->multi_count;
  970. }
  971. chan->start_pkt_count = num_packets;
  972. hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
  973. hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
  974. TSIZ_SC_MC_PID_MASK;
  975. dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
  976. if (dbg_hc(chan)) {
  977. dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
  978. hctsiz, chan->hc_num);
  979. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  980. chan->hc_num);
  981. dev_vdbg(hsotg->dev, " Xfer Size: %d\n",
  982. (hctsiz & TSIZ_XFERSIZE_MASK) >>
  983. TSIZ_XFERSIZE_SHIFT);
  984. dev_vdbg(hsotg->dev, " Num Pkts: %d\n",
  985. (hctsiz & TSIZ_PKTCNT_MASK) >>
  986. TSIZ_PKTCNT_SHIFT);
  987. dev_vdbg(hsotg->dev, " Start PID: %d\n",
  988. (hctsiz & TSIZ_SC_MC_PID_MASK) >>
  989. TSIZ_SC_MC_PID_SHIFT);
  990. }
  991. /* Start the split */
  992. if (chan->do_split) {
  993. u32 hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chan->hc_num));
  994. hcsplt |= HCSPLT_SPLTENA;
  995. dwc2_writel(hcsplt, hsotg->regs + HCSPLT(chan->hc_num));
  996. }
  997. hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
  998. hcchar &= ~HCCHAR_MULTICNT_MASK;
  999. hcchar |= (ec_mc << HCCHAR_MULTICNT_SHIFT) & HCCHAR_MULTICNT_MASK;
  1000. dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
  1001. if (hcchar & HCCHAR_CHDIS)
  1002. dev_warn(hsotg->dev,
  1003. "%s: chdis set, channel %d, hcchar 0x%08x\n",
  1004. __func__, chan->hc_num, hcchar);
  1005. /* Set host channel enable after all other setup is complete */
  1006. hcchar |= HCCHAR_CHENA;
  1007. hcchar &= ~HCCHAR_CHDIS;
  1008. if (dbg_hc(chan))
  1009. dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
  1010. (hcchar & HCCHAR_MULTICNT_MASK) >>
  1011. HCCHAR_MULTICNT_SHIFT);
  1012. dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
  1013. if (dbg_hc(chan))
  1014. dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
  1015. chan->hc_num);
  1016. chan->xfer_started = 1;
  1017. chan->requests++;
  1018. if (!chan->ep_is_in && chan->xfer_len > 0)
  1019. /* Load OUT packet into the appropriate Tx FIFO */
  1020. dwc2_hc_write_packet(hsotg, chan);
  1021. }
  1022. /**
  1023. * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
  1024. * a previous call to dwc2_hc_start_transfer()
  1025. *
  1026. * @hsotg: Programming view of DWC_otg controller
  1027. * @chan: Information needed to initialize the host channel
  1028. *
  1029. * The caller must ensure there is sufficient space in the request queue and Tx
  1030. * Data FIFO. This function should only be called in Slave mode. In DMA mode,
  1031. * the controller acts autonomously to complete transfers programmed to a host
  1032. * channel.
  1033. *
  1034. * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
  1035. * if there is any data remaining to be queued. For an IN transfer, another
  1036. * data packet is always requested. For the SETUP phase of a control transfer,
  1037. * this function does nothing.
  1038. *
  1039. * Return: 1 if a new request is queued, 0 if no more requests are required
  1040. * for this transfer
  1041. */
  1042. static int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
  1043. struct dwc2_host_chan *chan)
  1044. {
  1045. if (dbg_hc(chan))
  1046. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1047. chan->hc_num);
  1048. if (chan->do_split)
  1049. /* SPLITs always queue just once per channel */
  1050. return 0;
  1051. if (chan->data_pid_start == DWC2_HC_PID_SETUP)
  1052. /* SETUPs are queued only once since they can't be NAK'd */
  1053. return 0;
  1054. if (chan->ep_is_in) {
  1055. /*
  1056. * Always queue another request for other IN transfers. If
  1057. * back-to-back INs are issued and NAKs are received for both,
  1058. * the driver may still be processing the first NAK when the
  1059. * second NAK is received. When the interrupt handler clears
  1060. * the NAK interrupt for the first NAK, the second NAK will
  1061. * not be seen. So we can't depend on the NAK interrupt
  1062. * handler to requeue a NAK'd request. Instead, IN requests
  1063. * are issued each time this function is called. When the
  1064. * transfer completes, the extra requests for the channel will
  1065. * be flushed.
  1066. */
  1067. u32 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
  1068. dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
  1069. hcchar |= HCCHAR_CHENA;
  1070. hcchar &= ~HCCHAR_CHDIS;
  1071. if (dbg_hc(chan))
  1072. dev_vdbg(hsotg->dev, " IN xfer: hcchar = 0x%08x\n",
  1073. hcchar);
  1074. dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
  1075. chan->requests++;
  1076. return 1;
  1077. }
  1078. /* OUT transfers */
  1079. if (chan->xfer_count < chan->xfer_len) {
  1080. /* Load OUT packet into the appropriate Tx FIFO */
  1081. dwc2_hc_write_packet(hsotg, chan);
  1082. chan->requests++;
  1083. return 1;
  1084. }
  1085. return 0;
  1086. }
  1087. /*
  1088. * =========================================================================
  1089. * HCD
  1090. * =========================================================================
  1091. */
  1092. /*
  1093. * Processes all the URBs in a single list of QHs. Completes them with
  1094. * -ETIMEDOUT and frees the QTD.
  1095. *
  1096. * Must be called with interrupt disabled and spinlock held
  1097. */
  1098. static void dwc2_kill_urbs_in_qh_list(struct dwc2_hsotg *hsotg,
  1099. List_t *qh_list)
  1100. {
  1101. struct dwc2_qh *qh;
  1102. struct dwc2_qtd *qtd;
  1103. ListItem_t *pxListItem, *nListItem;
  1104. list_for_each_entry_safe(pxListItem, nListItem, qh, qh_list) {
  1105. ListItem_t *pxListItem1, *nListItem1;
  1106. list_for_each_entry_safe(pxListItem1, nListItem1, qtd, &qh->qtd_list) {
  1107. dwc2_host_complete(hsotg, qtd, -ECONNRESET);
  1108. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  1109. }
  1110. }
  1111. }
  1112. static void dwc2_qh_list_free(struct dwc2_hsotg *hsotg,
  1113. List_t *qh_list)
  1114. {
  1115. struct dwc2_qtd *qtd;
  1116. struct dwc2_qh *qh;
  1117. unsigned long flags;
  1118. if (!listLIST_IS_INITIALISED(qh_list))
  1119. /* The list hasn't been initialized yet */
  1120. return;
  1121. spin_lock_irqsave(&hsotg->lock, flags);
  1122. /* Ensure there are no QTDs or URBs left */
  1123. dwc2_kill_urbs_in_qh_list(hsotg, qh_list);
  1124. ListItem_t *pxListItem, *nListItem;
  1125. list_for_each_entry_safe(pxListItem, nListItem, qh, qh_list) {
  1126. dwc2_hcd_qh_unlink(hsotg, qh);
  1127. /* Free each QTD in the QH's QTD list */
  1128. ListItem_t *pxListItem1, *nListItem1;
  1129. list_for_each_entry_safe(pxListItem1, nListItem1, qtd, &qh->qtd_list)
  1130. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  1131. if (qh->channel && qh->channel->qh == qh)
  1132. qh->channel->qh = NULL;
  1133. spin_unlock_irqrestore(&hsotg->lock, flags);
  1134. dwc2_hcd_qh_free(hsotg, qh);
  1135. spin_lock_irqsave(&hsotg->lock, flags);
  1136. }
  1137. spin_unlock_irqrestore(&hsotg->lock, flags);
  1138. }
  1139. /*
  1140. * Responds with an error status of -ETIMEDOUT to all URBs in the non-periodic
  1141. * and periodic schedules. The QTD associated with each URB is removed from
  1142. * the schedule and freed. This function may be called when a disconnect is
  1143. * detected or when the HCD is being stopped.
  1144. *
  1145. * Must be called with interrupt disabled and spinlock held
  1146. */
  1147. static void dwc2_kill_all_urbs(struct dwc2_hsotg *hsotg)
  1148. {
  1149. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_inactive);
  1150. dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_active);
  1151. }
  1152. /**
  1153. * dwc2_hcd_start() - Starts the HCD when switching to Host mode
  1154. *
  1155. * @hsotg: Pointer to struct dwc2_hsotg
  1156. */
  1157. void dwc2_hcd_start(struct dwc2_hsotg *hsotg)
  1158. {
  1159. u32 hprt0;
  1160. if (hsotg->op_state == OTG_STATE_B_HOST) {
  1161. /*
  1162. * Reset the port. During a HNP mode switch the reset
  1163. * needs to occur within 1ms and have a duration of at
  1164. * least 50ms.
  1165. */
  1166. hprt0 = dwc2_read_hprt0(hsotg);
  1167. hprt0 |= HPRT0_RST;
  1168. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  1169. }
  1170. }
  1171. void dwc2_hcd_start_isr(struct dwc2_hsotg *hsotg)
  1172. {
  1173. u32 hprt0;
  1174. if (hsotg->op_state == OTG_STATE_B_HOST) {
  1175. /*
  1176. * Reset the port. During a HNP mode switch the reset
  1177. * needs to occur within 1ms and have a duration of at
  1178. * least 50ms.
  1179. */
  1180. hprt0 = dwc2_read_hprt0(hsotg);
  1181. hprt0 |= HPRT0_RST;
  1182. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  1183. }
  1184. }
  1185. /* Must be called with interrupt disabled and spinlock held */
  1186. static void dwc2_hcd_cleanup_channels(struct dwc2_hsotg *hsotg)
  1187. {
  1188. int num_channels = hsotg->params.host_channels;
  1189. struct dwc2_host_chan *channel;
  1190. u32 hcchar;
  1191. int i;
  1192. /* Flush out any channel requests in slave mode */
  1193. for (i = 0; i < num_channels; i++) {
  1194. channel = hsotg->hc_ptr_array[i];
  1195. //if (!list_empty(&channel->hc_list_entry))
  1196. if (!list_item_empty(&channel->hc_list_entry))
  1197. continue;
  1198. hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
  1199. if (hcchar & HCCHAR_CHENA) {
  1200. hcchar &= ~(HCCHAR_CHENA | HCCHAR_EPDIR);
  1201. hcchar |= HCCHAR_CHDIS;
  1202. dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
  1203. }
  1204. }
  1205. for (i = 0; i < num_channels; i++) {
  1206. channel = hsotg->hc_ptr_array[i];
  1207. //if (!list_empty(&channel->hc_list_entry))
  1208. if (!list_item_empty(&channel->hc_list_entry))
  1209. continue;
  1210. hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
  1211. if (hcchar & HCCHAR_CHENA) {
  1212. /* Halt the channel */
  1213. hcchar |= HCCHAR_CHDIS;
  1214. dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
  1215. }
  1216. dwc2_hc_cleanup(hsotg, channel);
  1217. list_add_tail(&channel->hc_list_entry, &hsotg->free_hc_list);
  1218. /*
  1219. * Added for Descriptor DMA to prevent channel double cleanup in
  1220. * release_channel_ddma(), which is called from ep_disable when
  1221. * device disconnects
  1222. */
  1223. channel->qh = NULL;
  1224. }
  1225. /* All channels have been freed, mark them available */
  1226. hsotg->non_periodic_channels = 0;
  1227. }
  1228. /**
  1229. * dwc2_hcd_connect() - Handles connect of the HCD
  1230. *
  1231. * @hsotg: Pointer to struct dwc2_hsotg
  1232. *
  1233. * Must be called with interrupt disabled and spinlock held
  1234. */
  1235. void dwc2_hcd_connect(struct dwc2_hsotg *hsotg)
  1236. {
  1237. if (hsotg->lx_state != DWC2_L0)
  1238. usb_hcd_resume_root_hub(hsotg->priv);
  1239. hsotg->flags.b.port_connect_status_change = 1;
  1240. hsotg->flags.b.port_connect_status = 1;
  1241. }
  1242. /**
  1243. * dwc2_hcd_disconnect() - Handles disconnect of the HCD
  1244. *
  1245. * @hsotg: Pointer to struct dwc2_hsotg
  1246. * @force: If true, we won't try to reconnect even if we see device connected.
  1247. *
  1248. * Must be called with interrupt disabled and spinlock held
  1249. */
  1250. void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force)
  1251. {
  1252. u32 intr;
  1253. u32 hprt0;
  1254. /* Set status flags for the hub driver */
  1255. hsotg->flags.b.port_connect_status_change = 1;
  1256. hsotg->flags.b.port_connect_status = 0;
  1257. /*
  1258. * Shutdown any transfers in process by clearing the Tx FIFO Empty
  1259. * interrupt mask and status bits and disabling subsequent host
  1260. * channel interrupts.
  1261. */
  1262. intr = dwc2_readl(hsotg->regs + GINTMSK);
  1263. intr &= ~(GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT);
  1264. dwc2_writel(intr, hsotg->regs + GINTMSK);
  1265. intr = GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT;
  1266. dwc2_writel(intr, hsotg->regs + GINTSTS);
  1267. /*
  1268. * Turn off the vbus power only if the core has transitioned to device
  1269. * mode. If still in host mode, need to keep power on to detect a
  1270. * reconnection.
  1271. */
  1272. /* Respond with an error status to all URBs in the schedule */
  1273. dwc2_kill_all_urbs(hsotg);
  1274. if (dwc2_is_host_mode(hsotg))
  1275. /* Clean up any host channels that were in use */
  1276. dwc2_hcd_cleanup_channels(hsotg);
  1277. dwc2_host_disconnect(hsotg);
  1278. /*
  1279. * Add an extra check here to see if we're actually connected but
  1280. * we don't have a detection interrupt pending. This can happen if:
  1281. * 1. hardware sees connect
  1282. * 2. hardware sees disconnect
  1283. * 3. hardware sees connect
  1284. * 4. dwc2_port_intr() - clears connect interrupt
  1285. * 5. dwc2_handle_common_intr() - calls here
  1286. *
  1287. * Without the extra check here we will end calling disconnect
  1288. * and won't get any future interrupts to handle the connect.
  1289. */
  1290. if (!force) {
  1291. hprt0 = dwc2_readl(hsotg->regs + HPRT0);
  1292. if (!(hprt0 & HPRT0_CONNDET) && (hprt0 & HPRT0_CONNSTS))
  1293. dwc2_hcd_connect(hsotg);
  1294. }
  1295. }
  1296. #if 0
  1297. /**
  1298. * dwc2_hcd_rem_wakeup() - Handles Remote Wakeup
  1299. *
  1300. * @hsotg: Pointer to struct dwc2_hsotg
  1301. */
  1302. static void dwc2_hcd_rem_wakeup(struct dwc2_hsotg *hsotg)
  1303. {
  1304. if (hsotg->bus_suspended) {
  1305. hsotg->flags.b.port_suspend_change = 1;
  1306. usb_hcd_resume_root_hub(hsotg->priv);
  1307. }
  1308. if (hsotg->lx_state == DWC2_L1)
  1309. hsotg->flags.b.port_l1_change = 1;
  1310. }
  1311. #endif
  1312. /**
  1313. * dwc2_hcd_stop() - Halts the DWC_otg host mode operations in a clean manner
  1314. *
  1315. * @hsotg: Pointer to struct dwc2_hsotg
  1316. *
  1317. * Must be called with interrupt disabled and spinlock held
  1318. */
  1319. void dwc2_hcd_stop(struct dwc2_hsotg *hsotg)
  1320. {
  1321. dev_dbg(hsotg->dev, "DWC OTG HCD STOP\n");
  1322. /*
  1323. * The root hub should be disconnected before this function is called.
  1324. * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
  1325. * and the QH lists (via ..._hcd_endpoint_disable).
  1326. */
  1327. /* Turn off all host-specific interrupts */
  1328. dwc2_disable_host_interrupts(hsotg);
  1329. /* Turn off the vbus power */
  1330. dev_dbg(hsotg->dev, "PortPower off\n");
  1331. dwc2_writel(0, hsotg->regs + HPRT0);
  1332. }
  1333. /* Caller must hold driver lock */
  1334. static int dwc2_hcd_urb_enqueue(struct dwc2_hsotg *hsotg,
  1335. struct dwc2_hcd_urb *urb, struct dwc2_qh *qh,
  1336. struct dwc2_qtd *qtd)
  1337. {
  1338. u32 intr_mask;
  1339. int retval;
  1340. int dev_speed;
  1341. if (!hsotg->flags.b.port_connect_status) {
  1342. /* No longer connected */
  1343. dev_err(hsotg->dev, "Not connected\n");
  1344. return -ENODEV;
  1345. }
  1346. dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
  1347. /* Some configurations cannot support LS traffic on a FS root port */
  1348. if ((dev_speed == USB_SPEED_LOW) &&
  1349. (hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) &&
  1350. (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI)) {
  1351. u32 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
  1352. u32 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
  1353. if (prtspd == HPRT0_SPD_FULL_SPEED)
  1354. return -ENODEV;
  1355. }
  1356. if (!qtd)
  1357. return -EINVAL;
  1358. dwc2_hcd_qtd_init(qtd, urb);
  1359. retval = dwc2_hcd_qtd_add(hsotg, qtd, qh);
  1360. if (retval) {
  1361. dev_err(hsotg->dev,
  1362. "DWC OTG HCD URB Enqueue failed adding QTD. Error status %d\n",
  1363. retval);
  1364. return retval;
  1365. }
  1366. intr_mask = dwc2_readl(hsotg->regs + GINTMSK);
  1367. if (!(intr_mask & GINTSTS_SOF)) {
  1368. enum dwc2_transaction_type tr_type;
  1369. if (qtd->qh->ep_type == USB_ENDPOINT_XFER_BULK &&
  1370. !(qtd->urb->flags & URB_GIVEBACK_ASAP))
  1371. /*
  1372. * Do not schedule SG transactions until qtd has
  1373. * URB_GIVEBACK_ASAP set
  1374. */
  1375. return 0;
  1376. tr_type = dwc2_hcd_select_transactions(hsotg);
  1377. if (tr_type != DWC2_TRANSACTION_NONE)
  1378. dwc2_hcd_queue_transactions(hsotg, tr_type);
  1379. }
  1380. return 0;
  1381. }
  1382. /* Must be called with interrupt disabled and spinlock held */
  1383. static int dwc2_hcd_urb_dequeue(struct dwc2_hsotg *hsotg,
  1384. struct dwc2_hcd_urb *urb)
  1385. {
  1386. struct dwc2_qh *qh;
  1387. struct dwc2_qtd *urb_qtd;
  1388. urb_qtd = urb->qtd;
  1389. if (!urb_qtd) {
  1390. dev_dbg(hsotg->dev, "## Urb QTD is NULL ##\n");
  1391. return -EINVAL;
  1392. }
  1393. qh = urb_qtd->qh;
  1394. if (!qh) {
  1395. dev_dbg(hsotg->dev, "## Urb QTD QH is NULL ##\n");
  1396. return -EINVAL;
  1397. }
  1398. urb->priv = NULL;
  1399. if (urb_qtd->in_process && qh->channel) {
  1400. /* The QTD is in process (it has been assigned to a channel) */
  1401. if (hsotg->flags.b.port_connect_status)
  1402. /*
  1403. * If still connected (i.e. in host mode), halt the
  1404. * channel so it can be used for other transfers. If
  1405. * no longer connected, the host registers can't be
  1406. * written to halt the channel since the core is in
  1407. * device mode.
  1408. */
  1409. dwc2_hc_halt(hsotg, qh->channel,
  1410. DWC2_HC_XFER_URB_DEQUEUE);
  1411. }
  1412. /*
  1413. * Free the QTD and clean up the associated QH. Leave the QH in the
  1414. * schedule if it has any remaining QTDs.
  1415. */
  1416. u8 in_process = urb_qtd->in_process;
  1417. dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
  1418. if (in_process) {
  1419. dwc2_hcd_qh_deactivate(hsotg, qh, 0);
  1420. qh->channel = NULL;
  1421. } else if (list_empty(&qh->qtd_list)) {
  1422. dwc2_hcd_qh_unlink(hsotg, qh);
  1423. }
  1424. return 0;
  1425. }
  1426. /* Must NOT be called with interrupt disabled or spinlock held */
  1427. static int dwc2_hcd_endpoint_disable(struct dwc2_hsotg *hsotg,
  1428. struct usb_host_endpoint *ep, int retry)
  1429. {
  1430. struct dwc2_qtd *qtd;//, *qtd_tmp;
  1431. struct dwc2_qh *qh;
  1432. unsigned long flags;
  1433. int rc;
  1434. spin_lock_irqsave(&hsotg->lock, flags);
  1435. qh = ep->hcpriv;
  1436. if (!qh) {
  1437. rc = -EINVAL;
  1438. goto err;
  1439. }
  1440. while (!list_empty(&qh->qtd_list) && retry--) {
  1441. if (retry == 0) {
  1442. dev_err(hsotg->dev,
  1443. "## timeout in dwc2_hcd_endpoint_disable() ##\n");
  1444. rc = -EBUSY;
  1445. goto err;
  1446. }
  1447. spin_unlock_irqrestore(&hsotg->lock, flags);
  1448. msleep(20);
  1449. spin_lock_irqsave(&hsotg->lock, flags);
  1450. qh = ep->hcpriv;
  1451. if (!qh) {
  1452. rc = -EINVAL;
  1453. goto err;
  1454. }
  1455. }
  1456. dwc2_hcd_qh_unlink(hsotg, qh);
  1457. /* Free each QTD in the QH's QTD list */
  1458. //list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry)
  1459. ListItem_t *pxListItem, *nListItem;
  1460. list_for_each_entry_safe(pxListItem, nListItem, qtd, &qh->qtd_list)
  1461. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  1462. ep->hcpriv = NULL;
  1463. if (qh->channel && qh->channel->qh == qh)
  1464. qh->channel->qh = NULL;
  1465. spin_unlock_irqrestore(&hsotg->lock, flags);
  1466. dwc2_hcd_qh_free(hsotg, qh);
  1467. return 0;
  1468. err:
  1469. ep->hcpriv = NULL;
  1470. spin_unlock_irqrestore(&hsotg->lock, flags);
  1471. return rc;
  1472. }
  1473. /* Must be called with interrupt disabled and spinlock held */
  1474. static int dwc2_hcd_endpoint_reset(struct dwc2_hsotg *hsotg,
  1475. struct usb_host_endpoint *ep)
  1476. {
  1477. struct dwc2_qh *qh = ep->hcpriv;
  1478. if (!qh)
  1479. return -EINVAL;
  1480. qh->data_toggle = DWC2_HC_PID_DATA0;
  1481. return 0;
  1482. }
  1483. /**
  1484. * dwc2_core_init() - Initializes the DWC_otg controller registers and
  1485. * prepares the core for device mode or host mode operation
  1486. *
  1487. * @hsotg: Programming view of the DWC_otg controller
  1488. * @initial_setup: If true then this is the first init for this instance.
  1489. */
  1490. static int dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup)
  1491. {
  1492. u32 usbcfg, otgctl;
  1493. int retval;
  1494. dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
  1495. usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  1496. /* Set ULPI External VBUS bit if needed */
  1497. usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
  1498. /* Set external TS Dline pulsing bit if needed */
  1499. usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
  1500. dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
  1501. /*
  1502. * Reset the Controller
  1503. *
  1504. * We only need to reset the controller if this is a re-init.
  1505. * For the first init we know for sure that earlier code reset us (it
  1506. * needed to in order to properly detect various parameters).
  1507. */
  1508. if (!initial_setup) {
  1509. retval = dwc2_core_reset_and_force_dr_mode(hsotg);
  1510. if (retval) {
  1511. dev_err(hsotg->dev, "%s(): Reset failed, aborting\n",
  1512. __func__);
  1513. return retval;
  1514. }
  1515. }
  1516. /*
  1517. * This needs to happen in FS mode before any other programming occurs
  1518. */
  1519. retval = dwc2_phy_init(hsotg, initial_setup);
  1520. if (retval)
  1521. return retval;
  1522. /* Program the GAHBCFG Register */
  1523. retval = dwc2_gahbcfg_init(hsotg);
  1524. if (retval)
  1525. return retval;
  1526. /* Program the GUSBCFG register */
  1527. dwc2_gusbcfg_init(hsotg);
  1528. /* Program the GOTGCTL register */
  1529. otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
  1530. otgctl &= ~GOTGCTL_OTGVER;
  1531. dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
  1532. /* Clear the SRP success bit for FS-I2c */
  1533. hsotg->srp_success = 0;
  1534. /* Enable common interrupts */
  1535. dwc2_enable_common_interrupts(hsotg);
  1536. /*
  1537. * Do device or host initialization based on mode during PCD and
  1538. * HCD initialization
  1539. */
  1540. if (dwc2_is_host_mode(hsotg)) {
  1541. dev_dbg(hsotg->dev, "Host Mode\n");
  1542. hsotg->op_state = OTG_STATE_A_HOST;
  1543. }
  1544. return 0;
  1545. }
  1546. /**
  1547. * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
  1548. * Host mode
  1549. *
  1550. * @hsotg: Programming view of DWC_otg controller
  1551. *
  1552. * This function flushes the Tx and Rx FIFOs and flushes any entries in the
  1553. * request queues. Host channels are reset to ensure that they are ready for
  1554. * performing transfers.
  1555. */
  1556. static void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
  1557. {
  1558. u32 hcfg, hfir, otgctl, usbcfg;
  1559. dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
  1560. /* Set HS/FS Timeout Calibration to 7 (max available value).
  1561. * The number of PHY clocks that the application programs in
  1562. * this field is added to the high/full speed interpacket timeout
  1563. * duration in the core to account for any additional delays
  1564. * introduced by the PHY. This can be required, because the delay
  1565. * introduced by the PHY in generating the linestate condition
  1566. * can vary from one PHY to another.
  1567. */
  1568. usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  1569. usbcfg |= GUSBCFG_TOUTCAL(7);
  1570. dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
  1571. /* Restart the Phy Clock */
  1572. dwc2_writel(0, hsotg->regs + PCGCTL);
  1573. /* Initialize Host Configuration Register */
  1574. dwc2_init_fs_ls_pclk_sel(hsotg);
  1575. if (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
  1576. hsotg->params.speed == DWC2_SPEED_PARAM_LOW) {
  1577. hcfg = dwc2_readl(hsotg->regs + HCFG);
  1578. hcfg |= HCFG_FSLSSUPP;
  1579. dwc2_writel(hcfg, hsotg->regs + HCFG);
  1580. }
  1581. /*
  1582. * This bit allows dynamic reloading of the HFIR register during
  1583. * runtime. This bit needs to be programmed during initial configuration
  1584. * and its value must not be changed during runtime.
  1585. */
  1586. if (hsotg->params.reload_ctl) {
  1587. hfir = dwc2_readl(hsotg->regs + HFIR);
  1588. hfir |= HFIR_RLDCTRL;
  1589. dwc2_writel(hfir, hsotg->regs + HFIR);
  1590. }
  1591. /* Configure data FIFO sizes */
  1592. dwc2_config_fifos(hsotg);
  1593. /* TODO - check this */
  1594. /* Clear Host Set HNP Enable in the OTG Control Register */
  1595. otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
  1596. otgctl &= ~GOTGCTL_HSTSETHNPEN;
  1597. dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
  1598. /* Make sure the FIFOs are flushed */
  1599. dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
  1600. dwc2_flush_rx_fifo(hsotg);
  1601. /* Clear Host Set HNP Enable in the OTG Control Register */
  1602. otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
  1603. otgctl &= ~GOTGCTL_HSTSETHNPEN;
  1604. dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
  1605. {
  1606. int num_channels, i;
  1607. u32 hcchar;
  1608. /* Flush out any leftover queued requests */
  1609. num_channels = hsotg->params.host_channels;
  1610. for (i = 0; i < num_channels; i++) {
  1611. hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
  1612. hcchar &= ~HCCHAR_CHENA;
  1613. hcchar |= HCCHAR_CHDIS;
  1614. hcchar &= ~HCCHAR_EPDIR;
  1615. dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
  1616. }
  1617. /* Halt all channels to put them into a known state */
  1618. for (i = 0; i < num_channels; i++) {
  1619. int count = 0;
  1620. hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
  1621. hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
  1622. hcchar &= ~HCCHAR_EPDIR;
  1623. dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
  1624. dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
  1625. __func__, i);
  1626. do {
  1627. hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
  1628. if (++count > 1000) {
  1629. dev_err(hsotg->dev,
  1630. "Unable to clear enable on channel %d\n",
  1631. i);
  1632. break;
  1633. }
  1634. udelay(1);
  1635. } while (hcchar & HCCHAR_CHENA);
  1636. }
  1637. }
  1638. /* Turn on the vbus power */
  1639. dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
  1640. if (hsotg->op_state == OTG_STATE_A_HOST) {
  1641. u32 hprt0 = dwc2_read_hprt0(hsotg);
  1642. dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
  1643. !!(hprt0 & HPRT0_PWR));
  1644. if (!(hprt0 & HPRT0_PWR)) {
  1645. hprt0 |= HPRT0_PWR;
  1646. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  1647. }
  1648. }
  1649. dwc2_enable_host_interrupts(hsotg);
  1650. }
  1651. /*
  1652. * Initializes dynamic portions of the DWC_otg HCD state
  1653. *
  1654. * Must be called with interrupt disabled and spinlock held
  1655. */
  1656. static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
  1657. {
  1658. struct dwc2_host_chan *chan;//, *chan_tmp;
  1659. int num_channels;
  1660. int i;
  1661. #if 1
  1662. hsotg->flags.d32 = 0;
  1663. hsotg->non_periodic_qh_ptr = (ListItem_t *)listGET_END_MARKER(&hsotg->non_periodic_sched_active);
  1664. //hsotg->non_periodic_qh_ptr = listGET_HEAD_ENTRY(&hsotg->non_periodic_sched_active);
  1665. hsotg->non_periodic_channels = 0;
  1666. /*
  1667. * Put all channels in the free channel list and clean up channel
  1668. * states
  1669. */
  1670. /*list_for_each_entry_safe(chan, chan_tmp, &hsotg->free_hc_list,
  1671. hc_list_entry)*/
  1672. ListItem_t *pxListItem, *nListItem;
  1673. list_for_each_entry_safe(pxListItem, nListItem, chan, &hsotg->free_hc_list)
  1674. list_del_init(&chan->hc_list_entry);
  1675. num_channels = hsotg->params.host_channels;
  1676. for (i = 0; i < num_channels; i++) {
  1677. chan = hsotg->hc_ptr_array[i];
  1678. list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
  1679. dwc2_hc_cleanup(hsotg, chan);
  1680. }
  1681. #endif
  1682. /* Initialize the DWC core for host mode operation */
  1683. dwc2_core_host_init(hsotg);
  1684. }
  1685. static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
  1686. struct dwc2_host_chan *chan,
  1687. struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
  1688. {
  1689. int hub_addr, hub_port;
  1690. chan->do_split = 1;
  1691. chan->xact_pos = qtd->isoc_split_pos;
  1692. chan->complete_split = qtd->complete_split;
  1693. dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
  1694. chan->hub_addr = (u8)hub_addr;
  1695. chan->hub_port = (u8)hub_port;
  1696. }
  1697. static void dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg,
  1698. struct dwc2_host_chan *chan,
  1699. struct dwc2_qtd *qtd)
  1700. {
  1701. struct dwc2_hcd_urb *urb = qtd->urb;
  1702. switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
  1703. case USB_ENDPOINT_XFER_CONTROL:
  1704. chan->ep_type = USB_ENDPOINT_XFER_CONTROL;
  1705. switch (qtd->control_phase) {
  1706. case DWC2_CONTROL_SETUP:
  1707. dev_vdbg(hsotg->dev, " ##Control setup transaction\n");
  1708. chan->do_ping = 0;
  1709. chan->ep_is_in = 0;
  1710. chan->data_pid_start = DWC2_HC_PID_SETUP;
  1711. chan->xfer_buf = urb->setup_packet;
  1712. chan->xfer_len = 8;
  1713. //unsigned char *a = urb->setup_packet;
  1714. //if (a)
  1715. //printf("xfer setup-->%02x %02x %02x %02x %02x %02x %02x %02x\r\n", a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
  1716. break;
  1717. case DWC2_CONTROL_DATA:
  1718. dev_vdbg(hsotg->dev, " Control data transaction\n");
  1719. chan->data_pid_start = qtd->data_toggle;
  1720. break;
  1721. case DWC2_CONTROL_STATUS:
  1722. /*
  1723. * Direction is opposite of data direction or IN if no
  1724. * data
  1725. */
  1726. dev_vdbg(hsotg->dev, " Control status transaction\n");
  1727. if (urb->length == 0)
  1728. chan->ep_is_in = 1;
  1729. else
  1730. chan->ep_is_in =
  1731. dwc2_hcd_is_pipe_out(&urb->pipe_info);
  1732. if (chan->ep_is_in)
  1733. chan->do_ping = 0;
  1734. chan->data_pid_start = DWC2_HC_PID_DATA1;
  1735. chan->xfer_len = 0;
  1736. chan->xfer_buf = hsotg->status_buf;
  1737. break;
  1738. }
  1739. break;
  1740. case USB_ENDPOINT_XFER_BULK:
  1741. chan->ep_type = USB_ENDPOINT_XFER_BULK;
  1742. break;
  1743. }
  1744. }
  1745. /**
  1746. * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host
  1747. * channel and initializes the host channel to perform the transactions. The
  1748. * host channel is removed from the free list.
  1749. *
  1750. * @hsotg: The HCD state structure
  1751. * @qh: Transactions from the first QTD for this QH are selected and assigned
  1752. * to a free host channel
  1753. */
  1754. static int dwc2_assign_and_init_hc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  1755. {
  1756. struct dwc2_host_chan *chan;
  1757. struct dwc2_hcd_urb *urb;
  1758. struct dwc2_qtd *qtd;
  1759. if (dbg_qh(qh))
  1760. dev_vdbg(hsotg->dev, "%s(%p,%p)\n", __func__, hsotg, qh);
  1761. if (list_empty(&qh->qtd_list)) {
  1762. dev_dbg(hsotg->dev, "No QTDs in QH list\n");
  1763. return -ENOMEM;
  1764. }
  1765. if (list_empty(&hsotg->free_hc_list)) {
  1766. dev_dbg(hsotg->dev, "No free channel to assign\n");
  1767. return -ENOMEM;
  1768. }
  1769. chan = list_first_entry(&hsotg->free_hc_list);
  1770. /* Remove host channel from free list */
  1771. list_del_init(&chan->hc_list_entry);
  1772. qtd = list_first_entry(&qh->qtd_list);
  1773. urb = qtd->urb;
  1774. qh->channel = chan;
  1775. qtd->in_process = 1;
  1776. /*
  1777. * Use usb_pipedevice to determine device address. This address is
  1778. * 0 before the SET_ADDRESS command and the correct address afterward.
  1779. */
  1780. chan->dev_addr = dwc2_hcd_get_dev_addr(&urb->pipe_info);
  1781. chan->ep_num = dwc2_hcd_get_ep_num(&urb->pipe_info);
  1782. chan->speed = qh->dev_speed;
  1783. chan->max_packet = dwc2_max_packet(qh->maxp);
  1784. chan->xfer_started = 0;
  1785. chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
  1786. chan->error_state = (qtd->error_count > 0);
  1787. chan->halt_on_queue = 0;
  1788. chan->halt_pending = 0;
  1789. chan->requests = 0;
  1790. /*
  1791. * The following values may be modified in the transfer type section
  1792. * below. The xfer_len value may be reduced when the transfer is
  1793. * started to accommodate the max widths of the XferSize and PktCnt
  1794. * fields in the HCTSIZn register.
  1795. */
  1796. chan->ep_is_in = (dwc2_hcd_is_pipe_in(&urb->pipe_info) != 0);
  1797. if (chan->ep_is_in)
  1798. chan->do_ping = 0;
  1799. else
  1800. chan->do_ping = qh->ping_state;
  1801. chan->data_pid_start = qh->data_toggle;
  1802. chan->multi_count = 1;
  1803. if (urb->actual_length > urb->length &&
  1804. !dwc2_hcd_is_pipe_in(&urb->pipe_info))
  1805. urb->actual_length = urb->length;
  1806. chan->xfer_buf = (u8 *)urb->buf + urb->actual_length;
  1807. chan->xfer_len = urb->length - urb->actual_length;
  1808. chan->xfer_count = 0;
  1809. /* Set the split attributes if required */
  1810. if (qh->do_split)
  1811. dwc2_hc_init_split(hsotg, chan, qtd, urb);
  1812. else
  1813. chan->do_split = 0;
  1814. /* Set the transfer attributes */
  1815. dwc2_hc_init_xfer(hsotg, chan, qtd);
  1816. /* For non-dword aligned buffers */
  1817. /*
  1818. * We assume that DMA is always aligned in non-split
  1819. * case or split out case. Warn if not.
  1820. */
  1821. chan->align_buf = 0;
  1822. dwc2_hc_init(hsotg, chan);
  1823. chan->qh = qh;
  1824. return 0;
  1825. }
  1826. /**
  1827. * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer
  1828. * schedule and assigns them to available host channels. Called from the HCD
  1829. * interrupt handler functions.
  1830. *
  1831. * @hsotg: The HCD state structure
  1832. *
  1833. * Return: The types of new transactions that were assigned to host channels
  1834. */
  1835. enum dwc2_transaction_type dwc2_hcd_select_transactions(
  1836. struct dwc2_hsotg *hsotg)
  1837. {
  1838. enum dwc2_transaction_type ret_val = DWC2_TRANSACTION_NONE;
  1839. ListItem_t* qh_ptr;
  1840. struct dwc2_qh *qh;
  1841. int num_channels;
  1842. #ifdef DWC2_DEBUG_SOF
  1843. dev_vdbg(hsotg->dev, " Select Transactions\n");
  1844. #endif
  1845. /*
  1846. * Process entries in the inactive portion of the non-periodic
  1847. * schedule. Some free host channels may not be used if they are
  1848. * reserved for periodic transfers.
  1849. */
  1850. num_channels = hsotg->params.host_channels;
  1851. qh_ptr = listGET_HEAD_ENTRY(&hsotg->non_periodic_sched_inactive);
  1852. while (qh_ptr != listGET_END_MARKER(&hsotg->non_periodic_sched_inactive)) {
  1853. if (hsotg->non_periodic_channels >= num_channels)
  1854. break;
  1855. if (list_empty(&hsotg->free_hc_list))
  1856. break;
  1857. qh = list_entry(qh_ptr);
  1858. if (dwc2_assign_and_init_hc(hsotg, qh))
  1859. break;
  1860. //printf("dwc2_hcd_select_transactions:2949\r\n");
  1861. /*
  1862. * Move the QH from the non-periodic inactive schedule to the
  1863. * non-periodic active schedule
  1864. */
  1865. qh_ptr = listGET_NEXT(qh_ptr);
  1866. list_move_tail(&qh->qh_list_entry,
  1867. &hsotg->non_periodic_sched_active);
  1868. if (ret_val == DWC2_TRANSACTION_NONE)
  1869. ret_val = DWC2_TRANSACTION_NON_PERIODIC;
  1870. else
  1871. ret_val = DWC2_TRANSACTION_ALL;
  1872. }
  1873. return ret_val;
  1874. }
  1875. /**
  1876. * dwc2_queue_transaction() - Attempts to queue a single transaction request for
  1877. * a host channel associated with either a periodic or non-periodic transfer
  1878. *
  1879. * @hsotg: The HCD state structure
  1880. * @chan: Host channel descriptor associated with either a periodic or
  1881. * non-periodic transfer
  1882. * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO
  1883. * for periodic transfers or the non-periodic Tx FIFO
  1884. * for non-periodic transfers
  1885. *
  1886. * Return: 1 if a request is queued and more requests may be needed to
  1887. * complete the transfer, 0 if no more requests are required for this
  1888. * transfer, -1 if there is insufficient space in the Tx FIFO
  1889. *
  1890. * This function assumes that there is space available in the appropriate
  1891. * request queue. For an OUT transfer or SETUP transaction in Slave mode,
  1892. * it checks whether space is available in the appropriate Tx FIFO.
  1893. *
  1894. * Must be called with interrupt disabled and spinlock held
  1895. */
  1896. static int dwc2_queue_transaction(struct dwc2_hsotg *hsotg,
  1897. struct dwc2_host_chan *chan,
  1898. u16 fifo_dwords_avail)
  1899. {
  1900. int retval = 0;
  1901. if (chan->do_split)
  1902. /* Put ourselves on the list to keep order straight */
  1903. list_move_tail(&chan->split_order_list_entry,
  1904. &hsotg->split_order);
  1905. if (chan->halt_pending) {
  1906. /* Don't queue a request if the channel has been halted */
  1907. } else if (chan->halt_on_queue) {
  1908. dwc2_hc_halt(hsotg, chan, chan->halt_status);
  1909. } else if (chan->do_ping) {
  1910. if (!chan->xfer_started)
  1911. dwc2_hc_start_transfer(hsotg, chan);
  1912. } else if (!chan->ep_is_in ||
  1913. chan->data_pid_start == DWC2_HC_PID_SETUP) {
  1914. if ((fifo_dwords_avail * 4) >= chan->max_packet) {
  1915. if (!chan->xfer_started) {
  1916. dwc2_hc_start_transfer(hsotg, chan);
  1917. retval = 1;
  1918. } else {
  1919. retval = dwc2_hc_continue_transfer(hsotg, chan);
  1920. }
  1921. } else {
  1922. retval = -1;
  1923. }
  1924. } else {
  1925. if (!chan->xfer_started) {
  1926. dwc2_hc_start_transfer(hsotg, chan);
  1927. retval = 1;
  1928. } else {
  1929. retval = dwc2_hc_continue_transfer(hsotg, chan);
  1930. }
  1931. }
  1932. return retval;
  1933. }
  1934. /*
  1935. * Processes active non-periodic channels and queues transactions for these
  1936. * channels to the DWC_otg controller. After queueing transactions, the NP Tx
  1937. * FIFO Empty interrupt is enabled if there are more transactions to queue as
  1938. * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
  1939. * FIFO Empty interrupt is disabled.
  1940. *
  1941. * Must be called with interrupt disabled and spinlock held
  1942. */
  1943. static void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg)
  1944. {
  1945. ListItem_t *orig_qh_ptr = NULL;
  1946. struct dwc2_qh *qh;
  1947. u32 tx_status;
  1948. u32 qspcavail;
  1949. u32 fspcavail;
  1950. u32 gintmsk;
  1951. int status;
  1952. int no_queue_space = 0;
  1953. int no_fifo_space = 0;
  1954. int more_to_do = 0;
  1955. dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n");
  1956. tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
  1957. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  1958. TXSTS_QSPCAVAIL_SHIFT;
  1959. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  1960. TXSTS_FSPCAVAIL_SHIFT;
  1961. dev_vdbg(hsotg->dev, " NP Tx Req Queue Space Avail (before queue): %d\n",
  1962. qspcavail);
  1963. dev_vdbg(hsotg->dev, " NP Tx FIFO Space Avail (before queue): %d\n",
  1964. fspcavail);
  1965. /*
  1966. * Keep track of the starting point. Skip over the start-of-list
  1967. * entry.
  1968. */
  1969. if (hsotg->non_periodic_qh_ptr == listGET_END_MARKER(&hsotg->non_periodic_sched_active)) {
  1970. hsotg->non_periodic_qh_ptr = listGET_HEAD_ENTRY(&hsotg->non_periodic_sched_active);
  1971. }
  1972. //orig_qh_ptr = hsotg->non_periodic_qh_ptr;
  1973. orig_qh_ptr = hsotg->non_periodic_qh_ptr;
  1974. /*
  1975. * Process once through the active list or until no more space is
  1976. * available in the request queue or the Tx FIFO
  1977. */
  1978. do {
  1979. tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
  1980. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  1981. TXSTS_QSPCAVAIL_SHIFT;
  1982. if (qspcavail == 0) {
  1983. no_queue_space = 1;
  1984. break;
  1985. }
  1986. qh = list_entry(hsotg->non_periodic_qh_ptr);
  1987. if (!qh->channel)
  1988. goto next;
  1989. /* Make sure EP's TT buffer is clean before queueing qtds */
  1990. if (qh->tt_buffer_dirty)
  1991. goto next;
  1992. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  1993. TXSTS_FSPCAVAIL_SHIFT;//printf("%s:%d\r\n", __func__, __LINE__);
  1994. status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
  1995. //printf("%s:%d\r\n", __func__, __LINE__);
  1996. if (status > 0) {
  1997. more_to_do = 1;
  1998. } else if (status < 0) {
  1999. no_fifo_space = 1;
  2000. break;
  2001. }
  2002. next:
  2003. /* Advance to next QH, skipping start-of-list entry */
  2004. hsotg->non_periodic_qh_ptr = listGET_NEXT(hsotg->non_periodic_qh_ptr);
  2005. if (hsotg->non_periodic_qh_ptr ==
  2006. listGET_END_MARKER(&hsotg->non_periodic_sched_active))
  2007. hsotg->non_periodic_qh_ptr = listGET_NEXT(hsotg->non_periodic_qh_ptr);
  2008. } while (hsotg->non_periodic_qh_ptr != orig_qh_ptr);
  2009. tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
  2010. qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
  2011. TXSTS_QSPCAVAIL_SHIFT;
  2012. fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
  2013. TXSTS_FSPCAVAIL_SHIFT;
  2014. dev_vdbg(hsotg->dev,
  2015. " NP Tx Req Queue Space Avail (after queue): %d\n",
  2016. qspcavail);
  2017. dev_vdbg(hsotg->dev,
  2018. " NP Tx FIFO Space Avail (after queue): %d\n",
  2019. fspcavail);
  2020. if (more_to_do || no_queue_space || no_fifo_space) {
  2021. /*
  2022. * May need to queue more transactions as the request
  2023. * queue or Tx FIFO empties. Enable the non-periodic
  2024. * Tx FIFO empty interrupt. (Always use the half-empty
  2025. * level to ensure that new requests are loaded as
  2026. * soon as possible.)
  2027. */
  2028. gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  2029. gintmsk |= GINTSTS_NPTXFEMP;
  2030. dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
  2031. } else {
  2032. /*
  2033. * Disable the Tx FIFO empty interrupt since there are
  2034. * no more transactions that need to be queued right
  2035. * now. This function is called from interrupt
  2036. * handlers to queue more transactions as transfer
  2037. * states change.
  2038. */
  2039. gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  2040. gintmsk &= ~GINTSTS_NPTXFEMP;
  2041. dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
  2042. }
  2043. }
  2044. /**
  2045. * dwc2_hcd_queue_transactions() - Processes the currently active host channels
  2046. * and queues transactions for these channels to the DWC_otg controller. Called
  2047. * from the HCD interrupt handler functions.
  2048. *
  2049. * @hsotg: The HCD state structure
  2050. * @tr_type: The type(s) of transactions to queue (non-periodic, periodic,
  2051. * or both)
  2052. *
  2053. * Must be called with interrupt disabled and spinlock held
  2054. */
  2055. void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg,
  2056. enum dwc2_transaction_type tr_type)
  2057. {
  2058. #ifdef DWC2_DEBUG_SOF
  2059. dev_vdbg(hsotg->dev, "Queue Transactions\n");
  2060. #endif
  2061. /* Process host channels associated with non-periodic transfers */
  2062. if (tr_type == DWC2_TRANSACTION_NON_PERIODIC ||
  2063. tr_type == DWC2_TRANSACTION_ALL) {
  2064. if (!list_empty(&hsotg->non_periodic_sched_active)) {
  2065. dwc2_process_non_periodic_channels(hsotg);
  2066. } else {
  2067. /*
  2068. * Ensure NP Tx FIFO empty interrupt is disabled when
  2069. * there are no non-periodic transfers to process
  2070. */
  2071. u32 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  2072. gintmsk &= ~GINTSTS_NPTXFEMP;
  2073. dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
  2074. }
  2075. }
  2076. }
  2077. static int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
  2078. {
  2079. struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
  2080. return hcd->self.b_hnp_enable;
  2081. }
  2082. /* Must NOT be called with interrupt disabled or spinlock held */
  2083. static void dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex)
  2084. {
  2085. unsigned long flags;
  2086. u32 hprt0;
  2087. u32 pcgctl;
  2088. u32 gotgctl;
  2089. dev_dbg(hsotg->dev, "%s()\n", __func__);
  2090. spin_lock_irqsave(&hsotg->lock, flags);
  2091. if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) {
  2092. gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
  2093. gotgctl |= GOTGCTL_HSTSETHNPEN;
  2094. dwc2_writel(gotgctl, hsotg->regs + GOTGCTL);
  2095. hsotg->op_state = OTG_STATE_A_SUSPEND;
  2096. }
  2097. hprt0 = dwc2_read_hprt0(hsotg);
  2098. hprt0 |= HPRT0_SUSP;
  2099. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  2100. hsotg->bus_suspended = true;
  2101. /*
  2102. * If hibernation is supported, Phy clock will be suspended
  2103. * after registers are backuped.
  2104. */
  2105. /* Suspend the Phy Clock */
  2106. pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
  2107. pcgctl |= PCGCTL_STOPPCLK;
  2108. dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
  2109. udelay(10);
  2110. /* For HNP the bus must be suspended for at least 200ms */
  2111. if (dwc2_host_is_b_hnp_enabled(hsotg)) {
  2112. pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
  2113. pcgctl &= ~PCGCTL_STOPPCLK;
  2114. dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
  2115. spin_unlock_irqrestore(&hsotg->lock, flags);
  2116. msleep(200);
  2117. } else {
  2118. spin_unlock_irqrestore(&hsotg->lock, flags);
  2119. }
  2120. }
  2121. /* Must NOT be called with interrupt disabled or spinlock held */
  2122. static void dwc2_port_resume(struct dwc2_hsotg *hsotg)
  2123. {
  2124. unsigned long flags;
  2125. u32 hprt0;
  2126. u32 pcgctl;
  2127. spin_lock_irqsave(&hsotg->lock, flags);
  2128. /*
  2129. * If hibernation is supported, Phy clock is already resumed
  2130. * after registers restore.
  2131. */
  2132. pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
  2133. pcgctl &= ~PCGCTL_STOPPCLK;
  2134. dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
  2135. spin_unlock_irqrestore(&hsotg->lock, flags);
  2136. msleep(20);
  2137. spin_lock_irqsave(&hsotg->lock, flags);
  2138. hprt0 = dwc2_read_hprt0(hsotg);
  2139. hprt0 |= HPRT0_RES;
  2140. hprt0 &= ~HPRT0_SUSP;
  2141. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  2142. spin_unlock_irqrestore(&hsotg->lock, flags);
  2143. msleep(USB_RESUME_TIMEOUT);
  2144. spin_lock_irqsave(&hsotg->lock, flags);
  2145. hprt0 = dwc2_read_hprt0(hsotg);
  2146. hprt0 &= ~(HPRT0_RES | HPRT0_SUSP);
  2147. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  2148. hsotg->bus_suspended = false;
  2149. spin_unlock_irqrestore(&hsotg->lock, flags);
  2150. }
  2151. /* Handles hub class-specific requests */
  2152. static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
  2153. u16 wvalue, u16 windex, char *buf, u16 wlength)
  2154. {
  2155. struct usb_hub_descriptor *hub_desc;
  2156. int retval = 0;
  2157. u32 hprt0;
  2158. u32 port_status;
  2159. u32 speed;
  2160. u32 pcgctl;//printf("GetPortStatus:%x\n", GetPortStatus);
  2161. switch (typereq) {
  2162. case ClearHubFeature:
  2163. dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue);
  2164. switch (wvalue) {
  2165. case C_HUB_LOCAL_POWER:
  2166. case C_HUB_OVER_CURRENT:
  2167. /* Nothing required here */
  2168. break;
  2169. default:
  2170. retval = -EINVAL;
  2171. dev_err(hsotg->dev,
  2172. "ClearHubFeature request %1xh unknown\n",
  2173. wvalue);
  2174. }
  2175. break;
  2176. case ClearPortFeature:
  2177. if (wvalue != USB_PORT_FEAT_L1)
  2178. if (!windex || windex > 1)
  2179. goto error;
  2180. switch (wvalue) {
  2181. case USB_PORT_FEAT_ENABLE:
  2182. dev_dbg(hsotg->dev,
  2183. "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
  2184. hprt0 = dwc2_read_hprt0(hsotg);
  2185. hprt0 |= HPRT0_ENA;
  2186. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  2187. break;
  2188. case USB_PORT_FEAT_SUSPEND:
  2189. dev_dbg(hsotg->dev,
  2190. "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
  2191. if (hsotg->bus_suspended)
  2192. dwc2_port_resume(hsotg);
  2193. break;
  2194. case USB_PORT_FEAT_POWER:
  2195. dev_dbg(hsotg->dev,
  2196. "ClearPortFeature USB_PORT_FEAT_POWER\n");
  2197. hprt0 = dwc2_read_hprt0(hsotg);
  2198. hprt0 &= ~HPRT0_PWR;
  2199. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  2200. break;
  2201. case USB_PORT_FEAT_INDICATOR:
  2202. dev_dbg(hsotg->dev,
  2203. "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
  2204. /* Port indicator not supported */
  2205. break;
  2206. case USB_PORT_FEAT_C_CONNECTION:
  2207. /*
  2208. * Clears driver's internal Connect Status Change flag
  2209. */
  2210. dev_dbg(hsotg->dev,
  2211. "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
  2212. hsotg->flags.b.port_connect_status_change = 0;
  2213. break;
  2214. case USB_PORT_FEAT_C_RESET:
  2215. /* Clears driver's internal Port Reset Change flag */
  2216. dev_dbg(hsotg->dev,
  2217. "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
  2218. hsotg->flags.b.port_reset_change = 0;
  2219. break;
  2220. case USB_PORT_FEAT_C_ENABLE:
  2221. /*
  2222. * Clears the driver's internal Port Enable/Disable
  2223. * Change flag
  2224. */
  2225. dev_dbg(hsotg->dev,
  2226. "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
  2227. hsotg->flags.b.port_enable_change = 0;
  2228. break;
  2229. case USB_PORT_FEAT_C_SUSPEND:
  2230. /*
  2231. * Clears the driver's internal Port Suspend Change
  2232. * flag, which is set when resume signaling on the host
  2233. * port is complete
  2234. */
  2235. dev_dbg(hsotg->dev,
  2236. "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
  2237. hsotg->flags.b.port_suspend_change = 0;
  2238. break;
  2239. case USB_PORT_FEAT_C_PORT_L1:
  2240. dev_dbg(hsotg->dev,
  2241. "ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n");
  2242. hsotg->flags.b.port_l1_change = 0;
  2243. break;
  2244. case USB_PORT_FEAT_C_OVER_CURRENT:
  2245. dev_dbg(hsotg->dev,
  2246. "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
  2247. hsotg->flags.b.port_over_current_change = 0;
  2248. break;
  2249. default:
  2250. retval = -EINVAL;
  2251. dev_err(hsotg->dev,
  2252. "ClearPortFeature request %1xh unknown or unsupported\n",
  2253. wvalue);
  2254. }
  2255. break;
  2256. case GetHubDescriptor:
  2257. dev_dbg(hsotg->dev, "GetHubDescriptor\n");
  2258. hub_desc = (struct usb_hub_descriptor *)buf;
  2259. hub_desc->bLength = 9;
  2260. hub_desc->bDescriptorType = USB_DT_HUB;
  2261. hub_desc->bNbrPorts = 1;
  2262. hub_desc->wHubCharacteristics =
  2263. cpu_to_le16(HUB_CHAR_COMMON_LPSM |
  2264. HUB_CHAR_INDV_PORT_OCPM);
  2265. hub_desc->bPwrOn2PwrGood = 1;
  2266. hub_desc->bHubContrCurrent = 0;
  2267. hub_desc->u.hs.DeviceRemovable[0] = 0;
  2268. hub_desc->u.hs.DeviceRemovable[1] = 0xff;
  2269. break;
  2270. case GetHubStatus:
  2271. dev_dbg(hsotg->dev, "GetHubStatus\n");
  2272. memset(buf, 0, 4);
  2273. break;
  2274. case GetPortStatus:
  2275. dev_vdbg(hsotg->dev,
  2276. "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex,
  2277. hsotg->flags.d32);
  2278. if (!windex || windex > 1)
  2279. goto error;
  2280. port_status = 0;
  2281. if (hsotg->flags.b.port_connect_status_change)
  2282. port_status |= USB_PORT_STAT_C_CONNECTION << 16;
  2283. if (hsotg->flags.b.port_enable_change)
  2284. port_status |= USB_PORT_STAT_C_ENABLE << 16;
  2285. if (hsotg->flags.b.port_suspend_change)
  2286. port_status |= USB_PORT_STAT_C_SUSPEND << 16;
  2287. if (hsotg->flags.b.port_l1_change)
  2288. port_status |= USB_PORT_STAT_C_L1 << 16;
  2289. if (hsotg->flags.b.port_reset_change)
  2290. port_status |= USB_PORT_STAT_C_RESET << 16;
  2291. if (hsotg->flags.b.port_over_current_change) {
  2292. dev_warn(hsotg->dev, "Overcurrent change detected\n");
  2293. port_status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  2294. }
  2295. if (!hsotg->flags.b.port_connect_status) {
  2296. /*
  2297. * The port is disconnected, which means the core is
  2298. * either in device mode or it soon will be. Just
  2299. * return 0's for the remainder of the port status
  2300. * since the port register can't be read if the core
  2301. * is in device mode.
  2302. */
  2303. *(__le32 *)buf = cpu_to_le32(port_status);
  2304. break;
  2305. }
  2306. hprt0 = dwc2_readl(hsotg->regs + HPRT0);
  2307. dev_vdbg(hsotg->dev, " HPRT0: 0x%08x\n", hprt0);
  2308. if (hprt0 & HPRT0_CONNSTS)
  2309. port_status |= USB_PORT_STAT_CONNECTION;
  2310. if (hprt0 & HPRT0_ENA)
  2311. port_status |= USB_PORT_STAT_ENABLE;
  2312. if (hprt0 & HPRT0_SUSP)
  2313. port_status |= USB_PORT_STAT_SUSPEND;
  2314. if (hprt0 & HPRT0_OVRCURRACT)
  2315. port_status |= USB_PORT_STAT_OVERCURRENT;
  2316. if (hprt0 & HPRT0_RST)
  2317. port_status |= USB_PORT_STAT_RESET;
  2318. if (hprt0 & HPRT0_PWR)
  2319. port_status |= USB_PORT_STAT_POWER;
  2320. speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
  2321. if (speed == HPRT0_SPD_HIGH_SPEED)
  2322. port_status |= USB_PORT_STAT_HIGH_SPEED;
  2323. else if (speed == HPRT0_SPD_LOW_SPEED)
  2324. port_status |= USB_PORT_STAT_LOW_SPEED;
  2325. if (hprt0 & HPRT0_TSTCTL_MASK)
  2326. port_status |= USB_PORT_STAT_TEST;
  2327. /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
  2328. dev_vdbg(hsotg->dev, "port_status=%08x\n", port_status);
  2329. *(__le32 *)buf = cpu_to_le32(port_status);
  2330. break;
  2331. case SetHubFeature:
  2332. dev_dbg(hsotg->dev, "SetHubFeature\n");
  2333. /* No HUB features supported */
  2334. break;
  2335. case SetPortFeature:
  2336. dev_dbg(hsotg->dev, "SetPortFeature\n");
  2337. if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1))
  2338. goto error;
  2339. if (!hsotg->flags.b.port_connect_status) {
  2340. /*
  2341. * The port is disconnected, which means the core is
  2342. * either in device mode or it soon will be. Just
  2343. * return without doing anything since the port
  2344. * register can't be written if the core is in device
  2345. * mode.
  2346. */
  2347. break;
  2348. }
  2349. switch (wvalue) {
  2350. case USB_PORT_FEAT_SUSPEND:
  2351. dev_dbg(hsotg->dev,
  2352. "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
  2353. if (windex != hsotg->otg_port)
  2354. goto error;
  2355. dwc2_port_suspend(hsotg, windex);
  2356. break;
  2357. case USB_PORT_FEAT_POWER:
  2358. dev_dbg(hsotg->dev,
  2359. "SetPortFeature - USB_PORT_FEAT_POWER\n");
  2360. hprt0 = dwc2_read_hprt0(hsotg);
  2361. hprt0 |= HPRT0_PWR;
  2362. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  2363. break;
  2364. case USB_PORT_FEAT_RESET:
  2365. hprt0 = dwc2_read_hprt0(hsotg);
  2366. dev_dbg(hsotg->dev,
  2367. "SetPortFeature - USB_PORT_FEAT_RESET\n");
  2368. pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
  2369. pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK);
  2370. dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
  2371. /* ??? Original driver does this */
  2372. dwc2_writel(0, hsotg->regs + PCGCTL);
  2373. hprt0 = dwc2_read_hprt0(hsotg);
  2374. /* Clear suspend bit if resetting from suspend state */
  2375. hprt0 &= ~HPRT0_SUSP;
  2376. /*
  2377. * When B-Host the Port reset bit is set in the Start
  2378. * HCD Callback function, so that the reset is started
  2379. * within 1ms of the HNP success interrupt
  2380. */
  2381. if (!dwc2_hcd_is_b_host(hsotg)) {
  2382. hprt0 |= HPRT0_PWR | HPRT0_RST;
  2383. dev_dbg(hsotg->dev,
  2384. "In host mode, hprt0=%08x\n", hprt0);
  2385. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  2386. }
  2387. /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
  2388. msleep(50);
  2389. hprt0 &= ~HPRT0_RST;
  2390. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  2391. hsotg->lx_state = DWC2_L0; /* Now back to On state */
  2392. break;
  2393. case USB_PORT_FEAT_INDICATOR:
  2394. dev_dbg(hsotg->dev,
  2395. "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
  2396. /* Not supported */
  2397. break;
  2398. case USB_PORT_FEAT_TEST:
  2399. hprt0 = dwc2_read_hprt0(hsotg);
  2400. dev_dbg(hsotg->dev,
  2401. "SetPortFeature - USB_PORT_FEAT_TEST\n");
  2402. hprt0 &= ~HPRT0_TSTCTL_MASK;
  2403. hprt0 |= (windex >> 8) << HPRT0_TSTCTL_SHIFT;
  2404. dwc2_writel(hprt0, hsotg->regs + HPRT0);
  2405. break;
  2406. default:
  2407. retval = -EINVAL;
  2408. dev_err(hsotg->dev,
  2409. "SetPortFeature %1xh unknown or unsupported\n",
  2410. wvalue);
  2411. break;
  2412. }
  2413. break;
  2414. default:
  2415. error:
  2416. retval = -EINVAL;
  2417. dev_dbg(hsotg->dev,
  2418. "Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n",
  2419. typereq, windex, wvalue);
  2420. break;
  2421. }
  2422. return retval;
  2423. }
  2424. static int dwc2_hcd_is_status_changed(struct dwc2_hsotg *hsotg, int port)
  2425. {
  2426. int retval;
  2427. if (port != 1)
  2428. return -EINVAL;
  2429. retval = (hsotg->flags.b.port_connect_status_change ||
  2430. hsotg->flags.b.port_reset_change ||
  2431. hsotg->flags.b.port_enable_change ||
  2432. hsotg->flags.b.port_suspend_change ||
  2433. hsotg->flags.b.port_over_current_change);
  2434. /*printf("%d %d %d %d %d\r\n", hsotg->flags.b.port_connect_status_change,
  2435. hsotg->flags.b.port_reset_change ,
  2436. hsotg->flags.b.port_enable_change ,
  2437. hsotg->flags.b.port_suspend_change ,
  2438. hsotg->flags.b.port_over_current_change);*/
  2439. if (retval) {
  2440. dev_dbg(hsotg->dev,
  2441. "DWC OTG HCD HUB STATUS DATA: Root port status changed\n");
  2442. dev_dbg(hsotg->dev, " port_connect_status_change: %d\n",
  2443. hsotg->flags.b.port_connect_status_change);
  2444. dev_dbg(hsotg->dev, " port_reset_change: %d\n",
  2445. hsotg->flags.b.port_reset_change);
  2446. dev_dbg(hsotg->dev, " port_enable_change: %d\n",
  2447. hsotg->flags.b.port_enable_change);
  2448. dev_dbg(hsotg->dev, " port_suspend_change: %d\n",
  2449. hsotg->flags.b.port_suspend_change);
  2450. dev_dbg(hsotg->dev, " port_over_current_change: %d\n",
  2451. hsotg->flags.b.port_over_current_change);
  2452. }
  2453. return retval;
  2454. }
  2455. int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
  2456. {
  2457. u32 hfnum = dwc2_readl(hsotg->regs + HFNUM);
  2458. #ifdef DWC2_DEBUG_SOF
  2459. dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n",
  2460. (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT);
  2461. #endif
  2462. return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
  2463. }
  2464. int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg, int us)
  2465. {
  2466. u32 hprt = dwc2_readl(hsotg->regs + HPRT0);
  2467. u32 hfir = dwc2_readl(hsotg->regs + HFIR);
  2468. u32 hfnum = dwc2_readl(hsotg->regs + HFNUM);
  2469. unsigned int us_per_frame;
  2470. unsigned int frame_number;
  2471. unsigned int remaining;
  2472. unsigned int interval;
  2473. unsigned int phy_clks;
  2474. /* High speed has 125 us per (micro) frame; others are 1 ms per */
  2475. us_per_frame = (hprt & HPRT0_SPD_MASK) ? 1000 : 125;
  2476. /* Extract fields */
  2477. frame_number = (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
  2478. remaining = (hfnum & HFNUM_FRREM_MASK) >> HFNUM_FRREM_SHIFT;
  2479. interval = (hfir & HFIR_FRINT_MASK) >> HFIR_FRINT_SHIFT;
  2480. /*
  2481. * Number of phy clocks since the last tick of the frame number after
  2482. * "us" has passed.
  2483. */
  2484. phy_clks = (interval - remaining) +
  2485. DIV_ROUND_UP(interval * us, us_per_frame);
  2486. return dwc2_frame_num_inc(frame_number, phy_clks / interval);
  2487. }
  2488. int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg)
  2489. {
  2490. return hsotg->op_state == OTG_STATE_B_HOST;
  2491. }
  2492. static struct dwc2_hcd_urb *dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg,
  2493. int iso_desc_count,
  2494. gfp_t mem_flags)
  2495. {
  2496. struct dwc2_hcd_urb *urb;
  2497. u32 size = sizeof(*urb) + iso_desc_count;
  2498. ListItem_t *pxListItem = NULL;
  2499. int found = 0, flags;
  2500. spin_lock_irqsave(&hsotg->lock, flags);
  2501. list_for_each_entry(pxListItem, urb, &hsotg->free_urb_list) {
  2502. if (urb && urb->packet_count == iso_desc_count) {
  2503. found = 1;
  2504. break;
  2505. }
  2506. }
  2507. if (found) {
  2508. list_del_init(&urb->free_list_entry);
  2509. spin_unlock_irqrestore(&hsotg->lock, flags);
  2510. memset(urb, 0, sizeof(struct dwc2_hcd_urb));
  2511. INIT_LIST_ITEM(&urb->free_list_entry);
  2512. listSET_LIST_ITEM_OWNER(&urb->free_list_entry, urb);
  2513. return urb;
  2514. }
  2515. spin_unlock_irqrestore(&hsotg->lock, flags);
  2516. urb = (struct dwc2_hcd_urb *)kzalloc(size, mem_flags);
  2517. if (urb)
  2518. urb->packet_count = iso_desc_count;
  2519. if (urb) {
  2520. INIT_LIST_ITEM(&urb->free_list_entry);
  2521. listSET_LIST_ITEM_OWNER(&urb->free_list_entry, urb);
  2522. }
  2523. return urb;
  2524. }
  2525. static struct dwc2_qtd *dwc2_hcd_qtd_alloc(struct dwc2_hsotg *hsotg,
  2526. gfp_t mem_flags)
  2527. {
  2528. struct dwc2_qtd *qtd = NULL;
  2529. if (!list_empty(&hsotg->free_qtd_list)) {
  2530. qtd = listGET_OWNER_OF_HEAD_ENTRY(&hsotg->free_qtd_list);
  2531. memset(qtd, 0, sizeof(struct dwc2_qtd));
  2532. list_del_init(&qtd->qtd_list_entry);
  2533. listSET_LIST_ITEM_OWNER(&qtd->qtd_list_entry, qtd);
  2534. return qtd;
  2535. }
  2536. qtd = (struct dwc2_qtd *)kzalloc(sizeof(*qtd), mem_flags);
  2537. if (qtd) {
  2538. listSET_LIST_ITEM_OWNER(&qtd->qtd_list_entry, qtd);
  2539. }
  2540. return qtd;
  2541. }
  2542. static void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg,
  2543. struct dwc2_hcd_urb *urb, u8 dev_addr,
  2544. u8 ep_num, u8 ep_type, u8 ep_dir, u16 mps)
  2545. {
  2546. if (dbg_perio() ||
  2547. ep_type == USB_ENDPOINT_XFER_BULK ||
  2548. ep_type == USB_ENDPOINT_XFER_CONTROL)
  2549. dev_vdbg(hsotg->dev,
  2550. "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, mps=%d\n",
  2551. dev_addr, ep_num, ep_dir, ep_type, mps);
  2552. urb->pipe_info.dev_addr = dev_addr;
  2553. urb->pipe_info.ep_num = ep_num;
  2554. urb->pipe_info.pipe_type = ep_type;
  2555. urb->pipe_info.pipe_dir = ep_dir;
  2556. urb->pipe_info.mps = mps;
  2557. }
  2558. struct wrapper_priv_data {
  2559. struct dwc2_hsotg *hsotg;
  2560. };
  2561. /* Gets the dwc2_hsotg from a usb_hcd */
  2562. static struct dwc2_hsotg *dwc2_hcd_to_hsotg(struct usb_hcd *hcd)
  2563. {
  2564. struct wrapper_priv_data *p;
  2565. p = (struct wrapper_priv_data *)hcd->hcd_priv;
  2566. return p->hsotg;
  2567. }
  2568. /**
  2569. * dwc2_host_get_tt_info() - Get the dwc2_tt associated with context
  2570. *
  2571. * This will get the dwc2_tt structure (and ttport) associated with the given
  2572. * context (which is really just a struct urb pointer).
  2573. *
  2574. * The first time this is called for a given TT we allocate memory for our
  2575. * structure. When everyone is done and has called dwc2_host_put_tt_info()
  2576. * then the refcount for the structure will go to 0 and we'll free it.
  2577. *
  2578. * @hsotg: The HCD state structure for the DWC OTG controller.
  2579. * @qh: The QH structure.
  2580. * @context: The priv pointer from a struct dwc2_hcd_urb.
  2581. * @mem_flags: Flags for allocating memory.
  2582. * @ttport: We'll return this device's port number here. That's used to
  2583. * reference into the bitmap if we're on a multi_tt hub.
  2584. *
  2585. * Return: a pointer to a struct dwc2_tt. Don't forget to call
  2586. * dwc2_host_put_tt_info()! Returns NULL upon memory alloc failure.
  2587. */
  2588. struct dwc2_tt *dwc2_host_get_tt_info(struct dwc2_hsotg *hsotg, void *context,
  2589. gfp_t mem_flags, int *ttport)
  2590. {
  2591. struct urb *urb = context;
  2592. struct dwc2_tt *dwc_tt = NULL;
  2593. if (urb->dev->tt) {
  2594. *ttport = urb->dev->ttport;
  2595. dwc_tt = urb->dev->tt->hcpriv;
  2596. if (!dwc_tt) {
  2597. size_t bitmap_size;
  2598. /*
  2599. * For single_tt we need one schedule. For multi_tt
  2600. * we need one per port.
  2601. */
  2602. bitmap_size = DWC2_ELEMENTS_PER_LS_BITMAP *
  2603. sizeof(dwc_tt->periodic_bitmaps[0]);
  2604. if (urb->dev->tt->multi)
  2605. bitmap_size *= urb->dev->tt->hub->maxchild;
  2606. dwc_tt = (struct dwc2_tt *)kzalloc(sizeof(*dwc_tt) + bitmap_size,
  2607. mem_flags);
  2608. if (!dwc_tt)
  2609. return NULL;
  2610. dwc_tt->usb_tt = urb->dev->tt;
  2611. dwc_tt->usb_tt->hcpriv = dwc_tt;
  2612. }
  2613. dwc_tt->refcount++;
  2614. }
  2615. return dwc_tt;
  2616. }
  2617. /**
  2618. * dwc2_host_put_tt_info() - Put the dwc2_tt from dwc2_host_get_tt_info()
  2619. *
  2620. * Frees resources allocated by dwc2_host_get_tt_info() if all current holders
  2621. * of the structure are done.
  2622. *
  2623. * It's OK to call this with NULL.
  2624. *
  2625. * @hsotg: The HCD state structure for the DWC OTG controller.
  2626. * @dwc_tt: The pointer returned by dwc2_host_get_tt_info.
  2627. */
  2628. void dwc2_host_put_tt_info(struct dwc2_hsotg *hsotg, struct dwc2_tt *dwc_tt)
  2629. {
  2630. /* Model kfree and make put of NULL a no-op */
  2631. if (!dwc_tt)
  2632. return;
  2633. WARN_ON(dwc_tt->refcount < 1);
  2634. dwc_tt->refcount--;
  2635. if (!dwc_tt->refcount) {
  2636. dwc_tt->usb_tt->hcpriv = NULL;
  2637. kfree(dwc_tt);
  2638. }
  2639. }
  2640. int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
  2641. {
  2642. struct urb *urb = context;
  2643. return urb->dev->speed;
  2644. }
  2645. static void dwc2_allocate_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
  2646. struct urb *urb)
  2647. {
  2648. struct usb_bus *bus = hcd_to_bus(hcd);
  2649. if (urb->interval)
  2650. bus->bandwidth_allocated += bw / urb->interval;
  2651. bus->bandwidth_int_reqs++;
  2652. }
  2653. /*
  2654. * Sets the final status of an URB and returns it to the upper layer. Any
  2655. * required cleanup of the URB is performed.
  2656. *
  2657. * Must be called with interrupt disabled and spinlock held
  2658. */
  2659. void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
  2660. int status)
  2661. {
  2662. struct urb *urb;
  2663. if (!qtd) {
  2664. dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
  2665. return;
  2666. }
  2667. if (!qtd->urb) {
  2668. dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
  2669. return;
  2670. }
  2671. urb = qtd->urb->priv;
  2672. if (!urb) {
  2673. dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
  2674. return;
  2675. }
  2676. urb->actual_length = dwc2_hcd_urb_get_actual_length(qtd->urb);
  2677. if (dbg_urb(urb))
  2678. dev_vdbg(hsotg->dev,
  2679. "%s: urb %p device %d ep %d-%s status %d actual %d\n",
  2680. __func__, urb, usb_pipedevice(urb->pipe),
  2681. usb_pipeendpoint(urb->pipe),
  2682. usb_pipein(urb->pipe) ? "IN" : "OUT", status,
  2683. urb->actual_length);
  2684. urb->status = status;
  2685. if (!status) {
  2686. if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
  2687. urb->actual_length < urb->transfer_buffer_length)
  2688. urb->status = -EREMOTEIO;
  2689. }
  2690. usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg), urb);
  2691. urb->hcpriv = NULL;
  2692. list_add_tail(&qtd->urb->free_list_entry, &hsotg->free_urb_list);
  2693. qtd->urb = NULL;
  2694. usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg), urb, status);
  2695. }
  2696. /*
  2697. * =========================================================================
  2698. * Linux HC Driver Functions
  2699. * =========================================================================
  2700. */
  2701. /*
  2702. * Initializes the DWC_otg controller and its root hub and prepares it for host
  2703. * mode operation. Activates the root port. Returns 0 on success and a negative
  2704. * error code on failure.
  2705. */
  2706. static int _dwc2_hcd_start(struct usb_hcd *hcd)
  2707. {
  2708. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2709. struct usb_bus *bus = hcd_to_bus(hcd);
  2710. unsigned long flags;
  2711. dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
  2712. spin_lock_irqsave(&hsotg->lock, flags);
  2713. hsotg->lx_state = DWC2_L0;
  2714. dwc2_hcd_reinit(hsotg);
  2715. spin_unlock_irqrestore(&hsotg->lock, flags);
  2716. return 0;
  2717. }
  2718. /*
  2719. * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  2720. * stopped.
  2721. */
  2722. static void _dwc2_hcd_stop(struct usb_hcd *hcd)
  2723. {
  2724. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2725. unsigned long flags;
  2726. /* Turn off all host-specific interrupts */
  2727. dwc2_disable_host_interrupts(hsotg);
  2728. spin_lock_irqsave(&hsotg->lock, flags);
  2729. /* Ensure hcd is disconnected */
  2730. dwc2_hcd_disconnect(hsotg, true);
  2731. dwc2_hcd_stop(hsotg);
  2732. hsotg->lx_state = DWC2_L3;
  2733. spin_unlock_irqrestore(&hsotg->lock, flags);
  2734. udelay(2000);
  2735. }
  2736. /* Returns the current frame number */
  2737. static int _dwc2_hcd_get_frame_number(struct usb_hcd *hcd)
  2738. {
  2739. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2740. return dwc2_hcd_get_frame_number(hsotg);
  2741. }
  2742. /*
  2743. * Starts processing a USB transfer request specified by a USB Request Block
  2744. * (URB). mem_flags indicates the type of memory allocation to use while
  2745. * processing this URB.
  2746. */
  2747. int dwc2_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  2748. gfp_t mem_flags)
  2749. {
  2750. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2751. struct usb_host_endpoint *ep = urb->ep;
  2752. struct dwc2_hcd_urb *dwc2_urb;
  2753. int retval;
  2754. int alloc_bandwidth = 0;
  2755. u8 ep_type = 0;
  2756. u32 tflags = 0;
  2757. void *buf;
  2758. unsigned long flags;
  2759. struct dwc2_qh *qh;
  2760. bool qh_allocated = false;
  2761. struct dwc2_qtd *qtd;
  2762. if (dbg_urb(urb)) {
  2763. dev_vdbg(hsotg->dev, "DWC OTG HCD URB Enqueue\n");
  2764. }
  2765. if (!ep)
  2766. return -EINVAL;
  2767. switch (usb_pipetype(urb->pipe)) {
  2768. case PIPE_CONTROL:
  2769. ep_type = USB_ENDPOINT_XFER_CONTROL;
  2770. break;
  2771. case PIPE_BULK:
  2772. ep_type = USB_ENDPOINT_XFER_BULK;
  2773. break;
  2774. }
  2775. /*if (usb_pipetype(urb->pipe) == PIPE_BULK && urb->transfer_buffer) {//dev_dbg(hsotg->dev, "data_toggle:%d\n", qh->data_toggle);
  2776. char *tmpbuf = urb->transfer_buffer;
  2777. for(i = 0; i < urb->transfer_buffer_length; i++) {
  2778. printf("%02x ", tmpbuf[i]);
  2779. }printf("\n");
  2780. }*/
  2781. dwc2_urb = dwc2_hcd_urb_alloc(hsotg, urb->number_of_packets,
  2782. mem_flags);
  2783. if (!dwc2_urb)
  2784. return -ENOMEM;
  2785. dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, usb_pipedevice(urb->pipe),
  2786. usb_pipeendpoint(urb->pipe), ep_type,
  2787. usb_pipein(urb->pipe),
  2788. usb_maxpacket(urb->dev, urb->pipe));
  2789. buf = urb->transfer_buffer;
  2790. if (hcd->self.uses_dma) {
  2791. if (!buf && (urb->transfer_dma & 3)) {
  2792. dev_err(hsotg->dev,
  2793. "%s: unaligned transfer with no transfer_buffer",
  2794. __func__);
  2795. retval = -EINVAL;
  2796. goto fail0;
  2797. }
  2798. }
  2799. if (!(urb->transfer_flags & URB_NO_INTERRUPT))
  2800. tflags |= URB_GIVEBACK_ASAP;
  2801. if (urb->transfer_flags & URB_ZERO_PACKET)
  2802. tflags |= URB_SEND_ZERO_PACKET;
  2803. dwc2_urb->priv = urb;
  2804. dwc2_urb->buf = buf;
  2805. dwc2_urb->dma = urb->transfer_dma;
  2806. dwc2_urb->length = urb->transfer_buffer_length;
  2807. dwc2_urb->setup_packet = urb->setup_packet;
  2808. dwc2_urb->setup_dma = urb->setup_dma;
  2809. dwc2_urb->flags = tflags;
  2810. dwc2_urb->interval = urb->interval;
  2811. dwc2_urb->status = -EINPROGRESS;
  2812. urb->hcpriv = dwc2_urb;
  2813. qh = (struct dwc2_qh *)ep->hcpriv;
  2814. /* Create QH for the endpoint if it doesn't exist */
  2815. if (!qh) {
  2816. qh = dwc2_hcd_qh_create(hsotg, dwc2_urb, mem_flags);
  2817. if (!qh) {
  2818. retval = -ENOMEM;
  2819. goto fail0;
  2820. }
  2821. ep->hcpriv = qh;
  2822. qh_allocated = true;
  2823. }//dev_dbg(hsotg->dev, "data_toggle:%d qh:%x\n", qh->data_toggle, qh);
  2824. spin_lock_irqsave(&hsotg->lock, flags);
  2825. qtd = dwc2_hcd_qtd_alloc(hsotg, mem_flags);
  2826. if (!qtd) {
  2827. retval = -ENOMEM;
  2828. goto fail1;
  2829. }
  2830. usb_hcd_link_urb_to_ep(hcd, urb);
  2831. retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, qh, qtd);
  2832. if (retval)
  2833. goto fail3;
  2834. if (alloc_bandwidth) {
  2835. dwc2_allocate_bus_bandwidth(hcd,
  2836. dwc2_hcd_get_ep_bandwidth(hsotg, ep),
  2837. urb);
  2838. }
  2839. spin_unlock_irqrestore(&hsotg->lock, flags);
  2840. return 0;
  2841. fail3:
  2842. dwc2_urb->priv = NULL;
  2843. usb_hcd_unlink_urb_from_ep(hcd, urb);
  2844. if (qh_allocated && qh->channel && qh->channel->qh == qh)
  2845. qh->channel->qh = NULL;
  2846. list_add_tail(&qtd->qtd_list_entry, &hsotg->free_qtd_list);
  2847. spin_unlock_irqrestore(&hsotg->lock, flags);
  2848. urb->hcpriv = NULL;
  2849. fail1:
  2850. if (qh_allocated) {
  2851. struct dwc2_qtd *qtd2;//, *qtd2_tmp;
  2852. ep->hcpriv = NULL;
  2853. dwc2_hcd_qh_unlink(hsotg, qh);
  2854. /* Free each QTD in the QH's QTD list */
  2855. /*list_for_each_entry_safe(qtd2, qtd2_tmp, &qh->qtd_list,
  2856. qtd_list_entry)*/
  2857. ListItem_t *pxListItem, *nListItem;
  2858. list_for_each_entry_safe(pxListItem, nListItem, qtd2, &qh->qtd_list)
  2859. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd2, qh);
  2860. dwc2_hcd_qh_free(hsotg, qh);
  2861. }
  2862. fail0:
  2863. spin_lock_irqsave(&hsotg->lock, flags);
  2864. list_add_tail(&dwc2_urb->free_list_entry, &hsotg->free_urb_list);
  2865. spin_unlock_irqrestore(&hsotg->lock, flags);
  2866. return retval;
  2867. }
  2868. /*
  2869. * Aborts/cancels a USB transfer request. Always returns 0 to indicate success.
  2870. */
  2871. int dwc2_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
  2872. int status)
  2873. {
  2874. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2875. int rc;
  2876. unsigned long flags;
  2877. dev_dbg(hsotg->dev, "DWC OTG HCD URB Dequeue\n");
  2878. spin_lock_irqsave(&hsotg->lock, flags);
  2879. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  2880. if (rc)
  2881. goto out;
  2882. if (!urb->hcpriv) {
  2883. dev_dbg(hsotg->dev, "## urb->hcpriv is NULL ##\n");
  2884. goto out;
  2885. }
  2886. rc = dwc2_hcd_urb_dequeue(hsotg, urb->hcpriv);
  2887. usb_hcd_unlink_urb_from_ep(hcd, urb);
  2888. kfree(urb->hcpriv);
  2889. urb->hcpriv = NULL;
  2890. /* Higher layer software sets URB status */
  2891. spin_unlock(&hsotg->lock);
  2892. usb_hcd_giveback_urb(hcd, urb, status);
  2893. spin_lock(&hsotg->lock);
  2894. dev_dbg(hsotg->dev, "Called usb_hcd_giveback_urb()\n");
  2895. dev_dbg(hsotg->dev, " urb->status = %d\n", urb->status);
  2896. out:
  2897. spin_unlock_irqrestore(&hsotg->lock, flags);
  2898. return rc;
  2899. }
  2900. /*
  2901. * Frees resources in the DWC_otg controller related to a given endpoint. Also
  2902. * clears state in the HCD related to the endpoint. Any URBs for the endpoint
  2903. * must already be dequeued.
  2904. */
  2905. static void _dwc2_hcd_endpoint_disable(struct usb_hcd *hcd,
  2906. struct usb_host_endpoint *ep)
  2907. {
  2908. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2909. dev_dbg(hsotg->dev,
  2910. "DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n",
  2911. ep->desc.bEndpointAddress, ep->hcpriv);
  2912. dwc2_hcd_endpoint_disable(hsotg, ep, 250);
  2913. }
  2914. /*
  2915. * Resets endpoint specific parameter values, in current version used to reset
  2916. * the data toggle (as a WA). This function can be called from usb_clear_halt
  2917. * routine.
  2918. */
  2919. static void _dwc2_hcd_endpoint_reset(struct usb_hcd *hcd,
  2920. struct usb_host_endpoint *ep)
  2921. {
  2922. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2923. unsigned long flags;
  2924. dev_dbg(hsotg->dev,
  2925. "DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n",
  2926. ep->desc.bEndpointAddress);
  2927. spin_lock_irqsave(&hsotg->lock, flags);
  2928. dwc2_hcd_endpoint_reset(hsotg, ep);
  2929. spin_unlock_irqrestore(&hsotg->lock, flags);
  2930. }
  2931. /*
  2932. * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
  2933. * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
  2934. * interrupt.
  2935. *
  2936. * This function is called by the USB core when an interrupt occurs
  2937. */
  2938. static irqreturn_t _dwc2_hcd_irq(struct usb_hcd *hcd)
  2939. {
  2940. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2941. return dwc2_handle_hcd_intr(hsotg);
  2942. }
  2943. void dwc2_hcd_irq(struct dwc2_hsotg *hsotg)
  2944. {
  2945. dwc2_handle_hcd_intr(hsotg);
  2946. }
  2947. /*
  2948. * Creates Status Change bitmap for the root hub and root port. The bitmap is
  2949. * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
  2950. * is the status change indicator for the single root port. Returns 1 if either
  2951. * change indicator is 1, otherwise returns 0.
  2952. */
  2953. static int _dwc2_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
  2954. {
  2955. struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
  2956. int ret;
  2957. ret = (dwc2_hcd_is_status_changed(hsotg, 1) << 1);
  2958. memcpy(buf, &hsotg->flags, sizeof(hsotg->flags));
  2959. return ret;
  2960. }
  2961. /* Handles hub class-specific requests */
  2962. static int _dwc2_hcd_hub_control(struct usb_hcd *hcd, u16 typereq, u16 wvalue,
  2963. u16 windex, char *buf, u16 wlength)
  2964. {
  2965. int retval = dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd), typereq,
  2966. wvalue, windex, buf, wlength);
  2967. return retval;
  2968. }
  2969. static struct hc_driver dwc2_hc_driver = {
  2970. .description = "dwc2_hsotg",
  2971. .product_desc = "DWC OTG Controller",
  2972. .hcd_priv_size = sizeof(struct wrapper_priv_data),
  2973. .irq = _dwc2_hcd_irq,
  2974. .flags = HCD_MEMORY | HCD_USB2 | HCD_BH,
  2975. .start = _dwc2_hcd_start,
  2976. .stop = _dwc2_hcd_stop,
  2977. .urb_enqueue = dwc2_urb_enqueue,
  2978. .urb_dequeue = dwc2_urb_dequeue,
  2979. .endpoint_disable = _dwc2_hcd_endpoint_disable,
  2980. .endpoint_reset = _dwc2_hcd_endpoint_reset,
  2981. .get_frame_number = _dwc2_hcd_get_frame_number,
  2982. .hub_status_data = _dwc2_hcd_hub_status_data,
  2983. .hub_control = _dwc2_hcd_hub_control,
  2984. };
  2985. struct hc_driver* dwc2_get_driver()
  2986. {
  2987. return &dwc2_hc_driver;
  2988. }
  2989. /*
  2990. * Frees secondary storage associated with the dwc2_hsotg structure contained
  2991. * in the struct usb_hcd field
  2992. */
  2993. static void dwc2_hcd_free(struct dwc2_hsotg *hsotg)
  2994. {
  2995. u32 ahbcfg;
  2996. u32 dctl;
  2997. int i;
  2998. dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n");
  2999. /* Free memory for QH/QTD lists */
  3000. dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive);
  3001. dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active);
  3002. /* Free memory for the host channels */
  3003. for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  3004. struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
  3005. if (chan) {
  3006. dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n",
  3007. i, chan);
  3008. hsotg->hc_ptr_array[i] = NULL;
  3009. kfree(chan);
  3010. }
  3011. }
  3012. kfree(hsotg->status_buf);
  3013. hsotg->status_buf = NULL;
  3014. ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
  3015. /* Disable all interrupts */
  3016. ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
  3017. dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
  3018. dwc2_writel(0, hsotg->regs + GINTMSK);
  3019. if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) {
  3020. dctl = dwc2_readl(hsotg->regs + DCTL);
  3021. dctl |= DCTL_SFTDISCON;
  3022. dwc2_writel(dctl, hsotg->regs + DCTL);
  3023. }
  3024. }
  3025. static void dwc2_hcd_release(struct dwc2_hsotg *hsotg)
  3026. {
  3027. /* Turn off all host-specific interrupts */
  3028. dwc2_disable_host_interrupts(hsotg);
  3029. dwc2_hcd_free(hsotg);
  3030. }
  3031. void reset_hcd_reg(struct dwc2_hsotg *hsotg)
  3032. {
  3033. dwc2_writel(0x0024863E, hsotg->regs + 0x000);
  3034. dwc2_writel(0x006086a3, hsotg->regs + 0x008);
  3035. dwc2_writel(0x3a40170f, hsotg->regs + 0x00c);
  3036. dwc2_writel(0x80000400, hsotg->regs + 0x010);
  3037. dwc2_writel(0xFFFFFFFF, hsotg->regs + 0x018);
  3038. dwc2_writel(0x00000634, hsotg->regs + 0x024);
  3039. dwc2_writel(0x01800634, hsotg->regs + 0x028);
  3040. dwc2_writel(0x00000000, hsotg->regs + 0x054);
  3041. dwc2_writel(0x030007B4, hsotg->regs + 0x100);
  3042. dwc2_writel(0x80008000, hsotg->regs + 0x400);
  3043. dwc2_writel(0x0001EA60, hsotg->regs + 0x404);
  3044. dwc2_writel(0x0000FFFF, hsotg->regs + 0x418);
  3045. dwc2_writel(0x00001000, hsotg->regs + 0x440);
  3046. dwc2_writel(0x00000000, hsotg->regs + 0x500);
  3047. }
  3048. /*
  3049. * Initializes the HCD. This function allocates memory for and initializes the
  3050. * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the
  3051. * USB bus with the core and calls the hc_driver->start() function. It returns
  3052. * a negative error on failure.
  3053. */
  3054. int dwc2_hcd_init(struct dwc2_hsotg *hsotg, struct usb_hcd *hcd)
  3055. {
  3056. //struct usb_hcd *hcd;
  3057. struct dwc2_host_chan *channel;
  3058. u32 hcfg;
  3059. int i, num_channels;
  3060. int retval;
  3061. dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n");
  3062. retval = -ENOMEM;
  3063. hcfg = dwc2_readl(hsotg->regs + HCFG);
  3064. USB_UNUSED(hcfg);
  3065. dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg);
  3066. hsotg->last_frame_num = HFNUM_MAX_FRNUM;
  3067. hcd->self.uses_dma = 0;
  3068. hcd->has_tt = 1;
  3069. struct wrapper_priv_data *hcd_pri = (struct wrapper_priv_data *)kmalloc(sizeof(struct wrapper_priv_data), __GFP_ZERO);
  3070. hcd_pri->hsotg = hsotg;
  3071. hcd->hcd_priv = hcd_pri;
  3072. hsotg->priv = (void*)hcd;
  3073. /*
  3074. * Disable the global interrupt until all the interrupt handlers are
  3075. * installed
  3076. */
  3077. dwc2_disable_global_interrupts(hsotg);
  3078. /* Initialize the DWC_otg core, and select the Phy type */
  3079. retval = dwc2_core_init(hsotg, true);
  3080. if (retval)
  3081. goto error2;
  3082. /* Create new workqueue and init work */
  3083. retval = -ENOMEM;
  3084. /* Initialize the non-periodic schedule */
  3085. INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive);
  3086. INIT_LIST_HEAD(&hsotg->non_periodic_sched_active);
  3087. INIT_LIST_HEAD(&hsotg->split_order);
  3088. /*
  3089. * Create a host channel descriptor for each host channel implemented
  3090. * in the controller. Initialize the channel descriptor array.
  3091. */
  3092. INIT_LIST_HEAD(&hsotg->free_hc_list);
  3093. num_channels = hsotg->params.host_channels;
  3094. memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array));
  3095. for (i = 0; i < num_channels; i++) {
  3096. channel = kzalloc(sizeof(*channel), GFP_KERNEL);
  3097. if (!channel)
  3098. goto error3;
  3099. channel->hc_num = i;
  3100. INIT_LIST_ITEM(&channel->split_order_list_entry);
  3101. //channel->split_order_list_entry.pvOwner = (void *)channel;
  3102. listSET_LIST_ITEM_OWNER(&channel->split_order_list_entry, channel);
  3103. //channel->hc_list_entry.pvOwner = (void *)channel;
  3104. listSET_LIST_ITEM_OWNER(&channel->hc_list_entry, channel);
  3105. hsotg->hc_ptr_array[i] = channel;
  3106. }
  3107. INIT_LIST_HEAD(&hsotg->free_qtd_list);
  3108. INIT_LIST_HEAD(&hsotg->free_urb_list);
  3109. /*
  3110. * Allocate space for storing data on status transactions. Normally no
  3111. * data is sent, but this space acts as a bit bucket. This must be
  3112. * done after usb_add_hcd since that function allocates the DMA buffer
  3113. * pool.
  3114. */
  3115. hsotg->status_buf = kzalloc(DWC2_HCD_STATUS_BUF_SIZE,
  3116. GFP_KERNEL);
  3117. if (!hsotg->status_buf)
  3118. goto error3;
  3119. hsotg->otg_port = 1;
  3120. hsotg->frame_list = NULL;
  3121. hsotg->frame_list_dma = 0;
  3122. /* Initiate lx_state to L3 disconnected state */
  3123. hsotg->lx_state = DWC2_L3;
  3124. hcd->self.otg_port = hsotg->otg_port;
  3125. /* Don't support SG list at this point */
  3126. hcd->self.sg_tablesize = 0;
  3127. //_dwc2_hcd_start(hcd);
  3128. dwc2_host_start(hsotg);
  3129. //reset_hcd_reg(hsotg);
  3130. dwc2_enable_global_interrupts(hsotg);
  3131. return 0;
  3132. /* error4:
  3133. kmem_cache_destroy(hsotg->unaligned_cache);
  3134. kmem_cache_destroy(hsotg->desc_hsisoc_cache);
  3135. kmem_cache_destroy(hsotg->desc_gen_cache); */
  3136. error3:
  3137. dwc2_hcd_release(hsotg);
  3138. error2:
  3139. //error1:
  3140. dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval);
  3141. return retval;
  3142. }
  3143. /*
  3144. * Removes the HCD.
  3145. * Frees memory and resources associated with the HCD and deregisters the bus.
  3146. */
  3147. void dwc2_hcd_remove(struct dwc2_hsotg *hsotg)
  3148. {
  3149. struct usb_hcd *hcd;
  3150. dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n");
  3151. hcd = dwc2_hsotg_to_hcd(hsotg);
  3152. dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd);
  3153. if (!hcd) {
  3154. dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n",
  3155. __func__);
  3156. return;
  3157. }
  3158. hsotg->priv = NULL;
  3159. dwc2_hcd_release(hsotg);
  3160. }