dwc_otg_adp.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. /* ==========================================================================
  2. * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.c $
  3. * $Revision: #12 $
  4. * $Date: 2011/10/26 $
  5. * $Change: 1873028 $
  6. *
  7. * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  8. * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  9. * otherwise expressly agreed to in writing between Synopsys and you.
  10. *
  11. * The Software IS NOT an item of Licensed Software or Licensed Product under
  12. * any End User Software License Agreement or Agreement for Licensed Product
  13. * with Synopsys or any supplement thereto. You are permitted to use and
  14. * redistribute this Software in source and binary forms, with or without
  15. * modification, provided that redistributions of source code must retain this
  16. * notice. You may not view, use, disclose, copy or distribute this file or
  17. * any information contained herein except pursuant to this license grant from
  18. * Synopsys. If you do not agree with this notice, including the disclaimer
  19. * below, then you are not authorized to use the Software.
  20. *
  21. * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  25. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  31. * DAMAGE.
  32. * ========================================================================== */
  33. #include "dwc_os.h"
  34. #include "dwc_otg_regs.h"
  35. #include "dwc_otg_cil.h"
  36. #include "dwc_otg_adp.h"
  37. /** @file
  38. *
  39. * This file contains the most of the Attach Detect Protocol implementation for
  40. * the driver to support OTG Rev2.0.
  41. *
  42. */
  43. void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value)
  44. {
  45. adpctl_data_t adpctl;
  46. adpctl.d32 = value;
  47. adpctl.b.ar = 0x2;
  48. DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
  49. DWC_WMB();
  50. while (adpctl.b.ar) {
  51. adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
  52. DWC_UDELAY(10);
  53. DWC_RMB();
  54. }
  55. }
  56. /**
  57. * Function is called to read ADP registers
  58. */
  59. uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if)
  60. {
  61. adpctl_data_t adpctl;
  62. adpctl.d32 = 0;
  63. adpctl.b.ar = 0x1;
  64. DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
  65. while (adpctl.b.ar) {
  66. adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
  67. DWC_RMB();
  68. }
  69. return adpctl.d32;
  70. }
  71. /**
  72. * Function is called to read ADPCTL register and filter Write-clear bits
  73. */
  74. uint32_t dwc_otg_adp_read_reg_filter(dwc_otg_core_if_t * core_if)
  75. {
  76. adpctl_data_t adpctl;
  77. adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  78. adpctl.b.adp_tmout_int = 0;
  79. adpctl.b.adp_prb_int = 0;
  80. adpctl.b.adp_tmout_int = 0;
  81. return adpctl.d32;
  82. }
  83. /**
  84. * Function is called to write ADP registers
  85. */
  86. void dwc_otg_adp_modify_reg(dwc_otg_core_if_t * core_if, uint32_t clr,
  87. uint32_t set)
  88. {
  89. dwc_otg_adp_write_reg(core_if,
  90. (dwc_otg_adp_read_reg(core_if) & (~clr)) | set);
  91. }
  92. static void adp_sense_timeout(void *ptr)
  93. {
  94. dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  95. core_if->adp.sense_timer_started = 0;
  96. DWC_PRINTF("ADP SENSE TIMEOUT\n");
  97. if (core_if->adp_enable) {
  98. dwc_otg_adp_sense_stop(core_if);
  99. dwc_otg_adp_probe_start(core_if);
  100. }
  101. }
  102. /**
  103. * This function is called when the ADP vbus timer expires. Timeout is 1.1s.
  104. */
  105. static void adp_vbuson_timeout(void *ptr)
  106. {
  107. gpwrdn_data_t gpwrdn;
  108. dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  109. hprt0_data_t hprt0 = {.d32 = 0 };
  110. pcgcctl_data_t pcgcctl = {.d32 = 0 };
  111. DWC_PRINTF("%s: 1.1 seconds expire after turning on VBUS\n",__FUNCTION__);
  112. if (core_if) {
  113. core_if->adp.vbuson_timer_started = 0;
  114. /* Turn off vbus */
  115. hprt0.b.prtpwr = 1;
  116. DWC_MODIFY_REG32(core_if->host_if->hprt0, hprt0.d32, 0);
  117. gpwrdn.d32 = 0;
  118. /* Power off the core */
  119. if (core_if->power_down == 2) {
  120. /* Enable Wakeup Logic */
  121. // gpwrdn.b.wkupactiv = 1;
  122. gpwrdn.b.pmuactv = 0;
  123. gpwrdn.b.pwrdnrstn = 1;
  124. gpwrdn.b.pwrdnclmp = 1;
  125. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  126. gpwrdn.d32);
  127. /* Suspend the Phy Clock */
  128. pcgcctl.b.stoppclk = 1;
  129. DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  130. /* Switch on VDD */
  131. // gpwrdn.b.wkupactiv = 1;
  132. gpwrdn.b.pmuactv = 1;
  133. gpwrdn.b.pwrdnrstn = 1;
  134. gpwrdn.b.pwrdnclmp = 1;
  135. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  136. gpwrdn.d32);
  137. } else {
  138. /* Enable Power Down Logic */
  139. gpwrdn.b.pmuintsel = 1;
  140. gpwrdn.b.pmuactv = 1;
  141. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  142. }
  143. /* Power off the core */
  144. if (core_if->power_down == 2) {
  145. gpwrdn.d32 = 0;
  146. gpwrdn.b.pwrdnswtch = 1;
  147. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn,
  148. gpwrdn.d32, 0);
  149. }
  150. /* Unmask SRP detected interrupt from Power Down Logic */
  151. gpwrdn.d32 = 0;
  152. gpwrdn.b.srp_det_msk = 1;
  153. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  154. dwc_otg_adp_probe_start(core_if);
  155. dwc_otg_dump_global_registers(core_if);
  156. dwc_otg_dump_host_registers(core_if);
  157. }
  158. }
  159. /**
  160. * Start the ADP Initial Probe timer to detect if Port Connected interrupt is
  161. * not asserted within 1.1 seconds.
  162. *
  163. * @param core_if the pointer to core_if strucure.
  164. */
  165. void dwc_otg_adp_vbuson_timer_start(dwc_otg_core_if_t * core_if)
  166. {
  167. core_if->adp.vbuson_timer_started = 1;
  168. if (core_if->adp.vbuson_timer)
  169. {
  170. DWC_PRINTF("SCHEDULING VBUSON TIMER\n");
  171. /* 1.1 secs + 60ms necessary for cil_hcd_start*/
  172. DWC_TIMER_SCHEDULE(core_if->adp.vbuson_timer, 1160);
  173. } else {
  174. DWC_WARN("VBUSON_TIMER = %p\n",core_if->adp.vbuson_timer);
  175. }
  176. }
  177. #if 0
  178. /**
  179. * Masks all DWC OTG core interrupts
  180. *
  181. */
  182. static void mask_all_interrupts(dwc_otg_core_if_t * core_if)
  183. {
  184. int i;
  185. gahbcfg_data_t ahbcfg = {.d32 = 0 };
  186. /* Mask Host Interrupts */
  187. /* Clear and disable HCINTs */
  188. for (i = 0; i < core_if->core_params->host_channels; i++) {
  189. DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk, 0);
  190. DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcint, 0xFFFFFFFF);
  191. }
  192. /* Clear and disable HAINT */
  193. DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk, 0x0000);
  194. DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haint, 0xFFFFFFFF);
  195. /* Mask Device Interrupts */
  196. if (!core_if->multiproc_int_enable) {
  197. /* Clear and disable IN Endpoint interrupts */
  198. DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, 0);
  199. for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  200. DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
  201. diepint, 0xFFFFFFFF);
  202. }
  203. /* Clear and disable OUT Endpoint interrupts */
  204. DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, 0);
  205. for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  206. DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
  207. doepint, 0xFFFFFFFF);
  208. }
  209. /* Clear and disable DAINT */
  210. DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daint,
  211. 0xFFFFFFFF);
  212. DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, 0);
  213. } else {
  214. for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  215. DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  216. diepeachintmsk[i], 0);
  217. DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
  218. diepint, 0xFFFFFFFF);
  219. }
  220. for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  221. DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  222. doepeachintmsk[i], 0);
  223. DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
  224. doepint, 0xFFFFFFFF);
  225. }
  226. DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
  227. 0);
  228. DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachint,
  229. 0xFFFFFFFF);
  230. }
  231. /* Disable interrupts */
  232. ahbcfg.b.glblintrmsk = 1;
  233. DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
  234. /* Disable all interrupts. */
  235. DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
  236. /* Clear any pending interrupts */
  237. DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  238. /* Clear any pending OTG Interrupts */
  239. DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, 0xFFFFFFFF);
  240. }
  241. /**
  242. * Unmask Port Connection Detected interrupt
  243. *
  244. */
  245. static void unmask_conn_det_intr(dwc_otg_core_if_t * core_if)
  246. {
  247. gintmsk_data_t gintmsk = {.d32 = 0,.b.portintr = 1 };
  248. DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
  249. }
  250. #endif
  251. /**
  252. * Starts the ADP Probing
  253. *
  254. * @param core_if the pointer to core_if structure.
  255. */
  256. uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if)
  257. {
  258. adpctl_data_t adpctl = {.d32 = 0};
  259. gpwrdn_data_t gpwrdn;
  260. #if 0
  261. adpctl_data_t adpctl_int = {.d32 = 0, .b.adp_prb_int = 1,
  262. .b.adp_sns_int = 1, b.adp_tmout_int};
  263. #endif
  264. dwc_otg_disable_global_interrupts(core_if);
  265. DWC_PRINTF("ADP Probe Start\n");
  266. core_if->adp.probe_enabled = 1;
  267. adpctl.b.adpres = 1;
  268. dwc_otg_adp_write_reg(core_if, adpctl.d32);
  269. while (adpctl.b.adpres) {
  270. adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  271. DWC_RMB();
  272. }
  273. adpctl.d32 = 0;
  274. gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  275. /* In Host mode unmask SRP detected interrupt */
  276. gpwrdn.d32 = 0;
  277. gpwrdn.b.sts_chngint_msk = 1;
  278. if (!gpwrdn.b.idsts) {
  279. gpwrdn.b.srp_det_msk = 1;
  280. }
  281. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  282. adpctl.b.adp_tmout_int_msk = 1;
  283. adpctl.b.adp_prb_int_msk = 1;
  284. adpctl.b.prb_dschg = 1;
  285. adpctl.b.prb_delta = 1;
  286. adpctl.b.prb_per = 1;
  287. adpctl.b.adpen = 1;
  288. adpctl.b.enaprb = 1;
  289. dwc_otg_adp_write_reg(core_if, adpctl.d32);
  290. DWC_PRINTF("ADP Probe Finish\n");
  291. return 0;
  292. }
  293. /**
  294. * Starts the ADP Sense timer to detect if ADP Sense interrupt is not asserted
  295. * within 3 seconds.
  296. *
  297. * @param core_if the pointer to core_if strucure.
  298. */
  299. void dwc_otg_adp_sense_timer_start(dwc_otg_core_if_t * core_if)
  300. {
  301. core_if->adp.sense_timer_started = 1;
  302. DWC_TIMER_SCHEDULE(core_if->adp.sense_timer, 3000 /* 3 secs */ );
  303. }
  304. /**
  305. * Starts the ADP Sense
  306. *
  307. * @param core_if the pointer to core_if strucure.
  308. */
  309. uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if)
  310. {
  311. adpctl_data_t adpctl;
  312. DWC_PRINTF("ADP Sense Start\n");
  313. /* Unmask ADP sense interrupt and mask all other from the core */
  314. adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  315. adpctl.b.adp_sns_int_msk = 1;
  316. dwc_otg_adp_write_reg(core_if, adpctl.d32);
  317. dwc_otg_disable_global_interrupts(core_if); // vahrama
  318. /* Set ADP reset bit*/
  319. adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  320. adpctl.b.adpres = 1;
  321. dwc_otg_adp_write_reg(core_if, adpctl.d32);
  322. while (adpctl.b.adpres) {
  323. adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  324. }
  325. adpctl.b.adpres = 0;
  326. adpctl.b.adpen = 1;
  327. adpctl.b.enasns = 1;
  328. dwc_otg_adp_write_reg(core_if, adpctl.d32);
  329. dwc_otg_adp_sense_timer_start(core_if);
  330. return 0;
  331. }
  332. /**
  333. * Stops the ADP Probing
  334. *
  335. * @param core_if the pointer to core_if strucure.
  336. */
  337. uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if)
  338. {
  339. adpctl_data_t adpctl;
  340. DWC_PRINTF("Stop ADP probe\n");
  341. core_if->adp.probe_enabled = 0;
  342. core_if->adp.probe_counter = 0;
  343. adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  344. adpctl.b.adpen = 0;
  345. adpctl.b.adp_prb_int = 1;
  346. adpctl.b.adp_tmout_int = 1;
  347. adpctl.b.adp_sns_int = 1;
  348. dwc_otg_adp_write_reg(core_if, adpctl.d32);
  349. return 0;
  350. }
  351. /**
  352. * Stops the ADP Sensing
  353. *
  354. * @param core_if the pointer to core_if strucure.
  355. */
  356. uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if)
  357. {
  358. adpctl_data_t adpctl;
  359. core_if->adp.sense_enabled = 0;
  360. adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  361. adpctl.b.enasns = 0;
  362. adpctl.b.adp_sns_int = 1;
  363. dwc_otg_adp_write_reg(core_if, adpctl.d32);
  364. return 0;
  365. }
  366. /**
  367. * Called to turn on the VBUS after initial ADP probe in host mode.
  368. * If port power was already enabled in cil_hcd_start function then
  369. * only schedule a timer.
  370. *
  371. * @param core_if the pointer to core_if structure.
  372. */
  373. void dwc_otg_adp_turnon_vbus(dwc_otg_core_if_t * core_if)
  374. {
  375. hprt0_data_t hprt0 = {.d32 = 0 };
  376. hprt0.d32 = dwc_otg_read_hprt0(core_if);
  377. DWC_PRINTF("Turn on VBUS for 1.1s, port power is %d\n", hprt0.b.prtpwr);
  378. if (hprt0.b.prtpwr == 0) {
  379. hprt0.b.prtpwr = 1;
  380. //DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  381. }
  382. dwc_otg_adp_vbuson_timer_start(core_if);
  383. }
  384. /**
  385. * Called right after driver is loaded
  386. * to perform initial actions for ADP
  387. *
  388. * @param core_if the pointer to core_if structure.
  389. * @param is_host - flag for current mode of operation either from GINTSTS or GPWRDN
  390. */
  391. void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host)
  392. {
  393. gpwrdn_data_t gpwrdn;
  394. DWC_PRINTF("ADP Initial Start\n");
  395. core_if->adp.adp_started = 1;
  396. DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  397. dwc_otg_disable_global_interrupts(core_if);
  398. if (is_host) {
  399. DWC_PRINTF("HOST MODE\n");
  400. /* Enable Power Down Logic Interrupt*/
  401. gpwrdn.d32 = 0;
  402. gpwrdn.b.pmuintsel = 1;
  403. gpwrdn.b.pmuactv = 1;
  404. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  405. /* Initialize first ADP probe to obtain Ramp Time value */
  406. core_if->adp.initial_probe = 1;
  407. dwc_otg_adp_probe_start(core_if);
  408. } else {
  409. gotgctl_data_t gotgctl;
  410. gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  411. DWC_PRINTF("DEVICE MODE\n");
  412. if (gotgctl.b.bsesvld == 0) {
  413. /* Enable Power Down Logic Interrupt*/
  414. gpwrdn.d32 = 0;
  415. DWC_PRINTF("VBUS is not valid - start ADP probe\n");
  416. gpwrdn.b.pmuintsel = 1;
  417. gpwrdn.b.pmuactv = 1;
  418. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  419. core_if->adp.initial_probe = 1;
  420. dwc_otg_adp_probe_start(core_if);
  421. } else {
  422. DWC_PRINTF("VBUS is valid - initialize core as a Device\n");
  423. core_if->op_state = B_PERIPHERAL;
  424. dwc_otg_core_init(core_if);
  425. dwc_otg_enable_global_interrupts(core_if);
  426. cil_pcd_start(core_if);
  427. dwc_otg_dump_global_registers(core_if);
  428. dwc_otg_dump_dev_registers(core_if);
  429. }
  430. }
  431. }
  432. void dwc_otg_adp_init(dwc_otg_core_if_t * core_if)
  433. {
  434. core_if->adp.adp_started = 0;
  435. core_if->adp.initial_probe = 0;
  436. core_if->adp.probe_timer_values[0] = -1;
  437. core_if->adp.probe_timer_values[1] = -1;
  438. core_if->adp.probe_enabled = 0;
  439. core_if->adp.sense_enabled = 0;
  440. core_if->adp.sense_timer_started = 0;
  441. core_if->adp.vbuson_timer_started = 0;
  442. core_if->adp.probe_counter = 0;
  443. core_if->adp.gpwrdn = 0;
  444. core_if->adp.attached = DWC_OTG_ADP_UNKOWN;
  445. /* Initialize timers */
  446. core_if->adp.sense_timer =
  447. DWC_TIMER_ALLOC("ADP SENSE TIMER", adp_sense_timeout, core_if);
  448. core_if->adp.vbuson_timer =
  449. DWC_TIMER_ALLOC("ADP VBUS ON TIMER", adp_vbuson_timeout, core_if);
  450. if (!core_if->adp.sense_timer || !core_if->adp.vbuson_timer)
  451. {
  452. DWC_ERROR("Could not allocate memory for ADP timers\n");
  453. }
  454. }
  455. void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if)
  456. {
  457. gpwrdn_data_t gpwrdn = { .d32 = 0 };
  458. gpwrdn.b.pmuintsel = 1;
  459. gpwrdn.b.pmuactv = 1;
  460. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  461. if (core_if->adp.probe_enabled)
  462. dwc_otg_adp_probe_stop(core_if);
  463. if (core_if->adp.sense_enabled)
  464. dwc_otg_adp_sense_stop(core_if);
  465. if (core_if->adp.sense_timer_started)
  466. DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  467. if (core_if->adp.vbuson_timer_started)
  468. DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
  469. DWC_TIMER_FREE(core_if->adp.sense_timer);
  470. DWC_TIMER_FREE(core_if->adp.vbuson_timer);
  471. }
  472. /////////////////////////////////////////////////////////////////////
  473. ////////////// ADP Interrupt Handlers ///////////////////////////////
  474. /////////////////////////////////////////////////////////////////////
  475. /**
  476. * This function sets Ramp Timer values
  477. */
  478. static uint32_t set_timer_value(dwc_otg_core_if_t * core_if, uint32_t val)
  479. {
  480. if (core_if->adp.probe_timer_values[0] == -1) {
  481. core_if->adp.probe_timer_values[0] = val;
  482. core_if->adp.probe_timer_values[1] = -1;
  483. return 1;
  484. } else {
  485. core_if->adp.probe_timer_values[1] =
  486. core_if->adp.probe_timer_values[0];
  487. core_if->adp.probe_timer_values[0] = val;
  488. return 0;
  489. }
  490. }
  491. /**
  492. * This function compares Ramp Timer values
  493. */
  494. static uint32_t compare_timer_values(dwc_otg_core_if_t * core_if)
  495. {
  496. uint32_t diff;
  497. if (core_if->adp.probe_timer_values[0]>=core_if->adp.probe_timer_values[1])
  498. diff = core_if->adp.probe_timer_values[0]-core_if->adp.probe_timer_values[1];
  499. else
  500. diff = core_if->adp.probe_timer_values[1]-core_if->adp.probe_timer_values[0];
  501. if(diff < 2) {
  502. return 0;
  503. } else {
  504. return 1;
  505. }
  506. }
  507. /**
  508. * This function handles ADP Probe Interrupts
  509. */
  510. static int32_t dwc_otg_adp_handle_prb_intr(dwc_otg_core_if_t * core_if,
  511. uint32_t val)
  512. {
  513. adpctl_data_t adpctl = {.d32 = 0 };
  514. gpwrdn_data_t gpwrdn, temp;
  515. adpctl.d32 = val;
  516. temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  517. core_if->adp.probe_counter++;
  518. core_if->adp.gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  519. if (adpctl.b.rtim == 0 && !temp.b.idsts){
  520. DWC_PRINTF("RTIM value is 0\n");
  521. goto exit;
  522. }
  523. if (set_timer_value(core_if, adpctl.b.rtim) &&
  524. core_if->adp.initial_probe) {
  525. core_if->adp.initial_probe = 0;
  526. dwc_otg_adp_probe_stop(core_if);
  527. gpwrdn.d32 = 0;
  528. gpwrdn.b.pmuactv = 1;
  529. gpwrdn.b.pmuintsel = 1;
  530. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  531. DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  532. /* check which value is for device mode and which for Host mode */
  533. if (!temp.b.idsts) { /* considered host mode value is 0 */
  534. /*
  535. * Turn on VBUS after initial ADP probe.
  536. */
  537. core_if->op_state = A_HOST;
  538. dwc_otg_enable_global_interrupts(core_if);
  539. DWC_SPINUNLOCK(core_if->lock);
  540. cil_hcd_start(core_if);
  541. dwc_otg_adp_turnon_vbus(core_if);
  542. DWC_SPINLOCK(core_if->lock);
  543. } else {
  544. /*
  545. * Initiate SRP after initial ADP probe.
  546. */
  547. dwc_otg_enable_global_interrupts(core_if);
  548. dwc_otg_initiate_srp(core_if);
  549. }
  550. } else if (core_if->adp.probe_counter > 2){
  551. gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  552. if (compare_timer_values(core_if)) {
  553. DWC_PRINTF("Difference in timer values !!! \n");
  554. // core_if->adp.attached = DWC_OTG_ADP_ATTACHED;
  555. dwc_otg_adp_probe_stop(core_if);
  556. /* Power on the core */
  557. if (core_if->power_down == 2) {
  558. gpwrdn.b.pwrdnswtch = 1;
  559. DWC_MODIFY_REG32(&core_if->core_global_regs->
  560. gpwrdn, 0, gpwrdn.d32);
  561. }
  562. /* check which value is for device mode and which for Host mode */
  563. if (!temp.b.idsts) { /* considered host mode value is 0 */
  564. /* Disable Interrupt from Power Down Logic */
  565. gpwrdn.d32 = 0;
  566. gpwrdn.b.pmuintsel = 1;
  567. gpwrdn.b.pmuactv = 1;
  568. DWC_MODIFY_REG32(&core_if->core_global_regs->
  569. gpwrdn, gpwrdn.d32, 0);
  570. /*
  571. * Initialize the Core for Host mode.
  572. */
  573. core_if->op_state = A_HOST;
  574. dwc_otg_core_init(core_if);
  575. dwc_otg_enable_global_interrupts(core_if);
  576. cil_hcd_start(core_if);
  577. } else {
  578. gotgctl_data_t gotgctl;
  579. /* Mask SRP detected interrupt from Power Down Logic */
  580. gpwrdn.d32 = 0;
  581. gpwrdn.b.srp_det_msk = 1;
  582. DWC_MODIFY_REG32(&core_if->core_global_regs->
  583. gpwrdn, gpwrdn.d32, 0);
  584. /* Disable Power Down Logic */
  585. gpwrdn.d32 = 0;
  586. gpwrdn.b.pmuintsel = 1;
  587. gpwrdn.b.pmuactv = 1;
  588. DWC_MODIFY_REG32(&core_if->core_global_regs->
  589. gpwrdn, gpwrdn.d32, 0);
  590. /*
  591. * Initialize the Core for Device mode.
  592. */
  593. core_if->op_state = B_PERIPHERAL;
  594. dwc_otg_core_init(core_if);
  595. dwc_otg_enable_global_interrupts(core_if);
  596. cil_pcd_start(core_if);
  597. gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  598. if (!gotgctl.b.bsesvld) {
  599. dwc_otg_initiate_srp(core_if);
  600. }
  601. }
  602. }
  603. if (core_if->power_down == 2) {
  604. if (gpwrdn.b.bsessvld) {
  605. /* Mask SRP detected interrupt from Power Down Logic */
  606. gpwrdn.d32 = 0;
  607. gpwrdn.b.srp_det_msk = 1;
  608. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  609. /* Disable Power Down Logic */
  610. gpwrdn.d32 = 0;
  611. gpwrdn.b.pmuactv = 1;
  612. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  613. /*
  614. * Initialize the Core for Device mode.
  615. */
  616. core_if->op_state = B_PERIPHERAL;
  617. dwc_otg_core_init(core_if);
  618. dwc_otg_enable_global_interrupts(core_if);
  619. cil_pcd_start(core_if);
  620. }
  621. }
  622. }
  623. exit:
  624. /* Clear interrupt */
  625. adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  626. adpctl.b.adp_prb_int = 1;
  627. dwc_otg_adp_write_reg(core_if, adpctl.d32);
  628. return 0;
  629. }
  630. /**
  631. * This function hadles ADP Sense Interrupt
  632. */
  633. static int32_t dwc_otg_adp_handle_sns_intr(dwc_otg_core_if_t * core_if)
  634. {
  635. adpctl_data_t adpctl;
  636. /* Stop ADP Sense timer */
  637. DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  638. /* Restart ADP Sense timer */
  639. dwc_otg_adp_sense_timer_start(core_if);
  640. /* Clear interrupt */
  641. adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  642. adpctl.b.adp_sns_int = 1;
  643. dwc_otg_adp_write_reg(core_if, adpctl.d32);
  644. return 0;
  645. }
  646. /**
  647. * This function handles ADP Probe Interrupts
  648. */
  649. static int32_t dwc_otg_adp_handle_prb_tmout_intr(dwc_otg_core_if_t * core_if,
  650. uint32_t val)
  651. {
  652. adpctl_data_t adpctl = {.d32 = 0 };
  653. adpctl.d32 = val;
  654. set_timer_value(core_if, adpctl.b.rtim);
  655. /* Clear interrupt */
  656. adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  657. adpctl.b.adp_tmout_int = 1;
  658. dwc_otg_adp_write_reg(core_if, adpctl.d32);
  659. return 0;
  660. }
  661. /**
  662. * ADP Interrupt handler.
  663. *
  664. */
  665. int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if)
  666. {
  667. int retval = 0;
  668. adpctl_data_t adpctl = {.d32 = 0};
  669. adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  670. DWC_PRINTF("ADPCTL = %08x\n",adpctl.d32);
  671. if (adpctl.b.adp_sns_int & adpctl.b.adp_sns_int_msk) {
  672. DWC_PRINTF("ADP Sense interrupt\n");
  673. retval |= dwc_otg_adp_handle_sns_intr(core_if);
  674. }
  675. if (adpctl.b.adp_tmout_int & adpctl.b.adp_tmout_int_msk) {
  676. DWC_PRINTF("ADP timeout interrupt\n");
  677. retval |= dwc_otg_adp_handle_prb_tmout_intr(core_if, adpctl.d32);
  678. }
  679. if (adpctl.b.adp_prb_int & adpctl.b.adp_prb_int_msk) {
  680. DWC_PRINTF("ADP Probe interrupt\n");
  681. adpctl.b.adp_prb_int = 1;
  682. retval |= dwc_otg_adp_handle_prb_intr(core_if, adpctl.d32);
  683. }
  684. // dwc_otg_adp_modify_reg(core_if, adpctl.d32, 0);
  685. //dwc_otg_adp_write_reg(core_if, adpctl.d32);
  686. DWC_PRINTF("RETURN FROM ADP ISR\n");
  687. return retval;
  688. }
  689. /**
  690. *
  691. * @param core_if Programming view of DWC_otg controller.
  692. */
  693. int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if)
  694. {
  695. #ifndef DWC_HOST_ONLY
  696. hprt0_data_t hprt0;
  697. gpwrdn_data_t gpwrdn;
  698. DWC_DEBUGPL(DBG_ANY, "++ Power Down Logic Session Request Interrupt++\n");
  699. gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  700. /* check which value is for device mode and which for Host mode */
  701. if (!gpwrdn.b.idsts) { /* considered host mode value is 0 */
  702. DWC_PRINTF("SRP: Host mode\n");
  703. if (core_if->adp_enable) {
  704. dwc_otg_adp_probe_stop(core_if);
  705. /* Power on the core */
  706. if (core_if->power_down == 2) {
  707. gpwrdn.b.pwrdnswtch = 1;
  708. DWC_MODIFY_REG32(&core_if->core_global_regs->
  709. gpwrdn, 0, gpwrdn.d32);
  710. }
  711. core_if->op_state = A_HOST;
  712. dwc_otg_core_init(core_if);
  713. dwc_otg_enable_global_interrupts(core_if);
  714. cil_hcd_start(core_if);
  715. }
  716. /* Turn on the port power bit. */
  717. hprt0.d32 = dwc_otg_read_hprt0(core_if);
  718. hprt0.b.prtpwr = 1;
  719. DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  720. /* Start the Connection timer. So a message can be displayed
  721. * if connect does not occur within 10 seconds. */
  722. cil_hcd_session_start(core_if);
  723. } else {
  724. DWC_PRINTF("SRP: Device mode %s\n", __FUNCTION__);
  725. if (core_if->adp_enable) {
  726. dwc_otg_adp_probe_stop(core_if);
  727. /* Power on the core */
  728. if (core_if->power_down == 2) {
  729. gpwrdn.b.pwrdnswtch = 1;
  730. DWC_MODIFY_REG32(&core_if->core_global_regs->
  731. gpwrdn, 0, gpwrdn.d32);
  732. }
  733. gpwrdn.d32 = 0;
  734. gpwrdn.b.pmuactv = 0;
  735. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  736. gpwrdn.d32);
  737. core_if->op_state = B_PERIPHERAL;
  738. dwc_otg_core_init(core_if);
  739. dwc_otg_enable_global_interrupts(core_if);
  740. cil_pcd_start(core_if);
  741. }
  742. }
  743. #endif
  744. return 1;
  745. }