clock_ti816x.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * clock_ti816x.c
  3. *
  4. * Clocks for TI816X based boards
  5. *
  6. * Copyright (C) 2013, Adeneo Embedded <www.adeneo-embedded.com>
  7. * Antoine Tenart, <atenart@adeneo-embedded.com>
  8. *
  9. * Based on TI-PSP-04.00.02.14 :
  10. *
  11. * Copyright (C) 2009, Texas Instruments, Incorporated
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR /PURPOSE. See the
  21. * GNU General Public License for more details.
  22. */
  23. #include <common.h>
  24. #include <asm/arch/ddr_defs.h>
  25. #include <asm/arch/cpu.h>
  26. #include <asm/arch/clock.h>
  27. #include <asm/arch/hardware.h>
  28. #include <asm/io.h>
  29. #include <asm/emif.h>
  30. #define CM_PLL_BASE (CTRL_BASE + 0x0400)
  31. /* Main PLL */
  32. #define MAIN_N 64
  33. #define MAIN_P 0x1
  34. #define MAIN_INTFREQ1 0x8
  35. #define MAIN_FRACFREQ1 0x800000
  36. #define MAIN_MDIV1 0x2
  37. #define MAIN_INTFREQ2 0xE
  38. #define MAIN_FRACFREQ2 0x0
  39. #define MAIN_MDIV2 0x1
  40. #define MAIN_INTFREQ3 0x8
  41. #define MAIN_FRACFREQ3 0xAAAAB0
  42. #define MAIN_MDIV3 0x3
  43. #define MAIN_INTFREQ4 0x9
  44. #define MAIN_FRACFREQ4 0x55554F
  45. #define MAIN_MDIV4 0x3
  46. #define MAIN_INTFREQ5 0x9
  47. #define MAIN_FRACFREQ5 0x374BC6
  48. #define MAIN_MDIV5 0xC
  49. #define MAIN_MDIV6 0x48
  50. #define MAIN_MDIV7 0x4
  51. /* DDR PLL */
  52. #if defined(CONFIG_TI816X_DDR_PLL_400) /* 400 MHz */
  53. #define DDR_N 59
  54. #define DDR_P 0x1
  55. #define DDR_MDIV1 0x4
  56. #define DDR_INTFREQ2 0x8
  57. #define DDR_FRACFREQ2 0xD99999
  58. #define DDR_MDIV2 0x1E
  59. #define DDR_INTFREQ3 0x8
  60. #define DDR_FRACFREQ3 0x0
  61. #define DDR_MDIV3 0x4
  62. #define DDR_INTFREQ4 0xE /* Expansion DDR clk */
  63. #define DDR_FRACFREQ4 0x0
  64. #define DDR_MDIV4 0x4
  65. #define DDR_INTFREQ5 0xE /* Expansion DDR clk */
  66. #define DDR_FRACFREQ5 0x0
  67. #define DDR_MDIV5 0x4
  68. #elif defined(CONFIG_TI816X_DDR_PLL_531) /* 531 MHz */
  69. #define DDR_N 59
  70. #define DDR_P 0x1
  71. #define DDR_MDIV1 0x3
  72. #define DDR_INTFREQ2 0x8
  73. #define DDR_FRACFREQ2 0xD99999
  74. #define DDR_MDIV2 0x1E
  75. #define DDR_INTFREQ3 0x8
  76. #define DDR_FRACFREQ3 0x0
  77. #define DDR_MDIV3 0x4
  78. #define DDR_INTFREQ4 0xE /* Expansion DDR clk */
  79. #define DDR_FRACFREQ4 0x0
  80. #define DDR_MDIV4 0x4
  81. #define DDR_INTFREQ5 0xE /* Expansion DDR clk */
  82. #define DDR_FRACFREQ5 0x0
  83. #define DDR_MDIV5 0x4
  84. #elif defined(CONFIG_TI816X_DDR_PLL_675) /* 675 MHz */
  85. #define DDR_N 50
  86. #define DDR_P 0x1
  87. #define DDR_MDIV1 0x2
  88. #define DDR_INTFREQ2 0x9
  89. #define DDR_FRACFREQ2 0x0
  90. #define DDR_MDIV2 0x19
  91. #define DDR_INTFREQ3 0x13
  92. #define DDR_FRACFREQ3 0x800000
  93. #define DDR_MDIV3 0x2
  94. #define DDR_INTFREQ4 0xE /* Expansion DDR clk */
  95. #define DDR_FRACFREQ4 0x0
  96. #define DDR_MDIV4 0x4
  97. #define DDR_INTFREQ5 0xE /* Expansion DDR clk */
  98. #define DDR_FRACFREQ5 0x0
  99. #define DDR_MDIV5 0x4
  100. #elif defined(CONFIG_TI816X_DDR_PLL_796) /* 796 MHz */
  101. #define DDR_N 59
  102. #define DDR_P 0x1
  103. #define DDR_MDIV1 0x2
  104. #define DDR_INTFREQ2 0x8
  105. #define DDR_FRACFREQ2 0xD99999
  106. #define DDR_MDIV2 0x1E
  107. #define DDR_INTFREQ3 0x8
  108. #define DDR_FRACFREQ3 0x0
  109. #define DDR_MDIV3 0x4
  110. #define DDR_INTFREQ4 0xE /* Expansion DDR clk */
  111. #define DDR_FRACFREQ4 0x0
  112. #define DDR_MDIV4 0x4
  113. #define DDR_INTFREQ5 0xE /* Expansion DDR clk */
  114. #define DDR_FRACFREQ5 0x0
  115. #define DDR_MDIV5 0x4
  116. #endif
  117. #define CONTROL_STATUS (CTRL_BASE + 0x40)
  118. #define DDR_RCD (CTRL_BASE + 0x070C)
  119. #define CM_TIMER1_CLKSEL (PRCM_BASE + 0x390)
  120. #define DMM_PAT_BASE_ADDR (DMM_BASE + 0x420)
  121. #define CM_ALWON_CUST_EFUSE_CLKCTRL (PRCM_BASE + 0x1628)
  122. #define INTCPS_SYSCONFIG 0x48200010
  123. #define CM_SYSCLK10_CLKSEL 0x48180324
  124. struct cm_pll {
  125. unsigned int mainpll_ctrl; /* offset 0x400 */
  126. unsigned int mainpll_pwd;
  127. unsigned int mainpll_freq1;
  128. unsigned int mainpll_div1;
  129. unsigned int mainpll_freq2;
  130. unsigned int mainpll_div2;
  131. unsigned int mainpll_freq3;
  132. unsigned int mainpll_div3;
  133. unsigned int mainpll_freq4;
  134. unsigned int mainpll_div4;
  135. unsigned int mainpll_freq5;
  136. unsigned int mainpll_div5;
  137. unsigned int resv0[1];
  138. unsigned int mainpll_div6;
  139. unsigned int resv1[1];
  140. unsigned int mainpll_div7;
  141. unsigned int ddrpll_ctrl; /* offset 0x440 */
  142. unsigned int ddrpll_pwd;
  143. unsigned int resv2[1];
  144. unsigned int ddrpll_div1;
  145. unsigned int ddrpll_freq2;
  146. unsigned int ddrpll_div2;
  147. unsigned int ddrpll_freq3;
  148. unsigned int ddrpll_div3;
  149. unsigned int ddrpll_freq4;
  150. unsigned int ddrpll_div4;
  151. unsigned int ddrpll_freq5;
  152. unsigned int ddrpll_div5;
  153. unsigned int videopll_ctrl; /* offset 0x470 */
  154. unsigned int videopll_pwd;
  155. unsigned int videopll_freq1;
  156. unsigned int videopll_div1;
  157. unsigned int videopll_freq2;
  158. unsigned int videopll_div2;
  159. unsigned int videopll_freq3;
  160. unsigned int videopll_div3;
  161. unsigned int resv3[4];
  162. unsigned int audiopll_ctrl; /* offset 0x4A0 */
  163. unsigned int audiopll_pwd;
  164. unsigned int resv4[2];
  165. unsigned int audiopll_freq2;
  166. unsigned int audiopll_div2;
  167. unsigned int audiopll_freq3;
  168. unsigned int audiopll_div3;
  169. unsigned int audiopll_freq4;
  170. unsigned int audiopll_div4;
  171. unsigned int audiopll_freq5;
  172. unsigned int audiopll_div5;
  173. };
  174. const struct cm_alwon *cmalwon = (struct cm_alwon *)CM_ALWON_BASE;
  175. const struct cm_def *cmdef = (struct cm_def *)CM_DEFAULT_BASE;
  176. const struct cm_pll *cmpll = (struct cm_pll *)CM_PLL_BASE;
  177. const struct wd_timer *wdtimer = (struct wd_timer *)WDT_BASE;
  178. void enable_dmm_clocks(void)
  179. {
  180. writel(PRCM_MOD_EN, &cmdef->l3fastclkstctrl);
  181. writel(PRCM_MOD_EN, &cmdef->emif0clkctrl);
  182. writel(PRCM_MOD_EN, &cmdef->emif1clkctrl);
  183. /* Wait for clocks to be active */
  184. while ((readl(&cmdef->l3fastclkstctrl) & 0x300) != 0x300)
  185. ;
  186. /* Wait for emif0 to be fully functional, including OCP */
  187. while (((readl(&cmdef->emif0clkctrl) >> 17) & 0x3) != 0)
  188. ;
  189. /* Wait for emif1 to be fully functional, including OCP */
  190. while (((readl(&cmdef->emif1clkctrl) >> 17) & 0x3) != 0)
  191. ;
  192. writel(PRCM_MOD_EN, &cmdef->dmmclkctrl);
  193. /* Wait for dmm to be fully functional, including OCP */
  194. while (((readl(&cmdef->dmmclkctrl) >> 17) & 0x3) != 0)
  195. ;
  196. /* Enable Tiled Access */
  197. writel(0x80000000, DMM_PAT_BASE_ADDR);
  198. }
  199. /* assume delay is aprox at least 1us */
  200. static void ddr_delay(int d)
  201. {
  202. int i;
  203. /*
  204. * read a control register.
  205. * this is a bit more delay and cannot be optimized by the compiler
  206. * assuming one read takes 200 cycles and A8 is runing 1 GHz
  207. * somewhat conservative setting
  208. */
  209. for (i = 0; i < 50*d; i++)
  210. readl(CONTROL_STATUS);
  211. }
  212. static void main_pll_init_ti816x(void)
  213. {
  214. u32 main_pll_ctrl = 0;
  215. /* Put the PLL in bypass mode by setting BIT2 in its ctrl reg */
  216. main_pll_ctrl = readl(&cmpll->mainpll_ctrl);
  217. main_pll_ctrl &= 0xFFFFFFFB;
  218. main_pll_ctrl |= BIT(2);
  219. writel(main_pll_ctrl, &cmpll->mainpll_ctrl);
  220. /* Enable PLL by setting BIT3 in its ctrl reg */
  221. main_pll_ctrl = readl(&cmpll->mainpll_ctrl);
  222. main_pll_ctrl &= 0xFFFFFFF7;
  223. main_pll_ctrl |= BIT(3);
  224. writel(main_pll_ctrl, &cmpll->mainpll_ctrl);
  225. /* Write the values of N,P in the CTRL reg */
  226. main_pll_ctrl = readl(&cmpll->mainpll_ctrl);
  227. main_pll_ctrl &= 0xFF;
  228. main_pll_ctrl |= (MAIN_N<<16 | MAIN_P<<8);
  229. writel(main_pll_ctrl, &cmpll->mainpll_ctrl);
  230. /* Power up clock1-7 */
  231. writel(0x0, &cmpll->mainpll_pwd);
  232. /* Program the freq and divider values for clock1-7 */
  233. writel((1<<31 | 1<<28 | (MAIN_INTFREQ1<<24) | MAIN_FRACFREQ1),
  234. &cmpll->mainpll_freq1);
  235. writel(((1<<8) | MAIN_MDIV1), &cmpll->mainpll_div1);
  236. writel((1<<31 | 1<<28 | (MAIN_INTFREQ2<<24) | MAIN_FRACFREQ2),
  237. &cmpll->mainpll_freq2);
  238. writel(((1<<8) | MAIN_MDIV2), &cmpll->mainpll_div2);
  239. writel((1<<31 | 1<<28 | (MAIN_INTFREQ3<<24) | MAIN_FRACFREQ3),
  240. &cmpll->mainpll_freq3);
  241. writel(((1<<8) | MAIN_MDIV3), &cmpll->mainpll_div3);
  242. writel((1<<31 | 1<<28 | (MAIN_INTFREQ4<<24) | MAIN_FRACFREQ4),
  243. &cmpll->mainpll_freq4);
  244. writel(((1<<8) | MAIN_MDIV4), &cmpll->mainpll_div4);
  245. writel((1<<31 | 1<<28 | (MAIN_INTFREQ5<<24) | MAIN_FRACFREQ5),
  246. &cmpll->mainpll_freq5);
  247. writel(((1<<8) | MAIN_MDIV5), &cmpll->mainpll_div5);
  248. writel((1<<8 | MAIN_MDIV6), &cmpll->mainpll_div6);
  249. writel((1<<8 | MAIN_MDIV7), &cmpll->mainpll_div7);
  250. /* Wait for PLL to lock */
  251. while ((readl(&cmpll->mainpll_ctrl) & BIT(7)) != BIT(7))
  252. ;
  253. /* Put the PLL in normal mode, disable bypass */
  254. main_pll_ctrl = readl(&cmpll->mainpll_ctrl);
  255. main_pll_ctrl &= 0xFFFFFFFB;
  256. writel(main_pll_ctrl, &cmpll->mainpll_ctrl);
  257. }
  258. static void ddr_pll_bypass_ti816x(void)
  259. {
  260. u32 ddr_pll_ctrl = 0;
  261. /* Put the PLL in bypass mode by setting BIT2 in its ctrl reg */
  262. ddr_pll_ctrl = readl(&cmpll->ddrpll_ctrl);
  263. ddr_pll_ctrl &= 0xFFFFFFFB;
  264. ddr_pll_ctrl |= BIT(2);
  265. writel(ddr_pll_ctrl, &cmpll->ddrpll_ctrl);
  266. }
  267. static void ddr_pll_init_ti816x(void)
  268. {
  269. u32 ddr_pll_ctrl = 0;
  270. /* Enable PLL by setting BIT3 in its ctrl reg */
  271. ddr_pll_ctrl = readl(&cmpll->ddrpll_ctrl);
  272. ddr_pll_ctrl &= 0xFFFFFFF7;
  273. ddr_pll_ctrl |= BIT(3);
  274. writel(ddr_pll_ctrl, &cmpll->ddrpll_ctrl);
  275. /* Write the values of N,P in the CTRL reg */
  276. ddr_pll_ctrl = readl(&cmpll->ddrpll_ctrl);
  277. ddr_pll_ctrl &= 0xFF;
  278. ddr_pll_ctrl |= (DDR_N<<16 | DDR_P<<8);
  279. writel(ddr_pll_ctrl, &cmpll->ddrpll_ctrl);
  280. ddr_delay(10);
  281. /* Power up clock1-5 */
  282. writel(0x0, &cmpll->ddrpll_pwd);
  283. /* Program the freq and divider values for clock1-3 */
  284. writel(((0<<8) | DDR_MDIV1), &cmpll->ddrpll_div1);
  285. ddr_delay(1);
  286. writel(((1<<8) | DDR_MDIV1), &cmpll->ddrpll_div1);
  287. writel((1<<31 | 1<<28 | (DDR_INTFREQ2<<24) | DDR_FRACFREQ2),
  288. &cmpll->ddrpll_freq2);
  289. writel(((1<<8) | DDR_MDIV2), &cmpll->ddrpll_div2);
  290. writel(((0<<8) | DDR_MDIV3), &cmpll->ddrpll_div3);
  291. ddr_delay(1);
  292. writel(((1<<8) | DDR_MDIV3), &cmpll->ddrpll_div3);
  293. ddr_delay(1);
  294. writel((0<<31 | 1<<28 | (DDR_INTFREQ3<<24) | DDR_FRACFREQ3),
  295. &cmpll->ddrpll_freq3);
  296. ddr_delay(1);
  297. writel((1<<31 | 1<<28 | (DDR_INTFREQ3<<24) | DDR_FRACFREQ3),
  298. &cmpll->ddrpll_freq3);
  299. ddr_delay(5);
  300. /* Wait for PLL to lock */
  301. while ((readl(&cmpll->ddrpll_ctrl) & BIT(7)) != BIT(7))
  302. ;
  303. /* Power up RCD */
  304. writel(BIT(0), DDR_RCD);
  305. }
  306. static void peripheral_enable(void)
  307. {
  308. /* Wake-up the l3_slow clock */
  309. writel(PRCM_MOD_EN, &cmalwon->l3slowclkstctrl);
  310. /*
  311. * Note on Timers:
  312. * There are 8 timers(0-7) out of which timer 0 is a secure timer.
  313. * Timer 0 mux should not be changed
  314. *
  315. * To access the timer registers we need the to be
  316. * enabled which is what we do in the first step
  317. */
  318. /* Enable timer1 */
  319. writel(PRCM_MOD_EN, &cmalwon->timer1clkctrl);
  320. /* Select timer1 clock to be CLKIN (27MHz) */
  321. writel(BIT(1), CM_TIMER1_CLKSEL);
  322. /* Wait for timer1 to be ON-ACTIVE */
  323. while (((readl(&cmalwon->l3slowclkstctrl)
  324. & (0x80000<<1))>>20) != 1)
  325. ;
  326. /* Wait for timer1 to be enabled */
  327. while (((readl(&cmalwon->timer1clkctrl) & 0x30000)>>16) != 0)
  328. ;
  329. /* Active posted mode */
  330. writel(PRCM_MOD_EN, (DM_TIMER1_BASE + 0x54));
  331. while (readl(DM_TIMER1_BASE + 0x10) & BIT(0))
  332. ;
  333. /* Start timer1 */
  334. writel(BIT(0), (DM_TIMER1_BASE + 0x38));
  335. /* eFuse */
  336. writel(PRCM_MOD_EN, CM_ALWON_CUST_EFUSE_CLKCTRL);
  337. while (readl(CM_ALWON_CUST_EFUSE_CLKCTRL) != PRCM_MOD_EN)
  338. ;
  339. /* Enable gpio0 */
  340. writel(PRCM_MOD_EN, &cmalwon->gpio0clkctrl);
  341. while (readl(&cmalwon->gpio0clkctrl) != PRCM_MOD_EN)
  342. ;
  343. writel((BIT(8)), &cmalwon->gpio0clkctrl);
  344. /* Enable spi */
  345. writel(PRCM_MOD_EN, &cmalwon->spiclkctrl);
  346. while (readl(&cmalwon->spiclkctrl) != PRCM_MOD_EN)
  347. ;
  348. /* Enable i2c0 */
  349. writel(PRCM_MOD_EN, &cmalwon->i2c0clkctrl);
  350. while (readl(&cmalwon->i2c0clkctrl) != PRCM_MOD_EN)
  351. ;
  352. /* Enable ethernet0 */
  353. writel(PRCM_MOD_EN, &cmalwon->ethclkstctrl);
  354. writel(PRCM_MOD_EN, &cmalwon->ethernet0clkctrl);
  355. writel(PRCM_MOD_EN, &cmalwon->ethernet1clkctrl);
  356. /* Enable hsmmc */
  357. writel(PRCM_MOD_EN, &cmalwon->sdioclkctrl);
  358. while (readl(&cmalwon->sdioclkctrl) != PRCM_MOD_EN)
  359. ;
  360. }
  361. void setup_clocks_for_console(void)
  362. {
  363. /* Fix ROM code bug - from TI-PSP-04.00.02.14 */
  364. writel(0x0, CM_SYSCLK10_CLKSEL);
  365. ddr_pll_bypass_ti816x();
  366. /* Enable uart0-2 */
  367. writel(PRCM_MOD_EN, &cmalwon->uart0clkctrl);
  368. while (readl(&cmalwon->uart0clkctrl) != PRCM_MOD_EN)
  369. ;
  370. writel(PRCM_MOD_EN, &cmalwon->uart1clkctrl);
  371. while (readl(&cmalwon->uart1clkctrl) != PRCM_MOD_EN)
  372. ;
  373. writel(PRCM_MOD_EN, &cmalwon->uart2clkctrl);
  374. while (readl(&cmalwon->uart2clkctrl) != PRCM_MOD_EN)
  375. ;
  376. while ((readl(&cmalwon->l3slowclkstctrl) & 0x2100) != 0x2100)
  377. ;
  378. }
  379. void prcm_init(void)
  380. {
  381. /* Enable the control */
  382. writel(PRCM_MOD_EN, &cmalwon->controlclkctrl);
  383. main_pll_init_ti816x();
  384. ddr_pll_init_ti816x();
  385. /*
  386. * With clk freqs setup to desired values,
  387. * enable the required peripherals
  388. */
  389. peripheral_enable();
  390. }