dm365_lowlevel.c 12 KB

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