clock.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * (C) Copyright 2008
  3. * Texas Instruments, <www.ti.com>
  4. *
  5. * Author :
  6. * Manikandan Pillai <mani.pillai@ti.com>
  7. *
  8. * Derived from Beagle Board and OMAP3 SDP code by
  9. * Richard Woodruff <r-woodruff2@ti.com>
  10. * Syed Mohammed Khasim <khasim@ti.com>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include <common.h>
  28. #include <asm/io.h>
  29. #include <asm/arch/clocks.h>
  30. #include <asm/arch/clocks_omap3.h>
  31. #include <asm/arch/mem.h>
  32. #include <asm/arch/sys_proto.h>
  33. #include <environment.h>
  34. #include <command.h>
  35. /******************************************************************************
  36. * get_sys_clk_speed() - determine reference oscillator speed
  37. * based on known 32kHz clock and gptimer.
  38. *****************************************************************************/
  39. u32 get_osc_clk_speed(void)
  40. {
  41. u32 start, cstart, cend, cdiff, val;
  42. prcm_t *prcm_base = (prcm_t *)PRCM_BASE;
  43. prm_t *prm_base = (prm_t *)PRM_BASE;
  44. gptimer_t *gpt1_base = (gptimer_t *)OMAP34XX_GPT1;
  45. s32ktimer_t *s32k_base = (s32ktimer_t *)SYNC_32KTIMER_BASE;
  46. val = readl(&prm_base->clksrc_ctrl);
  47. /* If SYS_CLK is being divided by 2, remove for now */
  48. val = (val & (~SYSCLKDIV_2)) | SYSCLKDIV_1;
  49. writel(val, &prm_base->clksrc_ctrl);
  50. /* enable timer2 */
  51. val = readl(&prcm_base->clksel_wkup) | CLKSEL_GPT1;
  52. /* select sys_clk for GPT1 */
  53. writel(val, &prcm_base->clksel_wkup);
  54. /* Enable I and F Clocks for GPT1 */
  55. val = readl(&prcm_base->iclken_wkup) | EN_GPT1 | EN_32KSYNC;
  56. writel(val, &prcm_base->iclken_wkup);
  57. val = readl(&prcm_base->fclken_wkup) | EN_GPT1;
  58. writel(val, &prcm_base->fclken_wkup);
  59. writel(0, &gpt1_base->tldr); /* start counting at 0 */
  60. writel(GPT_EN, &gpt1_base->tclr); /* enable clock */
  61. /* enable 32kHz source, determine sys_clk via gauging */
  62. /* start time in 20 cycles */
  63. start = 20 + readl(&s32k_base->s32k_cr);
  64. /* dead loop till start time */
  65. while (readl(&s32k_base->s32k_cr) < start);
  66. /* get start sys_clk count */
  67. cstart = readl(&gpt1_base->tcrr);
  68. /* wait for 40 cycles */
  69. while (readl(&s32k_base->s32k_cr) < (start + 20)) ;
  70. cend = readl(&gpt1_base->tcrr); /* get end sys_clk count */
  71. cdiff = cend - cstart; /* get elapsed ticks */
  72. /* based on number of ticks assign speed */
  73. if (cdiff > 19000)
  74. return S38_4M;
  75. else if (cdiff > 15200)
  76. return S26M;
  77. else if (cdiff > 13000)
  78. return S24M;
  79. else if (cdiff > 9000)
  80. return S19_2M;
  81. else if (cdiff > 7600)
  82. return S13M;
  83. else
  84. return S12M;
  85. }
  86. /******************************************************************************
  87. * get_sys_clkin_sel() - returns the sys_clkin_sel field value based on
  88. * input oscillator clock frequency.
  89. *****************************************************************************/
  90. void get_sys_clkin_sel(u32 osc_clk, u32 *sys_clkin_sel)
  91. {
  92. switch(osc_clk) {
  93. case S38_4M:
  94. *sys_clkin_sel = 4;
  95. break;
  96. case S26M:
  97. *sys_clkin_sel = 3;
  98. break;
  99. case S19_2M:
  100. *sys_clkin_sel = 2;
  101. break;
  102. case S13M:
  103. *sys_clkin_sel = 1;
  104. break;
  105. case S12M:
  106. default:
  107. *sys_clkin_sel = 0;
  108. }
  109. }
  110. /******************************************************************************
  111. * prcm_init() - inits clocks for PRCM as defined in clocks.h
  112. * called from SRAM, or Flash (using temp SRAM stack).
  113. *****************************************************************************/
  114. void prcm_init(void)
  115. {
  116. void (*f_lock_pll) (u32, u32, u32, u32);
  117. int xip_safe, p0, p1, p2, p3;
  118. u32 osc_clk = 0, sys_clkin_sel;
  119. u32 clk_index, sil_index = 0;
  120. prm_t *prm_base = (prm_t *)PRM_BASE;
  121. prcm_t *prcm_base = (prcm_t *)PRCM_BASE;
  122. dpll_param *dpll_param_p;
  123. f_lock_pll = (void *) ((u32) &_end_vect - (u32) &_start +
  124. SRAM_VECT_CODE);
  125. xip_safe = is_running_in_sram();
  126. /*
  127. * Gauge the input clock speed and find out the sys_clkin_sel
  128. * value corresponding to the input clock.
  129. */
  130. osc_clk = get_osc_clk_speed();
  131. get_sys_clkin_sel(osc_clk, &sys_clkin_sel);
  132. /* set input crystal speed */
  133. sr32(&prm_base->clksel, 0, 3, sys_clkin_sel);
  134. /* If the input clock is greater than 19.2M always divide/2 */
  135. if (sys_clkin_sel > 2) {
  136. /* input clock divider */
  137. sr32(&prm_base->clksrc_ctrl, 6, 2, 2);
  138. clk_index = sys_clkin_sel / 2;
  139. } else {
  140. /* input clock divider */
  141. sr32(&prm_base->clksrc_ctrl, 6, 2, 1);
  142. clk_index = sys_clkin_sel;
  143. }
  144. /*
  145. * The DPLL tables are defined according to sysclk value and
  146. * silicon revision. The clk_index value will be used to get
  147. * the values for that input sysclk from the DPLL param table
  148. * and sil_index will get the values for that SysClk for the
  149. * appropriate silicon rev.
  150. */
  151. if (get_cpu_rev())
  152. sil_index = 1;
  153. /* Unlock MPU DPLL (slows things down, and needed later) */
  154. sr32(&prcm_base->clken_pll_mpu, 0, 3, PLL_LOW_POWER_BYPASS);
  155. wait_on_value(ST_MPU_CLK, 0, &prcm_base->idlest_pll_mpu, LDELAY);
  156. /* Getting the base address of Core DPLL param table */
  157. dpll_param_p = (dpll_param *) get_core_dpll_param();
  158. /* Moving it to the right sysclk and ES rev base */
  159. dpll_param_p = dpll_param_p + 3 * clk_index + sil_index;
  160. if (xip_safe) {
  161. /*
  162. * CORE DPLL
  163. * sr32(CM_CLKSEL2_EMU) set override to work when asleep
  164. */
  165. sr32(&prcm_base->clken_pll, 0, 3, PLL_FAST_RELOCK_BYPASS);
  166. wait_on_value(ST_CORE_CLK, 0, &prcm_base->idlest_ckgen,
  167. LDELAY);
  168. /*
  169. * For OMAP3 ES1.0 Errata 1.50, default value directly doesn't
  170. * work. write another value and then default value.
  171. */
  172. /* m3x2 */
  173. sr32(&prcm_base->clksel1_emu, 16, 5, CORE_M3X2 + 1);
  174. /* m3x2 */
  175. sr32(&prcm_base->clksel1_emu, 16, 5, CORE_M3X2);
  176. /* Set M2 */
  177. sr32(&prcm_base->clksel1_pll, 27, 2, dpll_param_p->m2);
  178. /* Set M */
  179. sr32(&prcm_base->clksel1_pll, 16, 11, dpll_param_p->m);
  180. /* Set N */
  181. sr32(&prcm_base->clksel1_pll, 8, 7, dpll_param_p->n);
  182. /* 96M Src */
  183. sr32(&prcm_base->clksel1_pll, 6, 1, 0);
  184. /* ssi */
  185. sr32(&prcm_base->clksel_core, 8, 4, CORE_SSI_DIV);
  186. /* fsusb */
  187. sr32(&prcm_base->clksel_core, 4, 2, CORE_FUSB_DIV);
  188. /* l4 */
  189. sr32(&prcm_base->clksel_core, 2, 2, CORE_L4_DIV);
  190. /* l3 */
  191. sr32(&prcm_base->clksel_core, 0, 2, CORE_L3_DIV);
  192. /* gfx */
  193. sr32(&prcm_base->clksel_gfx, 0, 3, GFX_DIV);
  194. /* reset mgr */
  195. sr32(&prcm_base->clksel_wkup, 1, 2, WKUP_RSM);
  196. /* FREQSEL */
  197. sr32(&prcm_base->clken_pll, 4, 4, dpll_param_p->fsel);
  198. /* lock mode */
  199. sr32(&prcm_base->clken_pll, 0, 3, PLL_LOCK);
  200. wait_on_value(ST_CORE_CLK, 1, &prcm_base->idlest_ckgen,
  201. LDELAY);
  202. } else if (is_running_in_flash()) {
  203. /*
  204. * if running from flash, jump to small relocated code
  205. * area in SRAM.
  206. */
  207. p0 = readl(&prcm_base->clken_pll);
  208. sr32(&p0, 0, 3, PLL_FAST_RELOCK_BYPASS);
  209. sr32(&p0, 4, 4, dpll_param_p->fsel); /* FREQSEL */
  210. p1 = readl(&prcm_base->clksel1_pll);
  211. sr32(&p1, 27, 2, dpll_param_p->m2); /* Set M2 */
  212. sr32(&p1, 16, 11, dpll_param_p->m); /* Set M */
  213. sr32(&p1, 8, 7, dpll_param_p->n); /* Set N */
  214. sr32(&p1, 6, 1, 0); /* set source for 96M */
  215. p2 = readl(&prcm_base->clksel_core);
  216. sr32(&p2, 8, 4, CORE_SSI_DIV); /* ssi */
  217. sr32(&p2, 4, 2, CORE_FUSB_DIV); /* fsusb */
  218. sr32(&p2, 2, 2, CORE_L4_DIV); /* l4 */
  219. sr32(&p2, 0, 2, CORE_L3_DIV); /* l3 */
  220. p3 = (u32)&prcm_base->idlest_ckgen;
  221. (*f_lock_pll) (p0, p1, p2, p3);
  222. }
  223. /* PER DPLL */
  224. sr32(&prcm_base->clken_pll, 16, 3, PLL_STOP);
  225. wait_on_value(ST_PERIPH_CLK, 0, &prcm_base->idlest_ckgen, LDELAY);
  226. /* Getting the base address to PER DPLL param table */
  227. /* Set N */
  228. dpll_param_p = (dpll_param *) get_per_dpll_param();
  229. /* Moving it to the right sysclk base */
  230. dpll_param_p = dpll_param_p + clk_index;
  231. /*
  232. * Errata 1.50 Workaround for OMAP3 ES1.0 only
  233. * If using default divisors, write default divisor + 1
  234. * and then the actual divisor value
  235. */
  236. sr32(&prcm_base->clksel1_emu, 24, 5, PER_M6X2 + 1); /* set M6 */
  237. sr32(&prcm_base->clksel1_emu, 24, 5, PER_M6X2); /* set M6 */
  238. sr32(&prcm_base->clksel_cam, 0, 5, PER_M5X2 + 1); /* set M5 */
  239. sr32(&prcm_base->clksel_cam, 0, 5, PER_M5X2); /* set M5 */
  240. sr32(&prcm_base->clksel_dss, 0, 5, PER_M4X2 + 1); /* set M4 */
  241. sr32(&prcm_base->clksel_dss, 0, 5, PER_M4X2); /* set M4 */
  242. sr32(&prcm_base->clksel_dss, 8, 5, PER_M3X2 + 1); /* set M3 */
  243. sr32(&prcm_base->clksel_dss, 8, 5, PER_M3X2); /* set M3 */
  244. sr32(&prcm_base->clksel3_pll, 0, 5, dpll_param_p->m2 + 1); /* set M2 */
  245. sr32(&prcm_base->clksel3_pll, 0, 5, dpll_param_p->m2); /* set M2 */
  246. /* Workaround end */
  247. sr32(&prcm_base->clksel2_pll, 8, 11, dpll_param_p->m); /* set m */
  248. sr32(&prcm_base->clksel2_pll, 0, 7, dpll_param_p->n); /* set n */
  249. sr32(&prcm_base->clken_pll, 20, 4, dpll_param_p->fsel); /* FREQSEL */
  250. sr32(&prcm_base->clken_pll, 16, 3, PLL_LOCK); /* lock mode */
  251. wait_on_value(ST_PERIPH_CLK, 2, &prcm_base->idlest_ckgen, LDELAY);
  252. /* Getting the base address to MPU DPLL param table */
  253. dpll_param_p = (dpll_param *) get_mpu_dpll_param();
  254. /* Moving it to the right sysclk and ES rev base */
  255. dpll_param_p = dpll_param_p + 3 * clk_index + sil_index;
  256. /* MPU DPLL (unlocked already) */
  257. /* Set M2 */
  258. sr32(&prcm_base->clksel2_pll_mpu, 0, 5, dpll_param_p->m2);
  259. /* Set M */
  260. sr32(&prcm_base->clksel1_pll_mpu, 8, 11, dpll_param_p->m);
  261. /* Set N */
  262. sr32(&prcm_base->clksel1_pll_mpu, 0, 7, dpll_param_p->n);
  263. /* FREQSEL */
  264. sr32(&prcm_base->clken_pll_mpu, 4, 4, dpll_param_p->fsel);
  265. /* lock mode */
  266. sr32(&prcm_base->clken_pll_mpu, 0, 3, PLL_LOCK);
  267. wait_on_value(ST_MPU_CLK, 1, &prcm_base->idlest_pll_mpu, LDELAY);
  268. /* Getting the base address to IVA DPLL param table */
  269. dpll_param_p = (dpll_param *) get_iva_dpll_param();
  270. /* Moving it to the right sysclk and ES rev base */
  271. dpll_param_p = dpll_param_p + 3 * clk_index + sil_index;
  272. /* IVA DPLL (set to 12*20=240MHz) */
  273. sr32(&prcm_base->clken_pll_iva2, 0, 3, PLL_STOP);
  274. wait_on_value(ST_IVA2_CLK, 0, &prcm_base->idlest_pll_iva2, LDELAY);
  275. /* set M2 */
  276. sr32(&prcm_base->clksel2_pll_iva2, 0, 5, dpll_param_p->m2);
  277. /* set M */
  278. sr32(&prcm_base->clksel1_pll_iva2, 8, 11, dpll_param_p->m);
  279. /* set N */
  280. sr32(&prcm_base->clksel1_pll_iva2, 0, 7, dpll_param_p->n);
  281. /* FREQSEL */
  282. sr32(&prcm_base->clken_pll_iva2, 4, 4, dpll_param_p->fsel);
  283. /* lock mode */
  284. sr32(&prcm_base->clken_pll_iva2, 0, 3, PLL_LOCK);
  285. wait_on_value(ST_IVA2_CLK, 1, &prcm_base->idlest_pll_iva2, LDELAY);
  286. /* Set up GPTimers to sys_clk source only */
  287. sr32(&prcm_base->clksel_per, 0, 8, 0xff);
  288. sr32(&prcm_base->clksel_wkup, 0, 1, 1);
  289. sdelay(5000);
  290. }
  291. /******************************************************************************
  292. * peripheral_enable() - Enable the clks & power for perifs (GPT2, UART1,...)
  293. *****************************************************************************/
  294. void per_clocks_enable(void)
  295. {
  296. prcm_t *prcm_base = (prcm_t *)PRCM_BASE;
  297. /* Enable GP2 timer. */
  298. sr32(&prcm_base->clksel_per, 0, 1, 0x1); /* GPT2 = sys clk */
  299. sr32(&prcm_base->iclken_per, 3, 1, 0x1); /* ICKen GPT2 */
  300. sr32(&prcm_base->fclken_per, 3, 1, 0x1); /* FCKen GPT2 */
  301. #ifdef CONFIG_SYS_NS16550
  302. /* Enable UART1 clocks */
  303. sr32(&prcm_base->fclken1_core, 13, 1, 0x1);
  304. sr32(&prcm_base->iclken1_core, 13, 1, 0x1);
  305. /* UART 3 Clocks */
  306. sr32(&prcm_base->fclken_per, 11, 1, 0x1);
  307. sr32(&prcm_base->iclken_per, 11, 1, 0x1);
  308. #endif
  309. #ifdef CONFIG_OMAP3_GPIO_2
  310. sr32(&prcm_base->fclken_per, 13, 1, 1);
  311. sr32(&prcm_base->iclken_per, 13, 1, 1);
  312. #endif
  313. #ifdef CONFIG_OMAP3_GPIO_3
  314. sr32(&prcm_base->fclken_per, 14, 1, 1);
  315. sr32(&prcm_base->iclken_per, 14, 1, 1);
  316. #endif
  317. #ifdef CONFIG_OMAP3_GPIO_4
  318. sr32(&prcm_base->fclken_per, 15, 1, 1);
  319. sr32(&prcm_base->iclken_per, 15, 1, 1);
  320. #endif
  321. #ifdef CONFIG_OMAP3_GPIO_5
  322. sr32(&prcm_base->fclken_per, 16, 1, 1);
  323. sr32(&prcm_base->iclken_per, 16, 1, 1);
  324. #endif
  325. #ifdef CONFIG_OMAP3_GPIO_6
  326. sr32(&prcm_base->fclken_per, 17, 1, 1);
  327. sr32(&prcm_base->iclken_per, 17, 1, 1);
  328. #endif
  329. #ifdef CONFIG_DRIVER_OMAP34XX_I2C
  330. /* Turn on all 3 I2C clocks */
  331. sr32(&prcm_base->fclken1_core, 15, 3, 0x7);
  332. sr32(&prcm_base->iclken1_core, 15, 3, 0x7); /* I2C1,2,3 = on */
  333. #endif
  334. /* Enable the ICLK for 32K Sync Timer as its used in udelay */
  335. sr32(&prcm_base->iclken_wkup, 2, 1, 0x1);
  336. sr32(&prcm_base->fclken_iva2, 0, 32, FCK_IVA2_ON);
  337. sr32(&prcm_base->fclken1_core, 0, 32, FCK_CORE1_ON);
  338. sr32(&prcm_base->iclken1_core, 0, 32, ICK_CORE1_ON);
  339. sr32(&prcm_base->iclken2_core, 0, 32, ICK_CORE2_ON);
  340. sr32(&prcm_base->fclken_wkup, 0, 32, FCK_WKUP_ON);
  341. sr32(&prcm_base->iclken_wkup, 0, 32, ICK_WKUP_ON);
  342. sr32(&prcm_base->fclken_dss, 0, 32, FCK_DSS_ON);
  343. sr32(&prcm_base->iclken_dss, 0, 32, ICK_DSS_ON);
  344. sr32(&prcm_base->fclken_cam, 0, 32, FCK_CAM_ON);
  345. sr32(&prcm_base->iclken_cam, 0, 32, ICK_CAM_ON);
  346. sr32(&prcm_base->fclken_per, 0, 32, FCK_PER_ON);
  347. sr32(&prcm_base->iclken_per, 0, 32, ICK_PER_ON);
  348. sdelay(1000);
  349. }