2 * Copyright (C) 2009 Samsung Electronics
3 * Minkyu Kang <mk7.kang@samsung.com>
4 * Heungjun Kim <riverful.kim@samsung.com>
6 * SPDX-License-Identifier: GPL-2.0+
11 #include <asm/arch/clock.h>
12 #include <asm/arch/clk.h>
18 #ifndef CONFIG_SYS_CLK_FREQ_C100
19 #define CONFIG_SYS_CLK_FREQ_C100 12000000
21 #ifndef CONFIG_SYS_CLK_FREQ_C110
22 #define CONFIG_SYS_CLK_FREQ_C110 24000000
25 /* s5pc110: return pll clock frequency */
26 static unsigned long s5pc100_get_pll_clk(int pllreg)
28 struct s5pc100_clock *clk =
29 (struct s5pc100_clock *)samsung_get_base_clock();
30 unsigned long r, m, p, s, mask, fout;
35 r = readl(&clk->apll_con);
38 r = readl(&clk->mpll_con);
41 r = readl(&clk->epll_con);
44 r = readl(&clk->hpll_con);
47 printf("Unsupported PLL (%d)\n", pllreg);
52 * APLL_CON: MIDV [25:16]
53 * MPLL_CON: MIDV [23:16]
54 * EPLL_CON: MIDV [23:16]
55 * HPLL_CON: MIDV [23:16]
69 /* FOUT = MDIV * FIN / (PDIV * 2^SDIV) */
70 freq = CONFIG_SYS_CLK_FREQ_C100;
71 fout = m * (freq / (p * (1 << s)));
76 /* s5pc100: return pll clock frequency */
77 static unsigned long s5pc110_get_pll_clk(int pllreg)
79 struct s5pc110_clock *clk =
80 (struct s5pc110_clock *)samsung_get_base_clock();
81 unsigned long r, m, p, s, mask, fout;
86 r = readl(&clk->apll_con);
89 r = readl(&clk->mpll_con);
92 r = readl(&clk->epll_con);
95 r = readl(&clk->vpll_con);
98 printf("Unsupported PLL (%d)\n", pllreg);
103 * APLL_CON: MIDV [25:16]
104 * MPLL_CON: MIDV [25:16]
105 * EPLL_CON: MIDV [24:16]
106 * VPLL_CON: MIDV [24:16]
108 if (pllreg == APLL || pllreg == MPLL)
113 m = (r >> 16) & mask;
120 freq = CONFIG_SYS_CLK_FREQ_C110;
121 if (pllreg == APLL) {
124 /* FOUT = MDIV * FIN / (PDIV * 2^(SDIV - 1)) */
125 fout = m * (freq / (p * (1 << (s - 1))));
127 /* FOUT = MDIV * FIN / (PDIV * 2^SDIV) */
128 fout = m * (freq / (p * (1 << s)));
133 /* s5pc110: return ARM clock frequency */
134 static unsigned long s5pc110_get_arm_clk(void)
136 struct s5pc110_clock *clk =
137 (struct s5pc110_clock *)samsung_get_base_clock();
139 unsigned long dout_apll, armclk;
140 unsigned int apll_ratio;
142 div = readl(&clk->div0);
144 /* APLL_RATIO: [2:0] */
145 apll_ratio = div & 0x7;
147 dout_apll = get_pll_clk(APLL) / (apll_ratio + 1);
153 /* s5pc100: return ARM clock frequency */
154 static unsigned long s5pc100_get_arm_clk(void)
156 struct s5pc100_clock *clk =
157 (struct s5pc100_clock *)samsung_get_base_clock();
159 unsigned long dout_apll, armclk;
160 unsigned int apll_ratio, arm_ratio;
162 div = readl(&clk->div0);
164 /* ARM_RATIO: [6:4] */
165 arm_ratio = (div >> 4) & 0x7;
166 /* APLL_RATIO: [0] */
167 apll_ratio = div & 0x1;
169 dout_apll = get_pll_clk(APLL) / (apll_ratio + 1);
170 armclk = dout_apll / (arm_ratio + 1);
175 /* s5pc100: return HCLKD0 frequency */
176 static unsigned long get_hclk(void)
178 struct s5pc100_clock *clk =
179 (struct s5pc100_clock *)samsung_get_base_clock();
180 unsigned long hclkd0;
181 uint div, d0_bus_ratio;
183 div = readl(&clk->div0);
184 /* D0_BUS_RATIO: [10:8] */
185 d0_bus_ratio = (div >> 8) & 0x7;
187 hclkd0 = get_arm_clk() / (d0_bus_ratio + 1);
192 /* s5pc100: return PCLKD1 frequency */
193 static unsigned long get_pclkd1(void)
195 struct s5pc100_clock *clk =
196 (struct s5pc100_clock *)samsung_get_base_clock();
197 unsigned long d1_bus, pclkd1;
198 uint div, d1_bus_ratio, pclkd1_ratio;
200 div = readl(&clk->div0);
201 /* D1_BUS_RATIO: [14:12] */
202 d1_bus_ratio = (div >> 12) & 0x7;
203 /* PCLKD1_RATIO: [18:16] */
204 pclkd1_ratio = (div >> 16) & 0x7;
207 d1_bus = get_pll_clk(MPLL) / (d1_bus_ratio + 1);
208 pclkd1 = d1_bus / (pclkd1_ratio + 1);
213 /* s5pc110: return HCLKs frequency */
214 static unsigned long get_hclk_sys(int dom)
216 struct s5pc110_clock *clk =
217 (struct s5pc110_clock *)samsung_get_base_clock();
221 unsigned int hclk_sys_ratio;
226 div = readl(&clk->div0);
229 * HCLK_MSYS_RATIO: [10:8]
230 * HCLK_DSYS_RATIO: [19:16]
231 * HCLK_PSYS_RATIO: [27:24]
233 offset = 8 + (dom << 0x3);
235 hclk_sys_ratio = (div >> offset) & 0xf;
237 hclk = get_pll_clk(MPLL) / (hclk_sys_ratio + 1);
242 /* s5pc110: return PCLKs frequency */
243 static unsigned long get_pclk_sys(int dom)
245 struct s5pc110_clock *clk =
246 (struct s5pc110_clock *)samsung_get_base_clock();
250 unsigned int pclk_sys_ratio;
252 div = readl(&clk->div0);
255 * PCLK_MSYS_RATIO: [14:12]
256 * PCLK_DSYS_RATIO: [22:20]
257 * PCLK_PSYS_RATIO: [30:28]
259 offset = 12 + (dom << 0x3);
261 pclk_sys_ratio = (div >> offset) & 0x7;
263 pclk = get_hclk_sys(dom) / (pclk_sys_ratio + 1);
268 /* s5pc110: return peripheral clock frequency */
269 static unsigned long s5pc110_get_pclk(void)
271 return get_pclk_sys(CLK_P);
274 /* s5pc100: return peripheral clock frequency */
275 static unsigned long s5pc100_get_pclk(void)
280 /* s5pc1xx: return uart clock frequency */
281 static unsigned long s5pc1xx_get_uart_clk(int dev_index)
283 if (cpu_is_s5pc110())
284 return s5pc110_get_pclk();
286 return s5pc100_get_pclk();
289 /* s5pc1xx: return pwm clock frequency */
290 static unsigned long s5pc1xx_get_pwm_clk(void)
292 if (cpu_is_s5pc110())
293 return s5pc110_get_pclk();
295 return s5pc100_get_pclk();
298 unsigned long get_pll_clk(int pllreg)
300 if (cpu_is_s5pc110())
301 return s5pc110_get_pll_clk(pllreg);
303 return s5pc100_get_pll_clk(pllreg);
306 unsigned long get_arm_clk(void)
308 if (cpu_is_s5pc110())
309 return s5pc110_get_arm_clk();
311 return s5pc100_get_arm_clk();
314 unsigned long get_pwm_clk(void)
316 return s5pc1xx_get_pwm_clk();
319 unsigned long get_uart_clk(int dev_index)
321 return s5pc1xx_get_uart_clk(dev_index);
324 void set_mmc_clk(int dev_index, unsigned int div)