clock.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright (C) 2009 Samsung Electronics
  3. * Minkyu Kang <mk7.kang@samsung.com>
  4. * Heungjun Kim <riverful.kim@samsung.com>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <asm/io.h>
  10. #include <asm/arch/clock.h>
  11. #include <asm/arch/clk.h>
  12. #define CLK_M 0
  13. #define CLK_D 1
  14. #define CLK_P 2
  15. #ifndef CONFIG_SYS_CLK_FREQ_C100
  16. #define CONFIG_SYS_CLK_FREQ_C100 12000000
  17. #endif
  18. #ifndef CONFIG_SYS_CLK_FREQ_C110
  19. #define CONFIG_SYS_CLK_FREQ_C110 24000000
  20. #endif
  21. /* s5pc110: return pll clock frequency */
  22. static unsigned long s5pc100_get_pll_clk(int pllreg)
  23. {
  24. struct s5pc100_clock *clk =
  25. (struct s5pc100_clock *)samsung_get_base_clock();
  26. unsigned long r, m, p, s, mask, fout;
  27. unsigned int freq;
  28. switch (pllreg) {
  29. case APLL:
  30. r = readl(&clk->apll_con);
  31. break;
  32. case MPLL:
  33. r = readl(&clk->mpll_con);
  34. break;
  35. case EPLL:
  36. r = readl(&clk->epll_con);
  37. break;
  38. case HPLL:
  39. r = readl(&clk->hpll_con);
  40. break;
  41. default:
  42. printf("Unsupported PLL (%d)\n", pllreg);
  43. return 0;
  44. }
  45. /*
  46. * APLL_CON: MIDV [25:16]
  47. * MPLL_CON: MIDV [23:16]
  48. * EPLL_CON: MIDV [23:16]
  49. * HPLL_CON: MIDV [23:16]
  50. */
  51. if (pllreg == APLL)
  52. mask = 0x3ff;
  53. else
  54. mask = 0x0ff;
  55. m = (r >> 16) & mask;
  56. /* PDIV [13:8] */
  57. p = (r >> 8) & 0x3f;
  58. /* SDIV [2:0] */
  59. s = r & 0x7;
  60. /* FOUT = MDIV * FIN / (PDIV * 2^SDIV) */
  61. freq = CONFIG_SYS_CLK_FREQ_C100;
  62. fout = m * (freq / (p * (1 << s)));
  63. return fout;
  64. }
  65. /* s5pc100: return pll clock frequency */
  66. static unsigned long s5pc110_get_pll_clk(int pllreg)
  67. {
  68. struct s5pc110_clock *clk =
  69. (struct s5pc110_clock *)samsung_get_base_clock();
  70. unsigned long r, m, p, s, mask, fout;
  71. unsigned int freq;
  72. switch (pllreg) {
  73. case APLL:
  74. r = readl(&clk->apll_con);
  75. break;
  76. case MPLL:
  77. r = readl(&clk->mpll_con);
  78. break;
  79. case EPLL:
  80. r = readl(&clk->epll_con);
  81. break;
  82. case VPLL:
  83. r = readl(&clk->vpll_con);
  84. break;
  85. default:
  86. printf("Unsupported PLL (%d)\n", pllreg);
  87. return 0;
  88. }
  89. /*
  90. * APLL_CON: MIDV [25:16]
  91. * MPLL_CON: MIDV [25:16]
  92. * EPLL_CON: MIDV [24:16]
  93. * VPLL_CON: MIDV [24:16]
  94. */
  95. if (pllreg == APLL || pllreg == MPLL)
  96. mask = 0x3ff;
  97. else
  98. mask = 0x1ff;
  99. m = (r >> 16) & mask;
  100. /* PDIV [13:8] */
  101. p = (r >> 8) & 0x3f;
  102. /* SDIV [2:0] */
  103. s = r & 0x7;
  104. freq = CONFIG_SYS_CLK_FREQ_C110;
  105. if (pllreg == APLL) {
  106. if (s < 1)
  107. s = 1;
  108. /* FOUT = MDIV * FIN / (PDIV * 2^(SDIV - 1)) */
  109. fout = m * (freq / (p * (1 << (s - 1))));
  110. } else
  111. /* FOUT = MDIV * FIN / (PDIV * 2^SDIV) */
  112. fout = m * (freq / (p * (1 << s)));
  113. return fout;
  114. }
  115. /* s5pc110: return ARM clock frequency */
  116. static unsigned long s5pc110_get_arm_clk(void)
  117. {
  118. struct s5pc110_clock *clk =
  119. (struct s5pc110_clock *)samsung_get_base_clock();
  120. unsigned long div;
  121. unsigned long dout_apll, armclk;
  122. unsigned int apll_ratio;
  123. div = readl(&clk->div0);
  124. /* APLL_RATIO: [2:0] */
  125. apll_ratio = div & 0x7;
  126. dout_apll = get_pll_clk(APLL) / (apll_ratio + 1);
  127. armclk = dout_apll;
  128. return armclk;
  129. }
  130. /* s5pc100: return ARM clock frequency */
  131. static unsigned long s5pc100_get_arm_clk(void)
  132. {
  133. struct s5pc100_clock *clk =
  134. (struct s5pc100_clock *)samsung_get_base_clock();
  135. unsigned long div;
  136. unsigned long dout_apll, armclk;
  137. unsigned int apll_ratio, arm_ratio;
  138. div = readl(&clk->div0);
  139. /* ARM_RATIO: [6:4] */
  140. arm_ratio = (div >> 4) & 0x7;
  141. /* APLL_RATIO: [0] */
  142. apll_ratio = div & 0x1;
  143. dout_apll = get_pll_clk(APLL) / (apll_ratio + 1);
  144. armclk = dout_apll / (arm_ratio + 1);
  145. return armclk;
  146. }
  147. /* s5pc100: return HCLKD0 frequency */
  148. static unsigned long get_hclk(void)
  149. {
  150. struct s5pc100_clock *clk =
  151. (struct s5pc100_clock *)samsung_get_base_clock();
  152. unsigned long hclkd0;
  153. uint div, d0_bus_ratio;
  154. div = readl(&clk->div0);
  155. /* D0_BUS_RATIO: [10:8] */
  156. d0_bus_ratio = (div >> 8) & 0x7;
  157. hclkd0 = get_arm_clk() / (d0_bus_ratio + 1);
  158. return hclkd0;
  159. }
  160. /* s5pc100: return PCLKD1 frequency */
  161. static unsigned long get_pclkd1(void)
  162. {
  163. struct s5pc100_clock *clk =
  164. (struct s5pc100_clock *)samsung_get_base_clock();
  165. unsigned long d1_bus, pclkd1;
  166. uint div, d1_bus_ratio, pclkd1_ratio;
  167. div = readl(&clk->div0);
  168. /* D1_BUS_RATIO: [14:12] */
  169. d1_bus_ratio = (div >> 12) & 0x7;
  170. /* PCLKD1_RATIO: [18:16] */
  171. pclkd1_ratio = (div >> 16) & 0x7;
  172. /* ASYNC Mode */
  173. d1_bus = get_pll_clk(MPLL) / (d1_bus_ratio + 1);
  174. pclkd1 = d1_bus / (pclkd1_ratio + 1);
  175. return pclkd1;
  176. }
  177. /* s5pc110: return HCLKs frequency */
  178. static unsigned long get_hclk_sys(int dom)
  179. {
  180. struct s5pc110_clock *clk =
  181. (struct s5pc110_clock *)samsung_get_base_clock();
  182. unsigned long hclk;
  183. unsigned int div;
  184. unsigned int offset;
  185. unsigned int hclk_sys_ratio;
  186. if (dom == CLK_M)
  187. return get_hclk();
  188. div = readl(&clk->div0);
  189. /*
  190. * HCLK_MSYS_RATIO: [10:8]
  191. * HCLK_DSYS_RATIO: [19:16]
  192. * HCLK_PSYS_RATIO: [27:24]
  193. */
  194. offset = 8 + (dom << 0x3);
  195. hclk_sys_ratio = (div >> offset) & 0xf;
  196. hclk = get_pll_clk(MPLL) / (hclk_sys_ratio + 1);
  197. return hclk;
  198. }
  199. /* s5pc110: return PCLKs frequency */
  200. static unsigned long get_pclk_sys(int dom)
  201. {
  202. struct s5pc110_clock *clk =
  203. (struct s5pc110_clock *)samsung_get_base_clock();
  204. unsigned long pclk;
  205. unsigned int div;
  206. unsigned int offset;
  207. unsigned int pclk_sys_ratio;
  208. div = readl(&clk->div0);
  209. /*
  210. * PCLK_MSYS_RATIO: [14:12]
  211. * PCLK_DSYS_RATIO: [22:20]
  212. * PCLK_PSYS_RATIO: [30:28]
  213. */
  214. offset = 12 + (dom << 0x3);
  215. pclk_sys_ratio = (div >> offset) & 0x7;
  216. pclk = get_hclk_sys(dom) / (pclk_sys_ratio + 1);
  217. return pclk;
  218. }
  219. /* s5pc110: return peripheral clock frequency */
  220. static unsigned long s5pc110_get_pclk(void)
  221. {
  222. return get_pclk_sys(CLK_P);
  223. }
  224. /* s5pc100: return peripheral clock frequency */
  225. static unsigned long s5pc100_get_pclk(void)
  226. {
  227. return get_pclkd1();
  228. }
  229. /* s5pc1xx: return uart clock frequency */
  230. static unsigned long s5pc1xx_get_uart_clk(int dev_index)
  231. {
  232. if (cpu_is_s5pc110())
  233. return s5pc110_get_pclk();
  234. else
  235. return s5pc100_get_pclk();
  236. }
  237. /* s5pc1xx: return pwm clock frequency */
  238. static unsigned long s5pc1xx_get_pwm_clk(void)
  239. {
  240. if (cpu_is_s5pc110())
  241. return s5pc110_get_pclk();
  242. else
  243. return s5pc100_get_pclk();
  244. }
  245. unsigned long get_pll_clk(int pllreg)
  246. {
  247. if (cpu_is_s5pc110())
  248. return s5pc110_get_pll_clk(pllreg);
  249. else
  250. return s5pc100_get_pll_clk(pllreg);
  251. }
  252. unsigned long get_arm_clk(void)
  253. {
  254. if (cpu_is_s5pc110())
  255. return s5pc110_get_arm_clk();
  256. else
  257. return s5pc100_get_arm_clk();
  258. }
  259. unsigned long get_pwm_clk(void)
  260. {
  261. return s5pc1xx_get_pwm_clk();
  262. }
  263. unsigned long get_uart_clk(int dev_index)
  264. {
  265. return s5pc1xx_get_uart_clk(dev_index);
  266. }
  267. void set_mmc_clk(int dev_index, unsigned int div)
  268. {
  269. /* Do NOTHING */
  270. }