clock.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. /* s5pc210: return pll clock frequency */
  31. static unsigned long s5pc210_get_pll_clk(int pllreg)
  32. {
  33. struct s5pc210_clock *clk =
  34. (struct s5pc210_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. /* s5pc210: return ARM clock frequency */
  89. static unsigned long s5pc210_get_arm_clk(void)
  90. {
  91. struct s5pc210_clock *clk =
  92. (struct s5pc210_clock *)samsung_get_base_clock();
  93. unsigned long div;
  94. unsigned long dout_apll;
  95. unsigned int apll_ratio;
  96. div = readl(&clk->div_cpu0);
  97. /* APLL_RATIO: [26:24] */
  98. apll_ratio = (div >> 24) & 0x7;
  99. dout_apll = get_pll_clk(APLL) / (apll_ratio + 1);
  100. return dout_apll;
  101. }
  102. /* s5pc210: return pwm clock frequency */
  103. static unsigned long s5pc210_get_pwm_clk(void)
  104. {
  105. struct s5pc210_clock *clk =
  106. (struct s5pc210_clock *)samsung_get_base_clock();
  107. unsigned long pclk, sclk;
  108. unsigned int sel;
  109. unsigned int ratio;
  110. /*
  111. * CLK_SRC_PERIL0
  112. * PWM_SEL [27:24]
  113. */
  114. sel = readl(&clk->src_peril0);
  115. sel = (sel >> 24) & 0xf;
  116. if (sel == 0x6)
  117. sclk = get_pll_clk(MPLL);
  118. else if (sel == 0x7)
  119. sclk = get_pll_clk(EPLL);
  120. else if (sel == 0x8)
  121. sclk = get_pll_clk(VPLL);
  122. else
  123. return 0;
  124. /*
  125. * CLK_DIV_PERIL3
  126. * PWM_RATIO [3:0]
  127. */
  128. ratio = readl(&clk->div_peril3);
  129. ratio = ratio & 0xf;
  130. pclk = sclk / (ratio + 1);
  131. return pclk;
  132. }
  133. /* s5pc210: return uart clock frequency */
  134. static unsigned long s5pc210_get_uart_clk(int dev_index)
  135. {
  136. struct s5pc210_clock *clk =
  137. (struct s5pc210_clock *)samsung_get_base_clock();
  138. unsigned long uclk, sclk;
  139. unsigned int sel;
  140. unsigned int ratio;
  141. /*
  142. * CLK_SRC_PERIL0
  143. * UART0_SEL [3:0]
  144. * UART1_SEL [7:4]
  145. * UART2_SEL [8:11]
  146. * UART3_SEL [12:15]
  147. * UART4_SEL [16:19]
  148. * UART5_SEL [23:20]
  149. */
  150. sel = readl(&clk->src_peril0);
  151. sel = (sel >> (dev_index << 2)) & 0xf;
  152. if (sel == 0x6)
  153. sclk = get_pll_clk(MPLL);
  154. else if (sel == 0x7)
  155. sclk = get_pll_clk(EPLL);
  156. else if (sel == 0x8)
  157. sclk = get_pll_clk(VPLL);
  158. else
  159. return 0;
  160. /*
  161. * CLK_DIV_PERIL0
  162. * UART0_RATIO [3:0]
  163. * UART1_RATIO [7:4]
  164. * UART2_RATIO [8:11]
  165. * UART3_RATIO [12:15]
  166. * UART4_RATIO [16:19]
  167. * UART5_RATIO [23:20]
  168. */
  169. ratio = readl(&clk->div_peril0);
  170. ratio = (ratio >> (dev_index << 2)) & 0xf;
  171. uclk = sclk / (ratio + 1);
  172. return uclk;
  173. }
  174. unsigned long get_pll_clk(int pllreg)
  175. {
  176. return s5pc210_get_pll_clk(pllreg);
  177. }
  178. unsigned long get_arm_clk(void)
  179. {
  180. return s5pc210_get_arm_clk();
  181. }
  182. unsigned long get_pwm_clk(void)
  183. {
  184. return s5pc210_get_pwm_clk();
  185. }
  186. unsigned long get_uart_clk(int dev_index)
  187. {
  188. return s5pc210_get_uart_clk(dev_index);
  189. }