dm365_lowlevel.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. 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. void dm365_por_reset(void)
  217. {
  218. if (readl(&dv_pll0_regs->rstype) &
  219. (PLL_RSTYPE_POR | PLL_RSTYPE_XWRST))
  220. dm365_vpss_sync_reset();
  221. }
  222. void dm365_psc_init(void)
  223. {
  224. unsigned char i = 0;
  225. unsigned char lpsc_start;
  226. unsigned char lpsc_end, lpscgroup, lpscmin, lpscmax;
  227. unsigned int PdNum = 0;
  228. lpscmin = 0;
  229. lpscmax = 2;
  230. for (lpscgroup = lpscmin; lpscgroup <= lpscmax; lpscgroup++) {
  231. if (lpscgroup == 0) {
  232. /* Enabling LPSC 3 to 28 SCR first */
  233. lpsc_start = DAVINCI_LPSC_VPSSMSTR;
  234. lpsc_end = DAVINCI_LPSC_TIMER1;
  235. } else if (lpscgroup == 1) { /* Skip locked LPSCs [29-37] */
  236. lpsc_start = DAVINCI_LPSC_CFG5;
  237. lpsc_end = DAVINCI_LPSC_VPSSMASTER;
  238. } else {
  239. lpsc_start = DAVINCI_LPSC_MJCP;
  240. lpsc_end = DAVINCI_LPSC_HDVICP;
  241. }
  242. /* NEXT=0x3, Enable LPSC's */
  243. for (i = lpsc_start; i <= lpsc_end; i++)
  244. setbits_le32(&dv_psc_regs->mdctl[i], PSC_ENABLE);
  245. /*
  246. * Program goctl to start transition sequence for LPSCs
  247. * CSL_PSC_0_REGS->PTCMD = (1<<PdNum); Kick off Power
  248. * Domain 0 Modules
  249. */
  250. writel((1 << PdNum), &dv_psc_regs->ptcmd);
  251. /*
  252. * Wait for GOSTAT = NO TRANSITION from PSC for Powerdomain 0
  253. */
  254. while (!(((readl(&dv_psc_regs->ptstat) >> PdNum) & PSC_GOSTAT)
  255. == 0))
  256. ;
  257. /* Wait for MODSTAT = ENABLE from LPSC's */
  258. for (i = lpsc_start; i <= lpsc_end; i++)
  259. while (!((readl(&dv_psc_regs->mdstat[i]) &
  260. PSC_MD_STATE_MSK) == PSC_ENABLE))
  261. ;
  262. }
  263. }
  264. static void dm365_emif_init(void)
  265. {
  266. writel(CONFIG_SYS_DM36x_AWCCR, &davinci_emif_regs->awccr);
  267. writel(CONFIG_SYS_DM36x_AB1CR, &davinci_emif_regs->ab1cr);
  268. setbits_le32(&davinci_emif_regs->nandfcr, DAVINCI_NANDFCR_CS2NAND);
  269. writel(CONFIG_SYS_DM36x_AB2CR, &davinci_emif_regs->ab2cr);
  270. return;
  271. }
  272. void dm365_pinmux_ctl(unsigned long offset, unsigned long mask,
  273. unsigned long value)
  274. {
  275. clrbits_le32(&dv_sys_module_regs->pinmux[offset], mask);
  276. setbits_le32(&dv_sys_module_regs->pinmux[offset], (mask & value));
  277. }
  278. __attribute__((weak))
  279. void board_gpio_init(void)
  280. {
  281. return;
  282. }
  283. #if defined(CONFIG_POST)
  284. int post_log(char *format, ...)
  285. {
  286. return 0;
  287. }
  288. #endif
  289. void dm36x_lowlevel_init(ulong bootflag)
  290. {
  291. struct davinci_uart_ctrl_regs *davinci_uart_ctrl_regs =
  292. (struct davinci_uart_ctrl_regs *)(CONFIG_SYS_NS16550_COM1 +
  293. DAVINCI_UART_CTRL_BASE);
  294. /* Mask all interrupts */
  295. writel(DV_AINTC_INTCTL_IDMODE, &dv_aintc_regs->intctl);
  296. writel(0x0, &dv_aintc_regs->eabase);
  297. writel(0x0, &dv_aintc_regs->eint0);
  298. writel(0x0, &dv_aintc_regs->eint1);
  299. /* Clear all interrupts */
  300. writel(0xffffffff, &dv_aintc_regs->fiq0);
  301. writel(0xffffffff, &dv_aintc_regs->fiq1);
  302. writel(0xffffffff, &dv_aintc_regs->irq0);
  303. writel(0xffffffff, &dv_aintc_regs->irq1);
  304. /* System PSC setup - enable all */
  305. dm365_psc_init();
  306. /* Setup Pinmux */
  307. dm365_pinmux_ctl(0, 0xFFFFFFFF, CONFIG_SYS_DM36x_PINMUX0);
  308. dm365_pinmux_ctl(1, 0xFFFFFFFF, CONFIG_SYS_DM36x_PINMUX1);
  309. dm365_pinmux_ctl(2, 0xFFFFFFFF, CONFIG_SYS_DM36x_PINMUX2);
  310. dm365_pinmux_ctl(3, 0xFFFFFFFF, CONFIG_SYS_DM36x_PINMUX3);
  311. dm365_pinmux_ctl(4, 0xFFFFFFFF, CONFIG_SYS_DM36x_PINMUX4);
  312. /* PLL setup */
  313. dm365_pll1_init(CONFIG_SYS_DM36x_PLL1_PLLM,
  314. CONFIG_SYS_DM36x_PLL1_PREDIV);
  315. dm365_pll2_init(CONFIG_SYS_DM36x_PLL2_PLLM,
  316. CONFIG_SYS_DM36x_PLL2_PREDIV);
  317. /* GPIO setup */
  318. board_gpio_init();
  319. NS16550_init((NS16550_t)(CONFIG_SYS_NS16550_COM1),
  320. CONFIG_SYS_NS16550_CLK / 16 / CONFIG_BAUDRATE);
  321. /*
  322. * Fix Power and Emulation Management Register
  323. * see sprufh2.pdf page 38 Table 22
  324. */
  325. writel((DAVINCI_UART_PWREMU_MGMT_FREE | DAVINCI_UART_PWREMU_MGMT_URRST |
  326. DAVINCI_UART_PWREMU_MGMT_UTRST),
  327. &davinci_uart_ctrl_regs->pwremu_mgmt);
  328. puts("ddr init\n");
  329. dm365_ddr_setup();
  330. puts("emif init\n");
  331. dm365_emif_init();
  332. #if defined(CONFIG_POST)
  333. /*
  334. * Do memory tests, calls arch_memory_failure_handle()
  335. * if error detected.
  336. */
  337. memory_post_test(0);
  338. #endif
  339. }