core.c 82 KB

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