core.c 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860
  1. /*
  2. * core.c - DesignWare HS OTG Controller common 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. * The Core code provides basic services for accessing and managing the
  38. * DWC_otg hardware. These services are used by both the Host Controller
  39. * Driver and the Peripheral Controller Driver.
  40. */
  41. #include <linux/kernel.h>
  42. #include <linux/module.h>
  43. #include <linux/moduleparam.h>
  44. #include <linux/spinlock.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/dma-mapping.h>
  47. #include <linux/delay.h>
  48. #include <linux/io.h>
  49. #include <linux/slab.h>
  50. #include <linux/usb.h>
  51. #include <linux/usb/hcd.h>
  52. #include <linux/usb/ch11.h>
  53. #include "core.h"
  54. #include "hcd.h"
  55. /**
  56. * dwc2_enable_common_interrupts() - Initializes the commmon interrupts,
  57. * used in both device and host modes
  58. *
  59. * @hsotg: Programming view of the DWC_otg controller
  60. */
  61. static void dwc2_enable_common_interrupts(struct dwc2_hsotg *hsotg)
  62. {
  63. u32 intmsk;
  64. /* Clear any pending OTG Interrupts */
  65. writel(0xffffffff, hsotg->regs + GOTGINT);
  66. /* Clear any pending interrupts */
  67. writel(0xffffffff, hsotg->regs + GINTSTS);
  68. /* Enable the interrupts in the GINTMSK */
  69. intmsk = GINTSTS_MODEMIS | GINTSTS_OTGINT;
  70. if (hsotg->core_params->dma_enable <= 0)
  71. intmsk |= GINTSTS_RXFLVL;
  72. intmsk |= GINTSTS_CONIDSTSCHNG | GINTSTS_WKUPINT | GINTSTS_USBSUSP |
  73. GINTSTS_SESSREQINT;
  74. writel(intmsk, hsotg->regs + GINTMSK);
  75. }
  76. /*
  77. * Initializes the FSLSPClkSel field of the HCFG register depending on the
  78. * PHY type
  79. */
  80. static void dwc2_init_fs_ls_pclk_sel(struct dwc2_hsotg *hsotg)
  81. {
  82. u32 hcfg, val;
  83. if ((hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
  84. hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
  85. hsotg->core_params->ulpi_fs_ls > 0) ||
  86. hsotg->core_params->phy_type == DWC2_PHY_TYPE_PARAM_FS) {
  87. /* Full speed PHY */
  88. val = HCFG_FSLSPCLKSEL_48_MHZ;
  89. } else {
  90. /* High speed PHY running at full speed or high speed */
  91. val = HCFG_FSLSPCLKSEL_30_60_MHZ;
  92. }
  93. dev_dbg(hsotg->dev, "Initializing HCFG.FSLSPClkSel to %08x\n", val);
  94. hcfg = readl(hsotg->regs + HCFG);
  95. hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
  96. hcfg |= val << HCFG_FSLSPCLKSEL_SHIFT;
  97. writel(hcfg, hsotg->regs + HCFG);
  98. }
  99. /*
  100. * Do core a soft reset of the core. Be careful with this because it
  101. * resets all the internal state machines of the core.
  102. */
  103. static int dwc2_core_reset(struct dwc2_hsotg *hsotg)
  104. {
  105. u32 greset;
  106. int count = 0;
  107. u32 gusbcfg;
  108. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  109. /* Wait for AHB master IDLE state */
  110. do {
  111. usleep_range(20000, 40000);
  112. greset = readl(hsotg->regs + GRSTCTL);
  113. if (++count > 50) {
  114. dev_warn(hsotg->dev,
  115. "%s() HANG! AHB Idle GRSTCTL=%0x\n",
  116. __func__, greset);
  117. return -EBUSY;
  118. }
  119. } while (!(greset & GRSTCTL_AHBIDLE));
  120. /* Core Soft Reset */
  121. count = 0;
  122. greset |= GRSTCTL_CSFTRST;
  123. writel(greset, hsotg->regs + GRSTCTL);
  124. do {
  125. usleep_range(20000, 40000);
  126. greset = readl(hsotg->regs + GRSTCTL);
  127. if (++count > 50) {
  128. dev_warn(hsotg->dev,
  129. "%s() HANG! Soft Reset GRSTCTL=%0x\n",
  130. __func__, greset);
  131. return -EBUSY;
  132. }
  133. } while (greset & GRSTCTL_CSFTRST);
  134. if (hsotg->dr_mode == USB_DR_MODE_HOST) {
  135. gusbcfg = readl(hsotg->regs + GUSBCFG);
  136. gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
  137. gusbcfg |= GUSBCFG_FORCEHOSTMODE;
  138. writel(gusbcfg, hsotg->regs + GUSBCFG);
  139. } else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
  140. gusbcfg = readl(hsotg->regs + GUSBCFG);
  141. gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
  142. gusbcfg |= GUSBCFG_FORCEDEVMODE;
  143. writel(gusbcfg, hsotg->regs + GUSBCFG);
  144. } else if (hsotg->dr_mode == USB_DR_MODE_OTG) {
  145. gusbcfg = readl(hsotg->regs + GUSBCFG);
  146. gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
  147. gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
  148. writel(gusbcfg, hsotg->regs + GUSBCFG);
  149. }
  150. /*
  151. * NOTE: This long sleep is _very_ important, otherwise the core will
  152. * not stay in host mode after a connector ID change!
  153. */
  154. usleep_range(150000, 200000);
  155. return 0;
  156. }
  157. static int dwc2_fs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
  158. {
  159. u32 usbcfg, i2cctl;
  160. int retval = 0;
  161. /*
  162. * core_init() is now called on every switch so only call the
  163. * following for the first time through
  164. */
  165. if (select_phy) {
  166. dev_dbg(hsotg->dev, "FS PHY selected\n");
  167. usbcfg = readl(hsotg->regs + GUSBCFG);
  168. usbcfg |= GUSBCFG_PHYSEL;
  169. writel(usbcfg, hsotg->regs + GUSBCFG);
  170. /* Reset after a PHY select */
  171. retval = dwc2_core_reset(hsotg);
  172. if (retval) {
  173. dev_err(hsotg->dev, "%s() Reset failed, aborting",
  174. __func__);
  175. return retval;
  176. }
  177. }
  178. /*
  179. * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
  180. * do this on HNP Dev/Host mode switches (done in dev_init and
  181. * host_init).
  182. */
  183. if (dwc2_is_host_mode(hsotg))
  184. dwc2_init_fs_ls_pclk_sel(hsotg);
  185. if (hsotg->core_params->i2c_enable > 0) {
  186. dev_dbg(hsotg->dev, "FS PHY enabling I2C\n");
  187. /* Program GUSBCFG.OtgUtmiFsSel to I2C */
  188. usbcfg = readl(hsotg->regs + GUSBCFG);
  189. usbcfg |= GUSBCFG_OTG_UTMI_FS_SEL;
  190. writel(usbcfg, hsotg->regs + GUSBCFG);
  191. /* Program GI2CCTL.I2CEn */
  192. i2cctl = readl(hsotg->regs + GI2CCTL);
  193. i2cctl &= ~GI2CCTL_I2CDEVADDR_MASK;
  194. i2cctl |= 1 << GI2CCTL_I2CDEVADDR_SHIFT;
  195. i2cctl &= ~GI2CCTL_I2CEN;
  196. writel(i2cctl, hsotg->regs + GI2CCTL);
  197. i2cctl |= GI2CCTL_I2CEN;
  198. writel(i2cctl, hsotg->regs + GI2CCTL);
  199. }
  200. return retval;
  201. }
  202. static int dwc2_hs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
  203. {
  204. u32 usbcfg;
  205. int retval = 0;
  206. if (!select_phy)
  207. return 0;
  208. usbcfg = readl(hsotg->regs + GUSBCFG);
  209. /*
  210. * HS PHY parameters. These parameters are preserved during soft reset
  211. * so only program the first time. Do a soft reset immediately after
  212. * setting phyif.
  213. */
  214. switch (hsotg->core_params->phy_type) {
  215. case DWC2_PHY_TYPE_PARAM_ULPI:
  216. /* ULPI interface */
  217. dev_dbg(hsotg->dev, "HS ULPI PHY selected\n");
  218. usbcfg |= GUSBCFG_ULPI_UTMI_SEL;
  219. usbcfg &= ~(GUSBCFG_PHYIF16 | GUSBCFG_DDRSEL);
  220. if (hsotg->core_params->phy_ulpi_ddr > 0)
  221. usbcfg |= GUSBCFG_DDRSEL;
  222. break;
  223. case DWC2_PHY_TYPE_PARAM_UTMI:
  224. /* UTMI+ interface */
  225. dev_dbg(hsotg->dev, "HS UTMI+ PHY selected\n");
  226. usbcfg &= ~(GUSBCFG_ULPI_UTMI_SEL | GUSBCFG_PHYIF16);
  227. if (hsotg->core_params->phy_utmi_width == 16)
  228. usbcfg |= GUSBCFG_PHYIF16;
  229. break;
  230. default:
  231. dev_err(hsotg->dev, "FS PHY selected at HS!\n");
  232. break;
  233. }
  234. writel(usbcfg, hsotg->regs + GUSBCFG);
  235. /* Reset after setting the PHY parameters */
  236. retval = dwc2_core_reset(hsotg);
  237. if (retval) {
  238. dev_err(hsotg->dev, "%s() Reset failed, aborting",
  239. __func__);
  240. return retval;
  241. }
  242. return retval;
  243. }
  244. static int dwc2_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
  245. {
  246. u32 usbcfg;
  247. int retval = 0;
  248. if (hsotg->core_params->speed == DWC2_SPEED_PARAM_FULL &&
  249. hsotg->core_params->phy_type == DWC2_PHY_TYPE_PARAM_FS) {
  250. /* If FS mode with FS PHY */
  251. retval = dwc2_fs_phy_init(hsotg, select_phy);
  252. if (retval)
  253. return retval;
  254. } else {
  255. /* High speed PHY */
  256. retval = dwc2_hs_phy_init(hsotg, select_phy);
  257. if (retval)
  258. return retval;
  259. }
  260. if (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
  261. hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
  262. hsotg->core_params->ulpi_fs_ls > 0) {
  263. dev_dbg(hsotg->dev, "Setting ULPI FSLS\n");
  264. usbcfg = readl(hsotg->regs + GUSBCFG);
  265. usbcfg |= GUSBCFG_ULPI_FS_LS;
  266. usbcfg |= GUSBCFG_ULPI_CLK_SUSP_M;
  267. writel(usbcfg, hsotg->regs + GUSBCFG);
  268. } else {
  269. usbcfg = readl(hsotg->regs + GUSBCFG);
  270. usbcfg &= ~GUSBCFG_ULPI_FS_LS;
  271. usbcfg &= ~GUSBCFG_ULPI_CLK_SUSP_M;
  272. writel(usbcfg, hsotg->regs + GUSBCFG);
  273. }
  274. return retval;
  275. }
  276. static int dwc2_gahbcfg_init(struct dwc2_hsotg *hsotg)
  277. {
  278. u32 ahbcfg = readl(hsotg->regs + GAHBCFG);
  279. switch (hsotg->hw_params.arch) {
  280. case GHWCFG2_EXT_DMA_ARCH:
  281. dev_err(hsotg->dev, "External DMA Mode not supported\n");
  282. return -EINVAL;
  283. case GHWCFG2_INT_DMA_ARCH:
  284. dev_dbg(hsotg->dev, "Internal DMA Mode\n");
  285. if (hsotg->core_params->ahbcfg != -1) {
  286. ahbcfg &= GAHBCFG_CTRL_MASK;
  287. ahbcfg |= hsotg->core_params->ahbcfg &
  288. ~GAHBCFG_CTRL_MASK;
  289. }
  290. break;
  291. case GHWCFG2_SLAVE_ONLY_ARCH:
  292. default:
  293. dev_dbg(hsotg->dev, "Slave Only Mode\n");
  294. break;
  295. }
  296. dev_dbg(hsotg->dev, "dma_enable:%d dma_desc_enable:%d\n",
  297. hsotg->core_params->dma_enable,
  298. hsotg->core_params->dma_desc_enable);
  299. if (hsotg->core_params->dma_enable > 0) {
  300. if (hsotg->core_params->dma_desc_enable > 0)
  301. dev_dbg(hsotg->dev, "Using Descriptor DMA mode\n");
  302. else
  303. dev_dbg(hsotg->dev, "Using Buffer DMA mode\n");
  304. } else {
  305. dev_dbg(hsotg->dev, "Using Slave mode\n");
  306. hsotg->core_params->dma_desc_enable = 0;
  307. }
  308. if (hsotg->core_params->dma_enable > 0)
  309. ahbcfg |= GAHBCFG_DMA_EN;
  310. writel(ahbcfg, hsotg->regs + GAHBCFG);
  311. return 0;
  312. }
  313. static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
  314. {
  315. u32 usbcfg;
  316. usbcfg = readl(hsotg->regs + GUSBCFG);
  317. usbcfg &= ~(GUSBCFG_HNPCAP | GUSBCFG_SRPCAP);
  318. switch (hsotg->hw_params.op_mode) {
  319. case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
  320. if (hsotg->core_params->otg_cap ==
  321. DWC2_CAP_PARAM_HNP_SRP_CAPABLE)
  322. usbcfg |= GUSBCFG_HNPCAP;
  323. if (hsotg->core_params->otg_cap !=
  324. DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
  325. usbcfg |= GUSBCFG_SRPCAP;
  326. break;
  327. case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
  328. case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
  329. case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
  330. if (hsotg->core_params->otg_cap !=
  331. DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
  332. usbcfg |= GUSBCFG_SRPCAP;
  333. break;
  334. case GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE:
  335. case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE:
  336. case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST:
  337. default:
  338. break;
  339. }
  340. writel(usbcfg, hsotg->regs + GUSBCFG);
  341. }
  342. /**
  343. * dwc2_core_init() - Initializes the DWC_otg controller registers and
  344. * prepares the core for device mode or host mode operation
  345. *
  346. * @hsotg: Programming view of the DWC_otg controller
  347. * @select_phy: If true then also set the Phy type
  348. * @irq: If >= 0, the irq to register
  349. */
  350. int dwc2_core_init(struct dwc2_hsotg *hsotg, bool select_phy, int irq)
  351. {
  352. u32 usbcfg, otgctl;
  353. int retval;
  354. dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
  355. usbcfg = readl(hsotg->regs + GUSBCFG);
  356. /* Set ULPI External VBUS bit if needed */
  357. usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
  358. if (hsotg->core_params->phy_ulpi_ext_vbus ==
  359. DWC2_PHY_ULPI_EXTERNAL_VBUS)
  360. usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV;
  361. /* Set external TS Dline pulsing bit if needed */
  362. usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
  363. if (hsotg->core_params->ts_dline > 0)
  364. usbcfg |= GUSBCFG_TERMSELDLPULSE;
  365. writel(usbcfg, hsotg->regs + GUSBCFG);
  366. /* Reset the Controller */
  367. retval = dwc2_core_reset(hsotg);
  368. if (retval) {
  369. dev_err(hsotg->dev, "%s(): Reset failed, aborting\n",
  370. __func__);
  371. return retval;
  372. }
  373. /*
  374. * This needs to happen in FS mode before any other programming occurs
  375. */
  376. retval = dwc2_phy_init(hsotg, select_phy);
  377. if (retval)
  378. return retval;
  379. /* Program the GAHBCFG Register */
  380. retval = dwc2_gahbcfg_init(hsotg);
  381. if (retval)
  382. return retval;
  383. /* Program the GUSBCFG register */
  384. dwc2_gusbcfg_init(hsotg);
  385. /* Program the GOTGCTL register */
  386. otgctl = readl(hsotg->regs + GOTGCTL);
  387. otgctl &= ~GOTGCTL_OTGVER;
  388. if (hsotg->core_params->otg_ver > 0)
  389. otgctl |= GOTGCTL_OTGVER;
  390. writel(otgctl, hsotg->regs + GOTGCTL);
  391. dev_dbg(hsotg->dev, "OTG VER PARAM: %d\n", hsotg->core_params->otg_ver);
  392. /* Clear the SRP success bit for FS-I2c */
  393. hsotg->srp_success = 0;
  394. /* Enable common interrupts */
  395. dwc2_enable_common_interrupts(hsotg);
  396. /*
  397. * Do device or host initialization based on mode during PCD and
  398. * HCD initialization
  399. */
  400. if (dwc2_is_host_mode(hsotg)) {
  401. dev_dbg(hsotg->dev, "Host Mode\n");
  402. hsotg->op_state = OTG_STATE_A_HOST;
  403. } else {
  404. dev_dbg(hsotg->dev, "Device Mode\n");
  405. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  406. }
  407. return 0;
  408. }
  409. /**
  410. * dwc2_enable_host_interrupts() - Enables the Host mode interrupts
  411. *
  412. * @hsotg: Programming view of DWC_otg controller
  413. */
  414. void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg)
  415. {
  416. u32 intmsk;
  417. dev_dbg(hsotg->dev, "%s()\n", __func__);
  418. /* Disable all interrupts */
  419. writel(0, hsotg->regs + GINTMSK);
  420. writel(0, hsotg->regs + HAINTMSK);
  421. /* Enable the common interrupts */
  422. dwc2_enable_common_interrupts(hsotg);
  423. /* Enable host mode interrupts without disturbing common interrupts */
  424. intmsk = readl(hsotg->regs + GINTMSK);
  425. intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT;
  426. writel(intmsk, hsotg->regs + GINTMSK);
  427. }
  428. /**
  429. * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts
  430. *
  431. * @hsotg: Programming view of DWC_otg controller
  432. */
  433. void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg)
  434. {
  435. u32 intmsk = readl(hsotg->regs + GINTMSK);
  436. /* Disable host mode interrupts without disturbing common interrupts */
  437. intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT |
  438. GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP);
  439. writel(intmsk, hsotg->regs + GINTMSK);
  440. }
  441. /*
  442. * dwc2_calculate_dynamic_fifo() - Calculates the default fifo size
  443. * For system that have a total fifo depth that is smaller than the default
  444. * RX + TX fifo size.
  445. *
  446. * @hsotg: Programming view of DWC_otg controller
  447. */
  448. static void dwc2_calculate_dynamic_fifo(struct dwc2_hsotg *hsotg)
  449. {
  450. struct dwc2_core_params *params = hsotg->core_params;
  451. struct dwc2_hw_params *hw = &hsotg->hw_params;
  452. u32 rxfsiz, nptxfsiz, ptxfsiz, total_fifo_size;
  453. total_fifo_size = hw->total_fifo_size;
  454. rxfsiz = params->host_rx_fifo_size;
  455. nptxfsiz = params->host_nperio_tx_fifo_size;
  456. ptxfsiz = params->host_perio_tx_fifo_size;
  457. /*
  458. * Will use Method 2 defined in the DWC2 spec: minimum FIFO depth
  459. * allocation with support for high bandwidth endpoints. Synopsys
  460. * defines MPS(Max Packet size) for a periodic EP=1024, and for
  461. * non-periodic as 512.
  462. */
  463. if (total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)) {
  464. /*
  465. * For Buffer DMA mode/Scatter Gather DMA mode
  466. * 2 * ((Largest Packet size / 4) + 1 + 1) + n
  467. * with n = number of host channel.
  468. * 2 * ((1024/4) + 2) = 516
  469. */
  470. rxfsiz = 516 + hw->host_channels;
  471. /*
  472. * min non-periodic tx fifo depth
  473. * 2 * (largest non-periodic USB packet used / 4)
  474. * 2 * (512/4) = 256
  475. */
  476. nptxfsiz = 256;
  477. /*
  478. * min periodic tx fifo depth
  479. * (largest packet size*MC)/4
  480. * (1024 * 3)/4 = 768
  481. */
  482. ptxfsiz = 768;
  483. params->host_rx_fifo_size = rxfsiz;
  484. params->host_nperio_tx_fifo_size = nptxfsiz;
  485. params->host_perio_tx_fifo_size = ptxfsiz;
  486. }
  487. /*
  488. * If the summation of RX, NPTX and PTX fifo sizes is still
  489. * bigger than the total_fifo_size, then we have a problem.
  490. *
  491. * We won't be able to allocate as many endpoints. Right now,
  492. * we're just printing an error message, but ideally this FIFO
  493. * allocation algorithm would be improved in the future.
  494. *
  495. * FIXME improve this FIFO allocation algorithm.
  496. */
  497. if (unlikely(total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)))
  498. dev_err(hsotg->dev, "invalid fifo sizes\n");
  499. }
  500. static void dwc2_config_fifos(struct dwc2_hsotg *hsotg)
  501. {
  502. struct dwc2_core_params *params = hsotg->core_params;
  503. u32 nptxfsiz, hptxfsiz, dfifocfg, grxfsiz;
  504. if (!params->enable_dynamic_fifo)
  505. return;
  506. dwc2_calculate_dynamic_fifo(hsotg);
  507. /* Rx FIFO */
  508. grxfsiz = readl(hsotg->regs + GRXFSIZ);
  509. dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n", grxfsiz);
  510. grxfsiz &= ~GRXFSIZ_DEPTH_MASK;
  511. grxfsiz |= params->host_rx_fifo_size <<
  512. GRXFSIZ_DEPTH_SHIFT & GRXFSIZ_DEPTH_MASK;
  513. writel(grxfsiz, hsotg->regs + GRXFSIZ);
  514. dev_dbg(hsotg->dev, "new grxfsiz=%08x\n", readl(hsotg->regs + GRXFSIZ));
  515. /* Non-periodic Tx FIFO */
  516. dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n",
  517. readl(hsotg->regs + GNPTXFSIZ));
  518. nptxfsiz = params->host_nperio_tx_fifo_size <<
  519. FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
  520. nptxfsiz |= params->host_rx_fifo_size <<
  521. FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
  522. writel(nptxfsiz, hsotg->regs + GNPTXFSIZ);
  523. dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n",
  524. readl(hsotg->regs + GNPTXFSIZ));
  525. /* Periodic Tx FIFO */
  526. dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n",
  527. readl(hsotg->regs + HPTXFSIZ));
  528. hptxfsiz = params->host_perio_tx_fifo_size <<
  529. FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
  530. hptxfsiz |= (params->host_rx_fifo_size +
  531. params->host_nperio_tx_fifo_size) <<
  532. FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
  533. writel(hptxfsiz, hsotg->regs + HPTXFSIZ);
  534. dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n",
  535. readl(hsotg->regs + HPTXFSIZ));
  536. if (hsotg->core_params->en_multiple_tx_fifo > 0 &&
  537. hsotg->hw_params.snpsid <= DWC2_CORE_REV_2_94a) {
  538. /*
  539. * Global DFIFOCFG calculation for Host mode -
  540. * include RxFIFO, NPTXFIFO and HPTXFIFO
  541. */
  542. dfifocfg = readl(hsotg->regs + GDFIFOCFG);
  543. dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK;
  544. dfifocfg |= (params->host_rx_fifo_size +
  545. params->host_nperio_tx_fifo_size +
  546. params->host_perio_tx_fifo_size) <<
  547. GDFIFOCFG_EPINFOBASE_SHIFT &
  548. GDFIFOCFG_EPINFOBASE_MASK;
  549. writel(dfifocfg, hsotg->regs + GDFIFOCFG);
  550. }
  551. }
  552. /**
  553. * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
  554. * Host mode
  555. *
  556. * @hsotg: Programming view of DWC_otg controller
  557. *
  558. * This function flushes the Tx and Rx FIFOs and flushes any entries in the
  559. * request queues. Host channels are reset to ensure that they are ready for
  560. * performing transfers.
  561. */
  562. void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
  563. {
  564. u32 hcfg, hfir, otgctl;
  565. dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
  566. /* Restart the Phy Clock */
  567. writel(0, hsotg->regs + PCGCTL);
  568. /* Initialize Host Configuration Register */
  569. dwc2_init_fs_ls_pclk_sel(hsotg);
  570. if (hsotg->core_params->speed == DWC2_SPEED_PARAM_FULL) {
  571. hcfg = readl(hsotg->regs + HCFG);
  572. hcfg |= HCFG_FSLSSUPP;
  573. writel(hcfg, hsotg->regs + HCFG);
  574. }
  575. /*
  576. * This bit allows dynamic reloading of the HFIR register during
  577. * runtime. This bit needs to be programmed during initial configuration
  578. * and its value must not be changed during runtime.
  579. */
  580. if (hsotg->core_params->reload_ctl > 0) {
  581. hfir = readl(hsotg->regs + HFIR);
  582. hfir |= HFIR_RLDCTRL;
  583. writel(hfir, hsotg->regs + HFIR);
  584. }
  585. if (hsotg->core_params->dma_desc_enable > 0) {
  586. u32 op_mode = hsotg->hw_params.op_mode;
  587. if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
  588. !hsotg->hw_params.dma_desc_enable ||
  589. op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
  590. op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
  591. op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
  592. dev_err(hsotg->dev,
  593. "Hardware does not support descriptor DMA mode -\n");
  594. dev_err(hsotg->dev,
  595. "falling back to buffer DMA mode.\n");
  596. hsotg->core_params->dma_desc_enable = 0;
  597. } else {
  598. hcfg = readl(hsotg->regs + HCFG);
  599. hcfg |= HCFG_DESCDMA;
  600. writel(hcfg, hsotg->regs + HCFG);
  601. }
  602. }
  603. /* Configure data FIFO sizes */
  604. dwc2_config_fifos(hsotg);
  605. /* TODO - check this */
  606. /* Clear Host Set HNP Enable in the OTG Control Register */
  607. otgctl = readl(hsotg->regs + GOTGCTL);
  608. otgctl &= ~GOTGCTL_HSTSETHNPEN;
  609. writel(otgctl, hsotg->regs + GOTGCTL);
  610. /* Make sure the FIFOs are flushed */
  611. dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
  612. dwc2_flush_rx_fifo(hsotg);
  613. /* Clear Host Set HNP Enable in the OTG Control Register */
  614. otgctl = readl(hsotg->regs + GOTGCTL);
  615. otgctl &= ~GOTGCTL_HSTSETHNPEN;
  616. writel(otgctl, hsotg->regs + GOTGCTL);
  617. if (hsotg->core_params->dma_desc_enable <= 0) {
  618. int num_channels, i;
  619. u32 hcchar;
  620. /* Flush out any leftover queued requests */
  621. num_channels = hsotg->core_params->host_channels;
  622. for (i = 0; i < num_channels; i++) {
  623. hcchar = readl(hsotg->regs + HCCHAR(i));
  624. hcchar &= ~HCCHAR_CHENA;
  625. hcchar |= HCCHAR_CHDIS;
  626. hcchar &= ~HCCHAR_EPDIR;
  627. writel(hcchar, hsotg->regs + HCCHAR(i));
  628. }
  629. /* Halt all channels to put them into a known state */
  630. for (i = 0; i < num_channels; i++) {
  631. int count = 0;
  632. hcchar = readl(hsotg->regs + HCCHAR(i));
  633. hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
  634. hcchar &= ~HCCHAR_EPDIR;
  635. writel(hcchar, hsotg->regs + HCCHAR(i));
  636. dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
  637. __func__, i);
  638. do {
  639. hcchar = readl(hsotg->regs + HCCHAR(i));
  640. if (++count > 1000) {
  641. dev_err(hsotg->dev,
  642. "Unable to clear enable on channel %d\n",
  643. i);
  644. break;
  645. }
  646. udelay(1);
  647. } while (hcchar & HCCHAR_CHENA);
  648. }
  649. }
  650. /* Turn on the vbus power */
  651. dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
  652. if (hsotg->op_state == OTG_STATE_A_HOST) {
  653. u32 hprt0 = dwc2_read_hprt0(hsotg);
  654. dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
  655. !!(hprt0 & HPRT0_PWR));
  656. if (!(hprt0 & HPRT0_PWR)) {
  657. hprt0 |= HPRT0_PWR;
  658. writel(hprt0, hsotg->regs + HPRT0);
  659. }
  660. }
  661. dwc2_enable_host_interrupts(hsotg);
  662. }
  663. static void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg,
  664. struct dwc2_host_chan *chan)
  665. {
  666. u32 hcintmsk = HCINTMSK_CHHLTD;
  667. switch (chan->ep_type) {
  668. case USB_ENDPOINT_XFER_CONTROL:
  669. case USB_ENDPOINT_XFER_BULK:
  670. dev_vdbg(hsotg->dev, "control/bulk\n");
  671. hcintmsk |= HCINTMSK_XFERCOMPL;
  672. hcintmsk |= HCINTMSK_STALL;
  673. hcintmsk |= HCINTMSK_XACTERR;
  674. hcintmsk |= HCINTMSK_DATATGLERR;
  675. if (chan->ep_is_in) {
  676. hcintmsk |= HCINTMSK_BBLERR;
  677. } else {
  678. hcintmsk |= HCINTMSK_NAK;
  679. hcintmsk |= HCINTMSK_NYET;
  680. if (chan->do_ping)
  681. hcintmsk |= HCINTMSK_ACK;
  682. }
  683. if (chan->do_split) {
  684. hcintmsk |= HCINTMSK_NAK;
  685. if (chan->complete_split)
  686. hcintmsk |= HCINTMSK_NYET;
  687. else
  688. hcintmsk |= HCINTMSK_ACK;
  689. }
  690. if (chan->error_state)
  691. hcintmsk |= HCINTMSK_ACK;
  692. break;
  693. case USB_ENDPOINT_XFER_INT:
  694. if (dbg_perio())
  695. dev_vdbg(hsotg->dev, "intr\n");
  696. hcintmsk |= HCINTMSK_XFERCOMPL;
  697. hcintmsk |= HCINTMSK_NAK;
  698. hcintmsk |= HCINTMSK_STALL;
  699. hcintmsk |= HCINTMSK_XACTERR;
  700. hcintmsk |= HCINTMSK_DATATGLERR;
  701. hcintmsk |= HCINTMSK_FRMOVRUN;
  702. if (chan->ep_is_in)
  703. hcintmsk |= HCINTMSK_BBLERR;
  704. if (chan->error_state)
  705. hcintmsk |= HCINTMSK_ACK;
  706. if (chan->do_split) {
  707. if (chan->complete_split)
  708. hcintmsk |= HCINTMSK_NYET;
  709. else
  710. hcintmsk |= HCINTMSK_ACK;
  711. }
  712. break;
  713. case USB_ENDPOINT_XFER_ISOC:
  714. if (dbg_perio())
  715. dev_vdbg(hsotg->dev, "isoc\n");
  716. hcintmsk |= HCINTMSK_XFERCOMPL;
  717. hcintmsk |= HCINTMSK_FRMOVRUN;
  718. hcintmsk |= HCINTMSK_ACK;
  719. if (chan->ep_is_in) {
  720. hcintmsk |= HCINTMSK_XACTERR;
  721. hcintmsk |= HCINTMSK_BBLERR;
  722. }
  723. break;
  724. default:
  725. dev_err(hsotg->dev, "## Unknown EP type ##\n");
  726. break;
  727. }
  728. writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
  729. if (dbg_hc(chan))
  730. dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
  731. }
  732. static void dwc2_hc_enable_dma_ints(struct dwc2_hsotg *hsotg,
  733. struct dwc2_host_chan *chan)
  734. {
  735. u32 hcintmsk = HCINTMSK_CHHLTD;
  736. /*
  737. * For Descriptor DMA mode core halts the channel on AHB error.
  738. * Interrupt is not required.
  739. */
  740. if (hsotg->core_params->dma_desc_enable <= 0) {
  741. if (dbg_hc(chan))
  742. dev_vdbg(hsotg->dev, "desc DMA disabled\n");
  743. hcintmsk |= HCINTMSK_AHBERR;
  744. } else {
  745. if (dbg_hc(chan))
  746. dev_vdbg(hsotg->dev, "desc DMA enabled\n");
  747. if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  748. hcintmsk |= HCINTMSK_XFERCOMPL;
  749. }
  750. if (chan->error_state && !chan->do_split &&
  751. chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
  752. if (dbg_hc(chan))
  753. dev_vdbg(hsotg->dev, "setting ACK\n");
  754. hcintmsk |= HCINTMSK_ACK;
  755. if (chan->ep_is_in) {
  756. hcintmsk |= HCINTMSK_DATATGLERR;
  757. if (chan->ep_type != USB_ENDPOINT_XFER_INT)
  758. hcintmsk |= HCINTMSK_NAK;
  759. }
  760. }
  761. writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
  762. if (dbg_hc(chan))
  763. dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
  764. }
  765. static void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg,
  766. struct dwc2_host_chan *chan)
  767. {
  768. u32 intmsk;
  769. if (hsotg->core_params->dma_enable > 0) {
  770. if (dbg_hc(chan))
  771. dev_vdbg(hsotg->dev, "DMA enabled\n");
  772. dwc2_hc_enable_dma_ints(hsotg, chan);
  773. } else {
  774. if (dbg_hc(chan))
  775. dev_vdbg(hsotg->dev, "DMA disabled\n");
  776. dwc2_hc_enable_slave_ints(hsotg, chan);
  777. }
  778. /* Enable the top level host channel interrupt */
  779. intmsk = readl(hsotg->regs + HAINTMSK);
  780. intmsk |= 1 << chan->hc_num;
  781. writel(intmsk, hsotg->regs + HAINTMSK);
  782. if (dbg_hc(chan))
  783. dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk);
  784. /* Make sure host channel interrupts are enabled */
  785. intmsk = readl(hsotg->regs + GINTMSK);
  786. intmsk |= GINTSTS_HCHINT;
  787. writel(intmsk, hsotg->regs + GINTMSK);
  788. if (dbg_hc(chan))
  789. dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk);
  790. }
  791. /**
  792. * dwc2_hc_init() - Prepares a host channel for transferring packets to/from
  793. * a specific endpoint
  794. *
  795. * @hsotg: Programming view of DWC_otg controller
  796. * @chan: Information needed to initialize the host channel
  797. *
  798. * The HCCHARn register is set up with the characteristics specified in chan.
  799. * Host channel interrupts that may need to be serviced while this transfer is
  800. * in progress are enabled.
  801. */
  802. void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
  803. {
  804. u8 hc_num = chan->hc_num;
  805. u32 hcintmsk;
  806. u32 hcchar;
  807. u32 hcsplt = 0;
  808. if (dbg_hc(chan))
  809. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  810. /* Clear old interrupt conditions for this host channel */
  811. hcintmsk = 0xffffffff;
  812. hcintmsk &= ~HCINTMSK_RESERVED14_31;
  813. writel(hcintmsk, hsotg->regs + HCINT(hc_num));
  814. /* Enable channel interrupts required for this transfer */
  815. dwc2_hc_enable_ints(hsotg, chan);
  816. /*
  817. * Program the HCCHARn register with the endpoint characteristics for
  818. * the current transfer
  819. */
  820. hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK;
  821. hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK;
  822. if (chan->ep_is_in)
  823. hcchar |= HCCHAR_EPDIR;
  824. if (chan->speed == USB_SPEED_LOW)
  825. hcchar |= HCCHAR_LSPDDEV;
  826. hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK;
  827. hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK;
  828. writel(hcchar, hsotg->regs + HCCHAR(hc_num));
  829. if (dbg_hc(chan)) {
  830. dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n",
  831. hc_num, hcchar);
  832. dev_vdbg(hsotg->dev, "%s: Channel %d\n",
  833. __func__, hc_num);
  834. dev_vdbg(hsotg->dev, " Dev Addr: %d\n",
  835. chan->dev_addr);
  836. dev_vdbg(hsotg->dev, " Ep Num: %d\n",
  837. chan->ep_num);
  838. dev_vdbg(hsotg->dev, " Is In: %d\n",
  839. chan->ep_is_in);
  840. dev_vdbg(hsotg->dev, " Is Low Speed: %d\n",
  841. chan->speed == USB_SPEED_LOW);
  842. dev_vdbg(hsotg->dev, " Ep Type: %d\n",
  843. chan->ep_type);
  844. dev_vdbg(hsotg->dev, " Max Pkt: %d\n",
  845. chan->max_packet);
  846. }
  847. /* Program the HCSPLT register for SPLITs */
  848. if (chan->do_split) {
  849. if (dbg_hc(chan))
  850. dev_vdbg(hsotg->dev,
  851. "Programming HC %d with split --> %s\n",
  852. hc_num,
  853. chan->complete_split ? "CSPLIT" : "SSPLIT");
  854. if (chan->complete_split)
  855. hcsplt |= HCSPLT_COMPSPLT;
  856. hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT &
  857. HCSPLT_XACTPOS_MASK;
  858. hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT &
  859. HCSPLT_HUBADDR_MASK;
  860. hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT &
  861. HCSPLT_PRTADDR_MASK;
  862. if (dbg_hc(chan)) {
  863. dev_vdbg(hsotg->dev, " comp split %d\n",
  864. chan->complete_split);
  865. dev_vdbg(hsotg->dev, " xact pos %d\n",
  866. chan->xact_pos);
  867. dev_vdbg(hsotg->dev, " hub addr %d\n",
  868. chan->hub_addr);
  869. dev_vdbg(hsotg->dev, " hub port %d\n",
  870. chan->hub_port);
  871. dev_vdbg(hsotg->dev, " is_in %d\n",
  872. chan->ep_is_in);
  873. dev_vdbg(hsotg->dev, " Max Pkt %d\n",
  874. chan->max_packet);
  875. dev_vdbg(hsotg->dev, " xferlen %d\n",
  876. chan->xfer_len);
  877. }
  878. }
  879. writel(hcsplt, hsotg->regs + HCSPLT(hc_num));
  880. }
  881. /**
  882. * dwc2_hc_halt() - Attempts to halt a host channel
  883. *
  884. * @hsotg: Controller register interface
  885. * @chan: Host channel to halt
  886. * @halt_status: Reason for halting the channel
  887. *
  888. * This function should only be called in Slave mode or to abort a transfer in
  889. * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the
  890. * controller halts the channel when the transfer is complete or a condition
  891. * occurs that requires application intervention.
  892. *
  893. * In slave mode, checks for a free request queue entry, then sets the Channel
  894. * Enable and Channel Disable bits of the Host Channel Characteristics
  895. * register of the specified channel to intiate the halt. If there is no free
  896. * request queue entry, sets only the Channel Disable bit of the HCCHARn
  897. * register to flush requests for this channel. In the latter case, sets a
  898. * flag to indicate that the host channel needs to be halted when a request
  899. * queue slot is open.
  900. *
  901. * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
  902. * HCCHARn register. The controller ensures there is space in the request
  903. * queue before submitting the halt request.
  904. *
  905. * Some time may elapse before the core flushes any posted requests for this
  906. * host channel and halts. The Channel Halted interrupt handler completes the
  907. * deactivation of the host channel.
  908. */
  909. void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
  910. enum dwc2_halt_status halt_status)
  911. {
  912. u32 nptxsts, hptxsts, hcchar;
  913. if (dbg_hc(chan))
  914. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  915. if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
  916. dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
  917. if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
  918. halt_status == DWC2_HC_XFER_AHB_ERR) {
  919. /*
  920. * Disable all channel interrupts except Ch Halted. The QTD
  921. * and QH state associated with this transfer has been cleared
  922. * (in the case of URB_DEQUEUE), so the channel needs to be
  923. * shut down carefully to prevent crashes.
  924. */
  925. u32 hcintmsk = HCINTMSK_CHHLTD;
  926. dev_vdbg(hsotg->dev, "dequeue/error\n");
  927. writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
  928. /*
  929. * Make sure no other interrupts besides halt are currently
  930. * pending. Handling another interrupt could cause a crash due
  931. * to the QTD and QH state.
  932. */
  933. writel(~hcintmsk, hsotg->regs + HCINT(chan->hc_num));
  934. /*
  935. * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
  936. * even if the channel was already halted for some other
  937. * reason
  938. */
  939. chan->halt_status = halt_status;
  940. hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
  941. if (!(hcchar & HCCHAR_CHENA)) {
  942. /*
  943. * The channel is either already halted or it hasn't
  944. * started yet. In DMA mode, the transfer may halt if
  945. * it finishes normally or a condition occurs that
  946. * requires driver intervention. Don't want to halt
  947. * the channel again. In either Slave or DMA mode,
  948. * it's possible that the transfer has been assigned
  949. * to a channel, but not started yet when an URB is
  950. * dequeued. Don't want to halt a channel that hasn't
  951. * started yet.
  952. */
  953. return;
  954. }
  955. }
  956. if (chan->halt_pending) {
  957. /*
  958. * A halt has already been issued for this channel. This might
  959. * happen when a transfer is aborted by a higher level in
  960. * the stack.
  961. */
  962. dev_vdbg(hsotg->dev,
  963. "*** %s: Channel %d, chan->halt_pending already set ***\n",
  964. __func__, chan->hc_num);
  965. return;
  966. }
  967. hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
  968. /* No need to set the bit in DDMA for disabling the channel */
  969. /* TODO check it everywhere channel is disabled */
  970. if (hsotg->core_params->dma_desc_enable <= 0) {
  971. if (dbg_hc(chan))
  972. dev_vdbg(hsotg->dev, "desc DMA disabled\n");
  973. hcchar |= HCCHAR_CHENA;
  974. } else {
  975. if (dbg_hc(chan))
  976. dev_dbg(hsotg->dev, "desc DMA enabled\n");
  977. }
  978. hcchar |= HCCHAR_CHDIS;
  979. if (hsotg->core_params->dma_enable <= 0) {
  980. if (dbg_hc(chan))
  981. dev_vdbg(hsotg->dev, "DMA not enabled\n");
  982. hcchar |= HCCHAR_CHENA;
  983. /* Check for space in the request queue to issue the halt */
  984. if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
  985. chan->ep_type == USB_ENDPOINT_XFER_BULK) {
  986. dev_vdbg(hsotg->dev, "control/bulk\n");
  987. nptxsts = readl(hsotg->regs + GNPTXSTS);
  988. if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
  989. dev_vdbg(hsotg->dev, "Disabling channel\n");
  990. hcchar &= ~HCCHAR_CHENA;
  991. }
  992. } else {
  993. if (dbg_perio())
  994. dev_vdbg(hsotg->dev, "isoc/intr\n");
  995. hptxsts = readl(hsotg->regs + HPTXSTS);
  996. if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 ||
  997. hsotg->queuing_high_bandwidth) {
  998. if (dbg_perio())
  999. dev_vdbg(hsotg->dev, "Disabling channel\n");
  1000. hcchar &= ~HCCHAR_CHENA;
  1001. }
  1002. }
  1003. } else {
  1004. if (dbg_hc(chan))
  1005. dev_vdbg(hsotg->dev, "DMA enabled\n");
  1006. }
  1007. writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
  1008. chan->halt_status = halt_status;
  1009. if (hcchar & HCCHAR_CHENA) {
  1010. if (dbg_hc(chan))
  1011. dev_vdbg(hsotg->dev, "Channel enabled\n");
  1012. chan->halt_pending = 1;
  1013. chan->halt_on_queue = 0;
  1014. } else {
  1015. if (dbg_hc(chan))
  1016. dev_vdbg(hsotg->dev, "Channel disabled\n");
  1017. chan->halt_on_queue = 1;
  1018. }
  1019. if (dbg_hc(chan)) {
  1020. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1021. chan->hc_num);
  1022. dev_vdbg(hsotg->dev, " hcchar: 0x%08x\n",
  1023. hcchar);
  1024. dev_vdbg(hsotg->dev, " halt_pending: %d\n",
  1025. chan->halt_pending);
  1026. dev_vdbg(hsotg->dev, " halt_on_queue: %d\n",
  1027. chan->halt_on_queue);
  1028. dev_vdbg(hsotg->dev, " halt_status: %d\n",
  1029. chan->halt_status);
  1030. }
  1031. }
  1032. /**
  1033. * dwc2_hc_cleanup() - Clears the transfer state for a host channel
  1034. *
  1035. * @hsotg: Programming view of DWC_otg controller
  1036. * @chan: Identifies the host channel to clean up
  1037. *
  1038. * This function is normally called after a transfer is done and the host
  1039. * channel is being released
  1040. */
  1041. void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
  1042. {
  1043. u32 hcintmsk;
  1044. chan->xfer_started = 0;
  1045. /*
  1046. * Clear channel interrupt enables and any unhandled channel interrupt
  1047. * conditions
  1048. */
  1049. writel(0, hsotg->regs + HCINTMSK(chan->hc_num));
  1050. hcintmsk = 0xffffffff;
  1051. hcintmsk &= ~HCINTMSK_RESERVED14_31;
  1052. writel(hcintmsk, hsotg->regs + HCINT(chan->hc_num));
  1053. }
  1054. /**
  1055. * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
  1056. * which frame a periodic transfer should occur
  1057. *
  1058. * @hsotg: Programming view of DWC_otg controller
  1059. * @chan: Identifies the host channel to set up and its properties
  1060. * @hcchar: Current value of the HCCHAR register for the specified host channel
  1061. *
  1062. * This function has no effect on non-periodic transfers
  1063. */
  1064. static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
  1065. struct dwc2_host_chan *chan, u32 *hcchar)
  1066. {
  1067. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1068. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  1069. /* 1 if _next_ frame is odd, 0 if it's even */
  1070. if (!(dwc2_hcd_get_frame_number(hsotg) & 0x1))
  1071. *hcchar |= HCCHAR_ODDFRM;
  1072. }
  1073. }
  1074. static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
  1075. {
  1076. /* Set up the initial PID for the transfer */
  1077. if (chan->speed == USB_SPEED_HIGH) {
  1078. if (chan->ep_is_in) {
  1079. if (chan->multi_count == 1)
  1080. chan->data_pid_start = DWC2_HC_PID_DATA0;
  1081. else if (chan->multi_count == 2)
  1082. chan->data_pid_start = DWC2_HC_PID_DATA1;
  1083. else
  1084. chan->data_pid_start = DWC2_HC_PID_DATA2;
  1085. } else {
  1086. if (chan->multi_count == 1)
  1087. chan->data_pid_start = DWC2_HC_PID_DATA0;
  1088. else
  1089. chan->data_pid_start = DWC2_HC_PID_MDATA;
  1090. }
  1091. } else {
  1092. chan->data_pid_start = DWC2_HC_PID_DATA0;
  1093. }
  1094. }
  1095. /**
  1096. * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
  1097. * the Host Channel
  1098. *
  1099. * @hsotg: Programming view of DWC_otg controller
  1100. * @chan: Information needed to initialize the host channel
  1101. *
  1102. * This function should only be called in Slave mode. For a channel associated
  1103. * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
  1104. * associated with a periodic EP, the periodic Tx FIFO is written.
  1105. *
  1106. * Upon return the xfer_buf and xfer_count fields in chan are incremented by
  1107. * the number of bytes written to the Tx FIFO.
  1108. */
  1109. static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
  1110. struct dwc2_host_chan *chan)
  1111. {
  1112. u32 i;
  1113. u32 remaining_count;
  1114. u32 byte_count;
  1115. u32 dword_count;
  1116. u32 __iomem *data_fifo;
  1117. u32 *data_buf = (u32 *)chan->xfer_buf;
  1118. if (dbg_hc(chan))
  1119. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  1120. data_fifo = (u32 __iomem *)(hsotg->regs + HCFIFO(chan->hc_num));
  1121. remaining_count = chan->xfer_len - chan->xfer_count;
  1122. if (remaining_count > chan->max_packet)
  1123. byte_count = chan->max_packet;
  1124. else
  1125. byte_count = remaining_count;
  1126. dword_count = (byte_count + 3) / 4;
  1127. if (((unsigned long)data_buf & 0x3) == 0) {
  1128. /* xfer_buf is DWORD aligned */
  1129. for (i = 0; i < dword_count; i++, data_buf++)
  1130. writel(*data_buf, data_fifo);
  1131. } else {
  1132. /* xfer_buf is not DWORD aligned */
  1133. for (i = 0; i < dword_count; i++, data_buf++) {
  1134. u32 data = data_buf[0] | data_buf[1] << 8 |
  1135. data_buf[2] << 16 | data_buf[3] << 24;
  1136. writel(data, data_fifo);
  1137. }
  1138. }
  1139. chan->xfer_count += byte_count;
  1140. chan->xfer_buf += byte_count;
  1141. }
  1142. /**
  1143. * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
  1144. * channel and starts the transfer
  1145. *
  1146. * @hsotg: Programming view of DWC_otg controller
  1147. * @chan: Information needed to initialize the host channel. The xfer_len value
  1148. * may be reduced to accommodate the max widths of the XferSize and
  1149. * PktCnt fields in the HCTSIZn register. The multi_count value may be
  1150. * changed to reflect the final xfer_len value.
  1151. *
  1152. * This function may be called in either Slave mode or DMA mode. In Slave mode,
  1153. * the caller must ensure that there is sufficient space in the request queue
  1154. * and Tx Data FIFO.
  1155. *
  1156. * For an OUT transfer in Slave mode, it loads a data packet into the
  1157. * appropriate FIFO. If necessary, additional data packets are loaded in the
  1158. * Host ISR.
  1159. *
  1160. * For an IN transfer in Slave mode, a data packet is requested. The data
  1161. * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
  1162. * additional data packets are requested in the Host ISR.
  1163. *
  1164. * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
  1165. * register along with a packet count of 1 and the channel is enabled. This
  1166. * causes a single PING transaction to occur. Other fields in HCTSIZ are
  1167. * simply set to 0 since no data transfer occurs in this case.
  1168. *
  1169. * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
  1170. * all the information required to perform the subsequent data transfer. In
  1171. * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
  1172. * controller performs the entire PING protocol, then starts the data
  1173. * transfer.
  1174. */
  1175. void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
  1176. struct dwc2_host_chan *chan)
  1177. {
  1178. u32 max_hc_xfer_size = hsotg->core_params->max_transfer_size;
  1179. u16 max_hc_pkt_count = hsotg->core_params->max_packet_count;
  1180. u32 hcchar;
  1181. u32 hctsiz = 0;
  1182. u16 num_packets;
  1183. if (dbg_hc(chan))
  1184. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  1185. if (chan->do_ping) {
  1186. if (hsotg->core_params->dma_enable <= 0) {
  1187. if (dbg_hc(chan))
  1188. dev_vdbg(hsotg->dev, "ping, no DMA\n");
  1189. dwc2_hc_do_ping(hsotg, chan);
  1190. chan->xfer_started = 1;
  1191. return;
  1192. } else {
  1193. if (dbg_hc(chan))
  1194. dev_vdbg(hsotg->dev, "ping, DMA\n");
  1195. hctsiz |= TSIZ_DOPNG;
  1196. }
  1197. }
  1198. if (chan->do_split) {
  1199. if (dbg_hc(chan))
  1200. dev_vdbg(hsotg->dev, "split\n");
  1201. num_packets = 1;
  1202. if (chan->complete_split && !chan->ep_is_in)
  1203. /*
  1204. * For CSPLIT OUT Transfer, set the size to 0 so the
  1205. * core doesn't expect any data written to the FIFO
  1206. */
  1207. chan->xfer_len = 0;
  1208. else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
  1209. chan->xfer_len = chan->max_packet;
  1210. else if (!chan->ep_is_in && chan->xfer_len > 188)
  1211. chan->xfer_len = 188;
  1212. hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
  1213. TSIZ_XFERSIZE_MASK;
  1214. } else {
  1215. if (dbg_hc(chan))
  1216. dev_vdbg(hsotg->dev, "no split\n");
  1217. /*
  1218. * Ensure that the transfer length and packet count will fit
  1219. * in the widths allocated for them in the HCTSIZn register
  1220. */
  1221. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1222. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  1223. /*
  1224. * Make sure the transfer size is no larger than one
  1225. * (micro)frame's worth of data. (A check was done
  1226. * when the periodic transfer was accepted to ensure
  1227. * that a (micro)frame's worth of data can be
  1228. * programmed into a channel.)
  1229. */
  1230. u32 max_periodic_len =
  1231. chan->multi_count * chan->max_packet;
  1232. if (chan->xfer_len > max_periodic_len)
  1233. chan->xfer_len = max_periodic_len;
  1234. } else if (chan->xfer_len > max_hc_xfer_size) {
  1235. /*
  1236. * Make sure that xfer_len is a multiple of max packet
  1237. * size
  1238. */
  1239. chan->xfer_len =
  1240. max_hc_xfer_size - chan->max_packet + 1;
  1241. }
  1242. if (chan->xfer_len > 0) {
  1243. num_packets = (chan->xfer_len + chan->max_packet - 1) /
  1244. chan->max_packet;
  1245. if (num_packets > max_hc_pkt_count) {
  1246. num_packets = max_hc_pkt_count;
  1247. chan->xfer_len = num_packets * chan->max_packet;
  1248. }
  1249. } else {
  1250. /* Need 1 packet for transfer length of 0 */
  1251. num_packets = 1;
  1252. }
  1253. if (chan->ep_is_in)
  1254. /*
  1255. * Always program an integral # of max packets for IN
  1256. * transfers
  1257. */
  1258. chan->xfer_len = num_packets * chan->max_packet;
  1259. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1260. chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  1261. /*
  1262. * Make sure that the multi_count field matches the
  1263. * actual transfer length
  1264. */
  1265. chan->multi_count = num_packets;
  1266. if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  1267. dwc2_set_pid_isoc(chan);
  1268. hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
  1269. TSIZ_XFERSIZE_MASK;
  1270. }
  1271. chan->start_pkt_count = num_packets;
  1272. hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
  1273. hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
  1274. TSIZ_SC_MC_PID_MASK;
  1275. writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
  1276. if (dbg_hc(chan)) {
  1277. dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
  1278. hctsiz, chan->hc_num);
  1279. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1280. chan->hc_num);
  1281. dev_vdbg(hsotg->dev, " Xfer Size: %d\n",
  1282. (hctsiz & TSIZ_XFERSIZE_MASK) >>
  1283. TSIZ_XFERSIZE_SHIFT);
  1284. dev_vdbg(hsotg->dev, " Num Pkts: %d\n",
  1285. (hctsiz & TSIZ_PKTCNT_MASK) >>
  1286. TSIZ_PKTCNT_SHIFT);
  1287. dev_vdbg(hsotg->dev, " Start PID: %d\n",
  1288. (hctsiz & TSIZ_SC_MC_PID_MASK) >>
  1289. TSIZ_SC_MC_PID_SHIFT);
  1290. }
  1291. if (hsotg->core_params->dma_enable > 0) {
  1292. dma_addr_t dma_addr;
  1293. if (chan->align_buf) {
  1294. if (dbg_hc(chan))
  1295. dev_vdbg(hsotg->dev, "align_buf\n");
  1296. dma_addr = chan->align_buf;
  1297. } else {
  1298. dma_addr = chan->xfer_dma;
  1299. }
  1300. writel((u32)dma_addr, hsotg->regs + HCDMA(chan->hc_num));
  1301. if (dbg_hc(chan))
  1302. dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
  1303. (unsigned long)dma_addr, chan->hc_num);
  1304. }
  1305. /* Start the split */
  1306. if (chan->do_split) {
  1307. u32 hcsplt = readl(hsotg->regs + HCSPLT(chan->hc_num));
  1308. hcsplt |= HCSPLT_SPLTENA;
  1309. writel(hcsplt, hsotg->regs + HCSPLT(chan->hc_num));
  1310. }
  1311. hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
  1312. hcchar &= ~HCCHAR_MULTICNT_MASK;
  1313. hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
  1314. HCCHAR_MULTICNT_MASK;
  1315. dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
  1316. if (hcchar & HCCHAR_CHDIS)
  1317. dev_warn(hsotg->dev,
  1318. "%s: chdis set, channel %d, hcchar 0x%08x\n",
  1319. __func__, chan->hc_num, hcchar);
  1320. /* Set host channel enable after all other setup is complete */
  1321. hcchar |= HCCHAR_CHENA;
  1322. hcchar &= ~HCCHAR_CHDIS;
  1323. if (dbg_hc(chan))
  1324. dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
  1325. (hcchar & HCCHAR_MULTICNT_MASK) >>
  1326. HCCHAR_MULTICNT_SHIFT);
  1327. writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
  1328. if (dbg_hc(chan))
  1329. dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
  1330. chan->hc_num);
  1331. chan->xfer_started = 1;
  1332. chan->requests++;
  1333. if (hsotg->core_params->dma_enable <= 0 &&
  1334. !chan->ep_is_in && chan->xfer_len > 0)
  1335. /* Load OUT packet into the appropriate Tx FIFO */
  1336. dwc2_hc_write_packet(hsotg, chan);
  1337. }
  1338. /**
  1339. * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
  1340. * host channel and starts the transfer in Descriptor DMA mode
  1341. *
  1342. * @hsotg: Programming view of DWC_otg controller
  1343. * @chan: Information needed to initialize the host channel
  1344. *
  1345. * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
  1346. * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
  1347. * with micro-frame bitmap.
  1348. *
  1349. * Initializes HCDMA register with descriptor list address and CTD value then
  1350. * starts the transfer via enabling the channel.
  1351. */
  1352. void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
  1353. struct dwc2_host_chan *chan)
  1354. {
  1355. u32 hcchar;
  1356. u32 hc_dma;
  1357. u32 hctsiz = 0;
  1358. if (chan->do_ping)
  1359. hctsiz |= TSIZ_DOPNG;
  1360. if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
  1361. dwc2_set_pid_isoc(chan);
  1362. /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
  1363. hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
  1364. TSIZ_SC_MC_PID_MASK;
  1365. /* 0 - 1 descriptor, 1 - 2 descriptors, etc */
  1366. hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
  1367. /* Non-zero only for high-speed interrupt endpoints */
  1368. hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
  1369. if (dbg_hc(chan)) {
  1370. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1371. chan->hc_num);
  1372. dev_vdbg(hsotg->dev, " Start PID: %d\n",
  1373. chan->data_pid_start);
  1374. dev_vdbg(hsotg->dev, " NTD: %d\n", chan->ntd - 1);
  1375. }
  1376. writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
  1377. hc_dma = (u32)chan->desc_list_addr & HCDMA_DMA_ADDR_MASK;
  1378. /* Always start from first descriptor */
  1379. hc_dma &= ~HCDMA_CTD_MASK;
  1380. writel(hc_dma, hsotg->regs + HCDMA(chan->hc_num));
  1381. if (dbg_hc(chan))
  1382. dev_vdbg(hsotg->dev, "Wrote %08x to HCDMA(%d)\n",
  1383. hc_dma, chan->hc_num);
  1384. hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
  1385. hcchar &= ~HCCHAR_MULTICNT_MASK;
  1386. hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
  1387. HCCHAR_MULTICNT_MASK;
  1388. if (hcchar & HCCHAR_CHDIS)
  1389. dev_warn(hsotg->dev,
  1390. "%s: chdis set, channel %d, hcchar 0x%08x\n",
  1391. __func__, chan->hc_num, hcchar);
  1392. /* Set host channel enable after all other setup is complete */
  1393. hcchar |= HCCHAR_CHENA;
  1394. hcchar &= ~HCCHAR_CHDIS;
  1395. if (dbg_hc(chan))
  1396. dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
  1397. (hcchar & HCCHAR_MULTICNT_MASK) >>
  1398. HCCHAR_MULTICNT_SHIFT);
  1399. writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
  1400. if (dbg_hc(chan))
  1401. dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
  1402. chan->hc_num);
  1403. chan->xfer_started = 1;
  1404. chan->requests++;
  1405. }
  1406. /**
  1407. * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
  1408. * a previous call to dwc2_hc_start_transfer()
  1409. *
  1410. * @hsotg: Programming view of DWC_otg controller
  1411. * @chan: Information needed to initialize the host channel
  1412. *
  1413. * The caller must ensure there is sufficient space in the request queue and Tx
  1414. * Data FIFO. This function should only be called in Slave mode. In DMA mode,
  1415. * the controller acts autonomously to complete transfers programmed to a host
  1416. * channel.
  1417. *
  1418. * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
  1419. * if there is any data remaining to be queued. For an IN transfer, another
  1420. * data packet is always requested. For the SETUP phase of a control transfer,
  1421. * this function does nothing.
  1422. *
  1423. * Return: 1 if a new request is queued, 0 if no more requests are required
  1424. * for this transfer
  1425. */
  1426. int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
  1427. struct dwc2_host_chan *chan)
  1428. {
  1429. if (dbg_hc(chan))
  1430. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1431. chan->hc_num);
  1432. if (chan->do_split)
  1433. /* SPLITs always queue just once per channel */
  1434. return 0;
  1435. if (chan->data_pid_start == DWC2_HC_PID_SETUP)
  1436. /* SETUPs are queued only once since they can't be NAK'd */
  1437. return 0;
  1438. if (chan->ep_is_in) {
  1439. /*
  1440. * Always queue another request for other IN transfers. If
  1441. * back-to-back INs are issued and NAKs are received for both,
  1442. * the driver may still be processing the first NAK when the
  1443. * second NAK is received. When the interrupt handler clears
  1444. * the NAK interrupt for the first NAK, the second NAK will
  1445. * not be seen. So we can't depend on the NAK interrupt
  1446. * handler to requeue a NAK'd request. Instead, IN requests
  1447. * are issued each time this function is called. When the
  1448. * transfer completes, the extra requests for the channel will
  1449. * be flushed.
  1450. */
  1451. u32 hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
  1452. dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
  1453. hcchar |= HCCHAR_CHENA;
  1454. hcchar &= ~HCCHAR_CHDIS;
  1455. if (dbg_hc(chan))
  1456. dev_vdbg(hsotg->dev, " IN xfer: hcchar = 0x%08x\n",
  1457. hcchar);
  1458. writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
  1459. chan->requests++;
  1460. return 1;
  1461. }
  1462. /* OUT transfers */
  1463. if (chan->xfer_count < chan->xfer_len) {
  1464. if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
  1465. chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  1466. u32 hcchar = readl(hsotg->regs +
  1467. HCCHAR(chan->hc_num));
  1468. dwc2_hc_set_even_odd_frame(hsotg, chan,
  1469. &hcchar);
  1470. }
  1471. /* Load OUT packet into the appropriate Tx FIFO */
  1472. dwc2_hc_write_packet(hsotg, chan);
  1473. chan->requests++;
  1474. return 1;
  1475. }
  1476. return 0;
  1477. }
  1478. /**
  1479. * dwc2_hc_do_ping() - Starts a PING transfer
  1480. *
  1481. * @hsotg: Programming view of DWC_otg controller
  1482. * @chan: Information needed to initialize the host channel
  1483. *
  1484. * This function should only be called in Slave mode. The Do Ping bit is set in
  1485. * the HCTSIZ register, then the channel is enabled.
  1486. */
  1487. void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
  1488. {
  1489. u32 hcchar;
  1490. u32 hctsiz;
  1491. if (dbg_hc(chan))
  1492. dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
  1493. chan->hc_num);
  1494. hctsiz = TSIZ_DOPNG;
  1495. hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
  1496. writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
  1497. hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
  1498. hcchar |= HCCHAR_CHENA;
  1499. hcchar &= ~HCCHAR_CHDIS;
  1500. writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
  1501. }
  1502. /**
  1503. * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
  1504. * the HFIR register according to PHY type and speed
  1505. *
  1506. * @hsotg: Programming view of DWC_otg controller
  1507. *
  1508. * NOTE: The caller can modify the value of the HFIR register only after the
  1509. * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
  1510. * has been set
  1511. */
  1512. u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
  1513. {
  1514. u32 usbcfg;
  1515. u32 hprt0;
  1516. int clock = 60; /* default value */
  1517. usbcfg = readl(hsotg->regs + GUSBCFG);
  1518. hprt0 = readl(hsotg->regs + HPRT0);
  1519. if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) &&
  1520. !(usbcfg & GUSBCFG_PHYIF16))
  1521. clock = 60;
  1522. if ((usbcfg & GUSBCFG_PHYSEL) && hsotg->hw_params.fs_phy_type ==
  1523. GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
  1524. clock = 48;
  1525. if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
  1526. !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
  1527. clock = 30;
  1528. if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
  1529. !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16))
  1530. clock = 60;
  1531. if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
  1532. !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
  1533. clock = 48;
  1534. if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) &&
  1535. hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
  1536. clock = 48;
  1537. if ((usbcfg & GUSBCFG_PHYSEL) &&
  1538. hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
  1539. clock = 48;
  1540. if ((hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT == HPRT0_SPD_HIGH_SPEED)
  1541. /* High speed case */
  1542. return 125 * clock;
  1543. else
  1544. /* FS/LS case */
  1545. return 1000 * clock;
  1546. }
  1547. /**
  1548. * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination
  1549. * buffer
  1550. *
  1551. * @core_if: Programming view of DWC_otg controller
  1552. * @dest: Destination buffer for the packet
  1553. * @bytes: Number of bytes to copy to the destination
  1554. */
  1555. void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
  1556. {
  1557. u32 __iomem *fifo = hsotg->regs + HCFIFO(0);
  1558. u32 *data_buf = (u32 *)dest;
  1559. int word_count = (bytes + 3) / 4;
  1560. int i;
  1561. /*
  1562. * Todo: Account for the case where dest is not dword aligned. This
  1563. * requires reading data from the FIFO into a u32 temp buffer, then
  1564. * moving it into the data buffer.
  1565. */
  1566. dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
  1567. for (i = 0; i < word_count; i++, data_buf++)
  1568. *data_buf = readl(fifo);
  1569. }
  1570. /**
  1571. * dwc2_dump_host_registers() - Prints the host registers
  1572. *
  1573. * @hsotg: Programming view of DWC_otg controller
  1574. *
  1575. * NOTE: This function will be removed once the peripheral controller code
  1576. * is integrated and the driver is stable
  1577. */
  1578. void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg)
  1579. {
  1580. #ifdef DEBUG
  1581. u32 __iomem *addr;
  1582. int i;
  1583. dev_dbg(hsotg->dev, "Host Global Registers\n");
  1584. addr = hsotg->regs + HCFG;
  1585. dev_dbg(hsotg->dev, "HCFG @0x%08lX : 0x%08X\n",
  1586. (unsigned long)addr, readl(addr));
  1587. addr = hsotg->regs + HFIR;
  1588. dev_dbg(hsotg->dev, "HFIR @0x%08lX : 0x%08X\n",
  1589. (unsigned long)addr, readl(addr));
  1590. addr = hsotg->regs + HFNUM;
  1591. dev_dbg(hsotg->dev, "HFNUM @0x%08lX : 0x%08X\n",
  1592. (unsigned long)addr, readl(addr));
  1593. addr = hsotg->regs + HPTXSTS;
  1594. dev_dbg(hsotg->dev, "HPTXSTS @0x%08lX : 0x%08X\n",
  1595. (unsigned long)addr, readl(addr));
  1596. addr = hsotg->regs + HAINT;
  1597. dev_dbg(hsotg->dev, "HAINT @0x%08lX : 0x%08X\n",
  1598. (unsigned long)addr, readl(addr));
  1599. addr = hsotg->regs + HAINTMSK;
  1600. dev_dbg(hsotg->dev, "HAINTMSK @0x%08lX : 0x%08X\n",
  1601. (unsigned long)addr, readl(addr));
  1602. if (hsotg->core_params->dma_desc_enable > 0) {
  1603. addr = hsotg->regs + HFLBADDR;
  1604. dev_dbg(hsotg->dev, "HFLBADDR @0x%08lX : 0x%08X\n",
  1605. (unsigned long)addr, readl(addr));
  1606. }
  1607. addr = hsotg->regs + HPRT0;
  1608. dev_dbg(hsotg->dev, "HPRT0 @0x%08lX : 0x%08X\n",
  1609. (unsigned long)addr, readl(addr));
  1610. for (i = 0; i < hsotg->core_params->host_channels; i++) {
  1611. dev_dbg(hsotg->dev, "Host Channel %d Specific Registers\n", i);
  1612. addr = hsotg->regs + HCCHAR(i);
  1613. dev_dbg(hsotg->dev, "HCCHAR @0x%08lX : 0x%08X\n",
  1614. (unsigned long)addr, readl(addr));
  1615. addr = hsotg->regs + HCSPLT(i);
  1616. dev_dbg(hsotg->dev, "HCSPLT @0x%08lX : 0x%08X\n",
  1617. (unsigned long)addr, readl(addr));
  1618. addr = hsotg->regs + HCINT(i);
  1619. dev_dbg(hsotg->dev, "HCINT @0x%08lX : 0x%08X\n",
  1620. (unsigned long)addr, readl(addr));
  1621. addr = hsotg->regs + HCINTMSK(i);
  1622. dev_dbg(hsotg->dev, "HCINTMSK @0x%08lX : 0x%08X\n",
  1623. (unsigned long)addr, readl(addr));
  1624. addr = hsotg->regs + HCTSIZ(i);
  1625. dev_dbg(hsotg->dev, "HCTSIZ @0x%08lX : 0x%08X\n",
  1626. (unsigned long)addr, readl(addr));
  1627. addr = hsotg->regs + HCDMA(i);
  1628. dev_dbg(hsotg->dev, "HCDMA @0x%08lX : 0x%08X\n",
  1629. (unsigned long)addr, readl(addr));
  1630. if (hsotg->core_params->dma_desc_enable > 0) {
  1631. addr = hsotg->regs + HCDMAB(i);
  1632. dev_dbg(hsotg->dev, "HCDMAB @0x%08lX : 0x%08X\n",
  1633. (unsigned long)addr, readl(addr));
  1634. }
  1635. }
  1636. #endif
  1637. }
  1638. /**
  1639. * dwc2_dump_global_registers() - Prints the core global registers
  1640. *
  1641. * @hsotg: Programming view of DWC_otg controller
  1642. *
  1643. * NOTE: This function will be removed once the peripheral controller code
  1644. * is integrated and the driver is stable
  1645. */
  1646. void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg)
  1647. {
  1648. #ifdef DEBUG
  1649. u32 __iomem *addr;
  1650. dev_dbg(hsotg->dev, "Core Global Registers\n");
  1651. addr = hsotg->regs + GOTGCTL;
  1652. dev_dbg(hsotg->dev, "GOTGCTL @0x%08lX : 0x%08X\n",
  1653. (unsigned long)addr, readl(addr));
  1654. addr = hsotg->regs + GOTGINT;
  1655. dev_dbg(hsotg->dev, "GOTGINT @0x%08lX : 0x%08X\n",
  1656. (unsigned long)addr, readl(addr));
  1657. addr = hsotg->regs + GAHBCFG;
  1658. dev_dbg(hsotg->dev, "GAHBCFG @0x%08lX : 0x%08X\n",
  1659. (unsigned long)addr, readl(addr));
  1660. addr = hsotg->regs + GUSBCFG;
  1661. dev_dbg(hsotg->dev, "GUSBCFG @0x%08lX : 0x%08X\n",
  1662. (unsigned long)addr, readl(addr));
  1663. addr = hsotg->regs + GRSTCTL;
  1664. dev_dbg(hsotg->dev, "GRSTCTL @0x%08lX : 0x%08X\n",
  1665. (unsigned long)addr, readl(addr));
  1666. addr = hsotg->regs + GINTSTS;
  1667. dev_dbg(hsotg->dev, "GINTSTS @0x%08lX : 0x%08X\n",
  1668. (unsigned long)addr, readl(addr));
  1669. addr = hsotg->regs + GINTMSK;
  1670. dev_dbg(hsotg->dev, "GINTMSK @0x%08lX : 0x%08X\n",
  1671. (unsigned long)addr, readl(addr));
  1672. addr = hsotg->regs + GRXSTSR;
  1673. dev_dbg(hsotg->dev, "GRXSTSR @0x%08lX : 0x%08X\n",
  1674. (unsigned long)addr, readl(addr));
  1675. addr = hsotg->regs + GRXFSIZ;
  1676. dev_dbg(hsotg->dev, "GRXFSIZ @0x%08lX : 0x%08X\n",
  1677. (unsigned long)addr, readl(addr));
  1678. addr = hsotg->regs + GNPTXFSIZ;
  1679. dev_dbg(hsotg->dev, "GNPTXFSIZ @0x%08lX : 0x%08X\n",
  1680. (unsigned long)addr, readl(addr));
  1681. addr = hsotg->regs + GNPTXSTS;
  1682. dev_dbg(hsotg->dev, "GNPTXSTS @0x%08lX : 0x%08X\n",
  1683. (unsigned long)addr, readl(addr));
  1684. addr = hsotg->regs + GI2CCTL;
  1685. dev_dbg(hsotg->dev, "GI2CCTL @0x%08lX : 0x%08X\n",
  1686. (unsigned long)addr, readl(addr));
  1687. addr = hsotg->regs + GPVNDCTL;
  1688. dev_dbg(hsotg->dev, "GPVNDCTL @0x%08lX : 0x%08X\n",
  1689. (unsigned long)addr, readl(addr));
  1690. addr = hsotg->regs + GGPIO;
  1691. dev_dbg(hsotg->dev, "GGPIO @0x%08lX : 0x%08X\n",
  1692. (unsigned long)addr, readl(addr));
  1693. addr = hsotg->regs + GUID;
  1694. dev_dbg(hsotg->dev, "GUID @0x%08lX : 0x%08X\n",
  1695. (unsigned long)addr, readl(addr));
  1696. addr = hsotg->regs + GSNPSID;
  1697. dev_dbg(hsotg->dev, "GSNPSID @0x%08lX : 0x%08X\n",
  1698. (unsigned long)addr, readl(addr));
  1699. addr = hsotg->regs + GHWCFG1;
  1700. dev_dbg(hsotg->dev, "GHWCFG1 @0x%08lX : 0x%08X\n",
  1701. (unsigned long)addr, readl(addr));
  1702. addr = hsotg->regs + GHWCFG2;
  1703. dev_dbg(hsotg->dev, "GHWCFG2 @0x%08lX : 0x%08X\n",
  1704. (unsigned long)addr, readl(addr));
  1705. addr = hsotg->regs + GHWCFG3;
  1706. dev_dbg(hsotg->dev, "GHWCFG3 @0x%08lX : 0x%08X\n",
  1707. (unsigned long)addr, readl(addr));
  1708. addr = hsotg->regs + GHWCFG4;
  1709. dev_dbg(hsotg->dev, "GHWCFG4 @0x%08lX : 0x%08X\n",
  1710. (unsigned long)addr, readl(addr));
  1711. addr = hsotg->regs + GLPMCFG;
  1712. dev_dbg(hsotg->dev, "GLPMCFG @0x%08lX : 0x%08X\n",
  1713. (unsigned long)addr, readl(addr));
  1714. addr = hsotg->regs + GPWRDN;
  1715. dev_dbg(hsotg->dev, "GPWRDN @0x%08lX : 0x%08X\n",
  1716. (unsigned long)addr, readl(addr));
  1717. addr = hsotg->regs + GDFIFOCFG;
  1718. dev_dbg(hsotg->dev, "GDFIFOCFG @0x%08lX : 0x%08X\n",
  1719. (unsigned long)addr, readl(addr));
  1720. addr = hsotg->regs + HPTXFSIZ;
  1721. dev_dbg(hsotg->dev, "HPTXFSIZ @0x%08lX : 0x%08X\n",
  1722. (unsigned long)addr, readl(addr));
  1723. addr = hsotg->regs + PCGCTL;
  1724. dev_dbg(hsotg->dev, "PCGCTL @0x%08lX : 0x%08X\n",
  1725. (unsigned long)addr, readl(addr));
  1726. #endif
  1727. }
  1728. /**
  1729. * dwc2_flush_tx_fifo() - Flushes a Tx FIFO
  1730. *
  1731. * @hsotg: Programming view of DWC_otg controller
  1732. * @num: Tx FIFO to flush
  1733. */
  1734. void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num)
  1735. {
  1736. u32 greset;
  1737. int count = 0;
  1738. dev_vdbg(hsotg->dev, "Flush Tx FIFO %d\n", num);
  1739. greset = GRSTCTL_TXFFLSH;
  1740. greset |= num << GRSTCTL_TXFNUM_SHIFT & GRSTCTL_TXFNUM_MASK;
  1741. writel(greset, hsotg->regs + GRSTCTL);
  1742. do {
  1743. greset = readl(hsotg->regs + GRSTCTL);
  1744. if (++count > 10000) {
  1745. dev_warn(hsotg->dev,
  1746. "%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
  1747. __func__, greset,
  1748. readl(hsotg->regs + GNPTXSTS));
  1749. break;
  1750. }
  1751. udelay(1);
  1752. } while (greset & GRSTCTL_TXFFLSH);
  1753. /* Wait for at least 3 PHY Clocks */
  1754. udelay(1);
  1755. }
  1756. /**
  1757. * dwc2_flush_rx_fifo() - Flushes the Rx FIFO
  1758. *
  1759. * @hsotg: Programming view of DWC_otg controller
  1760. */
  1761. void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg)
  1762. {
  1763. u32 greset;
  1764. int count = 0;
  1765. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  1766. greset = GRSTCTL_RXFFLSH;
  1767. writel(greset, hsotg->regs + GRSTCTL);
  1768. do {
  1769. greset = readl(hsotg->regs + GRSTCTL);
  1770. if (++count > 10000) {
  1771. dev_warn(hsotg->dev, "%s() HANG! GRSTCTL=%0x\n",
  1772. __func__, greset);
  1773. break;
  1774. }
  1775. udelay(1);
  1776. } while (greset & GRSTCTL_RXFFLSH);
  1777. /* Wait for at least 3 PHY Clocks */
  1778. udelay(1);
  1779. }
  1780. #define DWC2_OUT_OF_BOUNDS(a, b, c) ((a) < (b) || (a) > (c))
  1781. /* Parameter access functions */
  1782. void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg, int val)
  1783. {
  1784. int valid = 1;
  1785. switch (val) {
  1786. case DWC2_CAP_PARAM_HNP_SRP_CAPABLE:
  1787. if (hsotg->hw_params.op_mode != GHWCFG2_OP_MODE_HNP_SRP_CAPABLE)
  1788. valid = 0;
  1789. break;
  1790. case DWC2_CAP_PARAM_SRP_ONLY_CAPABLE:
  1791. switch (hsotg->hw_params.op_mode) {
  1792. case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
  1793. case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
  1794. case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
  1795. case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
  1796. break;
  1797. default:
  1798. valid = 0;
  1799. break;
  1800. }
  1801. break;
  1802. case DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE:
  1803. /* always valid */
  1804. break;
  1805. default:
  1806. valid = 0;
  1807. break;
  1808. }
  1809. if (!valid) {
  1810. if (val >= 0)
  1811. dev_err(hsotg->dev,
  1812. "%d invalid for otg_cap parameter. Check HW configuration.\n",
  1813. val);
  1814. switch (hsotg->hw_params.op_mode) {
  1815. case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
  1816. val = DWC2_CAP_PARAM_HNP_SRP_CAPABLE;
  1817. break;
  1818. case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
  1819. case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
  1820. case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
  1821. val = DWC2_CAP_PARAM_SRP_ONLY_CAPABLE;
  1822. break;
  1823. default:
  1824. val = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
  1825. break;
  1826. }
  1827. dev_dbg(hsotg->dev, "Setting otg_cap to %d\n", val);
  1828. }
  1829. hsotg->core_params->otg_cap = val;
  1830. }
  1831. void dwc2_set_param_dma_enable(struct dwc2_hsotg *hsotg, int val)
  1832. {
  1833. int valid = 1;
  1834. if (val > 0 && hsotg->hw_params.arch == GHWCFG2_SLAVE_ONLY_ARCH)
  1835. valid = 0;
  1836. if (val < 0)
  1837. valid = 0;
  1838. if (!valid) {
  1839. if (val >= 0)
  1840. dev_err(hsotg->dev,
  1841. "%d invalid for dma_enable parameter. Check HW configuration.\n",
  1842. val);
  1843. val = hsotg->hw_params.arch != GHWCFG2_SLAVE_ONLY_ARCH;
  1844. dev_dbg(hsotg->dev, "Setting dma_enable to %d\n", val);
  1845. }
  1846. hsotg->core_params->dma_enable = val;
  1847. }
  1848. void dwc2_set_param_dma_desc_enable(struct dwc2_hsotg *hsotg, int val)
  1849. {
  1850. int valid = 1;
  1851. if (val > 0 && (hsotg->core_params->dma_enable <= 0 ||
  1852. !hsotg->hw_params.dma_desc_enable))
  1853. valid = 0;
  1854. if (val < 0)
  1855. valid = 0;
  1856. if (!valid) {
  1857. if (val >= 0)
  1858. dev_err(hsotg->dev,
  1859. "%d invalid for dma_desc_enable parameter. Check HW configuration.\n",
  1860. val);
  1861. val = (hsotg->core_params->dma_enable > 0 &&
  1862. hsotg->hw_params.dma_desc_enable);
  1863. dev_dbg(hsotg->dev, "Setting dma_desc_enable to %d\n", val);
  1864. }
  1865. hsotg->core_params->dma_desc_enable = val;
  1866. }
  1867. void dwc2_set_param_host_support_fs_ls_low_power(struct dwc2_hsotg *hsotg,
  1868. int val)
  1869. {
  1870. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  1871. if (val >= 0) {
  1872. dev_err(hsotg->dev,
  1873. "Wrong value for host_support_fs_low_power\n");
  1874. dev_err(hsotg->dev,
  1875. "host_support_fs_low_power must be 0 or 1\n");
  1876. }
  1877. val = 0;
  1878. dev_dbg(hsotg->dev,
  1879. "Setting host_support_fs_low_power to %d\n", val);
  1880. }
  1881. hsotg->core_params->host_support_fs_ls_low_power = val;
  1882. }
  1883. void dwc2_set_param_enable_dynamic_fifo(struct dwc2_hsotg *hsotg, int val)
  1884. {
  1885. int valid = 1;
  1886. if (val > 0 && !hsotg->hw_params.enable_dynamic_fifo)
  1887. valid = 0;
  1888. if (val < 0)
  1889. valid = 0;
  1890. if (!valid) {
  1891. if (val >= 0)
  1892. dev_err(hsotg->dev,
  1893. "%d invalid for enable_dynamic_fifo parameter. Check HW configuration.\n",
  1894. val);
  1895. val = hsotg->hw_params.enable_dynamic_fifo;
  1896. dev_dbg(hsotg->dev, "Setting enable_dynamic_fifo to %d\n", val);
  1897. }
  1898. hsotg->core_params->enable_dynamic_fifo = val;
  1899. }
  1900. void dwc2_set_param_host_rx_fifo_size(struct dwc2_hsotg *hsotg, int val)
  1901. {
  1902. int valid = 1;
  1903. if (val < 16 || val > hsotg->hw_params.host_rx_fifo_size)
  1904. valid = 0;
  1905. if (!valid) {
  1906. if (val >= 0)
  1907. dev_err(hsotg->dev,
  1908. "%d invalid for host_rx_fifo_size. Check HW configuration.\n",
  1909. val);
  1910. val = hsotg->hw_params.host_rx_fifo_size;
  1911. dev_dbg(hsotg->dev, "Setting host_rx_fifo_size to %d\n", val);
  1912. }
  1913. hsotg->core_params->host_rx_fifo_size = val;
  1914. }
  1915. void dwc2_set_param_host_nperio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
  1916. {
  1917. int valid = 1;
  1918. if (val < 16 || val > hsotg->hw_params.host_nperio_tx_fifo_size)
  1919. valid = 0;
  1920. if (!valid) {
  1921. if (val >= 0)
  1922. dev_err(hsotg->dev,
  1923. "%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
  1924. val);
  1925. val = hsotg->hw_params.host_nperio_tx_fifo_size;
  1926. dev_dbg(hsotg->dev, "Setting host_nperio_tx_fifo_size to %d\n",
  1927. val);
  1928. }
  1929. hsotg->core_params->host_nperio_tx_fifo_size = val;
  1930. }
  1931. void dwc2_set_param_host_perio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
  1932. {
  1933. int valid = 1;
  1934. if (val < 16 || val > hsotg->hw_params.host_perio_tx_fifo_size)
  1935. valid = 0;
  1936. if (!valid) {
  1937. if (val >= 0)
  1938. dev_err(hsotg->dev,
  1939. "%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
  1940. val);
  1941. val = hsotg->hw_params.host_perio_tx_fifo_size;
  1942. dev_dbg(hsotg->dev, "Setting host_perio_tx_fifo_size to %d\n",
  1943. val);
  1944. }
  1945. hsotg->core_params->host_perio_tx_fifo_size = val;
  1946. }
  1947. void dwc2_set_param_max_transfer_size(struct dwc2_hsotg *hsotg, int val)
  1948. {
  1949. int valid = 1;
  1950. if (val < 2047 || val > hsotg->hw_params.max_transfer_size)
  1951. valid = 0;
  1952. if (!valid) {
  1953. if (val >= 0)
  1954. dev_err(hsotg->dev,
  1955. "%d invalid for max_transfer_size. Check HW configuration.\n",
  1956. val);
  1957. val = hsotg->hw_params.max_transfer_size;
  1958. dev_dbg(hsotg->dev, "Setting max_transfer_size to %d\n", val);
  1959. }
  1960. hsotg->core_params->max_transfer_size = val;
  1961. }
  1962. void dwc2_set_param_max_packet_count(struct dwc2_hsotg *hsotg, int val)
  1963. {
  1964. int valid = 1;
  1965. if (val < 15 || val > hsotg->hw_params.max_packet_count)
  1966. valid = 0;
  1967. if (!valid) {
  1968. if (val >= 0)
  1969. dev_err(hsotg->dev,
  1970. "%d invalid for max_packet_count. Check HW configuration.\n",
  1971. val);
  1972. val = hsotg->hw_params.max_packet_count;
  1973. dev_dbg(hsotg->dev, "Setting max_packet_count to %d\n", val);
  1974. }
  1975. hsotg->core_params->max_packet_count = val;
  1976. }
  1977. void dwc2_set_param_host_channels(struct dwc2_hsotg *hsotg, int val)
  1978. {
  1979. int valid = 1;
  1980. if (val < 1 || val > hsotg->hw_params.host_channels)
  1981. valid = 0;
  1982. if (!valid) {
  1983. if (val >= 0)
  1984. dev_err(hsotg->dev,
  1985. "%d invalid for host_channels. Check HW configuration.\n",
  1986. val);
  1987. val = hsotg->hw_params.host_channels;
  1988. dev_dbg(hsotg->dev, "Setting host_channels to %d\n", val);
  1989. }
  1990. hsotg->core_params->host_channels = val;
  1991. }
  1992. void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg, int val)
  1993. {
  1994. int valid = 0;
  1995. u32 hs_phy_type, fs_phy_type;
  1996. if (DWC2_OUT_OF_BOUNDS(val, DWC2_PHY_TYPE_PARAM_FS,
  1997. DWC2_PHY_TYPE_PARAM_ULPI)) {
  1998. if (val >= 0) {
  1999. dev_err(hsotg->dev, "Wrong value for phy_type\n");
  2000. dev_err(hsotg->dev, "phy_type must be 0, 1 or 2\n");
  2001. }
  2002. valid = 0;
  2003. }
  2004. hs_phy_type = hsotg->hw_params.hs_phy_type;
  2005. fs_phy_type = hsotg->hw_params.fs_phy_type;
  2006. if (val == DWC2_PHY_TYPE_PARAM_UTMI &&
  2007. (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
  2008. hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
  2009. valid = 1;
  2010. else if (val == DWC2_PHY_TYPE_PARAM_ULPI &&
  2011. (hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI ||
  2012. hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
  2013. valid = 1;
  2014. else if (val == DWC2_PHY_TYPE_PARAM_FS &&
  2015. fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
  2016. valid = 1;
  2017. if (!valid) {
  2018. if (val >= 0)
  2019. dev_err(hsotg->dev,
  2020. "%d invalid for phy_type. Check HW configuration.\n",
  2021. val);
  2022. val = DWC2_PHY_TYPE_PARAM_FS;
  2023. if (hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED) {
  2024. if (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
  2025. hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI)
  2026. val = DWC2_PHY_TYPE_PARAM_UTMI;
  2027. else
  2028. val = DWC2_PHY_TYPE_PARAM_ULPI;
  2029. }
  2030. dev_dbg(hsotg->dev, "Setting phy_type to %d\n", val);
  2031. }
  2032. hsotg->core_params->phy_type = val;
  2033. }
  2034. static int dwc2_get_param_phy_type(struct dwc2_hsotg *hsotg)
  2035. {
  2036. return hsotg->core_params->phy_type;
  2037. }
  2038. void dwc2_set_param_speed(struct dwc2_hsotg *hsotg, int val)
  2039. {
  2040. int valid = 1;
  2041. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  2042. if (val >= 0) {
  2043. dev_err(hsotg->dev, "Wrong value for speed parameter\n");
  2044. dev_err(hsotg->dev, "max_speed parameter must be 0 or 1\n");
  2045. }
  2046. valid = 0;
  2047. }
  2048. if (val == DWC2_SPEED_PARAM_HIGH &&
  2049. dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS)
  2050. valid = 0;
  2051. if (!valid) {
  2052. if (val >= 0)
  2053. dev_err(hsotg->dev,
  2054. "%d invalid for speed parameter. Check HW configuration.\n",
  2055. val);
  2056. val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS ?
  2057. DWC2_SPEED_PARAM_FULL : DWC2_SPEED_PARAM_HIGH;
  2058. dev_dbg(hsotg->dev, "Setting speed to %d\n", val);
  2059. }
  2060. hsotg->core_params->speed = val;
  2061. }
  2062. void dwc2_set_param_host_ls_low_power_phy_clk(struct dwc2_hsotg *hsotg, int val)
  2063. {
  2064. int valid = 1;
  2065. if (DWC2_OUT_OF_BOUNDS(val, DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ,
  2066. DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)) {
  2067. if (val >= 0) {
  2068. dev_err(hsotg->dev,
  2069. "Wrong value for host_ls_low_power_phy_clk parameter\n");
  2070. dev_err(hsotg->dev,
  2071. "host_ls_low_power_phy_clk must be 0 or 1\n");
  2072. }
  2073. valid = 0;
  2074. }
  2075. if (val == DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ &&
  2076. dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS)
  2077. valid = 0;
  2078. if (!valid) {
  2079. if (val >= 0)
  2080. dev_err(hsotg->dev,
  2081. "%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
  2082. val);
  2083. val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS
  2084. ? DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ
  2085. : DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
  2086. dev_dbg(hsotg->dev, "Setting host_ls_low_power_phy_clk to %d\n",
  2087. val);
  2088. }
  2089. hsotg->core_params->host_ls_low_power_phy_clk = val;
  2090. }
  2091. void dwc2_set_param_phy_ulpi_ddr(struct dwc2_hsotg *hsotg, int val)
  2092. {
  2093. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  2094. if (val >= 0) {
  2095. dev_err(hsotg->dev, "Wrong value for phy_ulpi_ddr\n");
  2096. dev_err(hsotg->dev, "phy_upli_ddr must be 0 or 1\n");
  2097. }
  2098. val = 0;
  2099. dev_dbg(hsotg->dev, "Setting phy_upli_ddr to %d\n", val);
  2100. }
  2101. hsotg->core_params->phy_ulpi_ddr = val;
  2102. }
  2103. void dwc2_set_param_phy_ulpi_ext_vbus(struct dwc2_hsotg *hsotg, int val)
  2104. {
  2105. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  2106. if (val >= 0) {
  2107. dev_err(hsotg->dev,
  2108. "Wrong value for phy_ulpi_ext_vbus\n");
  2109. dev_err(hsotg->dev,
  2110. "phy_ulpi_ext_vbus must be 0 or 1\n");
  2111. }
  2112. val = 0;
  2113. dev_dbg(hsotg->dev, "Setting phy_ulpi_ext_vbus to %d\n", val);
  2114. }
  2115. hsotg->core_params->phy_ulpi_ext_vbus = val;
  2116. }
  2117. void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg, int val)
  2118. {
  2119. int valid = 0;
  2120. switch (hsotg->hw_params.utmi_phy_data_width) {
  2121. case GHWCFG4_UTMI_PHY_DATA_WIDTH_8:
  2122. valid = (val == 8);
  2123. break;
  2124. case GHWCFG4_UTMI_PHY_DATA_WIDTH_16:
  2125. valid = (val == 16);
  2126. break;
  2127. case GHWCFG4_UTMI_PHY_DATA_WIDTH_8_OR_16:
  2128. valid = (val == 8 || val == 16);
  2129. break;
  2130. }
  2131. if (!valid) {
  2132. if (val >= 0) {
  2133. dev_err(hsotg->dev,
  2134. "%d invalid for phy_utmi_width. Check HW configuration.\n",
  2135. val);
  2136. }
  2137. val = (hsotg->hw_params.utmi_phy_data_width ==
  2138. GHWCFG4_UTMI_PHY_DATA_WIDTH_8) ? 8 : 16;
  2139. dev_dbg(hsotg->dev, "Setting phy_utmi_width to %d\n", val);
  2140. }
  2141. hsotg->core_params->phy_utmi_width = val;
  2142. }
  2143. void dwc2_set_param_ulpi_fs_ls(struct dwc2_hsotg *hsotg, int val)
  2144. {
  2145. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  2146. if (val >= 0) {
  2147. dev_err(hsotg->dev, "Wrong value for ulpi_fs_ls\n");
  2148. dev_err(hsotg->dev, "ulpi_fs_ls must be 0 or 1\n");
  2149. }
  2150. val = 0;
  2151. dev_dbg(hsotg->dev, "Setting ulpi_fs_ls to %d\n", val);
  2152. }
  2153. hsotg->core_params->ulpi_fs_ls = val;
  2154. }
  2155. void dwc2_set_param_ts_dline(struct dwc2_hsotg *hsotg, int val)
  2156. {
  2157. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  2158. if (val >= 0) {
  2159. dev_err(hsotg->dev, "Wrong value for ts_dline\n");
  2160. dev_err(hsotg->dev, "ts_dline must be 0 or 1\n");
  2161. }
  2162. val = 0;
  2163. dev_dbg(hsotg->dev, "Setting ts_dline to %d\n", val);
  2164. }
  2165. hsotg->core_params->ts_dline = val;
  2166. }
  2167. void dwc2_set_param_i2c_enable(struct dwc2_hsotg *hsotg, int val)
  2168. {
  2169. int valid = 1;
  2170. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  2171. if (val >= 0) {
  2172. dev_err(hsotg->dev, "Wrong value for i2c_enable\n");
  2173. dev_err(hsotg->dev, "i2c_enable must be 0 or 1\n");
  2174. }
  2175. valid = 0;
  2176. }
  2177. if (val == 1 && !(hsotg->hw_params.i2c_enable))
  2178. valid = 0;
  2179. if (!valid) {
  2180. if (val >= 0)
  2181. dev_err(hsotg->dev,
  2182. "%d invalid for i2c_enable. Check HW configuration.\n",
  2183. val);
  2184. val = hsotg->hw_params.i2c_enable;
  2185. dev_dbg(hsotg->dev, "Setting i2c_enable to %d\n", val);
  2186. }
  2187. hsotg->core_params->i2c_enable = val;
  2188. }
  2189. void dwc2_set_param_en_multiple_tx_fifo(struct dwc2_hsotg *hsotg, int val)
  2190. {
  2191. int valid = 1;
  2192. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  2193. if (val >= 0) {
  2194. dev_err(hsotg->dev,
  2195. "Wrong value for en_multiple_tx_fifo,\n");
  2196. dev_err(hsotg->dev,
  2197. "en_multiple_tx_fifo must be 0 or 1\n");
  2198. }
  2199. valid = 0;
  2200. }
  2201. if (val == 1 && !hsotg->hw_params.en_multiple_tx_fifo)
  2202. valid = 0;
  2203. if (!valid) {
  2204. if (val >= 0)
  2205. dev_err(hsotg->dev,
  2206. "%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
  2207. val);
  2208. val = hsotg->hw_params.en_multiple_tx_fifo;
  2209. dev_dbg(hsotg->dev, "Setting en_multiple_tx_fifo to %d\n", val);
  2210. }
  2211. hsotg->core_params->en_multiple_tx_fifo = val;
  2212. }
  2213. void dwc2_set_param_reload_ctl(struct dwc2_hsotg *hsotg, int val)
  2214. {
  2215. int valid = 1;
  2216. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  2217. if (val >= 0) {
  2218. dev_err(hsotg->dev,
  2219. "'%d' invalid for parameter reload_ctl\n", val);
  2220. dev_err(hsotg->dev, "reload_ctl must be 0 or 1\n");
  2221. }
  2222. valid = 0;
  2223. }
  2224. if (val == 1 && hsotg->hw_params.snpsid < DWC2_CORE_REV_2_92a)
  2225. valid = 0;
  2226. if (!valid) {
  2227. if (val >= 0)
  2228. dev_err(hsotg->dev,
  2229. "%d invalid for parameter reload_ctl. Check HW configuration.\n",
  2230. val);
  2231. val = hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_92a;
  2232. dev_dbg(hsotg->dev, "Setting reload_ctl to %d\n", val);
  2233. }
  2234. hsotg->core_params->reload_ctl = val;
  2235. }
  2236. void dwc2_set_param_ahbcfg(struct dwc2_hsotg *hsotg, int val)
  2237. {
  2238. if (val != -1)
  2239. hsotg->core_params->ahbcfg = val;
  2240. else
  2241. hsotg->core_params->ahbcfg = GAHBCFG_HBSTLEN_INCR4 <<
  2242. GAHBCFG_HBSTLEN_SHIFT;
  2243. }
  2244. void dwc2_set_param_otg_ver(struct dwc2_hsotg *hsotg, int val)
  2245. {
  2246. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  2247. if (val >= 0) {
  2248. dev_err(hsotg->dev,
  2249. "'%d' invalid for parameter otg_ver\n", val);
  2250. dev_err(hsotg->dev,
  2251. "otg_ver must be 0 (for OTG 1.3 support) or 1 (for OTG 2.0 support)\n");
  2252. }
  2253. val = 0;
  2254. dev_dbg(hsotg->dev, "Setting otg_ver to %d\n", val);
  2255. }
  2256. hsotg->core_params->otg_ver = val;
  2257. }
  2258. static void dwc2_set_param_uframe_sched(struct dwc2_hsotg *hsotg, int val)
  2259. {
  2260. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  2261. if (val >= 0) {
  2262. dev_err(hsotg->dev,
  2263. "'%d' invalid for parameter uframe_sched\n",
  2264. val);
  2265. dev_err(hsotg->dev, "uframe_sched must be 0 or 1\n");
  2266. }
  2267. val = 1;
  2268. dev_dbg(hsotg->dev, "Setting uframe_sched to %d\n", val);
  2269. }
  2270. hsotg->core_params->uframe_sched = val;
  2271. }
  2272. /*
  2273. * This function is called during module intialization to pass module parameters
  2274. * for the DWC_otg core.
  2275. */
  2276. void dwc2_set_parameters(struct dwc2_hsotg *hsotg,
  2277. const struct dwc2_core_params *params)
  2278. {
  2279. dev_dbg(hsotg->dev, "%s()\n", __func__);
  2280. dwc2_set_param_otg_cap(hsotg, params->otg_cap);
  2281. dwc2_set_param_dma_enable(hsotg, params->dma_enable);
  2282. dwc2_set_param_dma_desc_enable(hsotg, params->dma_desc_enable);
  2283. dwc2_set_param_host_support_fs_ls_low_power(hsotg,
  2284. params->host_support_fs_ls_low_power);
  2285. dwc2_set_param_enable_dynamic_fifo(hsotg,
  2286. params->enable_dynamic_fifo);
  2287. dwc2_set_param_host_rx_fifo_size(hsotg,
  2288. params->host_rx_fifo_size);
  2289. dwc2_set_param_host_nperio_tx_fifo_size(hsotg,
  2290. params->host_nperio_tx_fifo_size);
  2291. dwc2_set_param_host_perio_tx_fifo_size(hsotg,
  2292. params->host_perio_tx_fifo_size);
  2293. dwc2_set_param_max_transfer_size(hsotg,
  2294. params->max_transfer_size);
  2295. dwc2_set_param_max_packet_count(hsotg,
  2296. params->max_packet_count);
  2297. dwc2_set_param_host_channels(hsotg, params->host_channels);
  2298. dwc2_set_param_phy_type(hsotg, params->phy_type);
  2299. dwc2_set_param_speed(hsotg, params->speed);
  2300. dwc2_set_param_host_ls_low_power_phy_clk(hsotg,
  2301. params->host_ls_low_power_phy_clk);
  2302. dwc2_set_param_phy_ulpi_ddr(hsotg, params->phy_ulpi_ddr);
  2303. dwc2_set_param_phy_ulpi_ext_vbus(hsotg,
  2304. params->phy_ulpi_ext_vbus);
  2305. dwc2_set_param_phy_utmi_width(hsotg, params->phy_utmi_width);
  2306. dwc2_set_param_ulpi_fs_ls(hsotg, params->ulpi_fs_ls);
  2307. dwc2_set_param_ts_dline(hsotg, params->ts_dline);
  2308. dwc2_set_param_i2c_enable(hsotg, params->i2c_enable);
  2309. dwc2_set_param_en_multiple_tx_fifo(hsotg,
  2310. params->en_multiple_tx_fifo);
  2311. dwc2_set_param_reload_ctl(hsotg, params->reload_ctl);
  2312. dwc2_set_param_ahbcfg(hsotg, params->ahbcfg);
  2313. dwc2_set_param_otg_ver(hsotg, params->otg_ver);
  2314. dwc2_set_param_uframe_sched(hsotg, params->uframe_sched);
  2315. }
  2316. /**
  2317. * During device initialization, read various hardware configuration
  2318. * registers and interpret the contents.
  2319. */
  2320. int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
  2321. {
  2322. struct dwc2_hw_params *hw = &hsotg->hw_params;
  2323. unsigned width;
  2324. u32 hwcfg1, hwcfg2, hwcfg3, hwcfg4;
  2325. u32 hptxfsiz, grxfsiz, gnptxfsiz;
  2326. u32 gusbcfg;
  2327. /*
  2328. * Attempt to ensure this device is really a DWC_otg Controller.
  2329. * Read and verify the GSNPSID register contents. The value should be
  2330. * 0x45f42xxx or 0x45f43xxx, which corresponds to either "OT2" or "OT3",
  2331. * as in "OTG version 2.xx" or "OTG version 3.xx".
  2332. */
  2333. hw->snpsid = readl(hsotg->regs + GSNPSID);
  2334. if ((hw->snpsid & 0xfffff000) != 0x4f542000 &&
  2335. (hw->snpsid & 0xfffff000) != 0x4f543000) {
  2336. dev_err(hsotg->dev, "Bad value for GSNPSID: 0x%08x\n",
  2337. hw->snpsid);
  2338. return -ENODEV;
  2339. }
  2340. dev_dbg(hsotg->dev, "Core Release: %1x.%1x%1x%1x (snpsid=%x)\n",
  2341. hw->snpsid >> 12 & 0xf, hw->snpsid >> 8 & 0xf,
  2342. hw->snpsid >> 4 & 0xf, hw->snpsid & 0xf, hw->snpsid);
  2343. hwcfg1 = readl(hsotg->regs + GHWCFG1);
  2344. hwcfg2 = readl(hsotg->regs + GHWCFG2);
  2345. hwcfg3 = readl(hsotg->regs + GHWCFG3);
  2346. hwcfg4 = readl(hsotg->regs + GHWCFG4);
  2347. grxfsiz = readl(hsotg->regs + GRXFSIZ);
  2348. dev_dbg(hsotg->dev, "hwcfg1=%08x\n", hwcfg1);
  2349. dev_dbg(hsotg->dev, "hwcfg2=%08x\n", hwcfg2);
  2350. dev_dbg(hsotg->dev, "hwcfg3=%08x\n", hwcfg3);
  2351. dev_dbg(hsotg->dev, "hwcfg4=%08x\n", hwcfg4);
  2352. dev_dbg(hsotg->dev, "grxfsiz=%08x\n", grxfsiz);
  2353. /* Force host mode to get HPTXFSIZ / GNPTXFSIZ exact power on value */
  2354. gusbcfg = readl(hsotg->regs + GUSBCFG);
  2355. gusbcfg |= GUSBCFG_FORCEHOSTMODE;
  2356. writel(gusbcfg, hsotg->regs + GUSBCFG);
  2357. usleep_range(100000, 150000);
  2358. gnptxfsiz = readl(hsotg->regs + GNPTXFSIZ);
  2359. hptxfsiz = readl(hsotg->regs + HPTXFSIZ);
  2360. dev_dbg(hsotg->dev, "gnptxfsiz=%08x\n", gnptxfsiz);
  2361. dev_dbg(hsotg->dev, "hptxfsiz=%08x\n", hptxfsiz);
  2362. gusbcfg = readl(hsotg->regs + GUSBCFG);
  2363. gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
  2364. writel(gusbcfg, hsotg->regs + GUSBCFG);
  2365. usleep_range(100000, 150000);
  2366. /* hwcfg2 */
  2367. hw->op_mode = (hwcfg2 & GHWCFG2_OP_MODE_MASK) >>
  2368. GHWCFG2_OP_MODE_SHIFT;
  2369. hw->arch = (hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) >>
  2370. GHWCFG2_ARCHITECTURE_SHIFT;
  2371. hw->enable_dynamic_fifo = !!(hwcfg2 & GHWCFG2_DYNAMIC_FIFO);
  2372. hw->host_channels = 1 + ((hwcfg2 & GHWCFG2_NUM_HOST_CHAN_MASK) >>
  2373. GHWCFG2_NUM_HOST_CHAN_SHIFT);
  2374. hw->hs_phy_type = (hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK) >>
  2375. GHWCFG2_HS_PHY_TYPE_SHIFT;
  2376. hw->fs_phy_type = (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) >>
  2377. GHWCFG2_FS_PHY_TYPE_SHIFT;
  2378. hw->num_dev_ep = (hwcfg2 & GHWCFG2_NUM_DEV_EP_MASK) >>
  2379. GHWCFG2_NUM_DEV_EP_SHIFT;
  2380. hw->nperio_tx_q_depth =
  2381. (hwcfg2 & GHWCFG2_NONPERIO_TX_Q_DEPTH_MASK) >>
  2382. GHWCFG2_NONPERIO_TX_Q_DEPTH_SHIFT << 1;
  2383. hw->host_perio_tx_q_depth =
  2384. (hwcfg2 & GHWCFG2_HOST_PERIO_TX_Q_DEPTH_MASK) >>
  2385. GHWCFG2_HOST_PERIO_TX_Q_DEPTH_SHIFT << 1;
  2386. hw->dev_token_q_depth =
  2387. (hwcfg2 & GHWCFG2_DEV_TOKEN_Q_DEPTH_MASK) >>
  2388. GHWCFG2_DEV_TOKEN_Q_DEPTH_SHIFT;
  2389. /* hwcfg3 */
  2390. width = (hwcfg3 & GHWCFG3_XFER_SIZE_CNTR_WIDTH_MASK) >>
  2391. GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT;
  2392. hw->max_transfer_size = (1 << (width + 11)) - 1;
  2393. /*
  2394. * Clip max_transfer_size to 65535. dwc2_hc_setup_align_buf() allocates
  2395. * coherent buffers with this size, and if it's too large we can
  2396. * exhaust the coherent DMA pool.
  2397. */
  2398. if (hw->max_transfer_size > 65535)
  2399. hw->max_transfer_size = 65535;
  2400. width = (hwcfg3 & GHWCFG3_PACKET_SIZE_CNTR_WIDTH_MASK) >>
  2401. GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT;
  2402. hw->max_packet_count = (1 << (width + 4)) - 1;
  2403. hw->i2c_enable = !!(hwcfg3 & GHWCFG3_I2C);
  2404. hw->total_fifo_size = (hwcfg3 & GHWCFG3_DFIFO_DEPTH_MASK) >>
  2405. GHWCFG3_DFIFO_DEPTH_SHIFT;
  2406. /* hwcfg4 */
  2407. hw->en_multiple_tx_fifo = !!(hwcfg4 & GHWCFG4_DED_FIFO_EN);
  2408. hw->num_dev_perio_in_ep = (hwcfg4 & GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK) >>
  2409. GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT;
  2410. hw->dma_desc_enable = !!(hwcfg4 & GHWCFG4_DESC_DMA);
  2411. hw->power_optimized = !!(hwcfg4 & GHWCFG4_POWER_OPTIMIZ);
  2412. hw->utmi_phy_data_width = (hwcfg4 & GHWCFG4_UTMI_PHY_DATA_WIDTH_MASK) >>
  2413. GHWCFG4_UTMI_PHY_DATA_WIDTH_SHIFT;
  2414. /* fifo sizes */
  2415. hw->host_rx_fifo_size = (grxfsiz & GRXFSIZ_DEPTH_MASK) >>
  2416. GRXFSIZ_DEPTH_SHIFT;
  2417. hw->host_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
  2418. FIFOSIZE_DEPTH_SHIFT;
  2419. hw->host_perio_tx_fifo_size = (hptxfsiz & FIFOSIZE_DEPTH_MASK) >>
  2420. FIFOSIZE_DEPTH_SHIFT;
  2421. dev_dbg(hsotg->dev, "Detected values from hardware:\n");
  2422. dev_dbg(hsotg->dev, " op_mode=%d\n",
  2423. hw->op_mode);
  2424. dev_dbg(hsotg->dev, " arch=%d\n",
  2425. hw->arch);
  2426. dev_dbg(hsotg->dev, " dma_desc_enable=%d\n",
  2427. hw->dma_desc_enable);
  2428. dev_dbg(hsotg->dev, " power_optimized=%d\n",
  2429. hw->power_optimized);
  2430. dev_dbg(hsotg->dev, " i2c_enable=%d\n",
  2431. hw->i2c_enable);
  2432. dev_dbg(hsotg->dev, " hs_phy_type=%d\n",
  2433. hw->hs_phy_type);
  2434. dev_dbg(hsotg->dev, " fs_phy_type=%d\n",
  2435. hw->fs_phy_type);
  2436. dev_dbg(hsotg->dev, " utmi_phy_data_wdith=%d\n",
  2437. hw->utmi_phy_data_width);
  2438. dev_dbg(hsotg->dev, " num_dev_ep=%d\n",
  2439. hw->num_dev_ep);
  2440. dev_dbg(hsotg->dev, " num_dev_perio_in_ep=%d\n",
  2441. hw->num_dev_perio_in_ep);
  2442. dev_dbg(hsotg->dev, " host_channels=%d\n",
  2443. hw->host_channels);
  2444. dev_dbg(hsotg->dev, " max_transfer_size=%d\n",
  2445. hw->max_transfer_size);
  2446. dev_dbg(hsotg->dev, " max_packet_count=%d\n",
  2447. hw->max_packet_count);
  2448. dev_dbg(hsotg->dev, " nperio_tx_q_depth=0x%0x\n",
  2449. hw->nperio_tx_q_depth);
  2450. dev_dbg(hsotg->dev, " host_perio_tx_q_depth=0x%0x\n",
  2451. hw->host_perio_tx_q_depth);
  2452. dev_dbg(hsotg->dev, " dev_token_q_depth=0x%0x\n",
  2453. hw->dev_token_q_depth);
  2454. dev_dbg(hsotg->dev, " enable_dynamic_fifo=%d\n",
  2455. hw->enable_dynamic_fifo);
  2456. dev_dbg(hsotg->dev, " en_multiple_tx_fifo=%d\n",
  2457. hw->en_multiple_tx_fifo);
  2458. dev_dbg(hsotg->dev, " total_fifo_size=%d\n",
  2459. hw->total_fifo_size);
  2460. dev_dbg(hsotg->dev, " host_rx_fifo_size=%d\n",
  2461. hw->host_rx_fifo_size);
  2462. dev_dbg(hsotg->dev, " host_nperio_tx_fifo_size=%d\n",
  2463. hw->host_nperio_tx_fifo_size);
  2464. dev_dbg(hsotg->dev, " host_perio_tx_fifo_size=%d\n",
  2465. hw->host_perio_tx_fifo_size);
  2466. dev_dbg(hsotg->dev, "\n");
  2467. return 0;
  2468. }
  2469. u16 dwc2_get_otg_version(struct dwc2_hsotg *hsotg)
  2470. {
  2471. return hsotg->core_params->otg_ver == 1 ? 0x0200 : 0x0103;
  2472. }
  2473. bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg)
  2474. {
  2475. if (readl(hsotg->regs + GSNPSID) == 0xffffffff)
  2476. return false;
  2477. else
  2478. return true;
  2479. }
  2480. /**
  2481. * dwc2_enable_global_interrupts() - Enables the controller's Global
  2482. * Interrupt in the AHB Config register
  2483. *
  2484. * @hsotg: Programming view of DWC_otg controller
  2485. */
  2486. void dwc2_enable_global_interrupts(struct dwc2_hsotg *hsotg)
  2487. {
  2488. u32 ahbcfg = readl(hsotg->regs + GAHBCFG);
  2489. ahbcfg |= GAHBCFG_GLBL_INTR_EN;
  2490. writel(ahbcfg, hsotg->regs + GAHBCFG);
  2491. }
  2492. /**
  2493. * dwc2_disable_global_interrupts() - Disables the controller's Global
  2494. * Interrupt in the AHB Config register
  2495. *
  2496. * @hsotg: Programming view of DWC_otg controller
  2497. */
  2498. void dwc2_disable_global_interrupts(struct dwc2_hsotg *hsotg)
  2499. {
  2500. u32 ahbcfg = readl(hsotg->regs + GAHBCFG);
  2501. ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
  2502. writel(ahbcfg, hsotg->regs + GAHBCFG);
  2503. }
  2504. MODULE_DESCRIPTION("DESIGNWARE HS OTG Core");
  2505. MODULE_AUTHOR("Synopsys, Inc.");
  2506. MODULE_LICENSE("Dual BSD/GPL");