clock.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (C) 2010 Samsung Electronics
  3. * Minkyu Kang <mk7.kang@samsung.com>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <asm/io.h>
  25. #include <asm/arch/clock.h>
  26. #include <asm/arch/clk.h>
  27. #ifndef CONFIG_SYS_CLK_FREQ_C210
  28. #define CONFIG_SYS_CLK_FREQ_C210 24000000
  29. #endif
  30. /* exynos4: return pll clock frequency */
  31. static unsigned long exynos4_get_pll_clk(int pllreg)
  32. {
  33. struct exynos4_clock *clk =
  34. (struct exynos4_clock *)samsung_get_base_clock();
  35. unsigned long r, m, p, s, k = 0, mask, fout;
  36. unsigned int freq;
  37. switch (pllreg) {
  38. case APLL:
  39. r = readl(&clk->apll_con0);
  40. break;
  41. case MPLL:
  42. r = readl(&clk->mpll_con0);
  43. break;
  44. case EPLL:
  45. r = readl(&clk->epll_con0);
  46. k = readl(&clk->epll_con1);
  47. break;
  48. case VPLL:
  49. r = readl(&clk->vpll_con0);
  50. k = readl(&clk->vpll_con1);
  51. break;
  52. default:
  53. printf("Unsupported PLL (%d)\n", pllreg);
  54. return 0;
  55. }
  56. /*
  57. * APLL_CON: MIDV [25:16]
  58. * MPLL_CON: MIDV [25:16]
  59. * EPLL_CON: MIDV [24:16]
  60. * VPLL_CON: MIDV [24:16]
  61. */
  62. if (pllreg == APLL || pllreg == MPLL)
  63. mask = 0x3ff;
  64. else
  65. mask = 0x1ff;
  66. m = (r >> 16) & mask;
  67. /* PDIV [13:8] */
  68. p = (r >> 8) & 0x3f;
  69. /* SDIV [2:0] */
  70. s = r & 0x7;
  71. freq = CONFIG_SYS_CLK_FREQ_C210;
  72. if (pllreg == EPLL) {
  73. k = k & 0xffff;
  74. /* FOUT = (MDIV + K / 65536) * FIN / (PDIV * 2^SDIV) */
  75. fout = (m + k / 65536) * (freq / (p * (1 << s)));
  76. } else if (pllreg == VPLL) {
  77. k = k & 0xfff;
  78. /* FOUT = (MDIV + K / 1024) * FIN / (PDIV * 2^SDIV) */
  79. fout = (m + k / 1024) * (freq / (p * (1 << s)));
  80. } else {
  81. if (s < 1)
  82. s = 1;
  83. /* FOUT = MDIV * FIN / (PDIV * 2^(SDIV - 1)) */
  84. fout = m * (freq / (p * (1 << (s - 1))));
  85. }
  86. return fout;
  87. }
  88. /* exynos4: return ARM clock frequency */
  89. static unsigned long exynos4_get_arm_clk(void)
  90. {
  91. struct exynos4_clock *clk =
  92. (struct exynos4_clock *)samsung_get_base_clock();
  93. unsigned long div;
  94. unsigned long armclk;
  95. unsigned int core_ratio;
  96. unsigned int core2_ratio;
  97. div = readl(&clk->div_cpu0);
  98. /* CORE_RATIO: [2:0], CORE2_RATIO: [30:28] */
  99. core_ratio = (div >> 0) & 0x7;
  100. core2_ratio = (div >> 28) & 0x7;
  101. armclk = get_pll_clk(APLL) / (core_ratio + 1);
  102. armclk /= (core2_ratio + 1);
  103. return armclk;
  104. }
  105. /* exynos4: return pwm clock frequency */
  106. static unsigned long exynos4_get_pwm_clk(void)
  107. {
  108. struct exynos4_clock *clk =
  109. (struct exynos4_clock *)samsung_get_base_clock();
  110. unsigned long pclk, sclk;
  111. unsigned int sel;
  112. unsigned int ratio;
  113. if (s5p_get_cpu_rev() == 0) {
  114. /*
  115. * CLK_SRC_PERIL0
  116. * PWM_SEL [27:24]
  117. */
  118. sel = readl(&clk->src_peril0);
  119. sel = (sel >> 24) & 0xf;
  120. if (sel == 0x6)
  121. sclk = get_pll_clk(MPLL);
  122. else if (sel == 0x7)
  123. sclk = get_pll_clk(EPLL);
  124. else if (sel == 0x8)
  125. sclk = get_pll_clk(VPLL);
  126. else
  127. return 0;
  128. /*
  129. * CLK_DIV_PERIL3
  130. * PWM_RATIO [3:0]
  131. */
  132. ratio = readl(&clk->div_peril3);
  133. ratio = ratio & 0xf;
  134. } else if (s5p_get_cpu_rev() == 1) {
  135. sclk = get_pll_clk(MPLL);
  136. ratio = 8;
  137. } else
  138. return 0;
  139. pclk = sclk / (ratio + 1);
  140. return pclk;
  141. }
  142. /* exynos4: return uart clock frequency */
  143. static unsigned long exynos4_get_uart_clk(int dev_index)
  144. {
  145. struct exynos4_clock *clk =
  146. (struct exynos4_clock *)samsung_get_base_clock();
  147. unsigned long uclk, sclk;
  148. unsigned int sel;
  149. unsigned int ratio;
  150. /*
  151. * CLK_SRC_PERIL0
  152. * UART0_SEL [3:0]
  153. * UART1_SEL [7:4]
  154. * UART2_SEL [8:11]
  155. * UART3_SEL [12:15]
  156. * UART4_SEL [16:19]
  157. * UART5_SEL [23:20]
  158. */
  159. sel = readl(&clk->src_peril0);
  160. sel = (sel >> (dev_index << 2)) & 0xf;
  161. if (sel == 0x6)
  162. sclk = get_pll_clk(MPLL);
  163. else if (sel == 0x7)
  164. sclk = get_pll_clk(EPLL);
  165. else if (sel == 0x8)
  166. sclk = get_pll_clk(VPLL);
  167. else
  168. return 0;
  169. /*
  170. * CLK_DIV_PERIL0
  171. * UART0_RATIO [3:0]
  172. * UART1_RATIO [7:4]
  173. * UART2_RATIO [8:11]
  174. * UART3_RATIO [12:15]
  175. * UART4_RATIO [16:19]
  176. * UART5_RATIO [23:20]
  177. */
  178. ratio = readl(&clk->div_peril0);
  179. ratio = (ratio >> (dev_index << 2)) & 0xf;
  180. uclk = sclk / (ratio + 1);
  181. return uclk;
  182. }
  183. /* exynos4: set the mmc clock */
  184. static void exynos4_set_mmc_clk(int dev_index, unsigned int div)
  185. {
  186. struct exynos4_clock *clk =
  187. (struct exynos4_clock *)samsung_get_base_clock();
  188. unsigned int addr;
  189. unsigned int val;
  190. /*
  191. * CLK_DIV_FSYS1
  192. * MMC0_PRE_RATIO [15:8], MMC1_PRE_RATIO [31:24]
  193. * CLK_DIV_FSYS2
  194. * MMC2_PRE_RATIO [15:8], MMC3_PRE_RATIO [31:24]
  195. */
  196. if (dev_index < 2) {
  197. addr = (unsigned int)&clk->div_fsys1;
  198. } else {
  199. addr = (unsigned int)&clk->div_fsys2;
  200. dev_index -= 2;
  201. }
  202. val = readl(addr);
  203. val &= ~(0xff << ((dev_index << 4) + 8));
  204. val |= (div & 0xff) << ((dev_index << 4) + 8);
  205. writel(val, addr);
  206. }
  207. unsigned long get_pll_clk(int pllreg)
  208. {
  209. return exynos4_get_pll_clk(pllreg);
  210. }
  211. unsigned long get_arm_clk(void)
  212. {
  213. return exynos4_get_arm_clk();
  214. }
  215. unsigned long get_pwm_clk(void)
  216. {
  217. return exynos4_get_pwm_clk();
  218. }
  219. unsigned long get_uart_clk(int dev_index)
  220. {
  221. return exynos4_get_uart_clk(dev_index);
  222. }
  223. void set_mmc_clk(int dev_index, unsigned int div)
  224. {
  225. exynos4_set_mmc_clk(dev_index, div);
  226. }