core.c 82 KB

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