dm365_lowlevel.c 13 KB

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