2 * Copyright (C) 2010 Samsung Electronics
3 * Minkyu Kang <mk7.kang@samsung.com>
5 * SPDX-License-Identifier: GPL-2.0+
10 #include <asm/arch/clock.h>
11 #include <asm/arch/clk.h>
12 #include <asm/arch/periph.h>
14 #define PLL_DIV_1024 1024
15 #define PLL_DIV_65535 65535
16 #define PLL_DIV_65536 65536
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
28 /* src_bit div_bit prediv_bit */
29 static struct clk_bit_info clk_bit_info[] = {
61 /* Epll Clock division values to achive different frequency output */
62 static struct set_epll_con_val exynos5_epll_div[] = {
63 { 192000000, 0, 48, 3, 1, 0 },
64 { 180000000, 0, 45, 3, 1, 0 },
65 { 73728000, 1, 73, 3, 3, 47710 },
66 { 67737600, 1, 90, 4, 3, 20762 },
67 { 49152000, 0, 49, 3, 3, 9961 },
68 { 45158400, 0, 45, 3, 3, 10381 },
69 { 180633600, 0, 45, 3, 1, 10381 }
72 /* exynos: return pll clock frequency */
73 static int exynos_get_pll_clk(int pllreg, unsigned int r, unsigned int k)
75 unsigned long m, p, s = 0, mask, fout;
79 * APLL_CON: MIDV [25:16]
80 * MPLL_CON: MIDV [25:16]
81 * EPLL_CON: MIDV [24:16]
82 * VPLL_CON: MIDV [24:16]
83 * BPLL_CON: MIDV [25:16]: Exynos5
85 if (pllreg == APLL || pllreg == MPLL || pllreg == BPLL ||
98 freq = CONFIG_SYS_CLK_FREQ;
100 if (pllreg == EPLL || pllreg == RPLL) {
102 /* FOUT = (MDIV + K / 65536) * FIN / (PDIV * 2^SDIV) */
103 fout = (m + k / PLL_DIV_65536) * (freq / (p * (1 << s)));
104 } else if (pllreg == VPLL) {
109 * FOUT = (MDIV + K / 1024) * FIN / (PDIV * 2^SDIV)
112 * FOUT = (MDIV + K / 65535) * FIN / (PDIV * 2^SDIV)
115 * FOUT = (MDIV + K / 65536) * FIN / (PDIV * 2^SDIV)
117 if (proid_is_exynos4210())
119 else if (proid_is_exynos4412())
121 else if (proid_is_exynos5250() || proid_is_exynos5420())
126 fout = (m + k / div) * (freq / (p * (1 << s)));
129 * Exynos4412 / Exynos5250
130 * FOUT = MDIV * FIN / (PDIV * 2^SDIV)
133 * FOUT = MDIV * FIN / (PDIV * 2^(SDIV-1))
135 if (proid_is_exynos4210())
136 fout = m * (freq / (p * (1 << (s - 1))));
138 fout = m * (freq / (p * (1 << s)));
143 /* exynos4: return pll clock frequency */
144 static unsigned long exynos4_get_pll_clk(int pllreg)
146 struct exynos4_clock *clk =
147 (struct exynos4_clock *)samsung_get_base_clock();
148 unsigned long r, k = 0;
152 r = readl(&clk->apll_con0);
155 r = readl(&clk->mpll_con0);
158 r = readl(&clk->epll_con0);
159 k = readl(&clk->epll_con1);
162 r = readl(&clk->vpll_con0);
163 k = readl(&clk->vpll_con1);
166 printf("Unsupported PLL (%d)\n", pllreg);
170 return exynos_get_pll_clk(pllreg, r, k);
173 /* exynos4x12: return pll clock frequency */
174 static unsigned long exynos4x12_get_pll_clk(int pllreg)
176 struct exynos4x12_clock *clk =
177 (struct exynos4x12_clock *)samsung_get_base_clock();
178 unsigned long r, k = 0;
182 r = readl(&clk->apll_con0);
185 r = readl(&clk->mpll_con0);
188 r = readl(&clk->epll_con0);
189 k = readl(&clk->epll_con1);
192 r = readl(&clk->vpll_con0);
193 k = readl(&clk->vpll_con1);
196 printf("Unsupported PLL (%d)\n", pllreg);
200 return exynos_get_pll_clk(pllreg, r, k);
203 /* exynos5: return pll clock frequency */
204 static unsigned long exynos5_get_pll_clk(int pllreg)
206 struct exynos5_clock *clk =
207 (struct exynos5_clock *)samsung_get_base_clock();
208 unsigned long r, k = 0, fout;
209 unsigned int pll_div2_sel, fout_sel;
213 r = readl(&clk->apll_con0);
216 r = readl(&clk->mpll_con0);
219 r = readl(&clk->epll_con0);
220 k = readl(&clk->epll_con1);
223 r = readl(&clk->vpll_con0);
224 k = readl(&clk->vpll_con1);
227 r = readl(&clk->bpll_con0);
230 printf("Unsupported PLL (%d)\n", pllreg);
234 fout = exynos_get_pll_clk(pllreg, r, k);
236 /* According to the user manual, in EVT1 MPLL and BPLL always gives
237 * 1.6GHz clock, so divide by 2 to get 800MHz MPLL clock.*/
238 if (pllreg == MPLL || pllreg == BPLL) {
239 pll_div2_sel = readl(&clk->pll_div2_sel);
243 fout_sel = (pll_div2_sel >> MPLL_FOUT_SEL_SHIFT)
244 & MPLL_FOUT_SEL_MASK;
247 fout_sel = (pll_div2_sel >> BPLL_FOUT_SEL_SHIFT)
248 & BPLL_FOUT_SEL_MASK;
262 static unsigned long exynos5_get_periph_rate(int peripheral)
264 struct clk_bit_info *bit_info = &clk_bit_info[peripheral];
265 unsigned long sclk, sub_clk;
266 unsigned int src, div, sub_div;
267 struct exynos5_clock *clk =
268 (struct exynos5_clock *)samsung_get_base_clock();
270 switch (peripheral) {
271 case PERIPH_ID_UART0:
272 case PERIPH_ID_UART1:
273 case PERIPH_ID_UART2:
274 case PERIPH_ID_UART3:
275 src = readl(&clk->src_peric0);
276 div = readl(&clk->div_peric0);
283 src = readl(&clk->src_peric0);
284 div = readl(&clk->div_peric3);
287 src = readl(&clk->src_mau);
288 div = readl(&clk->div_mau);
291 src = readl(&clk->src_peric1);
292 div = readl(&clk->div_peric1);
295 src = readl(&clk->src_peric1);
296 div = readl(&clk->div_peric2);
300 src = readl(&clk->sclk_src_isp);
301 div = readl(&clk->sclk_div_isp);
303 case PERIPH_ID_SDMMC0:
304 case PERIPH_ID_SDMMC1:
305 case PERIPH_ID_SDMMC2:
306 case PERIPH_ID_SDMMC3:
307 src = readl(&clk->src_fsys);
308 div = readl(&clk->div_fsys1);
318 sclk = exynos5_get_pll_clk(MPLL);
319 sub_div = ((readl(&clk->div_top1) >> bit_info->div_bit)
321 div = ((readl(&clk->div_top0) >> bit_info->prediv_bit)
323 return (sclk / sub_div) / div;
325 debug("%s: invalid peripheral %d", __func__, peripheral);
329 src = (src >> bit_info->src_bit) & 0xf;
332 case EXYNOS_SRC_MPLL:
333 sclk = exynos5_get_pll_clk(MPLL);
335 case EXYNOS_SRC_EPLL:
336 sclk = exynos5_get_pll_clk(EPLL);
338 case EXYNOS_SRC_VPLL:
339 sclk = exynos5_get_pll_clk(VPLL);
345 /* Ratio clock division for this peripheral */
346 sub_div = (div >> bit_info->div_bit) & 0xf;
347 sub_clk = sclk / (sub_div + 1);
349 /* Pre-ratio clock division for SDMMC0 and 2 */
350 if (peripheral == PERIPH_ID_SDMMC0 || peripheral == PERIPH_ID_SDMMC2) {
351 div = (div >> bit_info->prediv_bit) & 0xff;
352 return sub_clk / (div + 1);
358 unsigned long clock_get_periph_rate(int peripheral)
360 if (cpu_is_exynos5())
361 return exynos5_get_periph_rate(peripheral);
366 /* exynos5420: return pll clock frequency */
367 static unsigned long exynos5420_get_pll_clk(int pllreg)
369 struct exynos5420_clock *clk =
370 (struct exynos5420_clock *)samsung_get_base_clock();
371 unsigned long r, k = 0;
375 r = readl(&clk->apll_con0);
378 r = readl(&clk->mpll_con0);
381 r = readl(&clk->epll_con0);
382 k = readl(&clk->epll_con1);
385 r = readl(&clk->vpll_con0);
386 k = readl(&clk->vpll_con1);
389 r = readl(&clk->bpll_con0);
392 r = readl(&clk->rpll_con0);
393 k = readl(&clk->rpll_con1);
396 r = readl(&clk->spll_con0);
399 printf("Unsupported PLL (%d)\n", pllreg);
403 return exynos_get_pll_clk(pllreg, r, k);
406 /* exynos4: return ARM clock frequency */
407 static unsigned long exynos4_get_arm_clk(void)
409 struct exynos4_clock *clk =
410 (struct exynos4_clock *)samsung_get_base_clock();
412 unsigned long armclk;
413 unsigned int core_ratio;
414 unsigned int core2_ratio;
416 div = readl(&clk->div_cpu0);
418 /* CORE_RATIO: [2:0], CORE2_RATIO: [30:28] */
419 core_ratio = (div >> 0) & 0x7;
420 core2_ratio = (div >> 28) & 0x7;
422 armclk = get_pll_clk(APLL) / (core_ratio + 1);
423 armclk /= (core2_ratio + 1);
428 /* exynos4x12: return ARM clock frequency */
429 static unsigned long exynos4x12_get_arm_clk(void)
431 struct exynos4x12_clock *clk =
432 (struct exynos4x12_clock *)samsung_get_base_clock();
434 unsigned long armclk;
435 unsigned int core_ratio;
436 unsigned int core2_ratio;
438 div = readl(&clk->div_cpu0);
440 /* CORE_RATIO: [2:0], CORE2_RATIO: [30:28] */
441 core_ratio = (div >> 0) & 0x7;
442 core2_ratio = (div >> 28) & 0x7;
444 armclk = get_pll_clk(APLL) / (core_ratio + 1);
445 armclk /= (core2_ratio + 1);
450 /* exynos5: return ARM clock frequency */
451 static unsigned long exynos5_get_arm_clk(void)
453 struct exynos5_clock *clk =
454 (struct exynos5_clock *)samsung_get_base_clock();
456 unsigned long armclk;
457 unsigned int arm_ratio;
458 unsigned int arm2_ratio;
460 div = readl(&clk->div_cpu0);
462 /* ARM_RATIO: [2:0], ARM2_RATIO: [30:28] */
463 arm_ratio = (div >> 0) & 0x7;
464 arm2_ratio = (div >> 28) & 0x7;
466 armclk = get_pll_clk(APLL) / (arm_ratio + 1);
467 armclk /= (arm2_ratio + 1);
472 /* exynos4: return pwm clock frequency */
473 static unsigned long exynos4_get_pwm_clk(void)
475 struct exynos4_clock *clk =
476 (struct exynos4_clock *)samsung_get_base_clock();
477 unsigned long pclk, sclk;
481 if (s5p_get_cpu_rev() == 0) {
486 sel = readl(&clk->src_peril0);
487 sel = (sel >> 24) & 0xf;
490 sclk = get_pll_clk(MPLL);
492 sclk = get_pll_clk(EPLL);
494 sclk = get_pll_clk(VPLL);
502 ratio = readl(&clk->div_peril3);
504 } else if (s5p_get_cpu_rev() == 1) {
505 sclk = get_pll_clk(MPLL);
510 pclk = sclk / (ratio + 1);
515 /* exynos4x12: return pwm clock frequency */
516 static unsigned long exynos4x12_get_pwm_clk(void)
518 unsigned long pclk, sclk;
521 sclk = get_pll_clk(MPLL);
524 pclk = sclk / (ratio + 1);
529 /* exynos5420: return pwm clock frequency */
530 static unsigned long exynos5420_get_pwm_clk(void)
532 struct exynos5420_clock *clk =
533 (struct exynos5420_clock *)samsung_get_base_clock();
534 unsigned long pclk, sclk;
541 ratio = readl(&clk->div_peric0);
542 ratio = (ratio >> 28) & 0xf;
543 sclk = get_pll_clk(MPLL);
545 pclk = sclk / (ratio + 1);
550 /* exynos4: return uart clock frequency */
551 static unsigned long exynos4_get_uart_clk(int dev_index)
553 struct exynos4_clock *clk =
554 (struct exynos4_clock *)samsung_get_base_clock();
555 unsigned long uclk, sclk;
568 sel = readl(&clk->src_peril0);
569 sel = (sel >> (dev_index << 2)) & 0xf;
572 sclk = get_pll_clk(MPLL);
574 sclk = get_pll_clk(EPLL);
576 sclk = get_pll_clk(VPLL);
585 * UART3_RATIO [12:15]
586 * UART4_RATIO [16:19]
587 * UART5_RATIO [23:20]
589 ratio = readl(&clk->div_peril0);
590 ratio = (ratio >> (dev_index << 2)) & 0xf;
592 uclk = sclk / (ratio + 1);
597 /* exynos4x12: return uart clock frequency */
598 static unsigned long exynos4x12_get_uart_clk(int dev_index)
600 struct exynos4x12_clock *clk =
601 (struct exynos4x12_clock *)samsung_get_base_clock();
602 unsigned long uclk, sclk;
614 sel = readl(&clk->src_peril0);
615 sel = (sel >> (dev_index << 2)) & 0xf;
618 sclk = get_pll_clk(MPLL);
620 sclk = get_pll_clk(EPLL);
622 sclk = get_pll_clk(VPLL);
631 * UART3_RATIO [12:15]
632 * UART4_RATIO [16:19]
634 ratio = readl(&clk->div_peril0);
635 ratio = (ratio >> (dev_index << 2)) & 0xf;
637 uclk = sclk / (ratio + 1);
642 /* exynos5: return uart clock frequency */
643 static unsigned long exynos5_get_uart_clk(int dev_index)
645 struct exynos5_clock *clk =
646 (struct exynos5_clock *)samsung_get_base_clock();
647 unsigned long uclk, sclk;
660 sel = readl(&clk->src_peric0);
661 sel = (sel >> (dev_index << 2)) & 0xf;
664 sclk = get_pll_clk(MPLL);
666 sclk = get_pll_clk(EPLL);
668 sclk = get_pll_clk(VPLL);
677 * UART3_RATIO [12:15]
678 * UART4_RATIO [16:19]
679 * UART5_RATIO [23:20]
681 ratio = readl(&clk->div_peric0);
682 ratio = (ratio >> (dev_index << 2)) & 0xf;
684 uclk = sclk / (ratio + 1);
689 /* exynos5420: return uart clock frequency */
690 static unsigned long exynos5420_get_uart_clk(int dev_index)
692 struct exynos5420_clock *clk =
693 (struct exynos5420_clock *)samsung_get_base_clock();
694 unsigned long uclk, sclk;
704 * generalised calculation as follows
705 * sel = (sel >> ((dev_index * 4) + 4)) & mask;
707 sel = readl(&clk->src_peric0);
708 sel = (sel >> ((dev_index * 4) + 4)) & 0x7;
711 sclk = get_pll_clk(MPLL);
713 sclk = get_pll_clk(EPLL);
715 sclk = get_pll_clk(RPLL);
722 * UART1_RATIO [15:12]
723 * UART2_RATIO [19:16]
724 * UART3_RATIO [23:20]
725 * generalised calculation as follows
726 * ratio = (ratio >> ((dev_index * 4) + 8)) & mask;
728 ratio = readl(&clk->div_peric0);
729 ratio = (ratio >> ((dev_index * 4) + 8)) & 0xf;
731 uclk = sclk / (ratio + 1);
736 static unsigned long exynos4_get_mmc_clk(int dev_index)
738 struct exynos4_clock *clk =
739 (struct exynos4_clock *)samsung_get_base_clock();
740 unsigned long uclk, sclk;
741 unsigned int sel, ratio, pre_ratio;
744 sel = readl(&clk->src_fsys);
745 sel = (sel >> (dev_index << 2)) & 0xf;
748 sclk = get_pll_clk(MPLL);
750 sclk = get_pll_clk(EPLL);
752 sclk = get_pll_clk(VPLL);
759 ratio = readl(&clk->div_fsys1);
760 pre_ratio = readl(&clk->div_fsys1);
764 ratio = readl(&clk->div_fsys2);
765 pre_ratio = readl(&clk->div_fsys2);
768 ratio = readl(&clk->div_fsys3);
769 pre_ratio = readl(&clk->div_fsys3);
775 if (dev_index == 1 || dev_index == 3)
778 ratio = (ratio >> shift) & 0xf;
779 pre_ratio = (pre_ratio >> (shift + 8)) & 0xff;
780 uclk = (sclk / (ratio + 1)) / (pre_ratio + 1);
785 static unsigned long exynos5_get_mmc_clk(int dev_index)
787 struct exynos5_clock *clk =
788 (struct exynos5_clock *)samsung_get_base_clock();
789 unsigned long uclk, sclk;
790 unsigned int sel, ratio, pre_ratio;
793 sel = readl(&clk->src_fsys);
794 sel = (sel >> (dev_index << 2)) & 0xf;
797 sclk = get_pll_clk(MPLL);
799 sclk = get_pll_clk(EPLL);
801 sclk = get_pll_clk(VPLL);
808 ratio = readl(&clk->div_fsys1);
809 pre_ratio = readl(&clk->div_fsys1);
813 ratio = readl(&clk->div_fsys2);
814 pre_ratio = readl(&clk->div_fsys2);
820 if (dev_index == 1 || dev_index == 3)
823 ratio = (ratio >> shift) & 0xf;
824 pre_ratio = (pre_ratio >> (shift + 8)) & 0xff;
825 uclk = (sclk / (ratio + 1)) / (pre_ratio + 1);
830 static unsigned long exynos5420_get_mmc_clk(int dev_index)
832 struct exynos5420_clock *clk =
833 (struct exynos5420_clock *)samsung_get_base_clock();
834 unsigned long uclk, sclk;
835 unsigned int sel, ratio;
842 * generalised calculation as follows
843 * sel = (sel >> ((dev_index * 4) + 8)) & mask
845 sel = readl(&clk->src_fsys);
846 sel = (sel >> ((dev_index * 4) + 8)) & 0x7;
849 sclk = get_pll_clk(MPLL);
851 sclk = get_pll_clk(EPLL);
860 * generalised calculation as follows
861 * ratio = (ratio >> (dev_index * 10)) & mask
863 ratio = readl(&clk->div_fsys1);
864 ratio = (ratio >> (dev_index * 10)) & 0x3ff;
866 uclk = (sclk / (ratio + 1));
871 /* exynos4: set the mmc clock */
872 static void exynos4_set_mmc_clk(int dev_index, unsigned int div)
874 struct exynos4_clock *clk =
875 (struct exynos4_clock *)samsung_get_base_clock();
876 unsigned int addr, clear_bit, set_bit;
880 * MMC0_PRE_RATIO [15:8], MMC1_PRE_RATIO [31:24]
882 * MMC2_PRE_RATIO [15:8], MMC3_PRE_RATIO [31:24]
887 addr = (unsigned int)&clk->div_fsys1;
888 clear_bit = MASK_PRE_RATIO(dev_index);
889 set_bit = SET_PRE_RATIO(dev_index, div);
890 } else if (dev_index == 4) {
891 addr = (unsigned int)&clk->div_fsys3;
893 /* MMC4 is controlled with the MMC4_RATIO value */
894 clear_bit = MASK_RATIO(dev_index);
895 set_bit = SET_RATIO(dev_index, div);
897 addr = (unsigned int)&clk->div_fsys2;
899 clear_bit = MASK_PRE_RATIO(dev_index);
900 set_bit = SET_PRE_RATIO(dev_index, div);
903 clrsetbits_le32(addr, clear_bit, set_bit);
906 /* exynos5: set the mmc clock */
907 static void exynos5_set_mmc_clk(int dev_index, unsigned int div)
909 struct exynos5_clock *clk =
910 (struct exynos5_clock *)samsung_get_base_clock();
915 * MMC0_PRE_RATIO [15:8], MMC1_PRE_RATIO [31:24]
917 * MMC2_PRE_RATIO [15:8], MMC3_PRE_RATIO [31:24]
920 addr = (unsigned int)&clk->div_fsys1;
922 addr = (unsigned int)&clk->div_fsys2;
926 clrsetbits_le32(addr, 0xff << ((dev_index << 4) + 8),
927 (div & 0xff) << ((dev_index << 4) + 8));
930 /* exynos5: set the mmc clock */
931 static void exynos5420_set_mmc_clk(int dev_index, unsigned int div)
933 struct exynos5420_clock *clk =
934 (struct exynos5420_clock *)samsung_get_base_clock();
944 addr = (unsigned int)&clk->div_fsys1;
945 shift = dev_index * 10;
947 clrsetbits_le32(addr, 0x3ff << shift, (div & 0x3ff) << shift);
950 /* get_lcd_clk: return lcd clock frequency */
951 static unsigned long exynos4_get_lcd_clk(void)
953 struct exynos4_clock *clk =
954 (struct exynos4_clock *)samsung_get_base_clock();
955 unsigned long pclk, sclk;
963 sel = readl(&clk->src_lcd0);
972 sclk = get_pll_clk(MPLL);
974 sclk = get_pll_clk(EPLL);
976 sclk = get_pll_clk(VPLL);
984 ratio = readl(&clk->div_lcd0);
987 pclk = sclk / (ratio + 1);
992 /* get_lcd_clk: return lcd clock frequency */
993 static unsigned long exynos5_get_lcd_clk(void)
995 struct exynos5_clock *clk =
996 (struct exynos5_clock *)samsung_get_base_clock();
997 unsigned long pclk, sclk;
1005 sel = readl(&clk->src_disp1_0);
1014 sclk = get_pll_clk(MPLL);
1015 else if (sel == 0x7)
1016 sclk = get_pll_clk(EPLL);
1017 else if (sel == 0x8)
1018 sclk = get_pll_clk(VPLL);
1026 ratio = readl(&clk->div_disp1_0);
1027 ratio = ratio & 0xf;
1029 pclk = sclk / (ratio + 1);
1034 static unsigned long exynos5420_get_lcd_clk(void)
1036 struct exynos5420_clock *clk =
1037 (struct exynos5420_clock *)samsung_get_base_clock();
1038 unsigned long pclk, sclk;
1048 sel = readl(&clk->src_disp10);
1052 sclk = get_pll_clk(SPLL);
1054 sclk = get_pll_clk(RPLL);
1060 ratio = readl(&clk->div_disp10);
1061 ratio = ratio & 0xf;
1063 pclk = sclk / (ratio + 1);
1068 void exynos4_set_lcd_clk(void)
1070 struct exynos4_clock *clk =
1071 (struct exynos4_clock *)samsung_get_base_clock();
1083 setbits_le32(&clk->gate_block, 1 << 4);
1089 * MDNIE_PWM0_SEL [8:11]
1091 * set lcd0 src clock 0x6: SCLK_MPLL
1093 clrsetbits_le32(&clk->src_lcd0, 0xf, 0x6);
1103 * Gating all clocks for FIMD0
1105 setbits_le32(&clk->gate_ip_lcd0, 1 << 0);
1110 * MDNIE0_RATIO [7:4]
1111 * MDNIE_PWM0_RATIO [11:8]
1112 * MDNIE_PWM_PRE_RATIO [15:12]
1113 * MIPI0_RATIO [19:16]
1114 * MIPI0_PRE_RATIO [23:20]
1117 clrsetbits_le32(&clk->div_lcd0, 0xf, 0x1);
1120 void exynos5_set_lcd_clk(void)
1122 struct exynos5_clock *clk =
1123 (struct exynos5_clock *)samsung_get_base_clock();
1135 setbits_le32(&clk->gate_block, 1 << 4);
1141 * MDNIE_PWM0_SEL [8:11]
1143 * set lcd0 src clock 0x6: SCLK_MPLL
1145 clrsetbits_le32(&clk->src_disp1_0, 0xf, 0x6);
1155 * Gating all clocks for FIMD0
1157 setbits_le32(&clk->gate_ip_disp1, 1 << 0);
1162 * MDNIE0_RATIO [7:4]
1163 * MDNIE_PWM0_RATIO [11:8]
1164 * MDNIE_PWM_PRE_RATIO [15:12]
1165 * MIPI0_RATIO [19:16]
1166 * MIPI0_PRE_RATIO [23:20]
1169 clrsetbits_le32(&clk->div_disp1_0, 0xf, 0x0);
1172 void exynos5420_set_lcd_clk(void)
1174 struct exynos5420_clock *clk =
1175 (struct exynos5420_clock *)samsung_get_base_clock();
1184 cfg = readl(&clk->src_disp10);
1187 writel(cfg, &clk->src_disp10);
1193 cfg = readl(&clk->div_disp10);
1196 writel(cfg, &clk->div_disp10);
1199 void exynos4_set_mipi_clk(void)
1201 struct exynos4_clock *clk =
1202 (struct exynos4_clock *)samsung_get_base_clock();
1208 * MDNIE_PWM0_SEL [8:11]
1210 * set mipi0 src clock 0x6: SCLK_MPLL
1212 clrsetbits_le32(&clk->src_lcd0, 0xf << 12, 0x6 << 12);
1218 * MDNIE_PWM0_MASK [8]
1220 * set src mask mipi0 0x1: Unmask
1222 setbits_le32(&clk->src_mask_lcd0, 0x1 << 12);
1232 * Gating all clocks for MIPI0
1234 setbits_le32(&clk->gate_ip_lcd0, 1 << 3);
1239 * MDNIE0_RATIO [7:4]
1240 * MDNIE_PWM0_RATIO [11:8]
1241 * MDNIE_PWM_PRE_RATIO [15:12]
1242 * MIPI0_RATIO [19:16]
1243 * MIPI0_PRE_RATIO [23:20]
1246 clrsetbits_le32(&clk->div_lcd0, 0xf << 16, 0x1 << 16);
1252 * exynos5: obtaining the I2C clock
1254 static unsigned long exynos5_get_i2c_clk(void)
1256 struct exynos5_clock *clk =
1257 (struct exynos5_clock *)samsung_get_base_clock();
1258 unsigned long aclk_66, aclk_66_pre, sclk;
1261 sclk = get_pll_clk(MPLL);
1263 ratio = (readl(&clk->div_top1)) >> 24;
1265 aclk_66_pre = sclk / (ratio + 1);
1266 ratio = readl(&clk->div_top0);
1268 aclk_66 = aclk_66_pre / (ratio + 1);
1272 int exynos5_set_epll_clk(unsigned long rate)
1274 unsigned int epll_con, epll_con_k;
1276 unsigned int lockcnt;
1278 struct exynos5_clock *clk =
1279 (struct exynos5_clock *)samsung_get_base_clock();
1281 epll_con = readl(&clk->epll_con0);
1282 epll_con &= ~((EPLL_CON0_LOCK_DET_EN_MASK <<
1283 EPLL_CON0_LOCK_DET_EN_SHIFT) |
1284 EPLL_CON0_MDIV_MASK << EPLL_CON0_MDIV_SHIFT |
1285 EPLL_CON0_PDIV_MASK << EPLL_CON0_PDIV_SHIFT |
1286 EPLL_CON0_SDIV_MASK << EPLL_CON0_SDIV_SHIFT);
1288 for (i = 0; i < ARRAY_SIZE(exynos5_epll_div); i++) {
1289 if (exynos5_epll_div[i].freq_out == rate)
1293 if (i == ARRAY_SIZE(exynos5_epll_div))
1296 epll_con_k = exynos5_epll_div[i].k_dsm << 0;
1297 epll_con |= exynos5_epll_div[i].en_lock_det <<
1298 EPLL_CON0_LOCK_DET_EN_SHIFT;
1299 epll_con |= exynos5_epll_div[i].m_div << EPLL_CON0_MDIV_SHIFT;
1300 epll_con |= exynos5_epll_div[i].p_div << EPLL_CON0_PDIV_SHIFT;
1301 epll_con |= exynos5_epll_div[i].s_div << EPLL_CON0_SDIV_SHIFT;
1304 * Required period ( in cycles) to genarate a stable clock output.
1305 * The maximum clock time can be up to 3000 * PDIV cycles of PLLs
1306 * frequency input (as per spec)
1308 lockcnt = 3000 * exynos5_epll_div[i].p_div;
1310 writel(lockcnt, &clk->epll_lock);
1311 writel(epll_con, &clk->epll_con0);
1312 writel(epll_con_k, &clk->epll_con1);
1314 start = get_timer(0);
1316 while (!(readl(&clk->epll_con0) &
1317 (0x1 << EXYNOS5_EPLLCON0_LOCKED_SHIFT))) {
1318 if (get_timer(start) > TIMEOUT_EPLL_LOCK) {
1319 debug("%s: Timeout waiting for EPLL lock\n", __func__);
1326 int exynos5_set_i2s_clk_source(unsigned int i2s_id)
1328 struct exynos5_clock *clk =
1329 (struct exynos5_clock *)samsung_get_base_clock();
1330 unsigned int *audio_ass = (unsigned int *)samsung_get_base_audio_ass();
1333 setbits_le32(&clk->src_top2, CLK_SRC_MOUT_EPLL);
1334 clrsetbits_le32(&clk->src_mau, AUDIO0_SEL_MASK,
1335 (CLK_SRC_SCLK_EPLL));
1336 setbits_le32(audio_ass, AUDIO_CLKMUX_ASS);
1337 } else if (i2s_id == 1) {
1338 clrsetbits_le32(&clk->src_peric1, AUDIO1_SEL_MASK,
1339 (CLK_SRC_SCLK_EPLL));
1346 int exynos5_set_i2s_clk_prescaler(unsigned int src_frq,
1347 unsigned int dst_frq,
1348 unsigned int i2s_id)
1350 struct exynos5_clock *clk =
1351 (struct exynos5_clock *)samsung_get_base_clock();
1354 if ((dst_frq == 0) || (src_frq == 0)) {
1355 debug("%s: Invalid requency input for prescaler\n", __func__);
1356 debug("src frq = %d des frq = %d ", src_frq, dst_frq);
1360 div = (src_frq / dst_frq);
1362 if (div > AUDIO_0_RATIO_MASK) {
1363 debug("%s: Frequency ratio is out of range\n",
1365 debug("src frq = %d des frq = %d ", src_frq, dst_frq);
1368 clrsetbits_le32(&clk->div_mau, AUDIO_0_RATIO_MASK,
1369 (div & AUDIO_0_RATIO_MASK));
1370 } else if(i2s_id == 1) {
1371 if (div > AUDIO_1_RATIO_MASK) {
1372 debug("%s: Frequency ratio is out of range\n",
1374 debug("src frq = %d des frq = %d ", src_frq, dst_frq);
1377 clrsetbits_le32(&clk->div_peric4, AUDIO_1_RATIO_MASK,
1378 (div & AUDIO_1_RATIO_MASK));
1386 * Linearly searches for the most accurate main and fine stage clock scalars
1387 * (divisors) for a specified target frequency and scalar bit sizes by checking
1388 * all multiples of main_scalar_bits values. Will always return scalars up to or
1389 * slower than target.
1391 * @param main_scalar_bits Number of main scalar bits, must be > 0 and < 32
1392 * @param fine_scalar_bits Number of fine scalar bits, must be > 0 and < 32
1393 * @param input_freq Clock frequency to be scaled in Hz
1394 * @param target_freq Desired clock frequency in Hz
1395 * @param best_fine_scalar Pointer to store the fine stage divisor
1397 * @return best_main_scalar Main scalar for desired frequency or -1 if none
1400 static int clock_calc_best_scalar(unsigned int main_scaler_bits,
1401 unsigned int fine_scalar_bits, unsigned int input_rate,
1402 unsigned int target_rate, unsigned int *best_fine_scalar)
1405 int best_main_scalar = -1;
1406 unsigned int best_error = target_rate;
1407 const unsigned int cap = (1 << fine_scalar_bits) - 1;
1408 const unsigned int loops = 1 << main_scaler_bits;
1410 debug("Input Rate is %u, Target is %u, Cap is %u\n", input_rate,
1413 assert(best_fine_scalar != NULL);
1414 assert(main_scaler_bits <= fine_scalar_bits);
1416 *best_fine_scalar = 1;
1418 if (input_rate == 0 || target_rate == 0)
1421 if (target_rate >= input_rate)
1424 for (i = 1; i <= loops; i++) {
1425 const unsigned int effective_div =
1426 max(min(input_rate / i / target_rate, cap), 1U);
1427 const unsigned int effective_rate = input_rate / i /
1429 const int error = target_rate - effective_rate;
1431 debug("%d|effdiv:%u, effrate:%u, error:%d\n", i, effective_div,
1432 effective_rate, error);
1434 if (error >= 0 && error <= best_error) {
1436 best_main_scalar = i;
1437 *best_fine_scalar = effective_div;
1441 return best_main_scalar;
1444 static int exynos5_set_spi_clk(enum periph_id periph_id,
1447 struct exynos5_clock *clk =
1448 (struct exynos5_clock *)samsung_get_base_clock();
1451 unsigned shift, pre_shift;
1452 unsigned mask = 0xff;
1455 main = clock_calc_best_scalar(4, 8, 400000000, rate, &fine);
1457 debug("%s: Cannot set clock rate for periph %d",
1458 __func__, periph_id);
1464 switch (periph_id) {
1465 case PERIPH_ID_SPI0:
1466 reg = &clk->div_peric1;
1470 case PERIPH_ID_SPI1:
1471 reg = &clk->div_peric1;
1475 case PERIPH_ID_SPI2:
1476 reg = &clk->div_peric2;
1480 case PERIPH_ID_SPI3:
1481 reg = &clk->sclk_div_isp;
1485 case PERIPH_ID_SPI4:
1486 reg = &clk->sclk_div_isp;
1491 debug("%s: Unsupported peripheral ID %d\n", __func__,
1495 clrsetbits_le32(reg, mask << shift, (main & mask) << shift);
1496 clrsetbits_le32(reg, mask << pre_shift, (fine & mask) << pre_shift);
1501 static int exynos5420_set_spi_clk(enum periph_id periph_id,
1504 struct exynos5420_clock *clk =
1505 (struct exynos5420_clock *)samsung_get_base_clock();
1508 unsigned shift, pre_shift;
1509 unsigned div_mask = 0xf, pre_div_mask = 0xff;
1513 main = clock_calc_best_scalar(4, 8, 400000000, rate, &fine);
1515 debug("%s: Cannot set clock rate for periph %d",
1516 __func__, periph_id);
1522 switch (periph_id) {
1523 case PERIPH_ID_SPI0:
1524 reg = &clk->div_peric1;
1526 pre_reg = &clk->div_peric4;
1529 case PERIPH_ID_SPI1:
1530 reg = &clk->div_peric1;
1532 pre_reg = &clk->div_peric4;
1535 case PERIPH_ID_SPI2:
1536 reg = &clk->div_peric1;
1538 pre_reg = &clk->div_peric4;
1541 case PERIPH_ID_SPI3:
1542 reg = &clk->div_isp1;
1544 pre_reg = &clk->div_isp1;
1547 case PERIPH_ID_SPI4:
1548 reg = &clk->div_isp1;
1550 pre_reg = &clk->div_isp1;
1554 debug("%s: Unsupported peripheral ID %d\n", __func__,
1559 clrsetbits_le32(reg, div_mask << shift, (main & div_mask) << shift);
1560 clrsetbits_le32(pre_reg, pre_div_mask << pre_shift,
1561 (fine & pre_div_mask) << pre_shift);
1566 static unsigned long exynos4_get_i2c_clk(void)
1568 struct exynos4_clock *clk =
1569 (struct exynos4_clock *)samsung_get_base_clock();
1570 unsigned long sclk, aclk_100;
1573 sclk = get_pll_clk(APLL);
1575 ratio = (readl(&clk->div_top)) >> 4;
1577 aclk_100 = sclk / (ratio + 1);
1581 unsigned long get_pll_clk(int pllreg)
1583 if (cpu_is_exynos5()) {
1584 if (proid_is_exynos5420())
1585 return exynos5420_get_pll_clk(pllreg);
1586 return exynos5_get_pll_clk(pllreg);
1588 if (proid_is_exynos4412())
1589 return exynos4x12_get_pll_clk(pllreg);
1590 return exynos4_get_pll_clk(pllreg);
1594 unsigned long get_arm_clk(void)
1596 if (cpu_is_exynos5())
1597 return exynos5_get_arm_clk();
1599 if (proid_is_exynos4412())
1600 return exynos4x12_get_arm_clk();
1601 return exynos4_get_arm_clk();
1605 unsigned long get_i2c_clk(void)
1607 if (cpu_is_exynos5()) {
1608 return exynos5_get_i2c_clk();
1609 } else if (cpu_is_exynos4()) {
1610 return exynos4_get_i2c_clk();
1612 debug("I2C clock is not set for this CPU\n");
1617 unsigned long get_pwm_clk(void)
1619 if (cpu_is_exynos5()) {
1620 if (proid_is_exynos5420())
1621 return exynos5420_get_pwm_clk();
1622 return clock_get_periph_rate(PERIPH_ID_PWM0);
1624 if (proid_is_exynos4412())
1625 return exynos4x12_get_pwm_clk();
1626 return exynos4_get_pwm_clk();
1630 unsigned long get_uart_clk(int dev_index)
1632 if (cpu_is_exynos5()) {
1633 if (proid_is_exynos5420())
1634 return exynos5420_get_uart_clk(dev_index);
1635 return exynos5_get_uart_clk(dev_index);
1637 if (proid_is_exynos4412())
1638 return exynos4x12_get_uart_clk(dev_index);
1639 return exynos4_get_uart_clk(dev_index);
1643 unsigned long get_mmc_clk(int dev_index)
1645 if (cpu_is_exynos5()) {
1646 if (proid_is_exynos5420())
1647 return exynos5420_get_mmc_clk(dev_index);
1648 return exynos5_get_mmc_clk(dev_index);
1650 return exynos4_get_mmc_clk(dev_index);
1654 void set_mmc_clk(int dev_index, unsigned int div)
1656 if (cpu_is_exynos5()) {
1657 if (proid_is_exynos5420())
1658 exynos5420_set_mmc_clk(dev_index, div);
1660 exynos5_set_mmc_clk(dev_index, div);
1662 exynos4_set_mmc_clk(dev_index, div);
1666 unsigned long get_lcd_clk(void)
1668 if (cpu_is_exynos4())
1669 return exynos4_get_lcd_clk();
1671 if (proid_is_exynos5420())
1672 return exynos5420_get_lcd_clk();
1674 return exynos5_get_lcd_clk();
1678 void set_lcd_clk(void)
1680 if (cpu_is_exynos4())
1681 exynos4_set_lcd_clk();
1683 if (proid_is_exynos5250())
1684 exynos5_set_lcd_clk();
1685 else if (proid_is_exynos5420())
1686 exynos5420_set_lcd_clk();
1690 void set_mipi_clk(void)
1692 if (cpu_is_exynos4())
1693 exynos4_set_mipi_clk();
1696 int set_spi_clk(int periph_id, unsigned int rate)
1698 if (cpu_is_exynos5()) {
1699 if (proid_is_exynos5420())
1700 return exynos5420_set_spi_clk(periph_id, rate);
1701 return exynos5_set_spi_clk(periph_id, rate);
1707 int set_i2s_clk_prescaler(unsigned int src_frq, unsigned int dst_frq,
1708 unsigned int i2s_id)
1710 if (cpu_is_exynos5())
1711 return exynos5_set_i2s_clk_prescaler(src_frq, dst_frq, i2s_id);
1716 int set_i2s_clk_source(unsigned int i2s_id)
1718 if (cpu_is_exynos5())
1719 return exynos5_set_i2s_clk_source(i2s_id);
1724 int set_epll_clk(unsigned long rate)
1726 if (cpu_is_exynos5())
1727 return exynos5_set_epll_clk(rate);