clock.c 13 KB

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