blob: 2984867290774ed85cefba526843903d8e07ee8d [file] [log] [blame]
Minkyu Kangb1b24682011-01-24 15:22:23 +09001/*
2 * Copyright (C) 2010 Samsung Electronics
3 * Minkyu Kang <mk7.kang@samsung.com>
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Minkyu Kangb1b24682011-01-24 15:22:23 +09006 */
7
8#include <common.h>
9#include <asm/io.h>
10#include <asm/arch/clock.h>
11#include <asm/arch/clk.h>
Hatim RVe6365b62012-11-02 01:15:34 +000012#include <asm/arch/periph.h>
Minkyu Kangb1b24682011-01-24 15:22:23 +090013
Minkyu Kang368588e2013-07-05 19:08:33 +090014#define PLL_DIV_1024 1024
15#define PLL_DIV_65535 65535
16#define PLL_DIV_65536 65536
17
Padmavathi Venna37feb7b2013-03-28 04:32:21 +000018/* *
19 * This structure is to store the src bit, div bit and prediv bit
20 * positions of the peripheral clocks of the src and div registers
21 */
22struct clk_bit_info {
Akshay Saraswat3ad7ffc2015-02-04 15:59:59 +053023 enum periph_id id;
Akshay Saraswatd932c362015-02-04 16:00:04 +053024 int32_t src_mask;
25 int32_t div_mask;
26 int32_t prediv_mask;
Padmavathi Venna37feb7b2013-03-28 04:32:21 +000027 int8_t src_bit;
28 int8_t div_bit;
29 int8_t prediv_bit;
30};
31
Akshay Saraswatd5072262015-02-04 16:00:01 +053032static struct clk_bit_info exynos5_bit_info[] = {
Akshay Saraswatd932c362015-02-04 16:00:04 +053033 /* periph id s_mask d_mask p_mask s_bit d_bit p_bit */
34 {PERIPH_ID_UART0, 0xf, 0xf, -1, 0, 0, -1},
35 {PERIPH_ID_UART1, 0xf, 0xf, -1, 4, 4, -1},
36 {PERIPH_ID_UART2, 0xf, 0xf, -1, 8, 8, -1},
37 {PERIPH_ID_UART3, 0xf, 0xf, -1, 12, 12, -1},
38 {PERIPH_ID_I2C0, -1, 0x7, 0x7, -1, 24, 0},
39 {PERIPH_ID_I2C1, -1, 0x7, 0x7, -1, 24, 0},
40 {PERIPH_ID_I2C2, -1, 0x7, 0x7, -1, 24, 0},
41 {PERIPH_ID_I2C3, -1, 0x7, 0x7, -1, 24, 0},
42 {PERIPH_ID_I2C4, -1, 0x7, 0x7, -1, 24, 0},
43 {PERIPH_ID_I2C5, -1, 0x7, 0x7, -1, 24, 0},
44 {PERIPH_ID_I2C6, -1, 0x7, 0x7, -1, 24, 0},
45 {PERIPH_ID_I2C7, -1, 0x7, 0x7, -1, 24, 0},
46 {PERIPH_ID_SPI0, 0xf, 0xf, 0xff, 16, 0, 8},
47 {PERIPH_ID_SPI1, 0xf, 0xf, 0xff, 20, 16, 24},
48 {PERIPH_ID_SPI2, 0xf, 0xf, 0xff, 24, 0, 8},
49 {PERIPH_ID_SDMMC0, 0xf, 0xf, 0xff, 0, 0, 8},
50 {PERIPH_ID_SDMMC1, 0xf, 0xf, 0xff, 4, 16, 24},
51 {PERIPH_ID_SDMMC2, 0xf, 0xf, 0xff, 8, 0, 8},
52 {PERIPH_ID_SDMMC3, 0xf, 0xf, 0xff, 12, 16, 24},
53 {PERIPH_ID_I2S0, 0xf, 0xf, 0xff, 0, 0, 4},
54 {PERIPH_ID_I2S1, 0xf, 0xf, 0xff, 4, 12, 16},
55 {PERIPH_ID_SPI3, 0xf, 0xf, 0xff, 0, 0, 4},
56 {PERIPH_ID_SPI4, 0xf, 0xf, 0xff, 4, 12, 16},
57 {PERIPH_ID_SDMMC4, 0xf, 0xf, 0xff, 16, 0, 8},
58 {PERIPH_ID_PWM0, 0xf, 0xf, -1, 24, 0, -1},
59 {PERIPH_ID_PWM1, 0xf, 0xf, -1, 24, 0, -1},
60 {PERIPH_ID_PWM2, 0xf, 0xf, -1, 24, 0, -1},
61 {PERIPH_ID_PWM3, 0xf, 0xf, -1, 24, 0, -1},
62 {PERIPH_ID_PWM4, 0xf, 0xf, -1, 24, 0, -1},
Akshay Saraswat3ad7ffc2015-02-04 15:59:59 +053063
Akshay Saraswatd932c362015-02-04 16:00:04 +053064 {PERIPH_ID_NONE, -1, -1, -1, -1, -1, -1},
Padmavathi Venna37feb7b2013-03-28 04:32:21 +000065};
66
Akshay Saraswatd5072262015-02-04 16:00:01 +053067static struct clk_bit_info exynos542x_bit_info[] = {
Akshay Saraswatd932c362015-02-04 16:00:04 +053068 /* periph id s_mask d_mask p_mask s_bit d_bit p_bit */
69 {PERIPH_ID_UART0, 0xf, 0xf, -1, 4, 8, -1},
70 {PERIPH_ID_UART1, 0xf, 0xf, -1, 8, 12, -1},
71 {PERIPH_ID_UART2, 0xf, 0xf, -1, 12, 16, -1},
72 {PERIPH_ID_UART3, 0xf, 0xf, -1, 16, 20, -1},
73 {PERIPH_ID_I2C0, -1, 0x3f, -1, -1, 8, -1},
74 {PERIPH_ID_I2C1, -1, 0x3f, -1, -1, 8, -1},
75 {PERIPH_ID_I2C2, -1, 0x3f, -1, -1, 8, -1},
76 {PERIPH_ID_I2C3, -1, 0x3f, -1, -1, 8, -1},
77 {PERIPH_ID_I2C4, -1, 0x3f, -1, -1, 8, -1},
78 {PERIPH_ID_I2C5, -1, 0x3f, -1, -1, 8, -1},
79 {PERIPH_ID_I2C6, -1, 0x3f, -1, -1, 8, -1},
80 {PERIPH_ID_I2C7, -1, 0x3f, -1, -1, 8, -1},
81 {PERIPH_ID_SPI0, 0xf, 0xf, 0xff, 20, 20, 8},
82 {PERIPH_ID_SPI1, 0xf, 0xf, 0xff, 24, 24, 16},
83 {PERIPH_ID_SPI2, 0xf, 0xf, 0xff, 28, 28, 24},
84 {PERIPH_ID_SDMMC0, 0x7, 0x3ff, -1, 8, 0, -1},
85 {PERIPH_ID_SDMMC1, 0x7, 0x3ff, -1, 12, 10, -1},
86 {PERIPH_ID_SDMMC2, 0x7, 0x3ff, -1, 16, 20, -1},
87 {PERIPH_ID_I2C8, -1, 0x3f, -1, -1, 8, -1},
88 {PERIPH_ID_I2C9, -1, 0x3f, -1, -1, 8, -1},
89 {PERIPH_ID_I2S0, 0xf, 0xf, 0xff, 0, 0, 4},
90 {PERIPH_ID_I2S1, 0xf, 0xf, 0xff, 4, 12, 16},
91 {PERIPH_ID_SPI3, 0xf, 0xf, 0xff, 12, 16, 0},
92 {PERIPH_ID_SPI4, 0xf, 0xf, 0xff, 16, 20, 8},
93 {PERIPH_ID_PWM0, 0xf, 0xf, -1, 24, 28, -1},
94 {PERIPH_ID_PWM1, 0xf, 0xf, -1, 24, 28, -1},
95 {PERIPH_ID_PWM2, 0xf, 0xf, -1, 24, 28, -1},
96 {PERIPH_ID_PWM3, 0xf, 0xf, -1, 24, 28, -1},
97 {PERIPH_ID_PWM4, 0xf, 0xf, -1, 24, 28, -1},
98 {PERIPH_ID_I2C10, -1, 0x3f, -1, -1, 8, -1},
Akshay Saraswatd5072262015-02-04 16:00:01 +053099
Akshay Saraswatd932c362015-02-04 16:00:04 +0530100 {PERIPH_ID_NONE, -1, -1, -1, -1, -1, -1},
Akshay Saraswatd5072262015-02-04 16:00:01 +0530101};
102
Rajeshwari Shinde392a73a2012-10-25 19:49:29 +0000103/* Epll Clock division values to achive different frequency output */
104static struct set_epll_con_val exynos5_epll_div[] = {
105 { 192000000, 0, 48, 3, 1, 0 },
106 { 180000000, 0, 45, 3, 1, 0 },
107 { 73728000, 1, 73, 3, 3, 47710 },
108 { 67737600, 1, 90, 4, 3, 20762 },
109 { 49152000, 0, 49, 3, 3, 9961 },
110 { 45158400, 0, 45, 3, 3, 10381 },
111 { 180633600, 0, 45, 3, 1, 10381 }
112};
113
Minkyu Kang1a055aa2012-10-15 01:58:00 +0000114/* exynos: return pll clock frequency */
115static int exynos_get_pll_clk(int pllreg, unsigned int r, unsigned int k)
Minkyu Kangb1b24682011-01-24 15:22:23 +0900116{
Minkyu Kang1a055aa2012-10-15 01:58:00 +0000117 unsigned long m, p, s = 0, mask, fout;
Minkyu Kang368588e2013-07-05 19:08:33 +0900118 unsigned int div;
Minkyu Kangb1b24682011-01-24 15:22:23 +0900119 unsigned int freq;
Minkyu Kangb1b24682011-01-24 15:22:23 +0900120 /*
121 * APLL_CON: MIDV [25:16]
122 * MPLL_CON: MIDV [25:16]
123 * EPLL_CON: MIDV [24:16]
124 * VPLL_CON: MIDV [24:16]
Minkyu Kang1a055aa2012-10-15 01:58:00 +0000125 * BPLL_CON: MIDV [25:16]: Exynos5
Minkyu Kangb1b24682011-01-24 15:22:23 +0900126 */
Ajay Kumar914af872014-09-05 16:53:32 +0530127 if (pllreg == APLL || pllreg == MPLL || pllreg == BPLL ||
128 pllreg == SPLL)
Minkyu Kangb1b24682011-01-24 15:22:23 +0900129 mask = 0x3ff;
130 else
131 mask = 0x1ff;
132
133 m = (r >> 16) & mask;
134
135 /* PDIV [13:8] */
136 p = (r >> 8) & 0x3f;
137 /* SDIV [2:0] */
138 s = r & 0x7;
139
Chander Kashyap6a870e12012-02-05 23:01:45 +0000140 freq = CONFIG_SYS_CLK_FREQ;
Minkyu Kangb1b24682011-01-24 15:22:23 +0900141
Rajeshwari Birjeac892d02013-12-26 09:44:21 +0530142 if (pllreg == EPLL || pllreg == RPLL) {
Minkyu Kangb1b24682011-01-24 15:22:23 +0900143 k = k & 0xffff;
144 /* FOUT = (MDIV + K / 65536) * FIN / (PDIV * 2^SDIV) */
Minkyu Kang368588e2013-07-05 19:08:33 +0900145 fout = (m + k / PLL_DIV_65536) * (freq / (p * (1 << s)));
Minkyu Kangb1b24682011-01-24 15:22:23 +0900146 } else if (pllreg == VPLL) {
147 k = k & 0xfff;
Minkyu Kang368588e2013-07-05 19:08:33 +0900148
149 /*
150 * Exynos4210
151 * FOUT = (MDIV + K / 1024) * FIN / (PDIV * 2^SDIV)
152 *
153 * Exynos4412
154 * FOUT = (MDIV + K / 65535) * FIN / (PDIV * 2^SDIV)
155 *
156 * Exynos5250
157 * FOUT = (MDIV + K / 65536) * FIN / (PDIV * 2^SDIV)
158 */
159 if (proid_is_exynos4210())
160 div = PLL_DIV_1024;
161 else if (proid_is_exynos4412())
162 div = PLL_DIV_65535;
Akshay Saraswat9fba7b42014-11-13 22:38:15 +0530163 else if (proid_is_exynos5250() || proid_is_exynos5420()
164 || proid_is_exynos5800())
Minkyu Kang368588e2013-07-05 19:08:33 +0900165 div = PLL_DIV_65536;
166 else
167 return 0;
168
169 fout = (m + k / div) * (freq / (p * (1 << s)));
Minkyu Kangb1b24682011-01-24 15:22:23 +0900170 } else {
Minkyu Kang368588e2013-07-05 19:08:33 +0900171 /*
Łukasz Majewski326c4592013-07-12 19:08:25 +0200172 * Exynos4412 / Exynos5250
Minkyu Kang368588e2013-07-05 19:08:33 +0900173 * FOUT = MDIV * FIN / (PDIV * 2^SDIV)
174 *
Łukasz Majewski326c4592013-07-12 19:08:25 +0200175 * Exynos4210
Minkyu Kang368588e2013-07-05 19:08:33 +0900176 * FOUT = MDIV * FIN / (PDIV * 2^(SDIV-1))
177 */
178 if (proid_is_exynos4210())
Minkyu Kang368588e2013-07-05 19:08:33 +0900179 fout = m * (freq / (p * (1 << (s - 1))));
Łukasz Majewski326c4592013-07-12 19:08:25 +0200180 else
181 fout = m * (freq / (p * (1 << s)));
Minkyu Kangb1b24682011-01-24 15:22:23 +0900182 }
Minkyu Kangb1b24682011-01-24 15:22:23 +0900183 return fout;
184}
185
Minkyu Kang1a055aa2012-10-15 01:58:00 +0000186/* exynos4: return pll clock frequency */
187static unsigned long exynos4_get_pll_clk(int pllreg)
188{
189 struct exynos4_clock *clk =
190 (struct exynos4_clock *)samsung_get_base_clock();
191 unsigned long r, k = 0;
192
193 switch (pllreg) {
194 case APLL:
195 r = readl(&clk->apll_con0);
196 break;
197 case MPLL:
198 r = readl(&clk->mpll_con0);
199 break;
200 case EPLL:
201 r = readl(&clk->epll_con0);
202 k = readl(&clk->epll_con1);
203 break;
204 case VPLL:
205 r = readl(&clk->vpll_con0);
206 k = readl(&clk->vpll_con1);
207 break;
208 default:
209 printf("Unsupported PLL (%d)\n", pllreg);
210 return 0;
211 }
212
213 return exynos_get_pll_clk(pllreg, r, k);
214}
215
Chander Kashyap400ab162012-10-07 01:43:17 +0000216/* exynos4x12: return pll clock frequency */
217static unsigned long exynos4x12_get_pll_clk(int pllreg)
218{
219 struct exynos4x12_clock *clk =
220 (struct exynos4x12_clock *)samsung_get_base_clock();
221 unsigned long r, k = 0;
222
223 switch (pllreg) {
224 case APLL:
225 r = readl(&clk->apll_con0);
226 break;
227 case MPLL:
228 r = readl(&clk->mpll_con0);
229 break;
230 case EPLL:
231 r = readl(&clk->epll_con0);
232 k = readl(&clk->epll_con1);
233 break;
234 case VPLL:
235 r = readl(&clk->vpll_con0);
236 k = readl(&clk->vpll_con1);
237 break;
238 default:
239 printf("Unsupported PLL (%d)\n", pllreg);
240 return 0;
241 }
242
243 return exynos_get_pll_clk(pllreg, r, k);
244}
245
Chander Kashyap34076a02012-02-05 23:01:46 +0000246/* exynos5: return pll clock frequency */
247static unsigned long exynos5_get_pll_clk(int pllreg)
248{
249 struct exynos5_clock *clk =
250 (struct exynos5_clock *)samsung_get_base_clock();
Minkyu Kang1a055aa2012-10-15 01:58:00 +0000251 unsigned long r, k = 0, fout;
252 unsigned int pll_div2_sel, fout_sel;
Chander Kashyap34076a02012-02-05 23:01:46 +0000253
254 switch (pllreg) {
255 case APLL:
256 r = readl(&clk->apll_con0);
257 break;
258 case MPLL:
259 r = readl(&clk->mpll_con0);
260 break;
261 case EPLL:
262 r = readl(&clk->epll_con0);
263 k = readl(&clk->epll_con1);
264 break;
265 case VPLL:
266 r = readl(&clk->vpll_con0);
267 k = readl(&clk->vpll_con1);
268 break;
Rajeshwari Shinde84112862012-07-03 20:02:58 +0000269 case BPLL:
270 r = readl(&clk->bpll_con0);
271 break;
Chander Kashyap34076a02012-02-05 23:01:46 +0000272 default:
273 printf("Unsupported PLL (%d)\n", pllreg);
274 return 0;
275 }
276
Minkyu Kang1a055aa2012-10-15 01:58:00 +0000277 fout = exynos_get_pll_clk(pllreg, r, k);
Chander Kashyap34076a02012-02-05 23:01:46 +0000278
Rajeshwari Shinde84112862012-07-03 20:02:58 +0000279 /* According to the user manual, in EVT1 MPLL and BPLL always gives
Rajeshwari Shinde7b9afce2012-07-03 20:02:57 +0000280 * 1.6GHz clock, so divide by 2 to get 800MHz MPLL clock.*/
Rajeshwari Shinde84112862012-07-03 20:02:58 +0000281 if (pllreg == MPLL || pllreg == BPLL) {
Rajeshwari Shinde7b9afce2012-07-03 20:02:57 +0000282 pll_div2_sel = readl(&clk->pll_div2_sel);
Rajeshwari Shinde84112862012-07-03 20:02:58 +0000283
284 switch (pllreg) {
285 case MPLL:
286 fout_sel = (pll_div2_sel >> MPLL_FOUT_SEL_SHIFT)
287 & MPLL_FOUT_SEL_MASK;
288 break;
289 case BPLL:
290 fout_sel = (pll_div2_sel >> BPLL_FOUT_SEL_SHIFT)
291 & BPLL_FOUT_SEL_MASK;
292 break;
Jaehoon Chung0fc779c2012-07-09 21:20:34 +0000293 default:
294 fout_sel = -1;
295 break;
Rajeshwari Shinde84112862012-07-03 20:02:58 +0000296 }
297
298 if (fout_sel == 0)
Rajeshwari Shinde7b9afce2012-07-03 20:02:57 +0000299 fout /= 2;
300 }
301
Chander Kashyap34076a02012-02-05 23:01:46 +0000302 return fout;
303}
304
Akshay Saraswat1bd40e42015-02-04 16:00:00 +0530305/* exynos542x: return pll clock frequency */
306static unsigned long exynos542x_get_pll_clk(int pllreg)
307{
308 struct exynos5420_clock *clk =
309 (struct exynos5420_clock *)samsung_get_base_clock();
310 unsigned long r, k = 0;
311
312 switch (pllreg) {
313 case APLL:
314 r = readl(&clk->apll_con0);
315 break;
316 case MPLL:
317 r = readl(&clk->mpll_con0);
318 break;
319 case EPLL:
320 r = readl(&clk->epll_con0);
321 k = readl(&clk->epll_con1);
322 break;
323 case VPLL:
324 r = readl(&clk->vpll_con0);
325 k = readl(&clk->vpll_con1);
326 break;
327 case BPLL:
328 r = readl(&clk->bpll_con0);
329 break;
330 case RPLL:
331 r = readl(&clk->rpll_con0);
332 k = readl(&clk->rpll_con1);
333 break;
334 case SPLL:
335 r = readl(&clk->spll_con0);
336 break;
337 default:
338 printf("Unsupported PLL (%d)\n", pllreg);
339 return 0;
340 }
341
342 return exynos_get_pll_clk(pllreg, r, k);
343}
344
Akshay Saraswat3ad7ffc2015-02-04 15:59:59 +0530345static struct clk_bit_info *get_clk_bit_info(int peripheral)
346{
347 int i;
Akshay Saraswatd5072262015-02-04 16:00:01 +0530348 struct clk_bit_info *info;
349
350 if (proid_is_exynos5420() || proid_is_exynos5800())
351 info = exynos542x_bit_info;
352 else
353 info = exynos5_bit_info;
Akshay Saraswat3ad7ffc2015-02-04 15:59:59 +0530354
Akshay Saraswatd5072262015-02-04 16:00:01 +0530355 for (i = 0; info[i].id != PERIPH_ID_NONE; i++) {
356 if (info[i].id == peripheral)
Akshay Saraswat3ad7ffc2015-02-04 15:59:59 +0530357 break;
358 }
359
Akshay Saraswatd5072262015-02-04 16:00:01 +0530360 if (info[i].id == PERIPH_ID_NONE)
Akshay Saraswat3ad7ffc2015-02-04 15:59:59 +0530361 debug("ERROR: Peripheral ID %d not found\n", peripheral);
362
Akshay Saraswatd5072262015-02-04 16:00:01 +0530363 return &info[i];
Akshay Saraswat3ad7ffc2015-02-04 15:59:59 +0530364}
365
Padmavathi Venna37feb7b2013-03-28 04:32:21 +0000366static unsigned long exynos5_get_periph_rate(int peripheral)
367{
Akshay Saraswat3ad7ffc2015-02-04 15:59:59 +0530368 struct clk_bit_info *bit_info = get_clk_bit_info(peripheral);
Akshay Saraswate81cb882015-02-04 16:00:05 +0530369 unsigned long sclk = 0;
370 unsigned int src = 0, div = 0, sub_div = 0;
Padmavathi Venna37feb7b2013-03-28 04:32:21 +0000371 struct exynos5_clock *clk =
372 (struct exynos5_clock *)samsung_get_base_clock();
373
374 switch (peripheral) {
375 case PERIPH_ID_UART0:
376 case PERIPH_ID_UART1:
377 case PERIPH_ID_UART2:
378 case PERIPH_ID_UART3:
379 src = readl(&clk->src_peric0);
380 div = readl(&clk->div_peric0);
381 break;
382 case PERIPH_ID_PWM0:
383 case PERIPH_ID_PWM1:
384 case PERIPH_ID_PWM2:
385 case PERIPH_ID_PWM3:
386 case PERIPH_ID_PWM4:
387 src = readl(&clk->src_peric0);
388 div = readl(&clk->div_peric3);
389 break;
Dani Krishna Mohan65c7ee62013-09-11 16:38:48 +0530390 case PERIPH_ID_I2S0:
391 src = readl(&clk->src_mau);
Akshay Saraswate81cb882015-02-04 16:00:05 +0530392 div = sub_div = readl(&clk->div_mau);
Padmavathi Venna37feb7b2013-03-28 04:32:21 +0000393 case PERIPH_ID_SPI0:
394 case PERIPH_ID_SPI1:
395 src = readl(&clk->src_peric1);
Akshay Saraswate81cb882015-02-04 16:00:05 +0530396 div = sub_div = readl(&clk->div_peric1);
Padmavathi Venna37feb7b2013-03-28 04:32:21 +0000397 break;
398 case PERIPH_ID_SPI2:
399 src = readl(&clk->src_peric1);
Akshay Saraswate81cb882015-02-04 16:00:05 +0530400 div = sub_div = readl(&clk->div_peric2);
Padmavathi Venna37feb7b2013-03-28 04:32:21 +0000401 break;
402 case PERIPH_ID_SPI3:
403 case PERIPH_ID_SPI4:
404 src = readl(&clk->sclk_src_isp);
Akshay Saraswate81cb882015-02-04 16:00:05 +0530405 div = sub_div = readl(&clk->sclk_div_isp);
Padmavathi Venna37feb7b2013-03-28 04:32:21 +0000406 break;
407 case PERIPH_ID_SDMMC0:
408 case PERIPH_ID_SDMMC1:
Akshay Saraswatbad6a982015-02-04 16:00:02 +0530409 src = readl(&clk->src_fsys);
Akshay Saraswate81cb882015-02-04 16:00:05 +0530410 div = sub_div = readl(&clk->div_fsys1);
Akshay Saraswatbad6a982015-02-04 16:00:02 +0530411 break;
Padmavathi Venna37feb7b2013-03-28 04:32:21 +0000412 case PERIPH_ID_SDMMC2:
413 case PERIPH_ID_SDMMC3:
414 src = readl(&clk->src_fsys);
Akshay Saraswate81cb882015-02-04 16:00:05 +0530415 div = sub_div = readl(&clk->div_fsys2);
Padmavathi Venna37feb7b2013-03-28 04:32:21 +0000416 break;
417 case PERIPH_ID_I2C0:
418 case PERIPH_ID_I2C1:
419 case PERIPH_ID_I2C2:
420 case PERIPH_ID_I2C3:
421 case PERIPH_ID_I2C4:
422 case PERIPH_ID_I2C5:
423 case PERIPH_ID_I2C6:
424 case PERIPH_ID_I2C7:
Akshay Saraswate81cb882015-02-04 16:00:05 +0530425 src = EXYNOS_SRC_MPLL;
Guillaume GARDETf87f8ee2015-03-11 10:34:27 +0100426 div = readl(&clk->div_top1);
427 sub_div = readl(&clk->div_top0);
Akshay Saraswate81cb882015-02-04 16:00:05 +0530428 break;
Padmavathi Venna37feb7b2013-03-28 04:32:21 +0000429 default:
430 debug("%s: invalid peripheral %d", __func__, peripheral);
431 return -1;
432 };
433
Akshay Saraswatbad6a982015-02-04 16:00:02 +0530434 if (bit_info->src_bit >= 0)
Akshay Saraswatd932c362015-02-04 16:00:04 +0530435 src = (src >> bit_info->src_bit) & bit_info->src_mask;
Padmavathi Venna37feb7b2013-03-28 04:32:21 +0000436
437 switch (src) {
438 case EXYNOS_SRC_MPLL:
439 sclk = exynos5_get_pll_clk(MPLL);
440 break;
441 case EXYNOS_SRC_EPLL:
442 sclk = exynos5_get_pll_clk(EPLL);
443 break;
444 case EXYNOS_SRC_VPLL:
445 sclk = exynos5_get_pll_clk(VPLL);
446 break;
447 default:
Akshay Saraswate81cb882015-02-04 16:00:05 +0530448 debug("%s: EXYNOS_SRC %d not supported\n", __func__, src);
Padmavathi Venna37feb7b2013-03-28 04:32:21 +0000449 return 0;
450 }
451
Akshay Saraswate81cb882015-02-04 16:00:05 +0530452 /* Clock divider ratio for this peripheral */
453 if (bit_info->div_bit >= 0)
454 div = (div >> bit_info->div_bit) & bit_info->div_mask;
Padmavathi Venna37feb7b2013-03-28 04:32:21 +0000455
Akshay Saraswate81cb882015-02-04 16:00:05 +0530456 /* Clock pre-divider ratio for this peripheral */
457 if (bit_info->prediv_bit >= 0)
458 sub_div = (sub_div >> bit_info->prediv_bit)
459 & bit_info->prediv_mask;
Padmavathi Venna37feb7b2013-03-28 04:32:21 +0000460
Akshay Saraswate81cb882015-02-04 16:00:05 +0530461 /* Calculate and return required clock rate */
462 return (sclk / (div + 1)) / (sub_div + 1);
Padmavathi Venna37feb7b2013-03-28 04:32:21 +0000463}
464
Akshay Saraswatd5072262015-02-04 16:00:01 +0530465static unsigned long exynos542x_get_periph_rate(int peripheral)
466{
467 struct clk_bit_info *bit_info = get_clk_bit_info(peripheral);
Akshay Saraswate81cb882015-02-04 16:00:05 +0530468 unsigned long sclk = 0;
469 unsigned int src = 0, div = 0, sub_div = 0;
Akshay Saraswatd5072262015-02-04 16:00:01 +0530470 struct exynos5420_clock *clk =
471 (struct exynos5420_clock *)samsung_get_base_clock();
472
473 switch (peripheral) {
474 case PERIPH_ID_UART0:
475 case PERIPH_ID_UART1:
476 case PERIPH_ID_UART2:
477 case PERIPH_ID_UART3:
478 case PERIPH_ID_PWM0:
479 case PERIPH_ID_PWM1:
480 case PERIPH_ID_PWM2:
481 case PERIPH_ID_PWM3:
482 case PERIPH_ID_PWM4:
483 src = readl(&clk->src_peric0);
484 div = readl(&clk->div_peric0);
485 break;
486 case PERIPH_ID_SPI0:
487 case PERIPH_ID_SPI1:
488 case PERIPH_ID_SPI2:
489 src = readl(&clk->src_peric1);
490 div = readl(&clk->div_peric1);
491 sub_div = readl(&clk->div_peric4);
492 break;
493 case PERIPH_ID_SPI3:
494 case PERIPH_ID_SPI4:
495 src = readl(&clk->src_isp);
496 div = readl(&clk->div_isp1);
497 sub_div = readl(&clk->div_isp1);
498 break;
499 case PERIPH_ID_SDMMC0:
500 case PERIPH_ID_SDMMC1:
501 case PERIPH_ID_SDMMC2:
502 case PERIPH_ID_SDMMC3:
503 src = readl(&clk->src_fsys);
504 div = readl(&clk->div_fsys1);
505 break;
506 case PERIPH_ID_I2C0:
507 case PERIPH_ID_I2C1:
508 case PERIPH_ID_I2C2:
509 case PERIPH_ID_I2C3:
510 case PERIPH_ID_I2C4:
511 case PERIPH_ID_I2C5:
512 case PERIPH_ID_I2C6:
513 case PERIPH_ID_I2C7:
514 case PERIPH_ID_I2C8:
515 case PERIPH_ID_I2C9:
516 case PERIPH_ID_I2C10:
Akshay Saraswate81cb882015-02-04 16:00:05 +0530517 src = EXYNOS542X_SRC_MPLL;
518 div = readl(&clk->div_top1);
519 break;
Akshay Saraswatd5072262015-02-04 16:00:01 +0530520 default:
521 debug("%s: invalid peripheral %d", __func__, peripheral);
522 return -1;
523 };
524
525 if (bit_info->src_bit >= 0)
Akshay Saraswatd932c362015-02-04 16:00:04 +0530526 src = (src >> bit_info->src_bit) & bit_info->src_mask;
Akshay Saraswatd5072262015-02-04 16:00:01 +0530527
528 switch (src) {
529 case EXYNOS542X_SRC_MPLL:
530 sclk = exynos542x_get_pll_clk(MPLL);
531 break;
532 case EXYNOS542X_SRC_SPLL:
533 sclk = exynos542x_get_pll_clk(SPLL);
534 break;
535 case EXYNOS542X_SRC_EPLL:
536 sclk = exynos542x_get_pll_clk(EPLL);
537 break;
538 case EXYNOS542X_SRC_RPLL:
539 sclk = exynos542x_get_pll_clk(RPLL);
540 break;
541 default:
Akshay Saraswate81cb882015-02-04 16:00:05 +0530542 debug("%s: EXYNOS542X_SRC %d not supported", __func__, src);
Akshay Saraswatd5072262015-02-04 16:00:01 +0530543 return 0;
544 }
545
Akshay Saraswate81cb882015-02-04 16:00:05 +0530546 /* Clock divider ratio for this peripheral */
547 if (bit_info->div_bit >= 0)
Akshay Saraswatd932c362015-02-04 16:00:04 +0530548 div = (div >> bit_info->div_bit) & bit_info->div_mask;
Akshay Saraswatd5072262015-02-04 16:00:01 +0530549
Akshay Saraswate81cb882015-02-04 16:00:05 +0530550 /* Clock pre-divider ratio for this peripheral */
551 if (bit_info->prediv_bit >= 0)
Akshay Saraswatd932c362015-02-04 16:00:04 +0530552 sub_div = (sub_div >> bit_info->prediv_bit)
Akshay Saraswate81cb882015-02-04 16:00:05 +0530553 & bit_info->prediv_mask;
Akshay Saraswatd5072262015-02-04 16:00:01 +0530554
Akshay Saraswate81cb882015-02-04 16:00:05 +0530555 /* Calculate and return required clock rate */
556 return (sclk / (div + 1)) / (sub_div + 1);
Akshay Saraswatd5072262015-02-04 16:00:01 +0530557}
558
Padmavathi Venna37feb7b2013-03-28 04:32:21 +0000559unsigned long clock_get_periph_rate(int peripheral)
560{
Akshay Saraswatd5072262015-02-04 16:00:01 +0530561 if (cpu_is_exynos5()) {
562 if (proid_is_exynos5420() || proid_is_exynos5800())
563 return exynos542x_get_periph_rate(peripheral);
Padmavathi Venna37feb7b2013-03-28 04:32:21 +0000564 return exynos5_get_periph_rate(peripheral);
Akshay Saraswatd5072262015-02-04 16:00:01 +0530565 } else {
Padmavathi Venna37feb7b2013-03-28 04:32:21 +0000566 return 0;
Akshay Saraswatd5072262015-02-04 16:00:01 +0530567 }
Padmavathi Venna37feb7b2013-03-28 04:32:21 +0000568}
569
Chander Kashyap4131a772011-12-06 23:34:12 +0000570/* exynos4: return ARM clock frequency */
571static unsigned long exynos4_get_arm_clk(void)
Minkyu Kangb1b24682011-01-24 15:22:23 +0900572{
Chander Kashyap4131a772011-12-06 23:34:12 +0000573 struct exynos4_clock *clk =
574 (struct exynos4_clock *)samsung_get_base_clock();
Minkyu Kangb1b24682011-01-24 15:22:23 +0900575 unsigned long div;
Chander Kashyap3c7721f2011-12-18 22:56:44 +0000576 unsigned long armclk;
577 unsigned int core_ratio;
578 unsigned int core2_ratio;
Minkyu Kangb1b24682011-01-24 15:22:23 +0900579
580 div = readl(&clk->div_cpu0);
581
Chander Kashyap3c7721f2011-12-18 22:56:44 +0000582 /* CORE_RATIO: [2:0], CORE2_RATIO: [30:28] */
583 core_ratio = (div >> 0) & 0x7;
584 core2_ratio = (div >> 28) & 0x7;
Minkyu Kangb1b24682011-01-24 15:22:23 +0900585
Chander Kashyap3c7721f2011-12-18 22:56:44 +0000586 armclk = get_pll_clk(APLL) / (core_ratio + 1);
587 armclk /= (core2_ratio + 1);
Minkyu Kangb1b24682011-01-24 15:22:23 +0900588
Chander Kashyap3c7721f2011-12-18 22:56:44 +0000589 return armclk;
Minkyu Kangb1b24682011-01-24 15:22:23 +0900590}
591
Chander Kashyap400ab162012-10-07 01:43:17 +0000592/* exynos4x12: return ARM clock frequency */
593static unsigned long exynos4x12_get_arm_clk(void)
594{
595 struct exynos4x12_clock *clk =
596 (struct exynos4x12_clock *)samsung_get_base_clock();
597 unsigned long div;
598 unsigned long armclk;
599 unsigned int core_ratio;
600 unsigned int core2_ratio;
601
602 div = readl(&clk->div_cpu0);
603
604 /* CORE_RATIO: [2:0], CORE2_RATIO: [30:28] */
605 core_ratio = (div >> 0) & 0x7;
606 core2_ratio = (div >> 28) & 0x7;
607
608 armclk = get_pll_clk(APLL) / (core_ratio + 1);
609 armclk /= (core2_ratio + 1);
610
611 return armclk;
612}
613
Chander Kashyap34076a02012-02-05 23:01:46 +0000614/* exynos5: return ARM clock frequency */
615static unsigned long exynos5_get_arm_clk(void)
616{
617 struct exynos5_clock *clk =
618 (struct exynos5_clock *)samsung_get_base_clock();
619 unsigned long div;
620 unsigned long armclk;
621 unsigned int arm_ratio;
622 unsigned int arm2_ratio;
623
624 div = readl(&clk->div_cpu0);
625
626 /* ARM_RATIO: [2:0], ARM2_RATIO: [30:28] */
627 arm_ratio = (div >> 0) & 0x7;
628 arm2_ratio = (div >> 28) & 0x7;
629
630 armclk = get_pll_clk(APLL) / (arm_ratio + 1);
631 armclk /= (arm2_ratio + 1);
632
633 return armclk;
634}
635
Chander Kashyap4131a772011-12-06 23:34:12 +0000636/* exynos4: return pwm clock frequency */
637static unsigned long exynos4_get_pwm_clk(void)
Minkyu Kangb1b24682011-01-24 15:22:23 +0900638{
Chander Kashyap4131a772011-12-06 23:34:12 +0000639 struct exynos4_clock *clk =
640 (struct exynos4_clock *)samsung_get_base_clock();
Minkyu Kangb1b24682011-01-24 15:22:23 +0900641 unsigned long pclk, sclk;
642 unsigned int sel;
643 unsigned int ratio;
644
Minkyu Kang69b28242011-05-18 16:57:55 +0900645 if (s5p_get_cpu_rev() == 0) {
646 /*
647 * CLK_SRC_PERIL0
648 * PWM_SEL [27:24]
649 */
650 sel = readl(&clk->src_peril0);
651 sel = (sel >> 24) & 0xf;
Minkyu Kangb1b24682011-01-24 15:22:23 +0900652
Minkyu Kang69b28242011-05-18 16:57:55 +0900653 if (sel == 0x6)
654 sclk = get_pll_clk(MPLL);
655 else if (sel == 0x7)
656 sclk = get_pll_clk(EPLL);
657 else if (sel == 0x8)
658 sclk = get_pll_clk(VPLL);
659 else
660 return 0;
661
662 /*
663 * CLK_DIV_PERIL3
664 * PWM_RATIO [3:0]
665 */
666 ratio = readl(&clk->div_peril3);
667 ratio = ratio & 0xf;
668 } else if (s5p_get_cpu_rev() == 1) {
Minkyu Kangb1b24682011-01-24 15:22:23 +0900669 sclk = get_pll_clk(MPLL);
Minkyu Kang69b28242011-05-18 16:57:55 +0900670 ratio = 8;
671 } else
Minkyu Kangb1b24682011-01-24 15:22:23 +0900672 return 0;
673
Minkyu Kangb1b24682011-01-24 15:22:23 +0900674 pclk = sclk / (ratio + 1);
675
676 return pclk;
677}
678
Chander Kashyap400ab162012-10-07 01:43:17 +0000679/* exynos4x12: return pwm clock frequency */
680static unsigned long exynos4x12_get_pwm_clk(void)
681{
682 unsigned long pclk, sclk;
683 unsigned int ratio;
684
685 sclk = get_pll_clk(MPLL);
686 ratio = 8;
687
688 pclk = sclk / (ratio + 1);
689
690 return pclk;
691}
692
Chander Kashyap4131a772011-12-06 23:34:12 +0000693/* exynos4: return uart clock frequency */
694static unsigned long exynos4_get_uart_clk(int dev_index)
Minkyu Kangb1b24682011-01-24 15:22:23 +0900695{
Chander Kashyap4131a772011-12-06 23:34:12 +0000696 struct exynos4_clock *clk =
697 (struct exynos4_clock *)samsung_get_base_clock();
Minkyu Kangb1b24682011-01-24 15:22:23 +0900698 unsigned long uclk, sclk;
699 unsigned int sel;
700 unsigned int ratio;
701
702 /*
703 * CLK_SRC_PERIL0
704 * UART0_SEL [3:0]
705 * UART1_SEL [7:4]
706 * UART2_SEL [8:11]
707 * UART3_SEL [12:15]
708 * UART4_SEL [16:19]
709 * UART5_SEL [23:20]
710 */
711 sel = readl(&clk->src_peril0);
712 sel = (sel >> (dev_index << 2)) & 0xf;
713
714 if (sel == 0x6)
715 sclk = get_pll_clk(MPLL);
716 else if (sel == 0x7)
717 sclk = get_pll_clk(EPLL);
718 else if (sel == 0x8)
719 sclk = get_pll_clk(VPLL);
720 else
721 return 0;
722
723 /*
724 * CLK_DIV_PERIL0
725 * UART0_RATIO [3:0]
726 * UART1_RATIO [7:4]
727 * UART2_RATIO [8:11]
728 * UART3_RATIO [12:15]
729 * UART4_RATIO [16:19]
730 * UART5_RATIO [23:20]
731 */
732 ratio = readl(&clk->div_peril0);
733 ratio = (ratio >> (dev_index << 2)) & 0xf;
734
735 uclk = sclk / (ratio + 1);
736
737 return uclk;
738}
739
Chander Kashyap400ab162012-10-07 01:43:17 +0000740/* exynos4x12: return uart clock frequency */
741static unsigned long exynos4x12_get_uart_clk(int dev_index)
742{
743 struct exynos4x12_clock *clk =
744 (struct exynos4x12_clock *)samsung_get_base_clock();
745 unsigned long uclk, sclk;
746 unsigned int sel;
747 unsigned int ratio;
748
749 /*
750 * CLK_SRC_PERIL0
751 * UART0_SEL [3:0]
752 * UART1_SEL [7:4]
753 * UART2_SEL [8:11]
754 * UART3_SEL [12:15]
755 * UART4_SEL [16:19]
756 */
757 sel = readl(&clk->src_peril0);
758 sel = (sel >> (dev_index << 2)) & 0xf;
759
760 if (sel == 0x6)
761 sclk = get_pll_clk(MPLL);
762 else if (sel == 0x7)
763 sclk = get_pll_clk(EPLL);
764 else if (sel == 0x8)
765 sclk = get_pll_clk(VPLL);
766 else
767 return 0;
768
769 /*
770 * CLK_DIV_PERIL0
771 * UART0_RATIO [3:0]
772 * UART1_RATIO [7:4]
773 * UART2_RATIO [8:11]
774 * UART3_RATIO [12:15]
775 * UART4_RATIO [16:19]
776 */
777 ratio = readl(&clk->div_peril0);
778 ratio = (ratio >> (dev_index << 2)) & 0xf;
779
780 uclk = sclk / (ratio + 1);
781
782 return uclk;
783}
784
Jaehoon Chung8788e062012-12-27 22:30:32 +0000785static unsigned long exynos4_get_mmc_clk(int dev_index)
786{
787 struct exynos4_clock *clk =
788 (struct exynos4_clock *)samsung_get_base_clock();
789 unsigned long uclk, sclk;
790 unsigned int sel, ratio, pre_ratio;
Amare1df6282013-04-27 11:42:56 +0530791 int shift = 0;
Jaehoon Chung8788e062012-12-27 22:30:32 +0000792
793 sel = readl(&clk->src_fsys);
794 sel = (sel >> (dev_index << 2)) & 0xf;
795
796 if (sel == 0x6)
797 sclk = get_pll_clk(MPLL);
798 else if (sel == 0x7)
799 sclk = get_pll_clk(EPLL);
800 else if (sel == 0x8)
801 sclk = get_pll_clk(VPLL);
802 else
803 return 0;
804
805 switch (dev_index) {
806 case 0:
807 case 1:
808 ratio = readl(&clk->div_fsys1);
809 pre_ratio = readl(&clk->div_fsys1);
810 break;
811 case 2:
812 case 3:
813 ratio = readl(&clk->div_fsys2);
814 pre_ratio = readl(&clk->div_fsys2);
815 break;
816 case 4:
817 ratio = readl(&clk->div_fsys3);
818 pre_ratio = readl(&clk->div_fsys3);
819 break;
820 default:
821 return 0;
822 }
823
824 if (dev_index == 1 || dev_index == 3)
825 shift = 16;
826
827 ratio = (ratio >> shift) & 0xf;
828 pre_ratio = (pre_ratio >> (shift + 8)) & 0xff;
829 uclk = (sclk / (ratio + 1)) / (pre_ratio + 1);
830
831 return uclk;
832}
833
Chander Kashyap4131a772011-12-06 23:34:12 +0000834/* exynos4: set the mmc clock */
835static void exynos4_set_mmc_clk(int dev_index, unsigned int div)
Jaehoon Chung9a772212011-05-17 21:19:17 +0000836{
Chander Kashyap4131a772011-12-06 23:34:12 +0000837 struct exynos4_clock *clk =
838 (struct exynos4_clock *)samsung_get_base_clock();
Jaehoon Chungd2c83242014-05-16 13:59:50 +0900839 unsigned int addr, clear_bit, set_bit;
Jaehoon Chung9a772212011-05-17 21:19:17 +0000840
841 /*
842 * CLK_DIV_FSYS1
843 * MMC0_PRE_RATIO [15:8], MMC1_PRE_RATIO [31:24]
844 * CLK_DIV_FSYS2
845 * MMC2_PRE_RATIO [15:8], MMC3_PRE_RATIO [31:24]
Jaehoon Chung29fe8c52012-12-27 22:30:33 +0000846 * CLK_DIV_FSYS3
Jaehoon Chungd2c83242014-05-16 13:59:50 +0900847 * MMC4_RATIO [3:0]
Jaehoon Chung9a772212011-05-17 21:19:17 +0000848 */
849 if (dev_index < 2) {
850 addr = (unsigned int)&clk->div_fsys1;
Jaehoon Chungd2c83242014-05-16 13:59:50 +0900851 clear_bit = MASK_PRE_RATIO(dev_index);
852 set_bit = SET_PRE_RATIO(dev_index, div);
853 } else if (dev_index == 4) {
Jaehoon Chung29fe8c52012-12-27 22:30:33 +0000854 addr = (unsigned int)&clk->div_fsys3;
855 dev_index -= 4;
Jaehoon Chungd2c83242014-05-16 13:59:50 +0900856 /* MMC4 is controlled with the MMC4_RATIO value */
857 clear_bit = MASK_RATIO(dev_index);
858 set_bit = SET_RATIO(dev_index, div);
Jaehoon Chung9a772212011-05-17 21:19:17 +0000859 } else {
860 addr = (unsigned int)&clk->div_fsys2;
861 dev_index -= 2;
Jaehoon Chungd2c83242014-05-16 13:59:50 +0900862 clear_bit = MASK_PRE_RATIO(dev_index);
863 set_bit = SET_PRE_RATIO(dev_index, div);
Jaehoon Chung9a772212011-05-17 21:19:17 +0000864 }
865
Jaehoon Chungd2c83242014-05-16 13:59:50 +0900866 clrsetbits_le32(addr, clear_bit, set_bit);
Jaehoon Chung9a772212011-05-17 21:19:17 +0000867}
868
Chander Kashyap34076a02012-02-05 23:01:46 +0000869/* exynos5: set the mmc clock */
870static void exynos5_set_mmc_clk(int dev_index, unsigned int div)
871{
872 struct exynos5_clock *clk =
873 (struct exynos5_clock *)samsung_get_base_clock();
874 unsigned int addr;
Chander Kashyap34076a02012-02-05 23:01:46 +0000875
876 /*
877 * CLK_DIV_FSYS1
878 * MMC0_PRE_RATIO [15:8], MMC1_PRE_RATIO [31:24]
879 * CLK_DIV_FSYS2
880 * MMC2_PRE_RATIO [15:8], MMC3_PRE_RATIO [31:24]
881 */
882 if (dev_index < 2) {
883 addr = (unsigned int)&clk->div_fsys1;
884 } else {
885 addr = (unsigned int)&clk->div_fsys2;
886 dev_index -= 2;
887 }
888
Inha Song4e558532014-02-06 14:20:12 +0900889 clrsetbits_le32(addr, 0xff << ((dev_index << 4) + 8),
890 (div & 0xff) << ((dev_index << 4) + 8));
Chander Kashyap34076a02012-02-05 23:01:46 +0000891}
892
Rajeshwari Birjeac892d02013-12-26 09:44:21 +0530893/* exynos5: set the mmc clock */
894static void exynos5420_set_mmc_clk(int dev_index, unsigned int div)
895{
896 struct exynos5420_clock *clk =
897 (struct exynos5420_clock *)samsung_get_base_clock();
898 unsigned int addr;
Inha Song4e558532014-02-06 14:20:12 +0900899 unsigned int shift;
Rajeshwari Birjeac892d02013-12-26 09:44:21 +0530900
901 /*
902 * CLK_DIV_FSYS1
903 * MMC0_RATIO [9:0]
904 * MMC1_RATIO [19:10]
905 * MMC2_RATIO [29:20]
906 */
907 addr = (unsigned int)&clk->div_fsys1;
908 shift = dev_index * 10;
909
Inha Song4e558532014-02-06 14:20:12 +0900910 clrsetbits_le32(addr, 0x3ff << shift, (div & 0x3ff) << shift);
Rajeshwari Birjeac892d02013-12-26 09:44:21 +0530911}
912
Donghwa Lee77ba1912012-04-05 19:36:12 +0000913/* get_lcd_clk: return lcd clock frequency */
914static unsigned long exynos4_get_lcd_clk(void)
915{
916 struct exynos4_clock *clk =
917 (struct exynos4_clock *)samsung_get_base_clock();
918 unsigned long pclk, sclk;
919 unsigned int sel;
920 unsigned int ratio;
921
922 /*
923 * CLK_SRC_LCD0
924 * FIMD0_SEL [3:0]
925 */
926 sel = readl(&clk->src_lcd0);
927 sel = sel & 0xf;
928
929 /*
930 * 0x6: SCLK_MPLL
931 * 0x7: SCLK_EPLL
932 * 0x8: SCLK_VPLL
933 */
934 if (sel == 0x6)
935 sclk = get_pll_clk(MPLL);
936 else if (sel == 0x7)
937 sclk = get_pll_clk(EPLL);
938 else if (sel == 0x8)
939 sclk = get_pll_clk(VPLL);
940 else
941 return 0;
942
943 /*
944 * CLK_DIV_LCD0
945 * FIMD0_RATIO [3:0]
946 */
947 ratio = readl(&clk->div_lcd0);
948 ratio = ratio & 0xf;
949
950 pclk = sclk / (ratio + 1);
951
952 return pclk;
953}
954
Donghwa Lee3c9d4532012-07-02 01:15:49 +0000955/* get_lcd_clk: return lcd clock frequency */
956static unsigned long exynos5_get_lcd_clk(void)
957{
958 struct exynos5_clock *clk =
959 (struct exynos5_clock *)samsung_get_base_clock();
960 unsigned long pclk, sclk;
961 unsigned int sel;
962 unsigned int ratio;
963
964 /*
965 * CLK_SRC_LCD0
966 * FIMD0_SEL [3:0]
967 */
968 sel = readl(&clk->src_disp1_0);
969 sel = sel & 0xf;
970
971 /*
972 * 0x6: SCLK_MPLL
973 * 0x7: SCLK_EPLL
974 * 0x8: SCLK_VPLL
975 */
976 if (sel == 0x6)
977 sclk = get_pll_clk(MPLL);
978 else if (sel == 0x7)
979 sclk = get_pll_clk(EPLL);
980 else if (sel == 0x8)
981 sclk = get_pll_clk(VPLL);
982 else
983 return 0;
984
985 /*
986 * CLK_DIV_LCD0
987 * FIMD0_RATIO [3:0]
988 */
989 ratio = readl(&clk->div_disp1_0);
990 ratio = ratio & 0xf;
991
992 pclk = sclk / (ratio + 1);
993
994 return pclk;
995}
996
Ajay Kumar914af872014-09-05 16:53:32 +0530997static unsigned long exynos5420_get_lcd_clk(void)
998{
999 struct exynos5420_clock *clk =
1000 (struct exynos5420_clock *)samsung_get_base_clock();
1001 unsigned long pclk, sclk;
1002 unsigned int sel;
1003 unsigned int ratio;
1004
1005 /*
1006 * CLK_SRC_DISP10
1007 * FIMD1_SEL [4]
1008 * 0: SCLK_RPLL
1009 * 1: SCLK_SPLL
1010 */
1011 sel = readl(&clk->src_disp10);
1012 sel &= (1 << 4);
1013
1014 if (sel)
1015 sclk = get_pll_clk(SPLL);
1016 else
1017 sclk = get_pll_clk(RPLL);
1018
1019 /*
1020 * CLK_DIV_DISP10
1021 * FIMD1_RATIO [3:0]
1022 */
1023 ratio = readl(&clk->div_disp10);
1024 ratio = ratio & 0xf;
1025
1026 pclk = sclk / (ratio + 1);
1027
1028 return pclk;
1029}
1030
Donghwa Lee77ba1912012-04-05 19:36:12 +00001031void exynos4_set_lcd_clk(void)
1032{
1033 struct exynos4_clock *clk =
1034 (struct exynos4_clock *)samsung_get_base_clock();
Donghwa Lee77ba1912012-04-05 19:36:12 +00001035
1036 /*
1037 * CLK_GATE_BLOCK
1038 * CLK_CAM [0]
1039 * CLK_TV [1]
1040 * CLK_MFC [2]
1041 * CLK_G3D [3]
1042 * CLK_LCD0 [4]
1043 * CLK_LCD1 [5]
1044 * CLK_GPS [7]
1045 */
Inha Song4e558532014-02-06 14:20:12 +09001046 setbits_le32(&clk->gate_block, 1 << 4);
Donghwa Lee77ba1912012-04-05 19:36:12 +00001047
1048 /*
1049 * CLK_SRC_LCD0
1050 * FIMD0_SEL [3:0]
1051 * MDNIE0_SEL [7:4]
1052 * MDNIE_PWM0_SEL [8:11]
1053 * MIPI0_SEL [12:15]
1054 * set lcd0 src clock 0x6: SCLK_MPLL
1055 */
Inha Song4e558532014-02-06 14:20:12 +09001056 clrsetbits_le32(&clk->src_lcd0, 0xf, 0x6);
Donghwa Lee77ba1912012-04-05 19:36:12 +00001057
1058 /*
1059 * CLK_GATE_IP_LCD0
1060 * CLK_FIMD0 [0]
1061 * CLK_MIE0 [1]
1062 * CLK_MDNIE0 [2]
1063 * CLK_DSIM0 [3]
1064 * CLK_SMMUFIMD0 [4]
1065 * CLK_PPMULCD0 [5]
1066 * Gating all clocks for FIMD0
1067 */
Inha Song4e558532014-02-06 14:20:12 +09001068 setbits_le32(&clk->gate_ip_lcd0, 1 << 0);
Donghwa Lee77ba1912012-04-05 19:36:12 +00001069
1070 /*
1071 * CLK_DIV_LCD0
1072 * FIMD0_RATIO [3:0]
1073 * MDNIE0_RATIO [7:4]
1074 * MDNIE_PWM0_RATIO [11:8]
1075 * MDNIE_PWM_PRE_RATIO [15:12]
1076 * MIPI0_RATIO [19:16]
1077 * MIPI0_PRE_RATIO [23:20]
1078 * set fimd ratio
1079 */
Inha Song4e558532014-02-06 14:20:12 +09001080 clrsetbits_le32(&clk->div_lcd0, 0xf, 0x1);
Donghwa Lee77ba1912012-04-05 19:36:12 +00001081}
1082
Donghwa Lee3c9d4532012-07-02 01:15:49 +00001083void exynos5_set_lcd_clk(void)
1084{
1085 struct exynos5_clock *clk =
1086 (struct exynos5_clock *)samsung_get_base_clock();
Donghwa Lee3c9d4532012-07-02 01:15:49 +00001087
1088 /*
1089 * CLK_GATE_BLOCK
1090 * CLK_CAM [0]
1091 * CLK_TV [1]
1092 * CLK_MFC [2]
1093 * CLK_G3D [3]
1094 * CLK_LCD0 [4]
1095 * CLK_LCD1 [5]
1096 * CLK_GPS [7]
1097 */
Inha Song4e558532014-02-06 14:20:12 +09001098 setbits_le32(&clk->gate_block, 1 << 4);
Donghwa Lee3c9d4532012-07-02 01:15:49 +00001099
1100 /*
1101 * CLK_SRC_LCD0
1102 * FIMD0_SEL [3:0]
1103 * MDNIE0_SEL [7:4]
1104 * MDNIE_PWM0_SEL [8:11]
1105 * MIPI0_SEL [12:15]
1106 * set lcd0 src clock 0x6: SCLK_MPLL
1107 */
Inha Song4e558532014-02-06 14:20:12 +09001108 clrsetbits_le32(&clk->src_disp1_0, 0xf, 0x6);
Donghwa Lee3c9d4532012-07-02 01:15:49 +00001109
1110 /*
1111 * CLK_GATE_IP_LCD0
1112 * CLK_FIMD0 [0]
1113 * CLK_MIE0 [1]
1114 * CLK_MDNIE0 [2]
1115 * CLK_DSIM0 [3]
1116 * CLK_SMMUFIMD0 [4]
1117 * CLK_PPMULCD0 [5]
1118 * Gating all clocks for FIMD0
1119 */
Inha Song4e558532014-02-06 14:20:12 +09001120 setbits_le32(&clk->gate_ip_disp1, 1 << 0);
Donghwa Lee3c9d4532012-07-02 01:15:49 +00001121
1122 /*
1123 * CLK_DIV_LCD0
1124 * FIMD0_RATIO [3:0]
1125 * MDNIE0_RATIO [7:4]
1126 * MDNIE_PWM0_RATIO [11:8]
1127 * MDNIE_PWM_PRE_RATIO [15:12]
1128 * MIPI0_RATIO [19:16]
1129 * MIPI0_PRE_RATIO [23:20]
1130 * set fimd ratio
1131 */
Inha Song4e558532014-02-06 14:20:12 +09001132 clrsetbits_le32(&clk->div_disp1_0, 0xf, 0x0);
Donghwa Lee3c9d4532012-07-02 01:15:49 +00001133}
1134
Ajay Kumar914af872014-09-05 16:53:32 +05301135void exynos5420_set_lcd_clk(void)
1136{
1137 struct exynos5420_clock *clk =
1138 (struct exynos5420_clock *)samsung_get_base_clock();
1139 unsigned int cfg;
1140
1141 /*
1142 * CLK_SRC_DISP10
1143 * FIMD1_SEL [4]
1144 * 0: SCLK_RPLL
1145 * 1: SCLK_SPLL
1146 */
1147 cfg = readl(&clk->src_disp10);
1148 cfg &= ~(0x1 << 4);
1149 cfg |= (0 << 4);
1150 writel(cfg, &clk->src_disp10);
1151
1152 /*
1153 * CLK_DIV_DISP10
1154 * FIMD1_RATIO [3:0]
1155 */
1156 cfg = readl(&clk->div_disp10);
1157 cfg &= ~(0xf << 0);
1158 cfg |= (0 << 0);
1159 writel(cfg, &clk->div_disp10);
1160}
1161
Donghwa Lee77ba1912012-04-05 19:36:12 +00001162void exynos4_set_mipi_clk(void)
1163{
1164 struct exynos4_clock *clk =
1165 (struct exynos4_clock *)samsung_get_base_clock();
Donghwa Lee77ba1912012-04-05 19:36:12 +00001166
1167 /*
1168 * CLK_SRC_LCD0
1169 * FIMD0_SEL [3:0]
1170 * MDNIE0_SEL [7:4]
1171 * MDNIE_PWM0_SEL [8:11]
1172 * MIPI0_SEL [12:15]
1173 * set mipi0 src clock 0x6: SCLK_MPLL
1174 */
Inha Song4e558532014-02-06 14:20:12 +09001175 clrsetbits_le32(&clk->src_lcd0, 0xf << 12, 0x6 << 12);
Donghwa Lee77ba1912012-04-05 19:36:12 +00001176
1177 /*
1178 * CLK_SRC_MASK_LCD0
1179 * FIMD0_MASK [0]
1180 * MDNIE0_MASK [4]
1181 * MDNIE_PWM0_MASK [8]
1182 * MIPI0_MASK [12]
1183 * set src mask mipi0 0x1: Unmask
1184 */
Inha Song4e558532014-02-06 14:20:12 +09001185 setbits_le32(&clk->src_mask_lcd0, 0x1 << 12);
Donghwa Lee77ba1912012-04-05 19:36:12 +00001186
1187 /*
1188 * CLK_GATE_IP_LCD0
1189 * CLK_FIMD0 [0]
1190 * CLK_MIE0 [1]
1191 * CLK_MDNIE0 [2]
1192 * CLK_DSIM0 [3]
1193 * CLK_SMMUFIMD0 [4]
1194 * CLK_PPMULCD0 [5]
1195 * Gating all clocks for MIPI0
1196 */
Inha Song4e558532014-02-06 14:20:12 +09001197 setbits_le32(&clk->gate_ip_lcd0, 1 << 3);
Donghwa Lee77ba1912012-04-05 19:36:12 +00001198
1199 /*
1200 * CLK_DIV_LCD0
1201 * FIMD0_RATIO [3:0]
1202 * MDNIE0_RATIO [7:4]
1203 * MDNIE_PWM0_RATIO [11:8]
1204 * MDNIE_PWM_PRE_RATIO [15:12]
1205 * MIPI0_RATIO [19:16]
1206 * MIPI0_PRE_RATIO [23:20]
1207 * set mipi ratio
1208 */
Inha Song4e558532014-02-06 14:20:12 +09001209 clrsetbits_le32(&clk->div_lcd0, 0xf << 16, 0x1 << 16);
Donghwa Lee77ba1912012-04-05 19:36:12 +00001210}
1211
Rajeshwari Shinde392a73a2012-10-25 19:49:29 +00001212int exynos5_set_epll_clk(unsigned long rate)
1213{
1214 unsigned int epll_con, epll_con_k;
1215 unsigned int i;
1216 unsigned int lockcnt;
1217 unsigned int start;
1218 struct exynos5_clock *clk =
1219 (struct exynos5_clock *)samsung_get_base_clock();
1220
1221 epll_con = readl(&clk->epll_con0);
1222 epll_con &= ~((EPLL_CON0_LOCK_DET_EN_MASK <<
1223 EPLL_CON0_LOCK_DET_EN_SHIFT) |
1224 EPLL_CON0_MDIV_MASK << EPLL_CON0_MDIV_SHIFT |
1225 EPLL_CON0_PDIV_MASK << EPLL_CON0_PDIV_SHIFT |
1226 EPLL_CON0_SDIV_MASK << EPLL_CON0_SDIV_SHIFT);
1227
1228 for (i = 0; i < ARRAY_SIZE(exynos5_epll_div); i++) {
1229 if (exynos5_epll_div[i].freq_out == rate)
1230 break;
1231 }
1232
1233 if (i == ARRAY_SIZE(exynos5_epll_div))
1234 return -1;
1235
1236 epll_con_k = exynos5_epll_div[i].k_dsm << 0;
1237 epll_con |= exynos5_epll_div[i].en_lock_det <<
1238 EPLL_CON0_LOCK_DET_EN_SHIFT;
1239 epll_con |= exynos5_epll_div[i].m_div << EPLL_CON0_MDIV_SHIFT;
1240 epll_con |= exynos5_epll_div[i].p_div << EPLL_CON0_PDIV_SHIFT;
1241 epll_con |= exynos5_epll_div[i].s_div << EPLL_CON0_SDIV_SHIFT;
1242
1243 /*
1244 * Required period ( in cycles) to genarate a stable clock output.
1245 * The maximum clock time can be up to 3000 * PDIV cycles of PLLs
1246 * frequency input (as per spec)
1247 */
1248 lockcnt = 3000 * exynos5_epll_div[i].p_div;
1249
1250 writel(lockcnt, &clk->epll_lock);
1251 writel(epll_con, &clk->epll_con0);
1252 writel(epll_con_k, &clk->epll_con1);
1253
1254 start = get_timer(0);
1255
1256 while (!(readl(&clk->epll_con0) &
1257 (0x1 << EXYNOS5_EPLLCON0_LOCKED_SHIFT))) {
1258 if (get_timer(start) > TIMEOUT_EPLL_LOCK) {
1259 debug("%s: Timeout waiting for EPLL lock\n", __func__);
1260 return -1;
1261 }
1262 }
1263 return 0;
1264}
1265
Dani Krishna Mohan65c7ee62013-09-11 16:38:48 +05301266int exynos5_set_i2s_clk_source(unsigned int i2s_id)
Rajeshwari Shinde392a73a2012-10-25 19:49:29 +00001267{
1268 struct exynos5_clock *clk =
1269 (struct exynos5_clock *)samsung_get_base_clock();
Dani Krishna Mohan65c7ee62013-09-11 16:38:48 +05301270 unsigned int *audio_ass = (unsigned int *)samsung_get_base_audio_ass();
Rajeshwari Shinde392a73a2012-10-25 19:49:29 +00001271
Dani Krishna Mohan65c7ee62013-09-11 16:38:48 +05301272 if (i2s_id == 0) {
1273 setbits_le32(&clk->src_top2, CLK_SRC_MOUT_EPLL);
1274 clrsetbits_le32(&clk->src_mau, AUDIO0_SEL_MASK,
1275 (CLK_SRC_SCLK_EPLL));
1276 setbits_le32(audio_ass, AUDIO_CLKMUX_ASS);
1277 } else if (i2s_id == 1) {
1278 clrsetbits_le32(&clk->src_peric1, AUDIO1_SEL_MASK,
1279 (CLK_SRC_SCLK_EPLL));
1280 } else {
1281 return -1;
1282 }
1283 return 0;
Rajeshwari Shinde392a73a2012-10-25 19:49:29 +00001284}
1285
1286int exynos5_set_i2s_clk_prescaler(unsigned int src_frq,
Dani Krishna Mohan65c7ee62013-09-11 16:38:48 +05301287 unsigned int dst_frq,
1288 unsigned int i2s_id)
Rajeshwari Shinde392a73a2012-10-25 19:49:29 +00001289{
1290 struct exynos5_clock *clk =
1291 (struct exynos5_clock *)samsung_get_base_clock();
1292 unsigned int div;
1293
1294 if ((dst_frq == 0) || (src_frq == 0)) {
1295 debug("%s: Invalid requency input for prescaler\n", __func__);
1296 debug("src frq = %d des frq = %d ", src_frq, dst_frq);
1297 return -1;
1298 }
1299
1300 div = (src_frq / dst_frq);
Dani Krishna Mohan65c7ee62013-09-11 16:38:48 +05301301 if (i2s_id == 0) {
1302 if (div > AUDIO_0_RATIO_MASK) {
1303 debug("%s: Frequency ratio is out of range\n",
1304 __func__);
1305 debug("src frq = %d des frq = %d ", src_frq, dst_frq);
1306 return -1;
1307 }
1308 clrsetbits_le32(&clk->div_mau, AUDIO_0_RATIO_MASK,
1309 (div & AUDIO_0_RATIO_MASK));
1310 } else if(i2s_id == 1) {
1311 if (div > AUDIO_1_RATIO_MASK) {
1312 debug("%s: Frequency ratio is out of range\n",
1313 __func__);
1314 debug("src frq = %d des frq = %d ", src_frq, dst_frq);
1315 return -1;
1316 }
1317 clrsetbits_le32(&clk->div_peric4, AUDIO_1_RATIO_MASK,
1318 (div & AUDIO_1_RATIO_MASK));
1319 } else {
Rajeshwari Shinde392a73a2012-10-25 19:49:29 +00001320 return -1;
1321 }
Rajeshwari Shinde392a73a2012-10-25 19:49:29 +00001322 return 0;
1323}
1324
Hatim RVe6365b62012-11-02 01:15:34 +00001325/**
1326 * Linearly searches for the most accurate main and fine stage clock scalars
1327 * (divisors) for a specified target frequency and scalar bit sizes by checking
1328 * all multiples of main_scalar_bits values. Will always return scalars up to or
1329 * slower than target.
1330 *
1331 * @param main_scalar_bits Number of main scalar bits, must be > 0 and < 32
1332 * @param fine_scalar_bits Number of fine scalar bits, must be > 0 and < 32
1333 * @param input_freq Clock frequency to be scaled in Hz
1334 * @param target_freq Desired clock frequency in Hz
1335 * @param best_fine_scalar Pointer to store the fine stage divisor
1336 *
1337 * @return best_main_scalar Main scalar for desired frequency or -1 if none
1338 * found
1339 */
1340static int clock_calc_best_scalar(unsigned int main_scaler_bits,
1341 unsigned int fine_scalar_bits, unsigned int input_rate,
1342 unsigned int target_rate, unsigned int *best_fine_scalar)
1343{
1344 int i;
1345 int best_main_scalar = -1;
1346 unsigned int best_error = target_rate;
1347 const unsigned int cap = (1 << fine_scalar_bits) - 1;
1348 const unsigned int loops = 1 << main_scaler_bits;
1349
1350 debug("Input Rate is %u, Target is %u, Cap is %u\n", input_rate,
1351 target_rate, cap);
1352
1353 assert(best_fine_scalar != NULL);
1354 assert(main_scaler_bits <= fine_scalar_bits);
1355
1356 *best_fine_scalar = 1;
1357
1358 if (input_rate == 0 || target_rate == 0)
1359 return -1;
1360
1361 if (target_rate >= input_rate)
1362 return 1;
1363
1364 for (i = 1; i <= loops; i++) {
Masahiro Yamadadb204642014-11-07 03:03:31 +09001365 const unsigned int effective_div =
1366 max(min(input_rate / i / target_rate, cap), 1U);
Hatim RVe6365b62012-11-02 01:15:34 +00001367 const unsigned int effective_rate = input_rate / i /
1368 effective_div;
1369 const int error = target_rate - effective_rate;
1370
1371 debug("%d|effdiv:%u, effrate:%u, error:%d\n", i, effective_div,
1372 effective_rate, error);
1373
1374 if (error >= 0 && error <= best_error) {
1375 best_error = error;
1376 best_main_scalar = i;
1377 *best_fine_scalar = effective_div;
1378 }
1379 }
1380
1381 return best_main_scalar;
1382}
1383
1384static int exynos5_set_spi_clk(enum periph_id periph_id,
1385 unsigned int rate)
1386{
1387 struct exynos5_clock *clk =
1388 (struct exynos5_clock *)samsung_get_base_clock();
1389 int main;
1390 unsigned int fine;
1391 unsigned shift, pre_shift;
1392 unsigned mask = 0xff;
1393 u32 *reg;
1394
1395 main = clock_calc_best_scalar(4, 8, 400000000, rate, &fine);
1396 if (main < 0) {
1397 debug("%s: Cannot set clock rate for periph %d",
1398 __func__, periph_id);
1399 return -1;
1400 }
1401 main = main - 1;
1402 fine = fine - 1;
1403
1404 switch (periph_id) {
1405 case PERIPH_ID_SPI0:
1406 reg = &clk->div_peric1;
1407 shift = 0;
1408 pre_shift = 8;
1409 break;
1410 case PERIPH_ID_SPI1:
1411 reg = &clk->div_peric1;
1412 shift = 16;
1413 pre_shift = 24;
1414 break;
1415 case PERIPH_ID_SPI2:
1416 reg = &clk->div_peric2;
1417 shift = 0;
1418 pre_shift = 8;
1419 break;
1420 case PERIPH_ID_SPI3:
1421 reg = &clk->sclk_div_isp;
1422 shift = 0;
1423 pre_shift = 4;
1424 break;
1425 case PERIPH_ID_SPI4:
1426 reg = &clk->sclk_div_isp;
1427 shift = 12;
1428 pre_shift = 16;
1429 break;
1430 default:
1431 debug("%s: Unsupported peripheral ID %d\n", __func__,
1432 periph_id);
1433 return -1;
1434 }
1435 clrsetbits_le32(reg, mask << shift, (main & mask) << shift);
1436 clrsetbits_le32(reg, mask << pre_shift, (fine & mask) << pre_shift);
1437
1438 return 0;
1439}
1440
Rajeshwari Birjeac892d02013-12-26 09:44:21 +05301441static int exynos5420_set_spi_clk(enum periph_id periph_id,
1442 unsigned int rate)
1443{
1444 struct exynos5420_clock *clk =
1445 (struct exynos5420_clock *)samsung_get_base_clock();
1446 int main;
1447 unsigned int fine;
1448 unsigned shift, pre_shift;
1449 unsigned div_mask = 0xf, pre_div_mask = 0xff;
1450 u32 *reg;
1451 u32 *pre_reg;
1452
1453 main = clock_calc_best_scalar(4, 8, 400000000, rate, &fine);
1454 if (main < 0) {
1455 debug("%s: Cannot set clock rate for periph %d",
1456 __func__, periph_id);
1457 return -1;
1458 }
1459 main = main - 1;
1460 fine = fine - 1;
1461
1462 switch (periph_id) {
1463 case PERIPH_ID_SPI0:
1464 reg = &clk->div_peric1;
1465 shift = 20;
1466 pre_reg = &clk->div_peric4;
1467 pre_shift = 8;
1468 break;
1469 case PERIPH_ID_SPI1:
1470 reg = &clk->div_peric1;
1471 shift = 24;
1472 pre_reg = &clk->div_peric4;
1473 pre_shift = 16;
1474 break;
1475 case PERIPH_ID_SPI2:
1476 reg = &clk->div_peric1;
1477 shift = 28;
1478 pre_reg = &clk->div_peric4;
1479 pre_shift = 24;
1480 break;
1481 case PERIPH_ID_SPI3:
1482 reg = &clk->div_isp1;
1483 shift = 16;
1484 pre_reg = &clk->div_isp1;
1485 pre_shift = 0;
1486 break;
1487 case PERIPH_ID_SPI4:
1488 reg = &clk->div_isp1;
1489 shift = 20;
1490 pre_reg = &clk->div_isp1;
1491 pre_shift = 8;
1492 break;
1493 default:
1494 debug("%s: Unsupported peripheral ID %d\n", __func__,
1495 periph_id);
1496 return -1;
1497 }
1498
1499 clrsetbits_le32(reg, div_mask << shift, (main & div_mask) << shift);
1500 clrsetbits_le32(pre_reg, pre_div_mask << pre_shift,
1501 (fine & pre_div_mask) << pre_shift);
1502
1503 return 0;
1504}
1505
Piotr Wilczek01d589f2012-11-20 02:19:02 +00001506static unsigned long exynos4_get_i2c_clk(void)
1507{
1508 struct exynos4_clock *clk =
1509 (struct exynos4_clock *)samsung_get_base_clock();
1510 unsigned long sclk, aclk_100;
1511 unsigned int ratio;
1512
1513 sclk = get_pll_clk(APLL);
1514
1515 ratio = (readl(&clk->div_top)) >> 4;
1516 ratio &= 0xf;
1517 aclk_100 = sclk / (ratio + 1);
1518 return aclk_100;
Rajeshwari Shinde1c9412a2012-07-23 21:23:48 +00001519}
1520
Minkyu Kangb1b24682011-01-24 15:22:23 +09001521unsigned long get_pll_clk(int pllreg)
1522{
Rajeshwari Birjeac892d02013-12-26 09:44:21 +05301523 if (cpu_is_exynos5()) {
Akshay Saraswat9fba7b42014-11-13 22:38:15 +05301524 if (proid_is_exynos5420() || proid_is_exynos5800())
Akshay Saraswat1bd40e42015-02-04 16:00:00 +05301525 return exynos542x_get_pll_clk(pllreg);
Chander Kashyap34076a02012-02-05 23:01:46 +00001526 return exynos5_get_pll_clk(pllreg);
Rajeshwari Birjeac892d02013-12-26 09:44:21 +05301527 } else {
Chander Kashyap400ab162012-10-07 01:43:17 +00001528 if (proid_is_exynos4412())
1529 return exynos4x12_get_pll_clk(pllreg);
Chander Kashyap34076a02012-02-05 23:01:46 +00001530 return exynos4_get_pll_clk(pllreg);
Chander Kashyap400ab162012-10-07 01:43:17 +00001531 }
Minkyu Kangb1b24682011-01-24 15:22:23 +09001532}
1533
1534unsigned long get_arm_clk(void)
1535{
Chander Kashyap34076a02012-02-05 23:01:46 +00001536 if (cpu_is_exynos5())
1537 return exynos5_get_arm_clk();
Chander Kashyap400ab162012-10-07 01:43:17 +00001538 else {
1539 if (proid_is_exynos4412())
1540 return exynos4x12_get_arm_clk();
Chander Kashyap34076a02012-02-05 23:01:46 +00001541 return exynos4_get_arm_clk();
Chander Kashyap400ab162012-10-07 01:43:17 +00001542 }
Minkyu Kangb1b24682011-01-24 15:22:23 +09001543}
1544
Rajeshwari Shinde1c9412a2012-07-23 21:23:48 +00001545unsigned long get_i2c_clk(void)
1546{
1547 if (cpu_is_exynos5()) {
Akshay Saraswat348bc5f2015-02-04 16:00:03 +05301548 return clock_get_periph_rate(PERIPH_ID_I2C0);
Piotr Wilczek01d589f2012-11-20 02:19:02 +00001549 } else if (cpu_is_exynos4()) {
1550 return exynos4_get_i2c_clk();
Rajeshwari Shinde1c9412a2012-07-23 21:23:48 +00001551 } else {
1552 debug("I2C clock is not set for this CPU\n");
1553 return 0;
1554 }
1555}
1556
Minkyu Kangb1b24682011-01-24 15:22:23 +09001557unsigned long get_pwm_clk(void)
1558{
Rajeshwari Birjeac892d02013-12-26 09:44:21 +05301559 if (cpu_is_exynos5()) {
Padmavathi Vennabb714162013-03-28 04:32:23 +00001560 return clock_get_periph_rate(PERIPH_ID_PWM0);
Rajeshwari Birjeac892d02013-12-26 09:44:21 +05301561 } else {
Chander Kashyap400ab162012-10-07 01:43:17 +00001562 if (proid_is_exynos4412())
1563 return exynos4x12_get_pwm_clk();
Chander Kashyap34076a02012-02-05 23:01:46 +00001564 return exynos4_get_pwm_clk();
Chander Kashyap400ab162012-10-07 01:43:17 +00001565 }
Minkyu Kangb1b24682011-01-24 15:22:23 +09001566}
1567
1568unsigned long get_uart_clk(int dev_index)
1569{
Akshay Saraswat348bc5f2015-02-04 16:00:03 +05301570 enum periph_id id;
1571
1572 switch (dev_index) {
1573 case 0:
1574 id = PERIPH_ID_UART0;
1575 break;
1576 case 1:
1577 id = PERIPH_ID_UART1;
1578 break;
1579 case 2:
1580 id = PERIPH_ID_UART2;
1581 break;
1582 case 3:
1583 id = PERIPH_ID_UART3;
1584 break;
1585 default:
1586 debug("%s: invalid UART index %d", __func__, dev_index);
1587 return -1;
1588 }
1589
Rajeshwari Birjeac892d02013-12-26 09:44:21 +05301590 if (cpu_is_exynos5()) {
Akshay Saraswat348bc5f2015-02-04 16:00:03 +05301591 return clock_get_periph_rate(id);
Rajeshwari Birjeac892d02013-12-26 09:44:21 +05301592 } else {
Chander Kashyap400ab162012-10-07 01:43:17 +00001593 if (proid_is_exynos4412())
1594 return exynos4x12_get_uart_clk(dev_index);
Chander Kashyap34076a02012-02-05 23:01:46 +00001595 return exynos4_get_uart_clk(dev_index);
Chander Kashyap400ab162012-10-07 01:43:17 +00001596 }
Minkyu Kangb1b24682011-01-24 15:22:23 +09001597}
Jaehoon Chung9a772212011-05-17 21:19:17 +00001598
Jaehoon Chung8788e062012-12-27 22:30:32 +00001599unsigned long get_mmc_clk(int dev_index)
1600{
Akshay Saraswat348bc5f2015-02-04 16:00:03 +05301601 enum periph_id id;
1602
1603 switch (dev_index) {
1604 case 0:
1605 id = PERIPH_ID_SDMMC0;
1606 break;
1607 case 1:
1608 id = PERIPH_ID_SDMMC1;
1609 break;
1610 case 2:
1611 id = PERIPH_ID_SDMMC2;
1612 break;
1613 case 3:
1614 id = PERIPH_ID_SDMMC3;
1615 break;
1616 default:
1617 debug("%s: invalid MMC index %d", __func__, dev_index);
1618 return -1;
1619 }
1620
Rajeshwari Birjeac892d02013-12-26 09:44:21 +05301621 if (cpu_is_exynos5()) {
Akshay Saraswat348bc5f2015-02-04 16:00:03 +05301622 return clock_get_periph_rate(id);
Rajeshwari Birjeac892d02013-12-26 09:44:21 +05301623 } else {
Jaehoon Chung8788e062012-12-27 22:30:32 +00001624 return exynos4_get_mmc_clk(dev_index);
Rajeshwari Birjeac892d02013-12-26 09:44:21 +05301625 }
Jaehoon Chung8788e062012-12-27 22:30:32 +00001626}
1627
Jaehoon Chung9a772212011-05-17 21:19:17 +00001628void set_mmc_clk(int dev_index, unsigned int div)
1629{
Jaehoon Chung15cd6b52015-01-08 16:50:21 +09001630 /* If want to set correct value, it needs to substract one from div.*/
1631 if (div > 0)
1632 div -= 1;
1633
Rajeshwari Birjeac892d02013-12-26 09:44:21 +05301634 if (cpu_is_exynos5()) {
Akshay Saraswat9fba7b42014-11-13 22:38:15 +05301635 if (proid_is_exynos5420() || proid_is_exynos5800())
Rajeshwari Birjeac892d02013-12-26 09:44:21 +05301636 exynos5420_set_mmc_clk(dev_index, div);
1637 else
1638 exynos5_set_mmc_clk(dev_index, div);
1639 } else {
Beomho Seo99660cc2014-05-16 13:59:47 +09001640 exynos4_set_mmc_clk(dev_index, div);
Chander Kashyap400ab162012-10-07 01:43:17 +00001641 }
Jaehoon Chung9a772212011-05-17 21:19:17 +00001642}
Donghwa Lee77ba1912012-04-05 19:36:12 +00001643
1644unsigned long get_lcd_clk(void)
1645{
1646 if (cpu_is_exynos4())
1647 return exynos4_get_lcd_clk();
Ajay Kumar914af872014-09-05 16:53:32 +05301648 else {
Akshay Saraswat9fba7b42014-11-13 22:38:15 +05301649 if (proid_is_exynos5420() || proid_is_exynos5800())
Ajay Kumar914af872014-09-05 16:53:32 +05301650 return exynos5420_get_lcd_clk();
1651 else
1652 return exynos5_get_lcd_clk();
1653 }
Donghwa Lee77ba1912012-04-05 19:36:12 +00001654}
1655
1656void set_lcd_clk(void)
1657{
1658 if (cpu_is_exynos4())
1659 exynos4_set_lcd_clk();
Ajay Kumar914af872014-09-05 16:53:32 +05301660 else {
1661 if (proid_is_exynos5250())
1662 exynos5_set_lcd_clk();
Akshay Saraswat9fba7b42014-11-13 22:38:15 +05301663 else if (proid_is_exynos5420() || proid_is_exynos5800())
Ajay Kumar914af872014-09-05 16:53:32 +05301664 exynos5420_set_lcd_clk();
1665 }
Donghwa Lee77ba1912012-04-05 19:36:12 +00001666}
1667
1668void set_mipi_clk(void)
1669{
1670 if (cpu_is_exynos4())
1671 exynos4_set_mipi_clk();
1672}
Rajeshwari Shinde392a73a2012-10-25 19:49:29 +00001673
Hatim RVe6365b62012-11-02 01:15:34 +00001674int set_spi_clk(int periph_id, unsigned int rate)
1675{
Rajeshwari Birjeac892d02013-12-26 09:44:21 +05301676 if (cpu_is_exynos5()) {
Akshay Saraswat9fba7b42014-11-13 22:38:15 +05301677 if (proid_is_exynos5420() || proid_is_exynos5800())
Rajeshwari Birjeac892d02013-12-26 09:44:21 +05301678 return exynos5420_set_spi_clk(periph_id, rate);
Hatim RVe6365b62012-11-02 01:15:34 +00001679 return exynos5_set_spi_clk(periph_id, rate);
Rajeshwari Birjeac892d02013-12-26 09:44:21 +05301680 } else {
Hatim RVe6365b62012-11-02 01:15:34 +00001681 return 0;
Rajeshwari Birjeac892d02013-12-26 09:44:21 +05301682 }
Hatim RVe6365b62012-11-02 01:15:34 +00001683}
1684
Dani Krishna Mohan65c7ee62013-09-11 16:38:48 +05301685int set_i2s_clk_prescaler(unsigned int src_frq, unsigned int dst_frq,
1686 unsigned int i2s_id)
Rajeshwari Shinde392a73a2012-10-25 19:49:29 +00001687{
Rajeshwari Shinde392a73a2012-10-25 19:49:29 +00001688 if (cpu_is_exynos5())
Dani Krishna Mohan65c7ee62013-09-11 16:38:48 +05301689 return exynos5_set_i2s_clk_prescaler(src_frq, dst_frq, i2s_id);
Rajeshwari Shinde392a73a2012-10-25 19:49:29 +00001690 else
1691 return 0;
1692}
1693
Dani Krishna Mohan65c7ee62013-09-11 16:38:48 +05301694int set_i2s_clk_source(unsigned int i2s_id)
Rajeshwari Shinde392a73a2012-10-25 19:49:29 +00001695{
1696 if (cpu_is_exynos5())
Dani Krishna Mohan65c7ee62013-09-11 16:38:48 +05301697 return exynos5_set_i2s_clk_source(i2s_id);
1698 else
1699 return 0;
Rajeshwari Shinde392a73a2012-10-25 19:49:29 +00001700}
1701
1702int set_epll_clk(unsigned long rate)
1703{
1704 if (cpu_is_exynos5())
1705 return exynos5_set_epll_clk(rate);
1706 else
1707 return 0;
1708}