core.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  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");