core.c 45 KB

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