core.c 80 KB

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