clock.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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;
  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. sil_index = get_cpu_rev() - 1;
  152. /* Unlock MPU DPLL (slows things down, and needed later) */
  153. sr32(&prcm_base->clken_pll_mpu, 0, 3, PLL_LOW_POWER_BYPASS);
  154. wait_on_value(ST_MPU_CLK, 0, &prcm_base->idlest_pll_mpu, LDELAY);
  155. /* Getting the base address of Core DPLL param table */
  156. dpll_param_p = (dpll_param *) get_core_dpll_param();
  157. /* Moving it to the right sysclk and ES rev base */
  158. dpll_param_p = dpll_param_p + 3 * clk_index + sil_index;
  159. if (xip_safe) {
  160. /*
  161. * CORE DPLL
  162. * sr32(CM_CLKSEL2_EMU) set override to work when asleep
  163. */
  164. sr32(&prcm_base->clken_pll, 0, 3, PLL_FAST_RELOCK_BYPASS);
  165. wait_on_value(ST_CORE_CLK, 0, &prcm_base->idlest_ckgen,
  166. LDELAY);
  167. /*
  168. * For OMAP3 ES1.0 Errata 1.50, default value directly doesn't
  169. * work. write another value and then default value.
  170. */
  171. /* m3x2 */
  172. sr32(&prcm_base->clksel1_emu, 16, 5, CORE_M3X2 + 1);
  173. /* m3x2 */
  174. sr32(&prcm_base->clksel1_emu, 16, 5, CORE_M3X2);
  175. /* Set M2 */
  176. sr32(&prcm_base->clksel1_pll, 27, 2, dpll_param_p->m2);
  177. /* Set M */
  178. sr32(&prcm_base->clksel1_pll, 16, 11, dpll_param_p->m);
  179. /* Set N */
  180. sr32(&prcm_base->clksel1_pll, 8, 7, dpll_param_p->n);
  181. /* 96M Src */
  182. sr32(&prcm_base->clksel1_pll, 6, 1, 0);
  183. /* ssi */
  184. sr32(&prcm_base->clksel_core, 8, 4, CORE_SSI_DIV);
  185. /* fsusb */
  186. sr32(&prcm_base->clksel_core, 4, 2, CORE_FUSB_DIV);
  187. /* l4 */
  188. sr32(&prcm_base->clksel_core, 2, 2, CORE_L4_DIV);
  189. /* l3 */
  190. sr32(&prcm_base->clksel_core, 0, 2, CORE_L3_DIV);
  191. /* gfx */
  192. sr32(&prcm_base->clksel_gfx, 0, 3, GFX_DIV);
  193. /* reset mgr */
  194. sr32(&prcm_base->clksel_wkup, 1, 2, WKUP_RSM);
  195. /* FREQSEL */
  196. sr32(&prcm_base->clken_pll, 4, 4, dpll_param_p->fsel);
  197. /* lock mode */
  198. sr32(&prcm_base->clken_pll, 0, 3, PLL_LOCK);
  199. wait_on_value(ST_CORE_CLK, 1, &prcm_base->idlest_ckgen,
  200. LDELAY);
  201. } else if (is_running_in_flash()) {
  202. /*
  203. * if running from flash, jump to small relocated code
  204. * area in SRAM.
  205. */
  206. p0 = readl(&prcm_base->clken_pll);
  207. sr32(&p0, 0, 3, PLL_FAST_RELOCK_BYPASS);
  208. sr32(&p0, 4, 4, dpll_param_p->fsel); /* FREQSEL */
  209. p1 = readl(&prcm_base->clksel1_pll);
  210. sr32(&p1, 27, 2, dpll_param_p->m2); /* Set M2 */
  211. sr32(&p1, 16, 11, dpll_param_p->m); /* Set M */
  212. sr32(&p1, 8, 7, dpll_param_p->n); /* Set N */
  213. sr32(&p1, 6, 1, 0); /* set source for 96M */
  214. p2 = readl(&prcm_base->clksel_core);
  215. sr32(&p2, 8, 4, CORE_SSI_DIV); /* ssi */
  216. sr32(&p2, 4, 2, CORE_FUSB_DIV); /* fsusb */
  217. sr32(&p2, 2, 2, CORE_L4_DIV); /* l4 */
  218. sr32(&p2, 0, 2, CORE_L3_DIV); /* l3 */
  219. p3 = (u32)&prcm_base->idlest_ckgen;
  220. (*f_lock_pll) (p0, p1, p2, p3);
  221. }
  222. /* PER DPLL */
  223. sr32(&prcm_base->clken_pll, 16, 3, PLL_STOP);
  224. wait_on_value(ST_PERIPH_CLK, 0, &prcm_base->idlest_ckgen, LDELAY);
  225. /* Getting the base address to PER DPLL param table */
  226. /* Set N */
  227. dpll_param_p = (dpll_param *) get_per_dpll_param();
  228. /* Moving it to the right sysclk base */
  229. dpll_param_p = dpll_param_p + clk_index;
  230. /*
  231. * Errata 1.50 Workaround for OMAP3 ES1.0 only
  232. * If using default divisors, write default divisor + 1
  233. * and then the actual divisor value
  234. */
  235. sr32(&prcm_base->clksel1_emu, 24, 5, PER_M6X2 + 1); /* set M6 */
  236. sr32(&prcm_base->clksel1_emu, 24, 5, PER_M6X2); /* set M6 */
  237. sr32(&prcm_base->clksel_cam, 0, 5, PER_M5X2 + 1); /* set M5 */
  238. sr32(&prcm_base->clksel_cam, 0, 5, PER_M5X2); /* set M5 */
  239. sr32(&prcm_base->clksel_dss, 0, 5, PER_M4X2 + 1); /* set M4 */
  240. sr32(&prcm_base->clksel_dss, 0, 5, PER_M4X2); /* set M4 */
  241. sr32(&prcm_base->clksel_dss, 8, 5, PER_M3X2 + 1); /* set M3 */
  242. sr32(&prcm_base->clksel_dss, 8, 5, PER_M3X2); /* set M3 */
  243. sr32(&prcm_base->clksel3_pll, 0, 5, dpll_param_p->m2 + 1); /* set M2 */
  244. sr32(&prcm_base->clksel3_pll, 0, 5, dpll_param_p->m2); /* set M2 */
  245. /* Workaround end */
  246. sr32(&prcm_base->clksel2_pll, 8, 11, dpll_param_p->m); /* set m */
  247. sr32(&prcm_base->clksel2_pll, 0, 7, dpll_param_p->n); /* set n */
  248. sr32(&prcm_base->clken_pll, 20, 4, dpll_param_p->fsel); /* FREQSEL */
  249. sr32(&prcm_base->clken_pll, 16, 3, PLL_LOCK); /* lock mode */
  250. wait_on_value(ST_PERIPH_CLK, 2, &prcm_base->idlest_ckgen, LDELAY);
  251. /* Getting the base address to MPU DPLL param table */
  252. dpll_param_p = (dpll_param *) get_mpu_dpll_param();
  253. /* Moving it to the right sysclk and ES rev base */
  254. dpll_param_p = dpll_param_p + 3 * clk_index + sil_index;
  255. /* MPU DPLL (unlocked already) */
  256. /* Set M2 */
  257. sr32(&prcm_base->clksel2_pll_mpu, 0, 5, dpll_param_p->m2);
  258. /* Set M */
  259. sr32(&prcm_base->clksel1_pll_mpu, 8, 11, dpll_param_p->m);
  260. /* Set N */
  261. sr32(&prcm_base->clksel1_pll_mpu, 0, 7, dpll_param_p->n);
  262. /* FREQSEL */
  263. sr32(&prcm_base->clken_pll_mpu, 4, 4, dpll_param_p->fsel);
  264. /* lock mode */
  265. sr32(&prcm_base->clken_pll_mpu, 0, 3, PLL_LOCK);
  266. wait_on_value(ST_MPU_CLK, 1, &prcm_base->idlest_pll_mpu, LDELAY);
  267. /* Getting the base address to IVA DPLL param table */
  268. dpll_param_p = (dpll_param *) get_iva_dpll_param();
  269. /* Moving it to the right sysclk and ES rev base */
  270. dpll_param_p = dpll_param_p + 3 * clk_index + sil_index;
  271. /* IVA DPLL (set to 12*20=240MHz) */
  272. sr32(&prcm_base->clken_pll_iva2, 0, 3, PLL_STOP);
  273. wait_on_value(ST_IVA2_CLK, 0, &prcm_base->idlest_pll_iva2, LDELAY);
  274. /* set M2 */
  275. sr32(&prcm_base->clksel2_pll_iva2, 0, 5, dpll_param_p->m2);
  276. /* set M */
  277. sr32(&prcm_base->clksel1_pll_iva2, 8, 11, dpll_param_p->m);
  278. /* set N */
  279. sr32(&prcm_base->clksel1_pll_iva2, 0, 7, dpll_param_p->n);
  280. /* FREQSEL */
  281. sr32(&prcm_base->clken_pll_iva2, 4, 4, dpll_param_p->fsel);
  282. /* lock mode */
  283. sr32(&prcm_base->clken_pll_iva2, 0, 3, PLL_LOCK);
  284. wait_on_value(ST_IVA2_CLK, 1, &prcm_base->idlest_pll_iva2, LDELAY);
  285. /* Set up GPTimers to sys_clk source only */
  286. sr32(&prcm_base->clksel_per, 0, 8, 0xff);
  287. sr32(&prcm_base->clksel_wkup, 0, 1, 1);
  288. sdelay(5000);
  289. }
  290. /******************************************************************************
  291. * peripheral_enable() - Enable the clks & power for perifs (GPT2, UART1,...)
  292. *****************************************************************************/
  293. void per_clocks_enable(void)
  294. {
  295. prcm_t *prcm_base = (prcm_t *)PRCM_BASE;
  296. /* Enable GP2 timer. */
  297. sr32(&prcm_base->clksel_per, 0, 1, 0x1); /* GPT2 = sys clk */
  298. sr32(&prcm_base->iclken_per, 3, 1, 0x1); /* ICKen GPT2 */
  299. sr32(&prcm_base->fclken_per, 3, 1, 0x1); /* FCKen GPT2 */
  300. #ifdef CONFIG_SYS_NS16550
  301. /* Enable UART1 clocks */
  302. sr32(&prcm_base->fclken1_core, 13, 1, 0x1);
  303. sr32(&prcm_base->iclken1_core, 13, 1, 0x1);
  304. /* UART 3 Clocks */
  305. sr32(&prcm_base->fclken_per, 11, 1, 0x1);
  306. sr32(&prcm_base->iclken_per, 11, 1, 0x1);
  307. #endif
  308. #ifdef CONFIG_DRIVER_OMAP34XX_I2C
  309. /* Turn on all 3 I2C clocks */
  310. sr32(&prcm_base->fclken1_core, 15, 3, 0x7);
  311. sr32(&prcm_base->iclken1_core, 15, 3, 0x7); /* I2C1,2,3 = on */
  312. #endif
  313. /* Enable the ICLK for 32K Sync Timer as its used in udelay */
  314. sr32(&prcm_base->iclken_wkup, 2, 1, 0x1);
  315. sr32(&prcm_base->fclken_iva2, 0, 32, FCK_IVA2_ON);
  316. sr32(&prcm_base->fclken1_core, 0, 32, FCK_CORE1_ON);
  317. sr32(&prcm_base->iclken1_core, 0, 32, ICK_CORE1_ON);
  318. sr32(&prcm_base->iclken2_core, 0, 32, ICK_CORE2_ON);
  319. sr32(&prcm_base->fclken_wkup, 0, 32, FCK_WKUP_ON);
  320. sr32(&prcm_base->iclken_wkup, 0, 32, ICK_WKUP_ON);
  321. sr32(&prcm_base->fclken_dss, 0, 32, FCK_DSS_ON);
  322. sr32(&prcm_base->iclken_dss, 0, 32, ICK_DSS_ON);
  323. sr32(&prcm_base->fclken_cam, 0, 32, FCK_CAM_ON);
  324. sr32(&prcm_base->iclken_cam, 0, 32, ICK_CAM_ON);
  325. sr32(&prcm_base->fclken_per, 0, 32, FCK_PER_ON);
  326. sr32(&prcm_base->iclken_per, 0, 32, ICK_PER_ON);
  327. sdelay(1000);
  328. }