core.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748
  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_backup_global_registers() - Backup global controller registers.
  57. * When suspending usb bus, registers needs to be backuped
  58. * if controller power is disabled once suspended.
  59. *
  60. * @hsotg: Programming view of the DWC_otg controller
  61. */
  62. static int dwc2_backup_global_registers(struct dwc2_hsotg *hsotg)
  63. {
  64. struct dwc2_gregs_backup *gr;
  65. int i;
  66. /* Backup global regs */
  67. gr = &hsotg->gr_backup;
  68. gr->gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
  69. gr->gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
  70. gr->gahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
  71. gr->gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  72. gr->grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
  73. gr->gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
  74. gr->hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
  75. gr->gdfifocfg = dwc2_readl(hsotg->regs + GDFIFOCFG);
  76. for (i = 0; i < MAX_EPS_CHANNELS; i++)
  77. gr->dtxfsiz[i] = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
  78. gr->valid = true;
  79. return 0;
  80. }
  81. /**
  82. * dwc2_restore_global_registers() - Restore controller global registers.
  83. * When resuming usb bus, device registers needs to be restored
  84. * if controller power were disabled.
  85. *
  86. * @hsotg: Programming view of the DWC_otg controller
  87. */
  88. static int dwc2_restore_global_registers(struct dwc2_hsotg *hsotg)
  89. {
  90. struct dwc2_gregs_backup *gr;
  91. int i;
  92. dev_dbg(hsotg->dev, "%s\n", __func__);
  93. /* Restore global regs */
  94. gr = &hsotg->gr_backup;
  95. if (!gr->valid) {
  96. dev_err(hsotg->dev, "%s: no global registers to restore\n",
  97. __func__);
  98. return -EINVAL;
  99. }
  100. gr->valid = false;
  101. dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
  102. dwc2_writel(gr->gotgctl, hsotg->regs + GOTGCTL);
  103. dwc2_writel(gr->gintmsk, hsotg->regs + GINTMSK);
  104. dwc2_writel(gr->gusbcfg, hsotg->regs + GUSBCFG);
  105. dwc2_writel(gr->gahbcfg, hsotg->regs + GAHBCFG);
  106. dwc2_writel(gr->grxfsiz, hsotg->regs + GRXFSIZ);
  107. dwc2_writel(gr->gnptxfsiz, hsotg->regs + GNPTXFSIZ);
  108. dwc2_writel(gr->hptxfsiz, hsotg->regs + HPTXFSIZ);
  109. dwc2_writel(gr->gdfifocfg, hsotg->regs + GDFIFOCFG);
  110. for (i = 0; i < MAX_EPS_CHANNELS; i++)
  111. dwc2_writel(gr->dtxfsiz[i], hsotg->regs + DPTXFSIZN(i));
  112. return 0;
  113. }
  114. /**
  115. * dwc2_exit_hibernation() - Exit controller from Partial Power Down.
  116. *
  117. * @hsotg: Programming view of the DWC_otg controller
  118. * @restore: Controller registers need to be restored
  119. */
  120. int dwc2_exit_hibernation(struct dwc2_hsotg *hsotg, bool restore)
  121. {
  122. u32 pcgcctl;
  123. int ret = 0;
  124. if (!hsotg->core_params->hibernation)
  125. return -ENOTSUPP;
  126. pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
  127. pcgcctl &= ~PCGCTL_STOPPCLK;
  128. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  129. pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
  130. pcgcctl &= ~PCGCTL_PWRCLMP;
  131. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  132. pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
  133. pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
  134. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  135. udelay(100);
  136. if (restore) {
  137. ret = dwc2_restore_global_registers(hsotg);
  138. if (ret) {
  139. dev_err(hsotg->dev, "%s: failed to restore registers\n",
  140. __func__);
  141. return ret;
  142. }
  143. if (dwc2_is_host_mode(hsotg)) {
  144. ret = dwc2_restore_host_registers(hsotg);
  145. if (ret) {
  146. dev_err(hsotg->dev, "%s: failed to restore host registers\n",
  147. __func__);
  148. return ret;
  149. }
  150. } else {
  151. ret = dwc2_restore_device_registers(hsotg);
  152. if (ret) {
  153. dev_err(hsotg->dev, "%s: failed to restore device registers\n",
  154. __func__);
  155. return ret;
  156. }
  157. }
  158. }
  159. return ret;
  160. }
  161. /**
  162. * dwc2_enter_hibernation() - Put controller in Partial Power Down.
  163. *
  164. * @hsotg: Programming view of the DWC_otg controller
  165. */
  166. int dwc2_enter_hibernation(struct dwc2_hsotg *hsotg)
  167. {
  168. u32 pcgcctl;
  169. int ret = 0;
  170. if (!hsotg->core_params->hibernation)
  171. return -ENOTSUPP;
  172. /* Backup all registers */
  173. ret = dwc2_backup_global_registers(hsotg);
  174. if (ret) {
  175. dev_err(hsotg->dev, "%s: failed to backup global registers\n",
  176. __func__);
  177. return ret;
  178. }
  179. if (dwc2_is_host_mode(hsotg)) {
  180. ret = dwc2_backup_host_registers(hsotg);
  181. if (ret) {
  182. dev_err(hsotg->dev, "%s: failed to backup host registers\n",
  183. __func__);
  184. return ret;
  185. }
  186. } else {
  187. ret = dwc2_backup_device_registers(hsotg);
  188. if (ret) {
  189. dev_err(hsotg->dev, "%s: failed to backup device registers\n",
  190. __func__);
  191. return ret;
  192. }
  193. }
  194. /*
  195. * Clear any pending interrupts since dwc2 will not be able to
  196. * clear them after entering hibernation.
  197. */
  198. dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
  199. /* Put the controller in low power state */
  200. pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
  201. pcgcctl |= PCGCTL_PWRCLMP;
  202. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  203. ndelay(20);
  204. pcgcctl |= PCGCTL_RSTPDWNMODULE;
  205. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  206. ndelay(20);
  207. pcgcctl |= PCGCTL_STOPPCLK;
  208. dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
  209. return ret;
  210. }
  211. /**
  212. * dwc2_wait_for_mode() - Waits for the controller mode.
  213. * @hsotg: Programming view of the DWC_otg controller.
  214. * @host_mode: If true, waits for host mode, otherwise device mode.
  215. */
  216. static void dwc2_wait_for_mode(struct dwc2_hsotg *hsotg,
  217. bool host_mode)
  218. {
  219. ktime_t start;
  220. ktime_t end;
  221. unsigned int timeout = 110;
  222. dev_vdbg(hsotg->dev, "Waiting for %s mode\n",
  223. host_mode ? "host" : "device");
  224. start = ktime_get();
  225. while (1) {
  226. s64 ms;
  227. if (dwc2_is_host_mode(hsotg) == host_mode) {
  228. dev_vdbg(hsotg->dev, "%s mode set\n",
  229. host_mode ? "Host" : "Device");
  230. break;
  231. }
  232. end = ktime_get();
  233. ms = ktime_to_ms(ktime_sub(end, start));
  234. if (ms >= (s64)timeout) {
  235. dev_warn(hsotg->dev, "%s: Couldn't set %s mode\n",
  236. __func__, host_mode ? "host" : "device");
  237. break;
  238. }
  239. usleep_range(1000, 2000);
  240. }
  241. }
  242. /**
  243. * dwc2_iddig_filter_enabled() - Returns true if the IDDIG debounce
  244. * filter is enabled.
  245. */
  246. static bool dwc2_iddig_filter_enabled(struct dwc2_hsotg *hsotg)
  247. {
  248. u32 gsnpsid;
  249. u32 ghwcfg4;
  250. if (!dwc2_hw_is_otg(hsotg))
  251. return false;
  252. /* Check if core configuration includes the IDDIG filter. */
  253. ghwcfg4 = dwc2_readl(hsotg->regs + GHWCFG4);
  254. if (!(ghwcfg4 & GHWCFG4_IDDIG_FILT_EN))
  255. return false;
  256. /*
  257. * Check if the IDDIG debounce filter is bypassed. Available
  258. * in core version >= 3.10a.
  259. */
  260. gsnpsid = dwc2_readl(hsotg->regs + GSNPSID);
  261. if (gsnpsid >= DWC2_CORE_REV_3_10a) {
  262. u32 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
  263. if (gotgctl & GOTGCTL_DBNCE_FLTR_BYPASS)
  264. return false;
  265. }
  266. return true;
  267. }
  268. /*
  269. * Do core a soft reset of the core. Be careful with this because it
  270. * resets all the internal state machines of the core.
  271. */
  272. int dwc2_core_reset(struct dwc2_hsotg *hsotg)
  273. {
  274. u32 greset;
  275. int count = 0;
  276. bool wait_for_host_mode = false;
  277. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  278. /*
  279. * If the current mode is host, either due to the force mode
  280. * bit being set (which persists after core reset) or the
  281. * connector id pin, a core soft reset will temporarily reset
  282. * the mode to device. A delay from the IDDIG debounce filter
  283. * will occur before going back to host mode.
  284. *
  285. * Determine whether we will go back into host mode after a
  286. * reset and account for this delay after the reset.
  287. */
  288. if (dwc2_iddig_filter_enabled(hsotg)) {
  289. u32 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
  290. u32 gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  291. if (!(gotgctl & GOTGCTL_CONID_B) ||
  292. (gusbcfg & GUSBCFG_FORCEHOSTMODE)) {
  293. wait_for_host_mode = true;
  294. }
  295. }
  296. /* Core Soft Reset */
  297. greset = dwc2_readl(hsotg->regs + GRSTCTL);
  298. greset |= GRSTCTL_CSFTRST;
  299. dwc2_writel(greset, hsotg->regs + GRSTCTL);
  300. do {
  301. udelay(1);
  302. greset = dwc2_readl(hsotg->regs + GRSTCTL);
  303. if (++count > 50) {
  304. dev_warn(hsotg->dev,
  305. "%s() HANG! Soft Reset GRSTCTL=%0x\n",
  306. __func__, greset);
  307. return -EBUSY;
  308. }
  309. } while (greset & GRSTCTL_CSFTRST);
  310. /* Wait for AHB master IDLE state */
  311. count = 0;
  312. do {
  313. udelay(1);
  314. greset = dwc2_readl(hsotg->regs + GRSTCTL);
  315. if (++count > 50) {
  316. dev_warn(hsotg->dev,
  317. "%s() HANG! AHB Idle GRSTCTL=%0x\n",
  318. __func__, greset);
  319. return -EBUSY;
  320. }
  321. } while (!(greset & GRSTCTL_AHBIDLE));
  322. if (wait_for_host_mode)
  323. dwc2_wait_for_mode(hsotg, true);
  324. return 0;
  325. }
  326. /*
  327. * Force the mode of the controller.
  328. *
  329. * Forcing the mode is needed for two cases:
  330. *
  331. * 1) If the dr_mode is set to either HOST or PERIPHERAL we force the
  332. * controller to stay in a particular mode regardless of ID pin
  333. * changes. We do this usually after a core reset.
  334. *
  335. * 2) During probe we want to read reset values of the hw
  336. * configuration registers that are only available in either host or
  337. * device mode. We may need to force the mode if the current mode does
  338. * not allow us to access the register in the mode that we want.
  339. *
  340. * In either case it only makes sense to force the mode if the
  341. * controller hardware is OTG capable.
  342. *
  343. * Checks are done in this function to determine whether doing a force
  344. * would be valid or not.
  345. *
  346. * If a force is done, it requires a IDDIG debounce filter delay if
  347. * the filter is configured and enabled. We poll the current mode of
  348. * the controller to account for this delay.
  349. */
  350. static bool dwc2_force_mode(struct dwc2_hsotg *hsotg, bool host)
  351. {
  352. u32 gusbcfg;
  353. u32 set;
  354. u32 clear;
  355. dev_dbg(hsotg->dev, "Forcing mode to %s\n", host ? "host" : "device");
  356. /*
  357. * Force mode has no effect if the hardware is not OTG.
  358. */
  359. if (!dwc2_hw_is_otg(hsotg))
  360. return false;
  361. /*
  362. * If dr_mode is either peripheral or host only, there is no
  363. * need to ever force the mode to the opposite mode.
  364. */
  365. if (WARN_ON(host && hsotg->dr_mode == USB_DR_MODE_PERIPHERAL))
  366. return false;
  367. if (WARN_ON(!host && hsotg->dr_mode == USB_DR_MODE_HOST))
  368. return false;
  369. gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  370. set = host ? GUSBCFG_FORCEHOSTMODE : GUSBCFG_FORCEDEVMODE;
  371. clear = host ? GUSBCFG_FORCEDEVMODE : GUSBCFG_FORCEHOSTMODE;
  372. gusbcfg &= ~clear;
  373. gusbcfg |= set;
  374. dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
  375. dwc2_wait_for_mode(hsotg, host);
  376. return true;
  377. }
  378. /**
  379. * dwc2_clear_force_mode() - Clears the force mode bits.
  380. *
  381. * After clearing the bits, wait up to 100 ms to account for any
  382. * potential IDDIG filter delay. We can't know if we expect this delay
  383. * or not because the value of the connector ID status is affected by
  384. * the force mode. We only need to call this once during probe if
  385. * dr_mode == OTG.
  386. */
  387. static void dwc2_clear_force_mode(struct dwc2_hsotg *hsotg)
  388. {
  389. u32 gusbcfg;
  390. gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
  391. gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
  392. gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
  393. dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
  394. if (dwc2_iddig_filter_enabled(hsotg))
  395. msleep(100);
  396. }
  397. /*
  398. * Sets or clears force mode based on the dr_mode parameter.
  399. */
  400. void dwc2_force_dr_mode(struct dwc2_hsotg *hsotg)
  401. {
  402. bool ret;
  403. switch (hsotg->dr_mode) {
  404. case USB_DR_MODE_HOST:
  405. ret = dwc2_force_mode(hsotg, true);
  406. /*
  407. * NOTE: This is required for some rockchip soc based
  408. * platforms on their host-only dwc2.
  409. */
  410. if (!ret)
  411. msleep(50);
  412. break;
  413. case USB_DR_MODE_PERIPHERAL:
  414. dwc2_force_mode(hsotg, false);
  415. break;
  416. case USB_DR_MODE_OTG:
  417. dwc2_clear_force_mode(hsotg);
  418. break;
  419. default:
  420. dev_warn(hsotg->dev, "%s() Invalid dr_mode=%d\n",
  421. __func__, hsotg->dr_mode);
  422. break;
  423. }
  424. }
  425. /*
  426. * Do core a soft reset of the core. Be careful with this because it
  427. * resets all the internal state machines of the core.
  428. *
  429. * Additionally this will apply force mode as per the hsotg->dr_mode
  430. * parameter.
  431. */
  432. int dwc2_core_reset_and_force_dr_mode(struct dwc2_hsotg *hsotg)
  433. {
  434. int retval;
  435. retval = dwc2_core_reset(hsotg);
  436. if (retval)
  437. return retval;
  438. dwc2_force_dr_mode(hsotg);
  439. return 0;
  440. }
  441. /**
  442. * dwc2_dump_host_registers() - Prints the host registers
  443. *
  444. * @hsotg: Programming view of DWC_otg controller
  445. *
  446. * NOTE: This function will be removed once the peripheral controller code
  447. * is integrated and the driver is stable
  448. */
  449. void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg)
  450. {
  451. #ifdef DEBUG
  452. u32 __iomem *addr;
  453. int i;
  454. dev_dbg(hsotg->dev, "Host Global Registers\n");
  455. addr = hsotg->regs + HCFG;
  456. dev_dbg(hsotg->dev, "HCFG @0x%08lX : 0x%08X\n",
  457. (unsigned long)addr, dwc2_readl(addr));
  458. addr = hsotg->regs + HFIR;
  459. dev_dbg(hsotg->dev, "HFIR @0x%08lX : 0x%08X\n",
  460. (unsigned long)addr, dwc2_readl(addr));
  461. addr = hsotg->regs + HFNUM;
  462. dev_dbg(hsotg->dev, "HFNUM @0x%08lX : 0x%08X\n",
  463. (unsigned long)addr, dwc2_readl(addr));
  464. addr = hsotg->regs + HPTXSTS;
  465. dev_dbg(hsotg->dev, "HPTXSTS @0x%08lX : 0x%08X\n",
  466. (unsigned long)addr, dwc2_readl(addr));
  467. addr = hsotg->regs + HAINT;
  468. dev_dbg(hsotg->dev, "HAINT @0x%08lX : 0x%08X\n",
  469. (unsigned long)addr, dwc2_readl(addr));
  470. addr = hsotg->regs + HAINTMSK;
  471. dev_dbg(hsotg->dev, "HAINTMSK @0x%08lX : 0x%08X\n",
  472. (unsigned long)addr, dwc2_readl(addr));
  473. if (hsotg->core_params->dma_desc_enable > 0) {
  474. addr = hsotg->regs + HFLBADDR;
  475. dev_dbg(hsotg->dev, "HFLBADDR @0x%08lX : 0x%08X\n",
  476. (unsigned long)addr, dwc2_readl(addr));
  477. }
  478. addr = hsotg->regs + HPRT0;
  479. dev_dbg(hsotg->dev, "HPRT0 @0x%08lX : 0x%08X\n",
  480. (unsigned long)addr, dwc2_readl(addr));
  481. for (i = 0; i < hsotg->core_params->host_channels; i++) {
  482. dev_dbg(hsotg->dev, "Host Channel %d Specific Registers\n", i);
  483. addr = hsotg->regs + HCCHAR(i);
  484. dev_dbg(hsotg->dev, "HCCHAR @0x%08lX : 0x%08X\n",
  485. (unsigned long)addr, dwc2_readl(addr));
  486. addr = hsotg->regs + HCSPLT(i);
  487. dev_dbg(hsotg->dev, "HCSPLT @0x%08lX : 0x%08X\n",
  488. (unsigned long)addr, dwc2_readl(addr));
  489. addr = hsotg->regs + HCINT(i);
  490. dev_dbg(hsotg->dev, "HCINT @0x%08lX : 0x%08X\n",
  491. (unsigned long)addr, dwc2_readl(addr));
  492. addr = hsotg->regs + HCINTMSK(i);
  493. dev_dbg(hsotg->dev, "HCINTMSK @0x%08lX : 0x%08X\n",
  494. (unsigned long)addr, dwc2_readl(addr));
  495. addr = hsotg->regs + HCTSIZ(i);
  496. dev_dbg(hsotg->dev, "HCTSIZ @0x%08lX : 0x%08X\n",
  497. (unsigned long)addr, dwc2_readl(addr));
  498. addr = hsotg->regs + HCDMA(i);
  499. dev_dbg(hsotg->dev, "HCDMA @0x%08lX : 0x%08X\n",
  500. (unsigned long)addr, dwc2_readl(addr));
  501. if (hsotg->core_params->dma_desc_enable > 0) {
  502. addr = hsotg->regs + HCDMAB(i);
  503. dev_dbg(hsotg->dev, "HCDMAB @0x%08lX : 0x%08X\n",
  504. (unsigned long)addr, dwc2_readl(addr));
  505. }
  506. }
  507. #endif
  508. }
  509. /**
  510. * dwc2_dump_global_registers() - Prints the core global registers
  511. *
  512. * @hsotg: Programming view of DWC_otg controller
  513. *
  514. * NOTE: This function will be removed once the peripheral controller code
  515. * is integrated and the driver is stable
  516. */
  517. void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg)
  518. {
  519. #ifdef DEBUG
  520. u32 __iomem *addr;
  521. dev_dbg(hsotg->dev, "Core Global Registers\n");
  522. addr = hsotg->regs + GOTGCTL;
  523. dev_dbg(hsotg->dev, "GOTGCTL @0x%08lX : 0x%08X\n",
  524. (unsigned long)addr, dwc2_readl(addr));
  525. addr = hsotg->regs + GOTGINT;
  526. dev_dbg(hsotg->dev, "GOTGINT @0x%08lX : 0x%08X\n",
  527. (unsigned long)addr, dwc2_readl(addr));
  528. addr = hsotg->regs + GAHBCFG;
  529. dev_dbg(hsotg->dev, "GAHBCFG @0x%08lX : 0x%08X\n",
  530. (unsigned long)addr, dwc2_readl(addr));
  531. addr = hsotg->regs + GUSBCFG;
  532. dev_dbg(hsotg->dev, "GUSBCFG @0x%08lX : 0x%08X\n",
  533. (unsigned long)addr, dwc2_readl(addr));
  534. addr = hsotg->regs + GRSTCTL;
  535. dev_dbg(hsotg->dev, "GRSTCTL @0x%08lX : 0x%08X\n",
  536. (unsigned long)addr, dwc2_readl(addr));
  537. addr = hsotg->regs + GINTSTS;
  538. dev_dbg(hsotg->dev, "GINTSTS @0x%08lX : 0x%08X\n",
  539. (unsigned long)addr, dwc2_readl(addr));
  540. addr = hsotg->regs + GINTMSK;
  541. dev_dbg(hsotg->dev, "GINTMSK @0x%08lX : 0x%08X\n",
  542. (unsigned long)addr, dwc2_readl(addr));
  543. addr = hsotg->regs + GRXSTSR;
  544. dev_dbg(hsotg->dev, "GRXSTSR @0x%08lX : 0x%08X\n",
  545. (unsigned long)addr, dwc2_readl(addr));
  546. addr = hsotg->regs + GRXFSIZ;
  547. dev_dbg(hsotg->dev, "GRXFSIZ @0x%08lX : 0x%08X\n",
  548. (unsigned long)addr, dwc2_readl(addr));
  549. addr = hsotg->regs + GNPTXFSIZ;
  550. dev_dbg(hsotg->dev, "GNPTXFSIZ @0x%08lX : 0x%08X\n",
  551. (unsigned long)addr, dwc2_readl(addr));
  552. addr = hsotg->regs + GNPTXSTS;
  553. dev_dbg(hsotg->dev, "GNPTXSTS @0x%08lX : 0x%08X\n",
  554. (unsigned long)addr, dwc2_readl(addr));
  555. addr = hsotg->regs + GI2CCTL;
  556. dev_dbg(hsotg->dev, "GI2CCTL @0x%08lX : 0x%08X\n",
  557. (unsigned long)addr, dwc2_readl(addr));
  558. addr = hsotg->regs + GPVNDCTL;
  559. dev_dbg(hsotg->dev, "GPVNDCTL @0x%08lX : 0x%08X\n",
  560. (unsigned long)addr, dwc2_readl(addr));
  561. addr = hsotg->regs + GGPIO;
  562. dev_dbg(hsotg->dev, "GGPIO @0x%08lX : 0x%08X\n",
  563. (unsigned long)addr, dwc2_readl(addr));
  564. addr = hsotg->regs + GUID;
  565. dev_dbg(hsotg->dev, "GUID @0x%08lX : 0x%08X\n",
  566. (unsigned long)addr, dwc2_readl(addr));
  567. addr = hsotg->regs + GSNPSID;
  568. dev_dbg(hsotg->dev, "GSNPSID @0x%08lX : 0x%08X\n",
  569. (unsigned long)addr, dwc2_readl(addr));
  570. addr = hsotg->regs + GHWCFG1;
  571. dev_dbg(hsotg->dev, "GHWCFG1 @0x%08lX : 0x%08X\n",
  572. (unsigned long)addr, dwc2_readl(addr));
  573. addr = hsotg->regs + GHWCFG2;
  574. dev_dbg(hsotg->dev, "GHWCFG2 @0x%08lX : 0x%08X\n",
  575. (unsigned long)addr, dwc2_readl(addr));
  576. addr = hsotg->regs + GHWCFG3;
  577. dev_dbg(hsotg->dev, "GHWCFG3 @0x%08lX : 0x%08X\n",
  578. (unsigned long)addr, dwc2_readl(addr));
  579. addr = hsotg->regs + GHWCFG4;
  580. dev_dbg(hsotg->dev, "GHWCFG4 @0x%08lX : 0x%08X\n",
  581. (unsigned long)addr, dwc2_readl(addr));
  582. addr = hsotg->regs + GLPMCFG;
  583. dev_dbg(hsotg->dev, "GLPMCFG @0x%08lX : 0x%08X\n",
  584. (unsigned long)addr, dwc2_readl(addr));
  585. addr = hsotg->regs + GPWRDN;
  586. dev_dbg(hsotg->dev, "GPWRDN @0x%08lX : 0x%08X\n",
  587. (unsigned long)addr, dwc2_readl(addr));
  588. addr = hsotg->regs + GDFIFOCFG;
  589. dev_dbg(hsotg->dev, "GDFIFOCFG @0x%08lX : 0x%08X\n",
  590. (unsigned long)addr, dwc2_readl(addr));
  591. addr = hsotg->regs + HPTXFSIZ;
  592. dev_dbg(hsotg->dev, "HPTXFSIZ @0x%08lX : 0x%08X\n",
  593. (unsigned long)addr, dwc2_readl(addr));
  594. addr = hsotg->regs + PCGCTL;
  595. dev_dbg(hsotg->dev, "PCGCTL @0x%08lX : 0x%08X\n",
  596. (unsigned long)addr, dwc2_readl(addr));
  597. #endif
  598. }
  599. /**
  600. * dwc2_flush_tx_fifo() - Flushes a Tx FIFO
  601. *
  602. * @hsotg: Programming view of DWC_otg controller
  603. * @num: Tx FIFO to flush
  604. */
  605. void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num)
  606. {
  607. u32 greset;
  608. int count = 0;
  609. dev_vdbg(hsotg->dev, "Flush Tx FIFO %d\n", num);
  610. greset = GRSTCTL_TXFFLSH;
  611. greset |= num << GRSTCTL_TXFNUM_SHIFT & GRSTCTL_TXFNUM_MASK;
  612. dwc2_writel(greset, hsotg->regs + GRSTCTL);
  613. do {
  614. greset = dwc2_readl(hsotg->regs + GRSTCTL);
  615. if (++count > 10000) {
  616. dev_warn(hsotg->dev,
  617. "%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
  618. __func__, greset,
  619. dwc2_readl(hsotg->regs + GNPTXSTS));
  620. break;
  621. }
  622. udelay(1);
  623. } while (greset & GRSTCTL_TXFFLSH);
  624. /* Wait for at least 3 PHY Clocks */
  625. udelay(1);
  626. }
  627. /**
  628. * dwc2_flush_rx_fifo() - Flushes the Rx FIFO
  629. *
  630. * @hsotg: Programming view of DWC_otg controller
  631. */
  632. void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg)
  633. {
  634. u32 greset;
  635. int count = 0;
  636. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  637. greset = GRSTCTL_RXFFLSH;
  638. dwc2_writel(greset, hsotg->regs + GRSTCTL);
  639. do {
  640. greset = dwc2_readl(hsotg->regs + GRSTCTL);
  641. if (++count > 10000) {
  642. dev_warn(hsotg->dev, "%s() HANG! GRSTCTL=%0x\n",
  643. __func__, greset);
  644. break;
  645. }
  646. udelay(1);
  647. } while (greset & GRSTCTL_RXFFLSH);
  648. /* Wait for at least 3 PHY Clocks */
  649. udelay(1);
  650. }
  651. #define DWC2_OUT_OF_BOUNDS(a, b, c) ((a) < (b) || (a) > (c))
  652. /* Parameter access functions */
  653. void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg, int val)
  654. {
  655. int valid = 1;
  656. switch (val) {
  657. case DWC2_CAP_PARAM_HNP_SRP_CAPABLE:
  658. if (hsotg->hw_params.op_mode != GHWCFG2_OP_MODE_HNP_SRP_CAPABLE)
  659. valid = 0;
  660. break;
  661. case DWC2_CAP_PARAM_SRP_ONLY_CAPABLE:
  662. switch (hsotg->hw_params.op_mode) {
  663. case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
  664. case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
  665. case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
  666. case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
  667. break;
  668. default:
  669. valid = 0;
  670. break;
  671. }
  672. break;
  673. case DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE:
  674. /* always valid */
  675. break;
  676. default:
  677. valid = 0;
  678. break;
  679. }
  680. if (!valid) {
  681. if (val >= 0)
  682. dev_err(hsotg->dev,
  683. "%d invalid for otg_cap parameter. Check HW configuration.\n",
  684. val);
  685. switch (hsotg->hw_params.op_mode) {
  686. case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
  687. val = DWC2_CAP_PARAM_HNP_SRP_CAPABLE;
  688. break;
  689. case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
  690. case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
  691. case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
  692. val = DWC2_CAP_PARAM_SRP_ONLY_CAPABLE;
  693. break;
  694. default:
  695. val = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
  696. break;
  697. }
  698. dev_dbg(hsotg->dev, "Setting otg_cap to %d\n", val);
  699. }
  700. hsotg->core_params->otg_cap = val;
  701. }
  702. void dwc2_set_param_dma_enable(struct dwc2_hsotg *hsotg, int val)
  703. {
  704. int valid = 1;
  705. if (val > 0 && hsotg->hw_params.arch == GHWCFG2_SLAVE_ONLY_ARCH)
  706. valid = 0;
  707. if (val < 0)
  708. valid = 0;
  709. if (!valid) {
  710. if (val >= 0)
  711. dev_err(hsotg->dev,
  712. "%d invalid for dma_enable parameter. Check HW configuration.\n",
  713. val);
  714. val = hsotg->hw_params.arch != GHWCFG2_SLAVE_ONLY_ARCH;
  715. dev_dbg(hsotg->dev, "Setting dma_enable to %d\n", val);
  716. }
  717. hsotg->core_params->dma_enable = val;
  718. }
  719. void dwc2_set_param_dma_desc_enable(struct dwc2_hsotg *hsotg, int val)
  720. {
  721. int valid = 1;
  722. if (val > 0 && (hsotg->core_params->dma_enable <= 0 ||
  723. !hsotg->hw_params.dma_desc_enable))
  724. valid = 0;
  725. if (val < 0)
  726. valid = 0;
  727. if (!valid) {
  728. if (val >= 0)
  729. dev_err(hsotg->dev,
  730. "%d invalid for dma_desc_enable parameter. Check HW configuration.\n",
  731. val);
  732. val = (hsotg->core_params->dma_enable > 0 &&
  733. hsotg->hw_params.dma_desc_enable);
  734. dev_dbg(hsotg->dev, "Setting dma_desc_enable to %d\n", val);
  735. }
  736. hsotg->core_params->dma_desc_enable = val;
  737. }
  738. void dwc2_set_param_dma_desc_fs_enable(struct dwc2_hsotg *hsotg, int val)
  739. {
  740. int valid = 1;
  741. if (val > 0 && (hsotg->core_params->dma_enable <= 0 ||
  742. !hsotg->hw_params.dma_desc_enable))
  743. valid = 0;
  744. if (val < 0)
  745. valid = 0;
  746. if (!valid) {
  747. if (val >= 0)
  748. dev_err(hsotg->dev,
  749. "%d invalid for dma_desc_fs_enable parameter. Check HW configuration.\n",
  750. val);
  751. val = (hsotg->core_params->dma_enable > 0 &&
  752. hsotg->hw_params.dma_desc_enable);
  753. }
  754. hsotg->core_params->dma_desc_fs_enable = val;
  755. dev_dbg(hsotg->dev, "Setting dma_desc_fs_enable to %d\n", val);
  756. }
  757. void dwc2_set_param_host_support_fs_ls_low_power(struct dwc2_hsotg *hsotg,
  758. int val)
  759. {
  760. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  761. if (val >= 0) {
  762. dev_err(hsotg->dev,
  763. "Wrong value for host_support_fs_low_power\n");
  764. dev_err(hsotg->dev,
  765. "host_support_fs_low_power must be 0 or 1\n");
  766. }
  767. val = 0;
  768. dev_dbg(hsotg->dev,
  769. "Setting host_support_fs_low_power to %d\n", val);
  770. }
  771. hsotg->core_params->host_support_fs_ls_low_power = val;
  772. }
  773. void dwc2_set_param_enable_dynamic_fifo(struct dwc2_hsotg *hsotg, int val)
  774. {
  775. int valid = 1;
  776. if (val > 0 && !hsotg->hw_params.enable_dynamic_fifo)
  777. valid = 0;
  778. if (val < 0)
  779. valid = 0;
  780. if (!valid) {
  781. if (val >= 0)
  782. dev_err(hsotg->dev,
  783. "%d invalid for enable_dynamic_fifo parameter. Check HW configuration.\n",
  784. val);
  785. val = hsotg->hw_params.enable_dynamic_fifo;
  786. dev_dbg(hsotg->dev, "Setting enable_dynamic_fifo to %d\n", val);
  787. }
  788. hsotg->core_params->enable_dynamic_fifo = val;
  789. }
  790. void dwc2_set_param_host_rx_fifo_size(struct dwc2_hsotg *hsotg, int val)
  791. {
  792. int valid = 1;
  793. if (val < 16 || val > hsotg->hw_params.host_rx_fifo_size)
  794. valid = 0;
  795. if (!valid) {
  796. if (val >= 0)
  797. dev_err(hsotg->dev,
  798. "%d invalid for host_rx_fifo_size. Check HW configuration.\n",
  799. val);
  800. val = hsotg->hw_params.host_rx_fifo_size;
  801. dev_dbg(hsotg->dev, "Setting host_rx_fifo_size to %d\n", val);
  802. }
  803. hsotg->core_params->host_rx_fifo_size = val;
  804. }
  805. void dwc2_set_param_host_nperio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
  806. {
  807. int valid = 1;
  808. if (val < 16 || val > hsotg->hw_params.host_nperio_tx_fifo_size)
  809. valid = 0;
  810. if (!valid) {
  811. if (val >= 0)
  812. dev_err(hsotg->dev,
  813. "%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
  814. val);
  815. val = hsotg->hw_params.host_nperio_tx_fifo_size;
  816. dev_dbg(hsotg->dev, "Setting host_nperio_tx_fifo_size to %d\n",
  817. val);
  818. }
  819. hsotg->core_params->host_nperio_tx_fifo_size = val;
  820. }
  821. void dwc2_set_param_host_perio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
  822. {
  823. int valid = 1;
  824. if (val < 16 || val > hsotg->hw_params.host_perio_tx_fifo_size)
  825. valid = 0;
  826. if (!valid) {
  827. if (val >= 0)
  828. dev_err(hsotg->dev,
  829. "%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
  830. val);
  831. val = hsotg->hw_params.host_perio_tx_fifo_size;
  832. dev_dbg(hsotg->dev, "Setting host_perio_tx_fifo_size to %d\n",
  833. val);
  834. }
  835. hsotg->core_params->host_perio_tx_fifo_size = val;
  836. }
  837. void dwc2_set_param_max_transfer_size(struct dwc2_hsotg *hsotg, int val)
  838. {
  839. int valid = 1;
  840. if (val < 2047 || val > hsotg->hw_params.max_transfer_size)
  841. valid = 0;
  842. if (!valid) {
  843. if (val >= 0)
  844. dev_err(hsotg->dev,
  845. "%d invalid for max_transfer_size. Check HW configuration.\n",
  846. val);
  847. val = hsotg->hw_params.max_transfer_size;
  848. dev_dbg(hsotg->dev, "Setting max_transfer_size to %d\n", val);
  849. }
  850. hsotg->core_params->max_transfer_size = val;
  851. }
  852. void dwc2_set_param_max_packet_count(struct dwc2_hsotg *hsotg, int val)
  853. {
  854. int valid = 1;
  855. if (val < 15 || val > hsotg->hw_params.max_packet_count)
  856. valid = 0;
  857. if (!valid) {
  858. if (val >= 0)
  859. dev_err(hsotg->dev,
  860. "%d invalid for max_packet_count. Check HW configuration.\n",
  861. val);
  862. val = hsotg->hw_params.max_packet_count;
  863. dev_dbg(hsotg->dev, "Setting max_packet_count to %d\n", val);
  864. }
  865. hsotg->core_params->max_packet_count = val;
  866. }
  867. void dwc2_set_param_host_channels(struct dwc2_hsotg *hsotg, int val)
  868. {
  869. int valid = 1;
  870. if (val < 1 || val > hsotg->hw_params.host_channels)
  871. valid = 0;
  872. if (!valid) {
  873. if (val >= 0)
  874. dev_err(hsotg->dev,
  875. "%d invalid for host_channels. Check HW configuration.\n",
  876. val);
  877. val = hsotg->hw_params.host_channels;
  878. dev_dbg(hsotg->dev, "Setting host_channels to %d\n", val);
  879. }
  880. hsotg->core_params->host_channels = val;
  881. }
  882. void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg, int val)
  883. {
  884. int valid = 0;
  885. u32 hs_phy_type, fs_phy_type;
  886. if (DWC2_OUT_OF_BOUNDS(val, DWC2_PHY_TYPE_PARAM_FS,
  887. DWC2_PHY_TYPE_PARAM_ULPI)) {
  888. if (val >= 0) {
  889. dev_err(hsotg->dev, "Wrong value for phy_type\n");
  890. dev_err(hsotg->dev, "phy_type must be 0, 1 or 2\n");
  891. }
  892. valid = 0;
  893. }
  894. hs_phy_type = hsotg->hw_params.hs_phy_type;
  895. fs_phy_type = hsotg->hw_params.fs_phy_type;
  896. if (val == DWC2_PHY_TYPE_PARAM_UTMI &&
  897. (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
  898. hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
  899. valid = 1;
  900. else if (val == DWC2_PHY_TYPE_PARAM_ULPI &&
  901. (hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI ||
  902. hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
  903. valid = 1;
  904. else if (val == DWC2_PHY_TYPE_PARAM_FS &&
  905. fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
  906. valid = 1;
  907. if (!valid) {
  908. if (val >= 0)
  909. dev_err(hsotg->dev,
  910. "%d invalid for phy_type. Check HW configuration.\n",
  911. val);
  912. val = DWC2_PHY_TYPE_PARAM_FS;
  913. if (hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED) {
  914. if (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
  915. hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI)
  916. val = DWC2_PHY_TYPE_PARAM_UTMI;
  917. else
  918. val = DWC2_PHY_TYPE_PARAM_ULPI;
  919. }
  920. dev_dbg(hsotg->dev, "Setting phy_type to %d\n", val);
  921. }
  922. hsotg->core_params->phy_type = val;
  923. }
  924. static int dwc2_get_param_phy_type(struct dwc2_hsotg *hsotg)
  925. {
  926. return hsotg->core_params->phy_type;
  927. }
  928. void dwc2_set_param_speed(struct dwc2_hsotg *hsotg, int val)
  929. {
  930. int valid = 1;
  931. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  932. if (val >= 0) {
  933. dev_err(hsotg->dev, "Wrong value for speed parameter\n");
  934. dev_err(hsotg->dev, "max_speed parameter must be 0 or 1\n");
  935. }
  936. valid = 0;
  937. }
  938. if (val == DWC2_SPEED_PARAM_HIGH &&
  939. dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS)
  940. valid = 0;
  941. if (!valid) {
  942. if (val >= 0)
  943. dev_err(hsotg->dev,
  944. "%d invalid for speed parameter. Check HW configuration.\n",
  945. val);
  946. val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS ?
  947. DWC2_SPEED_PARAM_FULL : DWC2_SPEED_PARAM_HIGH;
  948. dev_dbg(hsotg->dev, "Setting speed to %d\n", val);
  949. }
  950. hsotg->core_params->speed = val;
  951. }
  952. void dwc2_set_param_host_ls_low_power_phy_clk(struct dwc2_hsotg *hsotg, int val)
  953. {
  954. int valid = 1;
  955. if (DWC2_OUT_OF_BOUNDS(val, DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ,
  956. DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)) {
  957. if (val >= 0) {
  958. dev_err(hsotg->dev,
  959. "Wrong value for host_ls_low_power_phy_clk parameter\n");
  960. dev_err(hsotg->dev,
  961. "host_ls_low_power_phy_clk must be 0 or 1\n");
  962. }
  963. valid = 0;
  964. }
  965. if (val == DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ &&
  966. dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS)
  967. valid = 0;
  968. if (!valid) {
  969. if (val >= 0)
  970. dev_err(hsotg->dev,
  971. "%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
  972. val);
  973. val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS
  974. ? DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ
  975. : DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
  976. dev_dbg(hsotg->dev, "Setting host_ls_low_power_phy_clk to %d\n",
  977. val);
  978. }
  979. hsotg->core_params->host_ls_low_power_phy_clk = val;
  980. }
  981. void dwc2_set_param_phy_ulpi_ddr(struct dwc2_hsotg *hsotg, int val)
  982. {
  983. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  984. if (val >= 0) {
  985. dev_err(hsotg->dev, "Wrong value for phy_ulpi_ddr\n");
  986. dev_err(hsotg->dev, "phy_upli_ddr must be 0 or 1\n");
  987. }
  988. val = 0;
  989. dev_dbg(hsotg->dev, "Setting phy_upli_ddr to %d\n", val);
  990. }
  991. hsotg->core_params->phy_ulpi_ddr = val;
  992. }
  993. void dwc2_set_param_phy_ulpi_ext_vbus(struct dwc2_hsotg *hsotg, int val)
  994. {
  995. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  996. if (val >= 0) {
  997. dev_err(hsotg->dev,
  998. "Wrong value for phy_ulpi_ext_vbus\n");
  999. dev_err(hsotg->dev,
  1000. "phy_ulpi_ext_vbus must be 0 or 1\n");
  1001. }
  1002. val = 0;
  1003. dev_dbg(hsotg->dev, "Setting phy_ulpi_ext_vbus to %d\n", val);
  1004. }
  1005. hsotg->core_params->phy_ulpi_ext_vbus = val;
  1006. }
  1007. void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg, int val)
  1008. {
  1009. int valid = 0;
  1010. switch (hsotg->hw_params.utmi_phy_data_width) {
  1011. case GHWCFG4_UTMI_PHY_DATA_WIDTH_8:
  1012. valid = (val == 8);
  1013. break;
  1014. case GHWCFG4_UTMI_PHY_DATA_WIDTH_16:
  1015. valid = (val == 16);
  1016. break;
  1017. case GHWCFG4_UTMI_PHY_DATA_WIDTH_8_OR_16:
  1018. valid = (val == 8 || val == 16);
  1019. break;
  1020. }
  1021. if (!valid) {
  1022. if (val >= 0) {
  1023. dev_err(hsotg->dev,
  1024. "%d invalid for phy_utmi_width. Check HW configuration.\n",
  1025. val);
  1026. }
  1027. val = (hsotg->hw_params.utmi_phy_data_width ==
  1028. GHWCFG4_UTMI_PHY_DATA_WIDTH_8) ? 8 : 16;
  1029. dev_dbg(hsotg->dev, "Setting phy_utmi_width to %d\n", val);
  1030. }
  1031. hsotg->core_params->phy_utmi_width = val;
  1032. }
  1033. void dwc2_set_param_ulpi_fs_ls(struct dwc2_hsotg *hsotg, int val)
  1034. {
  1035. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  1036. if (val >= 0) {
  1037. dev_err(hsotg->dev, "Wrong value for ulpi_fs_ls\n");
  1038. dev_err(hsotg->dev, "ulpi_fs_ls must be 0 or 1\n");
  1039. }
  1040. val = 0;
  1041. dev_dbg(hsotg->dev, "Setting ulpi_fs_ls to %d\n", val);
  1042. }
  1043. hsotg->core_params->ulpi_fs_ls = val;
  1044. }
  1045. void dwc2_set_param_ts_dline(struct dwc2_hsotg *hsotg, int val)
  1046. {
  1047. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  1048. if (val >= 0) {
  1049. dev_err(hsotg->dev, "Wrong value for ts_dline\n");
  1050. dev_err(hsotg->dev, "ts_dline must be 0 or 1\n");
  1051. }
  1052. val = 0;
  1053. dev_dbg(hsotg->dev, "Setting ts_dline to %d\n", val);
  1054. }
  1055. hsotg->core_params->ts_dline = val;
  1056. }
  1057. void dwc2_set_param_i2c_enable(struct dwc2_hsotg *hsotg, int val)
  1058. {
  1059. int valid = 1;
  1060. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  1061. if (val >= 0) {
  1062. dev_err(hsotg->dev, "Wrong value for i2c_enable\n");
  1063. dev_err(hsotg->dev, "i2c_enable must be 0 or 1\n");
  1064. }
  1065. valid = 0;
  1066. }
  1067. if (val == 1 && !(hsotg->hw_params.i2c_enable))
  1068. valid = 0;
  1069. if (!valid) {
  1070. if (val >= 0)
  1071. dev_err(hsotg->dev,
  1072. "%d invalid for i2c_enable. Check HW configuration.\n",
  1073. val);
  1074. val = hsotg->hw_params.i2c_enable;
  1075. dev_dbg(hsotg->dev, "Setting i2c_enable to %d\n", val);
  1076. }
  1077. hsotg->core_params->i2c_enable = val;
  1078. }
  1079. void dwc2_set_param_en_multiple_tx_fifo(struct dwc2_hsotg *hsotg, int val)
  1080. {
  1081. int valid = 1;
  1082. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  1083. if (val >= 0) {
  1084. dev_err(hsotg->dev,
  1085. "Wrong value for en_multiple_tx_fifo,\n");
  1086. dev_err(hsotg->dev,
  1087. "en_multiple_tx_fifo must be 0 or 1\n");
  1088. }
  1089. valid = 0;
  1090. }
  1091. if (val == 1 && !hsotg->hw_params.en_multiple_tx_fifo)
  1092. valid = 0;
  1093. if (!valid) {
  1094. if (val >= 0)
  1095. dev_err(hsotg->dev,
  1096. "%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
  1097. val);
  1098. val = hsotg->hw_params.en_multiple_tx_fifo;
  1099. dev_dbg(hsotg->dev, "Setting en_multiple_tx_fifo to %d\n", val);
  1100. }
  1101. hsotg->core_params->en_multiple_tx_fifo = val;
  1102. }
  1103. void dwc2_set_param_reload_ctl(struct dwc2_hsotg *hsotg, int val)
  1104. {
  1105. int valid = 1;
  1106. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  1107. if (val >= 0) {
  1108. dev_err(hsotg->dev,
  1109. "'%d' invalid for parameter reload_ctl\n", val);
  1110. dev_err(hsotg->dev, "reload_ctl must be 0 or 1\n");
  1111. }
  1112. valid = 0;
  1113. }
  1114. if (val == 1 && hsotg->hw_params.snpsid < DWC2_CORE_REV_2_92a)
  1115. valid = 0;
  1116. if (!valid) {
  1117. if (val >= 0)
  1118. dev_err(hsotg->dev,
  1119. "%d invalid for parameter reload_ctl. Check HW configuration.\n",
  1120. val);
  1121. val = hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_92a;
  1122. dev_dbg(hsotg->dev, "Setting reload_ctl to %d\n", val);
  1123. }
  1124. hsotg->core_params->reload_ctl = val;
  1125. }
  1126. void dwc2_set_param_ahbcfg(struct dwc2_hsotg *hsotg, int val)
  1127. {
  1128. if (val != -1)
  1129. hsotg->core_params->ahbcfg = val;
  1130. else
  1131. hsotg->core_params->ahbcfg = GAHBCFG_HBSTLEN_INCR4 <<
  1132. GAHBCFG_HBSTLEN_SHIFT;
  1133. }
  1134. void dwc2_set_param_otg_ver(struct dwc2_hsotg *hsotg, int val)
  1135. {
  1136. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  1137. if (val >= 0) {
  1138. dev_err(hsotg->dev,
  1139. "'%d' invalid for parameter otg_ver\n", val);
  1140. dev_err(hsotg->dev,
  1141. "otg_ver must be 0 (for OTG 1.3 support) or 1 (for OTG 2.0 support)\n");
  1142. }
  1143. val = 0;
  1144. dev_dbg(hsotg->dev, "Setting otg_ver to %d\n", val);
  1145. }
  1146. hsotg->core_params->otg_ver = val;
  1147. }
  1148. static void dwc2_set_param_uframe_sched(struct dwc2_hsotg *hsotg, int val)
  1149. {
  1150. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  1151. if (val >= 0) {
  1152. dev_err(hsotg->dev,
  1153. "'%d' invalid for parameter uframe_sched\n",
  1154. val);
  1155. dev_err(hsotg->dev, "uframe_sched must be 0 or 1\n");
  1156. }
  1157. val = 1;
  1158. dev_dbg(hsotg->dev, "Setting uframe_sched to %d\n", val);
  1159. }
  1160. hsotg->core_params->uframe_sched = val;
  1161. }
  1162. static void dwc2_set_param_external_id_pin_ctl(struct dwc2_hsotg *hsotg,
  1163. int val)
  1164. {
  1165. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  1166. if (val >= 0) {
  1167. dev_err(hsotg->dev,
  1168. "'%d' invalid for parameter external_id_pin_ctl\n",
  1169. val);
  1170. dev_err(hsotg->dev, "external_id_pin_ctl must be 0 or 1\n");
  1171. }
  1172. val = 0;
  1173. dev_dbg(hsotg->dev, "Setting external_id_pin_ctl to %d\n", val);
  1174. }
  1175. hsotg->core_params->external_id_pin_ctl = val;
  1176. }
  1177. static void dwc2_set_param_hibernation(struct dwc2_hsotg *hsotg,
  1178. int val)
  1179. {
  1180. if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
  1181. if (val >= 0) {
  1182. dev_err(hsotg->dev,
  1183. "'%d' invalid for parameter hibernation\n",
  1184. val);
  1185. dev_err(hsotg->dev, "hibernation must be 0 or 1\n");
  1186. }
  1187. val = 0;
  1188. dev_dbg(hsotg->dev, "Setting hibernation to %d\n", val);
  1189. }
  1190. hsotg->core_params->hibernation = val;
  1191. }
  1192. /*
  1193. * This function is called during module intialization to pass module parameters
  1194. * for the DWC_otg core.
  1195. */
  1196. void dwc2_set_parameters(struct dwc2_hsotg *hsotg,
  1197. const struct dwc2_core_params *params)
  1198. {
  1199. dev_dbg(hsotg->dev, "%s()\n", __func__);
  1200. dwc2_set_param_otg_cap(hsotg, params->otg_cap);
  1201. dwc2_set_param_dma_enable(hsotg, params->dma_enable);
  1202. dwc2_set_param_dma_desc_enable(hsotg, params->dma_desc_enable);
  1203. dwc2_set_param_dma_desc_fs_enable(hsotg, params->dma_desc_fs_enable);
  1204. dwc2_set_param_host_support_fs_ls_low_power(hsotg,
  1205. params->host_support_fs_ls_low_power);
  1206. dwc2_set_param_enable_dynamic_fifo(hsotg,
  1207. params->enable_dynamic_fifo);
  1208. dwc2_set_param_host_rx_fifo_size(hsotg,
  1209. params->host_rx_fifo_size);
  1210. dwc2_set_param_host_nperio_tx_fifo_size(hsotg,
  1211. params->host_nperio_tx_fifo_size);
  1212. dwc2_set_param_host_perio_tx_fifo_size(hsotg,
  1213. params->host_perio_tx_fifo_size);
  1214. dwc2_set_param_max_transfer_size(hsotg,
  1215. params->max_transfer_size);
  1216. dwc2_set_param_max_packet_count(hsotg,
  1217. params->max_packet_count);
  1218. dwc2_set_param_host_channels(hsotg, params->host_channels);
  1219. dwc2_set_param_phy_type(hsotg, params->phy_type);
  1220. dwc2_set_param_speed(hsotg, params->speed);
  1221. dwc2_set_param_host_ls_low_power_phy_clk(hsotg,
  1222. params->host_ls_low_power_phy_clk);
  1223. dwc2_set_param_phy_ulpi_ddr(hsotg, params->phy_ulpi_ddr);
  1224. dwc2_set_param_phy_ulpi_ext_vbus(hsotg,
  1225. params->phy_ulpi_ext_vbus);
  1226. dwc2_set_param_phy_utmi_width(hsotg, params->phy_utmi_width);
  1227. dwc2_set_param_ulpi_fs_ls(hsotg, params->ulpi_fs_ls);
  1228. dwc2_set_param_ts_dline(hsotg, params->ts_dline);
  1229. dwc2_set_param_i2c_enable(hsotg, params->i2c_enable);
  1230. dwc2_set_param_en_multiple_tx_fifo(hsotg,
  1231. params->en_multiple_tx_fifo);
  1232. dwc2_set_param_reload_ctl(hsotg, params->reload_ctl);
  1233. dwc2_set_param_ahbcfg(hsotg, params->ahbcfg);
  1234. dwc2_set_param_otg_ver(hsotg, params->otg_ver);
  1235. dwc2_set_param_uframe_sched(hsotg, params->uframe_sched);
  1236. dwc2_set_param_external_id_pin_ctl(hsotg, params->external_id_pin_ctl);
  1237. dwc2_set_param_hibernation(hsotg, params->hibernation);
  1238. }
  1239. /*
  1240. * Forces either host or device mode if the controller is not
  1241. * currently in that mode.
  1242. *
  1243. * Returns true if the mode was forced.
  1244. */
  1245. static bool dwc2_force_mode_if_needed(struct dwc2_hsotg *hsotg, bool host)
  1246. {
  1247. if (host && dwc2_is_host_mode(hsotg))
  1248. return false;
  1249. else if (!host && dwc2_is_device_mode(hsotg))
  1250. return false;
  1251. return dwc2_force_mode(hsotg, host);
  1252. }
  1253. /*
  1254. * Gets host hardware parameters. Forces host mode if not currently in
  1255. * host mode. Should be called immediately after a core soft reset in
  1256. * order to get the reset values.
  1257. */
  1258. static void dwc2_get_host_hwparams(struct dwc2_hsotg *hsotg)
  1259. {
  1260. struct dwc2_hw_params *hw = &hsotg->hw_params;
  1261. u32 gnptxfsiz;
  1262. u32 hptxfsiz;
  1263. bool forced;
  1264. if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
  1265. return;
  1266. forced = dwc2_force_mode_if_needed(hsotg, true);
  1267. gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
  1268. hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
  1269. dev_dbg(hsotg->dev, "gnptxfsiz=%08x\n", gnptxfsiz);
  1270. dev_dbg(hsotg->dev, "hptxfsiz=%08x\n", hptxfsiz);
  1271. if (forced)
  1272. dwc2_clear_force_mode(hsotg);
  1273. hw->host_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
  1274. FIFOSIZE_DEPTH_SHIFT;
  1275. hw->host_perio_tx_fifo_size = (hptxfsiz & FIFOSIZE_DEPTH_MASK) >>
  1276. FIFOSIZE_DEPTH_SHIFT;
  1277. }
  1278. /*
  1279. * Gets device hardware parameters. Forces device mode if not
  1280. * currently in device mode. Should be called immediately after a core
  1281. * soft reset in order to get the reset values.
  1282. */
  1283. static void dwc2_get_dev_hwparams(struct dwc2_hsotg *hsotg)
  1284. {
  1285. struct dwc2_hw_params *hw = &hsotg->hw_params;
  1286. bool forced;
  1287. u32 gnptxfsiz;
  1288. if (hsotg->dr_mode == USB_DR_MODE_HOST)
  1289. return;
  1290. forced = dwc2_force_mode_if_needed(hsotg, false);
  1291. gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
  1292. dev_dbg(hsotg->dev, "gnptxfsiz=%08x\n", gnptxfsiz);
  1293. if (forced)
  1294. dwc2_clear_force_mode(hsotg);
  1295. hw->dev_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
  1296. FIFOSIZE_DEPTH_SHIFT;
  1297. }
  1298. /**
  1299. * During device initialization, read various hardware configuration
  1300. * registers and interpret the contents.
  1301. */
  1302. int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
  1303. {
  1304. struct dwc2_hw_params *hw = &hsotg->hw_params;
  1305. unsigned width;
  1306. u32 hwcfg1, hwcfg2, hwcfg3, hwcfg4;
  1307. u32 grxfsiz;
  1308. /*
  1309. * Attempt to ensure this device is really a DWC_otg Controller.
  1310. * Read and verify the GSNPSID register contents. The value should be
  1311. * 0x45f42xxx or 0x45f43xxx, which corresponds to either "OT2" or "OT3",
  1312. * as in "OTG version 2.xx" or "OTG version 3.xx".
  1313. */
  1314. hw->snpsid = dwc2_readl(hsotg->regs + GSNPSID);
  1315. if ((hw->snpsid & 0xfffff000) != 0x4f542000 &&
  1316. (hw->snpsid & 0xfffff000) != 0x4f543000) {
  1317. dev_err(hsotg->dev, "Bad value for GSNPSID: 0x%08x\n",
  1318. hw->snpsid);
  1319. return -ENODEV;
  1320. }
  1321. dev_dbg(hsotg->dev, "Core Release: %1x.%1x%1x%1x (snpsid=%x)\n",
  1322. hw->snpsid >> 12 & 0xf, hw->snpsid >> 8 & 0xf,
  1323. hw->snpsid >> 4 & 0xf, hw->snpsid & 0xf, hw->snpsid);
  1324. hwcfg1 = dwc2_readl(hsotg->regs + GHWCFG1);
  1325. hwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
  1326. hwcfg3 = dwc2_readl(hsotg->regs + GHWCFG3);
  1327. hwcfg4 = dwc2_readl(hsotg->regs + GHWCFG4);
  1328. grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
  1329. dev_dbg(hsotg->dev, "hwcfg1=%08x\n", hwcfg1);
  1330. dev_dbg(hsotg->dev, "hwcfg2=%08x\n", hwcfg2);
  1331. dev_dbg(hsotg->dev, "hwcfg3=%08x\n", hwcfg3);
  1332. dev_dbg(hsotg->dev, "hwcfg4=%08x\n", hwcfg4);
  1333. dev_dbg(hsotg->dev, "grxfsiz=%08x\n", grxfsiz);
  1334. /*
  1335. * Host specific hardware parameters. Reading these parameters
  1336. * requires the controller to be in host mode. The mode will
  1337. * be forced, if necessary, to read these values.
  1338. */
  1339. dwc2_get_host_hwparams(hsotg);
  1340. dwc2_get_dev_hwparams(hsotg);
  1341. /* hwcfg1 */
  1342. hw->dev_ep_dirs = hwcfg1;
  1343. /* hwcfg2 */
  1344. hw->op_mode = (hwcfg2 & GHWCFG2_OP_MODE_MASK) >>
  1345. GHWCFG2_OP_MODE_SHIFT;
  1346. hw->arch = (hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) >>
  1347. GHWCFG2_ARCHITECTURE_SHIFT;
  1348. hw->enable_dynamic_fifo = !!(hwcfg2 & GHWCFG2_DYNAMIC_FIFO);
  1349. hw->host_channels = 1 + ((hwcfg2 & GHWCFG2_NUM_HOST_CHAN_MASK) >>
  1350. GHWCFG2_NUM_HOST_CHAN_SHIFT);
  1351. hw->hs_phy_type = (hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK) >>
  1352. GHWCFG2_HS_PHY_TYPE_SHIFT;
  1353. hw->fs_phy_type = (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) >>
  1354. GHWCFG2_FS_PHY_TYPE_SHIFT;
  1355. hw->num_dev_ep = (hwcfg2 & GHWCFG2_NUM_DEV_EP_MASK) >>
  1356. GHWCFG2_NUM_DEV_EP_SHIFT;
  1357. hw->nperio_tx_q_depth =
  1358. (hwcfg2 & GHWCFG2_NONPERIO_TX_Q_DEPTH_MASK) >>
  1359. GHWCFG2_NONPERIO_TX_Q_DEPTH_SHIFT << 1;
  1360. hw->host_perio_tx_q_depth =
  1361. (hwcfg2 & GHWCFG2_HOST_PERIO_TX_Q_DEPTH_MASK) >>
  1362. GHWCFG2_HOST_PERIO_TX_Q_DEPTH_SHIFT << 1;
  1363. hw->dev_token_q_depth =
  1364. (hwcfg2 & GHWCFG2_DEV_TOKEN_Q_DEPTH_MASK) >>
  1365. GHWCFG2_DEV_TOKEN_Q_DEPTH_SHIFT;
  1366. /* hwcfg3 */
  1367. width = (hwcfg3 & GHWCFG3_XFER_SIZE_CNTR_WIDTH_MASK) >>
  1368. GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT;
  1369. hw->max_transfer_size = (1 << (width + 11)) - 1;
  1370. width = (hwcfg3 & GHWCFG3_PACKET_SIZE_CNTR_WIDTH_MASK) >>
  1371. GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT;
  1372. hw->max_packet_count = (1 << (width + 4)) - 1;
  1373. hw->i2c_enable = !!(hwcfg3 & GHWCFG3_I2C);
  1374. hw->total_fifo_size = (hwcfg3 & GHWCFG3_DFIFO_DEPTH_MASK) >>
  1375. GHWCFG3_DFIFO_DEPTH_SHIFT;
  1376. /* hwcfg4 */
  1377. hw->en_multiple_tx_fifo = !!(hwcfg4 & GHWCFG4_DED_FIFO_EN);
  1378. hw->num_dev_perio_in_ep = (hwcfg4 & GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK) >>
  1379. GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT;
  1380. hw->dma_desc_enable = !!(hwcfg4 & GHWCFG4_DESC_DMA);
  1381. hw->power_optimized = !!(hwcfg4 & GHWCFG4_POWER_OPTIMIZ);
  1382. hw->utmi_phy_data_width = (hwcfg4 & GHWCFG4_UTMI_PHY_DATA_WIDTH_MASK) >>
  1383. GHWCFG4_UTMI_PHY_DATA_WIDTH_SHIFT;
  1384. /* fifo sizes */
  1385. hw->host_rx_fifo_size = (grxfsiz & GRXFSIZ_DEPTH_MASK) >>
  1386. GRXFSIZ_DEPTH_SHIFT;
  1387. dev_dbg(hsotg->dev, "Detected values from hardware:\n");
  1388. dev_dbg(hsotg->dev, " op_mode=%d\n",
  1389. hw->op_mode);
  1390. dev_dbg(hsotg->dev, " arch=%d\n",
  1391. hw->arch);
  1392. dev_dbg(hsotg->dev, " dma_desc_enable=%d\n",
  1393. hw->dma_desc_enable);
  1394. dev_dbg(hsotg->dev, " power_optimized=%d\n",
  1395. hw->power_optimized);
  1396. dev_dbg(hsotg->dev, " i2c_enable=%d\n",
  1397. hw->i2c_enable);
  1398. dev_dbg(hsotg->dev, " hs_phy_type=%d\n",
  1399. hw->hs_phy_type);
  1400. dev_dbg(hsotg->dev, " fs_phy_type=%d\n",
  1401. hw->fs_phy_type);
  1402. dev_dbg(hsotg->dev, " utmi_phy_data_width=%d\n",
  1403. hw->utmi_phy_data_width);
  1404. dev_dbg(hsotg->dev, " num_dev_ep=%d\n",
  1405. hw->num_dev_ep);
  1406. dev_dbg(hsotg->dev, " num_dev_perio_in_ep=%d\n",
  1407. hw->num_dev_perio_in_ep);
  1408. dev_dbg(hsotg->dev, " host_channels=%d\n",
  1409. hw->host_channels);
  1410. dev_dbg(hsotg->dev, " max_transfer_size=%d\n",
  1411. hw->max_transfer_size);
  1412. dev_dbg(hsotg->dev, " max_packet_count=%d\n",
  1413. hw->max_packet_count);
  1414. dev_dbg(hsotg->dev, " nperio_tx_q_depth=0x%0x\n",
  1415. hw->nperio_tx_q_depth);
  1416. dev_dbg(hsotg->dev, " host_perio_tx_q_depth=0x%0x\n",
  1417. hw->host_perio_tx_q_depth);
  1418. dev_dbg(hsotg->dev, " dev_token_q_depth=0x%0x\n",
  1419. hw->dev_token_q_depth);
  1420. dev_dbg(hsotg->dev, " enable_dynamic_fifo=%d\n",
  1421. hw->enable_dynamic_fifo);
  1422. dev_dbg(hsotg->dev, " en_multiple_tx_fifo=%d\n",
  1423. hw->en_multiple_tx_fifo);
  1424. dev_dbg(hsotg->dev, " total_fifo_size=%d\n",
  1425. hw->total_fifo_size);
  1426. dev_dbg(hsotg->dev, " host_rx_fifo_size=%d\n",
  1427. hw->host_rx_fifo_size);
  1428. dev_dbg(hsotg->dev, " host_nperio_tx_fifo_size=%d\n",
  1429. hw->host_nperio_tx_fifo_size);
  1430. dev_dbg(hsotg->dev, " host_perio_tx_fifo_size=%d\n",
  1431. hw->host_perio_tx_fifo_size);
  1432. dev_dbg(hsotg->dev, "\n");
  1433. return 0;
  1434. }
  1435. /*
  1436. * Sets all parameters to the given value.
  1437. *
  1438. * Assumes that the dwc2_core_params struct contains only integers.
  1439. */
  1440. void dwc2_set_all_params(struct dwc2_core_params *params, int value)
  1441. {
  1442. int *p = (int *)params;
  1443. size_t size = sizeof(*params) / sizeof(*p);
  1444. int i;
  1445. for (i = 0; i < size; i++)
  1446. p[i] = value;
  1447. }
  1448. u16 dwc2_get_otg_version(struct dwc2_hsotg *hsotg)
  1449. {
  1450. return hsotg->core_params->otg_ver == 1 ? 0x0200 : 0x0103;
  1451. }
  1452. bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg)
  1453. {
  1454. if (dwc2_readl(hsotg->regs + GSNPSID) == 0xffffffff)
  1455. return false;
  1456. else
  1457. return true;
  1458. }
  1459. /**
  1460. * dwc2_enable_global_interrupts() - Enables the controller's Global
  1461. * Interrupt in the AHB Config register
  1462. *
  1463. * @hsotg: Programming view of DWC_otg controller
  1464. */
  1465. void dwc2_enable_global_interrupts(struct dwc2_hsotg *hsotg)
  1466. {
  1467. u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
  1468. ahbcfg |= GAHBCFG_GLBL_INTR_EN;
  1469. dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
  1470. }
  1471. /**
  1472. * dwc2_disable_global_interrupts() - Disables the controller's Global
  1473. * Interrupt in the AHB Config register
  1474. *
  1475. * @hsotg: Programming view of DWC_otg controller
  1476. */
  1477. void dwc2_disable_global_interrupts(struct dwc2_hsotg *hsotg)
  1478. {
  1479. u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
  1480. ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
  1481. dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
  1482. }
  1483. /* Returns the controller's GHWCFG2.OTG_MODE. */
  1484. unsigned dwc2_op_mode(struct dwc2_hsotg *hsotg)
  1485. {
  1486. u32 ghwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
  1487. return (ghwcfg2 & GHWCFG2_OP_MODE_MASK) >>
  1488. GHWCFG2_OP_MODE_SHIFT;
  1489. }
  1490. /* Returns true if the controller is capable of DRD. */
  1491. bool dwc2_hw_is_otg(struct dwc2_hsotg *hsotg)
  1492. {
  1493. unsigned op_mode = dwc2_op_mode(hsotg);
  1494. return (op_mode == GHWCFG2_OP_MODE_HNP_SRP_CAPABLE) ||
  1495. (op_mode == GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE) ||
  1496. (op_mode == GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE);
  1497. }
  1498. /* Returns true if the controller is host-only. */
  1499. bool dwc2_hw_is_host(struct dwc2_hsotg *hsotg)
  1500. {
  1501. unsigned op_mode = dwc2_op_mode(hsotg);
  1502. return (op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_HOST) ||
  1503. (op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST);
  1504. }
  1505. /* Returns true if the controller is device-only. */
  1506. bool dwc2_hw_is_device(struct dwc2_hsotg *hsotg)
  1507. {
  1508. unsigned op_mode = dwc2_op_mode(hsotg);
  1509. return (op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE) ||
  1510. (op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE);
  1511. }
  1512. MODULE_DESCRIPTION("DESIGNWARE HS OTG Core");
  1513. MODULE_AUTHOR("Synopsys, Inc.");
  1514. MODULE_LICENSE("Dual BSD/GPL");