clock.c 6.4 KB

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