dm365_lowlevel.c 12 KB

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