dm365_lowlevel.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * SoC-specific lowlevel code for tms320dm365 and similar chips
  4. * Actually used for booting from NAND with nand_spl.
  5. *
  6. * Copyright (C) 2011
  7. * Heiko Schocher, DENX Software Engineering, hs@denx.de.
  8. */
  9. #include <common.h>
  10. #include <nand.h>
  11. #include <ns16550.h>
  12. #include <post.h>
  13. #include <asm/ti-common/davinci_nand.h>
  14. #include <asm/arch/dm365_lowlevel.h>
  15. #include <asm/arch/hardware.h>
  16. void dm365_waitloop(unsigned long loopcnt)
  17. {
  18. unsigned long i;
  19. for (i = 0; i < loopcnt; i++)
  20. asm(" NOP");
  21. }
  22. int dm365_pll1_init(unsigned long pllmult, unsigned long prediv)
  23. {
  24. unsigned int clksrc = 0x0;
  25. /* Power up the PLL */
  26. clrbits_le32(&dv_pll0_regs->pllctl, PLLCTL_PLLPWRDN);
  27. clrbits_le32(&dv_pll0_regs->pllctl, PLLCTL_RES_9);
  28. setbits_le32(&dv_pll0_regs->pllctl,
  29. clksrc << PLLCTL_CLOCK_MODE_SHIFT);
  30. /*
  31. * Set PLLENSRC '0', PLL Enable(PLLEN) selection is controlled
  32. * through MMR
  33. */
  34. clrbits_le32(&dv_pll0_regs->pllctl, PLLCTL_PLLENSRC);
  35. /* Set PLLEN=0 => PLL BYPASS MODE */
  36. clrbits_le32(&dv_pll0_regs->pllctl, PLLCTL_PLLEN);
  37. dm365_waitloop(150);
  38. /* PLLRST=1(reset assert) */
  39. setbits_le32(&dv_pll0_regs->pllctl, PLLCTL_PLLRST);
  40. dm365_waitloop(300);
  41. /*Bring PLL out of Reset*/
  42. clrbits_le32(&dv_pll0_regs->pllctl, PLLCTL_PLLRST);
  43. /* Program the Multiper and Pre-Divider for PLL1 */
  44. writel(pllmult, &dv_pll0_regs->pllm);
  45. writel(prediv, &dv_pll0_regs->prediv);
  46. /* Assert TENABLE = 1, TENABLEDIV = 1, TINITZ = 1 */
  47. writel(PLLSECCTL_STOPMODE | PLLSECCTL_TENABLEDIV | PLLSECCTL_TENABLE |
  48. PLLSECCTL_TINITZ, &dv_pll0_regs->secctl);
  49. /* Assert TENABLE = 1, TENABLEDIV = 1, TINITZ = 0 */
  50. writel(PLLSECCTL_STOPMODE | PLLSECCTL_TENABLEDIV | PLLSECCTL_TENABLE,
  51. &dv_pll0_regs->secctl);
  52. /* Assert TENABLE = 0, TENABLEDIV = 0, TINITZ = 0 */
  53. writel(PLLSECCTL_STOPMODE, &dv_pll0_regs->secctl);
  54. /* Assert TENABLE = 0, TENABLEDIV = 0, TINITZ = 1 */
  55. writel(PLLSECCTL_STOPMODE | PLLSECCTL_TINITZ, &dv_pll0_regs->secctl);
  56. /* Program the PostDiv for PLL1 */
  57. writel(PLL_POSTDEN, &dv_pll0_regs->postdiv);
  58. /* Post divider setting for PLL1 */
  59. writel(CONFIG_SYS_DM36x_PLL1_PLLDIV1, &dv_pll0_regs->plldiv1);
  60. writel(CONFIG_SYS_DM36x_PLL1_PLLDIV2, &dv_pll0_regs->plldiv2);
  61. writel(CONFIG_SYS_DM36x_PLL1_PLLDIV3, &dv_pll0_regs->plldiv3);
  62. writel(CONFIG_SYS_DM36x_PLL1_PLLDIV4, &dv_pll0_regs->plldiv4);
  63. writel(CONFIG_SYS_DM36x_PLL1_PLLDIV5, &dv_pll0_regs->plldiv5);
  64. writel(CONFIG_SYS_DM36x_PLL1_PLLDIV6, &dv_pll0_regs->plldiv6);
  65. writel(CONFIG_SYS_DM36x_PLL1_PLLDIV7, &dv_pll0_regs->plldiv7);
  66. writel(CONFIG_SYS_DM36x_PLL1_PLLDIV8, &dv_pll0_regs->plldiv8);
  67. writel(CONFIG_SYS_DM36x_PLL1_PLLDIV9, &dv_pll0_regs->plldiv9);
  68. dm365_waitloop(300);
  69. /* Set the GOSET bit */
  70. writel(PLLCMD_GOSET, &dv_pll0_regs->pllcmd); /* Go */
  71. dm365_waitloop(300);
  72. /* Wait for PLL to LOCK */
  73. while (!((readl(&dv_sys_module_regs->pll0_config) & PLL0_LOCK)
  74. == PLL0_LOCK))
  75. ;
  76. /* Enable the PLL Bit of PLLCTL*/
  77. setbits_le32(&dv_pll0_regs->pllctl, PLLCTL_PLLEN);
  78. return 0;
  79. }
  80. int dm365_pll2_init(unsigned long pllm, unsigned long prediv)
  81. {
  82. unsigned int clksrc = 0x0;
  83. /* Power up the PLL*/
  84. clrbits_le32(&dv_pll1_regs->pllctl, PLLCTL_PLLPWRDN);
  85. /*
  86. * Select the Clock Mode as Onchip Oscilator or External Clock on
  87. * MXI pin
  88. * VDB has input on MXI pin
  89. */
  90. clrbits_le32(&dv_pll1_regs->pllctl, PLLCTL_RES_9);
  91. setbits_le32(&dv_pll1_regs->pllctl,
  92. clksrc << PLLCTL_CLOCK_MODE_SHIFT);
  93. /*
  94. * Set PLLENSRC '0', PLL Enable(PLLEN) selection is controlled
  95. * through MMR
  96. */
  97. clrbits_le32(&dv_pll1_regs->pllctl, PLLCTL_PLLENSRC);
  98. /* Set PLLEN=0 => PLL BYPASS MODE */
  99. clrbits_le32(&dv_pll1_regs->pllctl, PLLCTL_PLLEN);
  100. dm365_waitloop(50);
  101. /* PLLRST=1(reset assert) */
  102. setbits_le32(&dv_pll1_regs->pllctl, PLLCTL_PLLRST);
  103. dm365_waitloop(300);
  104. /* Bring PLL out of Reset */
  105. clrbits_le32(&dv_pll1_regs->pllctl, PLLCTL_PLLRST);
  106. /* Program the Multiper and Pre-Divider for PLL2 */
  107. writel(pllm, &dv_pll1_regs->pllm);
  108. writel(prediv, &dv_pll1_regs->prediv);
  109. writel(PLL_POSTDEN, &dv_pll1_regs->postdiv);
  110. /* Assert TENABLE = 1, TENABLEDIV = 1, TINITZ = 1 */
  111. writel(PLLSECCTL_STOPMODE | PLLSECCTL_TENABLEDIV | PLLSECCTL_TENABLE |
  112. PLLSECCTL_TINITZ, &dv_pll1_regs->secctl);
  113. /* Assert TENABLE = 1, TENABLEDIV = 1, TINITZ = 0 */
  114. writel(PLLSECCTL_STOPMODE | PLLSECCTL_TENABLEDIV | PLLSECCTL_TENABLE,
  115. &dv_pll1_regs->secctl);
  116. /* Assert TENABLE = 0, TENABLEDIV = 0, TINITZ = 0 */
  117. writel(PLLSECCTL_STOPMODE, &dv_pll1_regs->secctl);
  118. /* Assert TENABLE = 0, TENABLEDIV = 0, TINITZ = 1 */
  119. writel(PLLSECCTL_STOPMODE | PLLSECCTL_TINITZ, &dv_pll1_regs->secctl);
  120. /* Post divider setting for PLL2 */
  121. writel(CONFIG_SYS_DM36x_PLL2_PLLDIV1, &dv_pll1_regs->plldiv1);
  122. writel(CONFIG_SYS_DM36x_PLL2_PLLDIV2, &dv_pll1_regs->plldiv2);
  123. writel(CONFIG_SYS_DM36x_PLL2_PLLDIV3, &dv_pll1_regs->plldiv3);
  124. writel(CONFIG_SYS_DM36x_PLL2_PLLDIV4, &dv_pll1_regs->plldiv4);
  125. writel(CONFIG_SYS_DM36x_PLL2_PLLDIV5, &dv_pll1_regs->plldiv5);
  126. /* GoCmd for PostDivider to take effect */
  127. writel(PLLCMD_GOSET, &dv_pll1_regs->pllcmd);
  128. dm365_waitloop(150);
  129. /* Wait for PLL to LOCK */
  130. while (!((readl(&dv_sys_module_regs->pll1_config) & PLL1_LOCK)
  131. == PLL1_LOCK))
  132. ;
  133. dm365_waitloop(4100);
  134. /* Enable the PLL2 */
  135. setbits_le32(&dv_pll1_regs->pllctl, PLLCTL_PLLEN);
  136. /* do this after PLL's have been set up */
  137. writel(CONFIG_SYS_DM36x_PERI_CLK_CTRL,
  138. &dv_sys_module_regs->peri_clkctl);
  139. return 0;
  140. }
  141. int dm365_ddr_setup(void)
  142. {
  143. lpsc_on(DAVINCI_LPSC_DDR_EMIF);
  144. clrbits_le32(&dv_sys_module_regs->vtpiocr,
  145. VPTIO_IOPWRDN | VPTIO_CLRZ | VPTIO_LOCK | VPTIO_PWRDN);
  146. /* Set bit CLRZ (bit 13) */
  147. setbits_le32(&dv_sys_module_regs->vtpiocr, VPTIO_CLRZ);
  148. /* Check VTP READY Status */
  149. while (!(readl(&dv_sys_module_regs->vtpiocr) & VPTIO_RDY))
  150. ;
  151. /* Set bit VTP_IOPWRDWN bit 14 for DDR input buffers) */
  152. setbits_le32(&dv_sys_module_regs->vtpiocr, VPTIO_IOPWRDN);
  153. /* Set bit LOCK(bit7) */
  154. setbits_le32(&dv_sys_module_regs->vtpiocr, VPTIO_LOCK);
  155. /*
  156. * Powerdown VTP as it is locked (bit 6)
  157. * Set bit VTP_IOPWRDWN bit 14 for DDR input buffers)
  158. */
  159. setbits_le32(&dv_sys_module_regs->vtpiocr,
  160. VPTIO_IOPWRDN | VPTIO_PWRDN);
  161. /* Wait for calibration to complete */
  162. dm365_waitloop(150);
  163. /* Set the DDR2 to synreset, then enable it again */
  164. lpsc_syncreset(DAVINCI_LPSC_DDR_EMIF);
  165. lpsc_on(DAVINCI_LPSC_DDR_EMIF);
  166. writel(CONFIG_SYS_DM36x_DDR2_DDRPHYCR, &dv_ddr2_regs_ctrl->ddrphycr);
  167. /* Program SDRAM Bank Config Register */
  168. writel((CONFIG_SYS_DM36x_DDR2_SDBCR | DV_DDR_BOOTUNLOCK),
  169. &dv_ddr2_regs_ctrl->sdbcr);
  170. writel((CONFIG_SYS_DM36x_DDR2_SDBCR | DV_DDR_TIMUNLOCK),
  171. &dv_ddr2_regs_ctrl->sdbcr);
  172. /* Program SDRAM Timing Control Register1 */
  173. writel(CONFIG_SYS_DM36x_DDR2_SDTIMR, &dv_ddr2_regs_ctrl->sdtimr);
  174. /* Program SDRAM Timing Control Register2 */
  175. writel(CONFIG_SYS_DM36x_DDR2_SDTIMR2, &dv_ddr2_regs_ctrl->sdtimr2);
  176. writel(CONFIG_SYS_DM36x_DDR2_PBBPR, &dv_ddr2_regs_ctrl->pbbpr);
  177. writel(CONFIG_SYS_DM36x_DDR2_SDBCR, &dv_ddr2_regs_ctrl->sdbcr);
  178. /* Program SDRAM Refresh Control Register */
  179. writel(CONFIG_SYS_DM36x_DDR2_SDRCR, &dv_ddr2_regs_ctrl->sdrcr);
  180. lpsc_syncreset(DAVINCI_LPSC_DDR_EMIF);
  181. lpsc_on(DAVINCI_LPSC_DDR_EMIF);
  182. return 0;
  183. }
  184. static void dm365_vpss_sync_reset(void)
  185. {
  186. unsigned int PdNum = 0;
  187. /* VPSS_CLKMD 1:1 */
  188. setbits_le32(&dv_sys_module_regs->vpss_clkctl,
  189. VPSS_CLK_CTL_VPSS_CLKMD);
  190. /* LPSC SyncReset DDR Clock Enable */
  191. writel(((readl(&dv_psc_regs->mdctl[DAVINCI_LPSC_VPSSMASTER]) &
  192. ~PSC_MD_STATE_MSK) | PSC_SYNCRESET),
  193. &dv_psc_regs->mdctl[DAVINCI_LPSC_VPSSMASTER]);
  194. writel((1 << PdNum), &dv_psc_regs->ptcmd);
  195. while (!(((readl(&dv_psc_regs->ptstat) >> PdNum) & PSC_GOSTAT) == 0))
  196. ;
  197. while (!((readl(&dv_psc_regs->mdstat[DAVINCI_LPSC_VPSSMASTER]) &
  198. PSC_MD_STATE_MSK) == PSC_SYNCRESET))
  199. ;
  200. }
  201. static void dm365_por_reset(void)
  202. {
  203. struct davinci_timer *wdog =
  204. (struct davinci_timer *)DAVINCI_WDOG_BASE;
  205. if (readl(&dv_pll0_regs->rstype) &
  206. (PLL_RSTYPE_POR | PLL_RSTYPE_XWRST)) {
  207. dm365_vpss_sync_reset();
  208. writel(DV_TMPBUF_VAL, TMPBUF);
  209. setbits_le32(TMPSTATUS, FLAG_PORRST);
  210. writel(DV_WDT_ENABLE_SYS_RESET, &wdog->na1);
  211. writel(DV_WDT_TRIGGER_SYS_RESET, &wdog->na2);
  212. while (1);
  213. }
  214. }
  215. static void dm365_wdt_reset(void)
  216. {
  217. struct davinci_timer *wdog =
  218. (struct davinci_timer *)DAVINCI_WDOG_BASE;
  219. if (readl(TMPBUF) != DV_TMPBUF_VAL) {
  220. writel(DV_TMPBUF_VAL, TMPBUF);
  221. setbits_le32(TMPSTATUS, FLAG_PORRST);
  222. setbits_le32(TMPSTATUS, FLAG_FLGOFF);
  223. dm365_waitloop(100);
  224. dm365_vpss_sync_reset();
  225. writel(DV_WDT_ENABLE_SYS_RESET, &wdog->na1);
  226. writel(DV_WDT_TRIGGER_SYS_RESET, &wdog->na2);
  227. while (1);
  228. }
  229. }
  230. static void dm365_wdt_flag_on(void)
  231. {
  232. /* VPSS_CLKMD 1:2 */
  233. clrbits_le32(&dv_sys_module_regs->vpss_clkctl,
  234. VPSS_CLK_CTL_VPSS_CLKMD);
  235. writel(0, TMPBUF);
  236. setbits_le32(TMPSTATUS, FLAG_FLGON);
  237. }
  238. void dm365_psc_init(void)
  239. {
  240. unsigned char i = 0;
  241. unsigned char lpsc_start;
  242. unsigned char lpsc_end, lpscgroup, lpscmin, lpscmax;
  243. unsigned int PdNum = 0;
  244. lpscmin = 0;
  245. lpscmax = 2;
  246. for (lpscgroup = lpscmin; lpscgroup <= lpscmax; lpscgroup++) {
  247. if (lpscgroup == 0) {
  248. /* Enabling LPSC 3 to 28 SCR first */
  249. lpsc_start = DAVINCI_LPSC_VPSSMSTR;
  250. lpsc_end = DAVINCI_LPSC_TIMER1;
  251. } else if (lpscgroup == 1) { /* Skip locked LPSCs [29-37] */
  252. lpsc_start = DAVINCI_LPSC_CFG5;
  253. lpsc_end = DAVINCI_LPSC_VPSSMASTER;
  254. } else {
  255. lpsc_start = DAVINCI_LPSC_MJCP;
  256. lpsc_end = DAVINCI_LPSC_HDVICP;
  257. }
  258. /* NEXT=0x3, Enable LPSC's */
  259. for (i = lpsc_start; i <= lpsc_end; i++)
  260. setbits_le32(&dv_psc_regs->mdctl[i], PSC_ENABLE);
  261. /*
  262. * Program goctl to start transition sequence for LPSCs
  263. * CSL_PSC_0_REGS->PTCMD = (1<<PdNum); Kick off Power
  264. * Domain 0 Modules
  265. */
  266. writel((1 << PdNum), &dv_psc_regs->ptcmd);
  267. /*
  268. * Wait for GOSTAT = NO TRANSITION from PSC for Powerdomain 0
  269. */
  270. while (!(((readl(&dv_psc_regs->ptstat) >> PdNum) & PSC_GOSTAT)
  271. == 0))
  272. ;
  273. /* Wait for MODSTAT = ENABLE from LPSC's */
  274. for (i = lpsc_start; i <= lpsc_end; i++)
  275. while (!((readl(&dv_psc_regs->mdstat[i]) &
  276. PSC_MD_STATE_MSK) == PSC_ENABLE))
  277. ;
  278. }
  279. }
  280. static void dm365_emif_init(void)
  281. {
  282. writel(CONFIG_SYS_DM36x_AWCCR, &davinci_emif_regs->awccr);
  283. writel(CONFIG_SYS_DM36x_AB1CR, &davinci_emif_regs->ab1cr);
  284. setbits_le32(&davinci_emif_regs->nandfcr, DAVINCI_NANDFCR_CS2NAND);
  285. writel(CONFIG_SYS_DM36x_AB2CR, &davinci_emif_regs->ab2cr);
  286. return;
  287. }
  288. void dm365_pinmux_ctl(unsigned long offset, unsigned long mask,
  289. unsigned long value)
  290. {
  291. clrbits_le32(&dv_sys_module_regs->pinmux[offset], mask);
  292. setbits_le32(&dv_sys_module_regs->pinmux[offset], (mask & value));
  293. }
  294. __attribute__((weak))
  295. void board_gpio_init(void)
  296. {
  297. return;
  298. }
  299. #if defined(CONFIG_POST)
  300. int post_log(char *format, ...)
  301. {
  302. return 0;
  303. }
  304. #endif
  305. void dm36x_lowlevel_init(ulong bootflag)
  306. {
  307. struct davinci_uart_ctrl_regs *davinci_uart_ctrl_regs =
  308. (struct davinci_uart_ctrl_regs *)(CONFIG_SYS_NS16550_COM1 +
  309. DAVINCI_UART_CTRL_BASE);
  310. /* Mask all interrupts */
  311. writel(DV_AINTC_INTCTL_IDMODE, &dv_aintc_regs->intctl);
  312. writel(0x0, &dv_aintc_regs->eabase);
  313. writel(0x0, &dv_aintc_regs->eint0);
  314. writel(0x0, &dv_aintc_regs->eint1);
  315. /* Clear all interrupts */
  316. writel(0xffffffff, &dv_aintc_regs->fiq0);
  317. writel(0xffffffff, &dv_aintc_regs->fiq1);
  318. writel(0xffffffff, &dv_aintc_regs->irq0);
  319. writel(0xffffffff, &dv_aintc_regs->irq1);
  320. dm365_por_reset();
  321. dm365_wdt_reset();
  322. /* System PSC setup - enable all */
  323. dm365_psc_init();
  324. /* Setup Pinmux */
  325. dm365_pinmux_ctl(0, 0xFFFFFFFF, CONFIG_SYS_DM36x_PINMUX0);
  326. dm365_pinmux_ctl(1, 0xFFFFFFFF, CONFIG_SYS_DM36x_PINMUX1);
  327. dm365_pinmux_ctl(2, 0xFFFFFFFF, CONFIG_SYS_DM36x_PINMUX2);
  328. dm365_pinmux_ctl(3, 0xFFFFFFFF, CONFIG_SYS_DM36x_PINMUX3);
  329. dm365_pinmux_ctl(4, 0xFFFFFFFF, CONFIG_SYS_DM36x_PINMUX4);
  330. /* PLL setup */
  331. dm365_pll1_init(CONFIG_SYS_DM36x_PLL1_PLLM,
  332. CONFIG_SYS_DM36x_PLL1_PREDIV);
  333. dm365_pll2_init(CONFIG_SYS_DM36x_PLL2_PLLM,
  334. CONFIG_SYS_DM36x_PLL2_PREDIV);
  335. /* GPIO setup */
  336. board_gpio_init();
  337. NS16550_init((NS16550_t)(CONFIG_SYS_NS16550_COM1),
  338. CONFIG_SYS_NS16550_CLK / 16 / CONFIG_BAUDRATE);
  339. /*
  340. * Fix Power and Emulation Management Register
  341. * see sprufh2.pdf page 38 Table 22
  342. */
  343. writel((DAVINCI_UART_PWREMU_MGMT_FREE | DAVINCI_UART_PWREMU_MGMT_URRST |
  344. DAVINCI_UART_PWREMU_MGMT_UTRST),
  345. &davinci_uart_ctrl_regs->pwremu_mgmt);
  346. puts("ddr init\n");
  347. dm365_ddr_setup();
  348. puts("emif init\n");
  349. dm365_emif_init();
  350. dm365_wdt_flag_on();
  351. #if defined(CONFIG_POST)
  352. /*
  353. * Do memory tests, calls arch_memory_failure_handle()
  354. * if error detected.
  355. */
  356. memory_post_test(0);
  357. #endif
  358. }