xref: /rk3399_rockchip-uboot/arch/arm/mach-exynos/clock.c (revision 5f5620ab2679608f94b3a77e51c77d0a770103bd)
177b55e8cSThomas Abraham /*
277b55e8cSThomas Abraham  * Copyright (C) 2010 Samsung Electronics
377b55e8cSThomas Abraham  * Minkyu Kang <mk7.kang@samsung.com>
477b55e8cSThomas Abraham  *
577b55e8cSThomas Abraham  * SPDX-License-Identifier:	GPL-2.0+
677b55e8cSThomas Abraham  */
777b55e8cSThomas Abraham 
877b55e8cSThomas Abraham #include <common.h>
977b55e8cSThomas Abraham #include <asm/io.h>
1077b55e8cSThomas Abraham #include <asm/arch/clock.h>
1177b55e8cSThomas Abraham #include <asm/arch/clk.h>
1277b55e8cSThomas Abraham #include <asm/arch/periph.h>
1377b55e8cSThomas Abraham 
1477b55e8cSThomas Abraham #define PLL_DIV_1024	1024
1577b55e8cSThomas Abraham #define PLL_DIV_65535	65535
1677b55e8cSThomas Abraham #define PLL_DIV_65536	65536
1777b55e8cSThomas Abraham /* *
1877b55e8cSThomas Abraham  * This structure is to store the src bit, div bit and prediv bit
1977b55e8cSThomas Abraham  * positions of the peripheral clocks of the src and div registers
2077b55e8cSThomas Abraham  */
2177b55e8cSThomas Abraham struct clk_bit_info {
2277b55e8cSThomas Abraham 	enum periph_id id;
2377b55e8cSThomas Abraham 	int32_t src_mask;
2477b55e8cSThomas Abraham 	int32_t div_mask;
2577b55e8cSThomas Abraham 	int32_t prediv_mask;
2677b55e8cSThomas Abraham 	int8_t src_bit;
2777b55e8cSThomas Abraham 	int8_t div_bit;
2877b55e8cSThomas Abraham 	int8_t prediv_bit;
2977b55e8cSThomas Abraham };
3077b55e8cSThomas Abraham 
3177b55e8cSThomas Abraham static struct clk_bit_info exynos5_bit_info[] = {
3277b55e8cSThomas Abraham 	/* periph id		s_mask	d_mask	p_mask	s_bit	d_bit	p_bit */
3377b55e8cSThomas Abraham 	{PERIPH_ID_UART0,	0xf,	0xf,	-1,	0,	0,	-1},
3477b55e8cSThomas Abraham 	{PERIPH_ID_UART1,	0xf,	0xf,	-1,	4,	4,	-1},
3577b55e8cSThomas Abraham 	{PERIPH_ID_UART2,	0xf,	0xf,	-1,	8,	8,	-1},
3677b55e8cSThomas Abraham 	{PERIPH_ID_UART3,	0xf,	0xf,	-1,	12,	12,	-1},
3777b55e8cSThomas Abraham 	{PERIPH_ID_I2C0,	-1,	0x7,	0x7,	-1,	24,	0},
3877b55e8cSThomas Abraham 	{PERIPH_ID_I2C1,	-1,	0x7,	0x7,	-1,	24,	0},
3977b55e8cSThomas Abraham 	{PERIPH_ID_I2C2,	-1,	0x7,	0x7,	-1,	24,	0},
4077b55e8cSThomas Abraham 	{PERIPH_ID_I2C3,	-1,	0x7,	0x7,	-1,	24,	0},
4177b55e8cSThomas Abraham 	{PERIPH_ID_I2C4,	-1,	0x7,	0x7,	-1,	24,	0},
4277b55e8cSThomas Abraham 	{PERIPH_ID_I2C5,	-1,	0x7,	0x7,	-1,	24,	0},
4377b55e8cSThomas Abraham 	{PERIPH_ID_I2C6,	-1,	0x7,	0x7,	-1,	24,	0},
4477b55e8cSThomas Abraham 	{PERIPH_ID_I2C7,	-1,	0x7,	0x7,	-1,	24,	0},
4577b55e8cSThomas Abraham 	{PERIPH_ID_SPI0,	0xf,	0xf,	0xff,	16,	0,	8},
4677b55e8cSThomas Abraham 	{PERIPH_ID_SPI1,	0xf,	0xf,	0xff,	20,	16,	24},
4777b55e8cSThomas Abraham 	{PERIPH_ID_SPI2,	0xf,	0xf,	0xff,	24,	0,	8},
4877b55e8cSThomas Abraham 	{PERIPH_ID_SDMMC0,	0xf,	0xf,	0xff,	0,	0,	8},
4977b55e8cSThomas Abraham 	{PERIPH_ID_SDMMC1,	0xf,	0xf,	0xff,	4,	16,	24},
5077b55e8cSThomas Abraham 	{PERIPH_ID_SDMMC2,	0xf,	0xf,	0xff,	8,	0,	8},
5177b55e8cSThomas Abraham 	{PERIPH_ID_SDMMC3,	0xf,	0xf,	0xff,	12,	16,	24},
5277b55e8cSThomas Abraham 	{PERIPH_ID_I2S0,	0xf,	0xf,	0xff,	0,	0,	4},
5377b55e8cSThomas Abraham 	{PERIPH_ID_I2S1,	0xf,	0xf,	0xff,	4,	12,	16},
5477b55e8cSThomas Abraham 	{PERIPH_ID_SPI3,	0xf,	0xf,	0xff,	0,	0,	4},
5577b55e8cSThomas Abraham 	{PERIPH_ID_SPI4,	0xf,	0xf,	0xff,	4,	12,	16},
5677b55e8cSThomas Abraham 	{PERIPH_ID_SDMMC4,	0xf,	0xf,	0xff,	16,	0,	8},
5777b55e8cSThomas Abraham 	{PERIPH_ID_PWM0,	0xf,	0xf,	-1,	24,	0,	-1},
5877b55e8cSThomas Abraham 	{PERIPH_ID_PWM1,	0xf,	0xf,	-1,	24,	0,	-1},
5977b55e8cSThomas Abraham 	{PERIPH_ID_PWM2,	0xf,	0xf,	-1,	24,	0,	-1},
6077b55e8cSThomas Abraham 	{PERIPH_ID_PWM3,	0xf,	0xf,	-1,	24,	0,	-1},
6177b55e8cSThomas Abraham 	{PERIPH_ID_PWM4,	0xf,	0xf,	-1,	24,	0,	-1},
6277b55e8cSThomas Abraham 
6377b55e8cSThomas Abraham 	{PERIPH_ID_NONE,	-1,	-1,	-1,	-1,	-1,	-1},
6477b55e8cSThomas Abraham };
6577b55e8cSThomas Abraham 
6677b55e8cSThomas Abraham static struct clk_bit_info exynos542x_bit_info[] = {
6777b55e8cSThomas Abraham 	/* periph id		s_mask	d_mask	p_mask	s_bit	d_bit	p_bit */
6877b55e8cSThomas Abraham 	{PERIPH_ID_UART0,	0xf,	0xf,	-1,	4,	8,	-1},
6977b55e8cSThomas Abraham 	{PERIPH_ID_UART1,	0xf,	0xf,	-1,	8,	12,	-1},
7077b55e8cSThomas Abraham 	{PERIPH_ID_UART2,	0xf,	0xf,	-1,	12,	16,	-1},
7177b55e8cSThomas Abraham 	{PERIPH_ID_UART3,	0xf,	0xf,	-1,	16,	20,	-1},
7277b55e8cSThomas Abraham 	{PERIPH_ID_I2C0,	-1,	0x3f,	-1,	-1,	8,	-1},
7377b55e8cSThomas Abraham 	{PERIPH_ID_I2C1,	-1,	0x3f,	-1,	-1,	8,	-1},
7477b55e8cSThomas Abraham 	{PERIPH_ID_I2C2,	-1,	0x3f,	-1,	-1,	8,	-1},
7577b55e8cSThomas Abraham 	{PERIPH_ID_I2C3,	-1,	0x3f,	-1,	-1,	8,	-1},
7677b55e8cSThomas Abraham 	{PERIPH_ID_I2C4,	-1,	0x3f,	-1,	-1,	8,	-1},
7777b55e8cSThomas Abraham 	{PERIPH_ID_I2C5,	-1,	0x3f,	-1,	-1,	8,	-1},
7877b55e8cSThomas Abraham 	{PERIPH_ID_I2C6,	-1,	0x3f,	-1,	-1,	8,	-1},
7977b55e8cSThomas Abraham 	{PERIPH_ID_I2C7,	-1,	0x3f,	-1,	-1,	8,	-1},
8077b55e8cSThomas Abraham 	{PERIPH_ID_SPI0,	0xf,	0xf,	0xff,	20,	20,	8},
8177b55e8cSThomas Abraham 	{PERIPH_ID_SPI1,	0xf,	0xf,	0xff,	24,	24,	16},
8277b55e8cSThomas Abraham 	{PERIPH_ID_SPI2,	0xf,	0xf,	0xff,	28,	28,	24},
8377b55e8cSThomas Abraham 	{PERIPH_ID_SDMMC0,	0x7,	0x3ff,	-1,	8,	0,	-1},
8477b55e8cSThomas Abraham 	{PERIPH_ID_SDMMC1,	0x7,	0x3ff,	-1,	12,	10,	-1},
8577b55e8cSThomas Abraham 	{PERIPH_ID_SDMMC2,	0x7,	0x3ff,	-1,	16,	20,	-1},
8677b55e8cSThomas Abraham 	{PERIPH_ID_I2C8,	-1,	0x3f,	-1,	-1,	8,	-1},
8777b55e8cSThomas Abraham 	{PERIPH_ID_I2C9,	-1,	0x3f,	-1,	-1,	8,	-1},
8877b55e8cSThomas Abraham 	{PERIPH_ID_I2S0,	0xf,	0xf,	0xff,	0,	0,	4},
8977b55e8cSThomas Abraham 	{PERIPH_ID_I2S1,	0xf,	0xf,	0xff,	4,	12,	16},
9077b55e8cSThomas Abraham 	{PERIPH_ID_SPI3,	0xf,	0xf,	0xff,	12,	16,	0},
9177b55e8cSThomas Abraham 	{PERIPH_ID_SPI4,	0xf,	0xf,	0xff,	16,	20,	8},
9277b55e8cSThomas Abraham 	{PERIPH_ID_PWM0,	0xf,	0xf,	-1,	24,	28,	-1},
9377b55e8cSThomas Abraham 	{PERIPH_ID_PWM1,	0xf,	0xf,	-1,	24,	28,	-1},
9477b55e8cSThomas Abraham 	{PERIPH_ID_PWM2,	0xf,	0xf,	-1,	24,	28,	-1},
9577b55e8cSThomas Abraham 	{PERIPH_ID_PWM3,	0xf,	0xf,	-1,	24,	28,	-1},
9677b55e8cSThomas Abraham 	{PERIPH_ID_PWM4,	0xf,	0xf,	-1,	24,	28,	-1},
9777b55e8cSThomas Abraham 	{PERIPH_ID_I2C10,	-1,	0x3f,	-1,	-1,	8,	-1},
9877b55e8cSThomas Abraham 
9977b55e8cSThomas Abraham 	{PERIPH_ID_NONE,	-1,	-1,	-1,	-1,	-1,	-1},
10077b55e8cSThomas Abraham };
10177b55e8cSThomas Abraham 
10277b55e8cSThomas Abraham /* Epll Clock division values to achive different frequency output */
10377b55e8cSThomas Abraham static struct set_epll_con_val exynos5_epll_div[] = {
10477b55e8cSThomas Abraham 	{ 192000000, 0, 48, 3, 1, 0 },
10577b55e8cSThomas Abraham 	{ 180000000, 0, 45, 3, 1, 0 },
10677b55e8cSThomas Abraham 	{  73728000, 1, 73, 3, 3, 47710 },
10777b55e8cSThomas Abraham 	{  67737600, 1, 90, 4, 3, 20762 },
10877b55e8cSThomas Abraham 	{  49152000, 0, 49, 3, 3, 9961 },
10977b55e8cSThomas Abraham 	{  45158400, 0, 45, 3, 3, 10381 },
11077b55e8cSThomas Abraham 	{ 180633600, 0, 45, 3, 1, 10381 }
11177b55e8cSThomas Abraham };
11277b55e8cSThomas Abraham 
11377b55e8cSThomas Abraham /* exynos: return pll clock frequency */
exynos_get_pll_clk(int pllreg,unsigned int r,unsigned int k)11477b55e8cSThomas Abraham static int exynos_get_pll_clk(int pllreg, unsigned int r, unsigned int k)
11577b55e8cSThomas Abraham {
11677b55e8cSThomas Abraham 	unsigned long m, p, s = 0, mask, fout;
11777b55e8cSThomas Abraham 	unsigned int div;
11877b55e8cSThomas Abraham 	unsigned int freq;
11977b55e8cSThomas Abraham 	/*
12077b55e8cSThomas Abraham 	 * APLL_CON: MIDV [25:16]
12177b55e8cSThomas Abraham 	 * MPLL_CON: MIDV [25:16]
12277b55e8cSThomas Abraham 	 * EPLL_CON: MIDV [24:16]
12377b55e8cSThomas Abraham 	 * VPLL_CON: MIDV [24:16]
12477b55e8cSThomas Abraham 	 * BPLL_CON: MIDV [25:16]: Exynos5
12577b55e8cSThomas Abraham 	 */
12677b55e8cSThomas Abraham 	if (pllreg == APLL || pllreg == MPLL || pllreg == BPLL ||
12777b55e8cSThomas Abraham 	    pllreg == SPLL)
12877b55e8cSThomas Abraham 		mask = 0x3ff;
12977b55e8cSThomas Abraham 	else
13077b55e8cSThomas Abraham 		mask = 0x1ff;
13177b55e8cSThomas Abraham 
13277b55e8cSThomas Abraham 	m = (r >> 16) & mask;
13377b55e8cSThomas Abraham 
13477b55e8cSThomas Abraham 	/* PDIV [13:8] */
13577b55e8cSThomas Abraham 	p = (r >> 8) & 0x3f;
13677b55e8cSThomas Abraham 	/* SDIV [2:0] */
13777b55e8cSThomas Abraham 	s = r & 0x7;
13877b55e8cSThomas Abraham 
13977b55e8cSThomas Abraham 	freq = CONFIG_SYS_CLK_FREQ;
14077b55e8cSThomas Abraham 
14177b55e8cSThomas Abraham 	if (pllreg == EPLL || pllreg == RPLL) {
14277b55e8cSThomas Abraham 		k = k & 0xffff;
14377b55e8cSThomas Abraham 		/* FOUT = (MDIV + K / 65536) * FIN / (PDIV * 2^SDIV) */
14477b55e8cSThomas Abraham 		fout = (m + k / PLL_DIV_65536) * (freq / (p * (1 << s)));
14577b55e8cSThomas Abraham 	} else if (pllreg == VPLL) {
14677b55e8cSThomas Abraham 		k = k & 0xfff;
14777b55e8cSThomas Abraham 
14877b55e8cSThomas Abraham 		/*
14977b55e8cSThomas Abraham 		 * Exynos4210
15077b55e8cSThomas Abraham 		 * FOUT = (MDIV + K / 1024) * FIN / (PDIV * 2^SDIV)
15177b55e8cSThomas Abraham 		 *
15277b55e8cSThomas Abraham 		 * Exynos4412
15377b55e8cSThomas Abraham 		 * FOUT = (MDIV + K / 65535) * FIN / (PDIV * 2^SDIV)
15477b55e8cSThomas Abraham 		 *
15577b55e8cSThomas Abraham 		 * Exynos5250
15677b55e8cSThomas Abraham 		 * FOUT = (MDIV + K / 65536) * FIN / (PDIV * 2^SDIV)
15777b55e8cSThomas Abraham 		 */
15877b55e8cSThomas Abraham 		if (proid_is_exynos4210())
15977b55e8cSThomas Abraham 			div = PLL_DIV_1024;
16077b55e8cSThomas Abraham 		else if (proid_is_exynos4412())
16177b55e8cSThomas Abraham 			div = PLL_DIV_65535;
162*d64c8adeSPrzemyslaw Marczak 		else if (proid_is_exynos5250() || proid_is_exynos5420() ||
163*d64c8adeSPrzemyslaw Marczak 			 proid_is_exynos5422())
16477b55e8cSThomas Abraham 			div = PLL_DIV_65536;
16577b55e8cSThomas Abraham 		else
16677b55e8cSThomas Abraham 			return 0;
16777b55e8cSThomas Abraham 
16877b55e8cSThomas Abraham 		fout = (m + k / div) * (freq / (p * (1 << s)));
16977b55e8cSThomas Abraham 	} else {
17077b55e8cSThomas Abraham 		/*
17177b55e8cSThomas Abraham 		 * Exynos4412 / Exynos5250
17277b55e8cSThomas Abraham 		 * FOUT = MDIV * FIN / (PDIV * 2^SDIV)
17377b55e8cSThomas Abraham 		 *
17477b55e8cSThomas Abraham 		 * Exynos4210
17577b55e8cSThomas Abraham 		 * FOUT = MDIV * FIN / (PDIV * 2^(SDIV-1))
17677b55e8cSThomas Abraham 		 */
17777b55e8cSThomas Abraham 		if (proid_is_exynos4210())
17877b55e8cSThomas Abraham 			fout = m * (freq / (p * (1 << (s - 1))));
17977b55e8cSThomas Abraham 		else
18077b55e8cSThomas Abraham 			fout = m * (freq / (p * (1 << s)));
18177b55e8cSThomas Abraham 	}
18277b55e8cSThomas Abraham 	return fout;
18377b55e8cSThomas Abraham }
18477b55e8cSThomas Abraham 
18577b55e8cSThomas Abraham /* exynos4: return pll clock frequency */
exynos4_get_pll_clk(int pllreg)18677b55e8cSThomas Abraham static unsigned long exynos4_get_pll_clk(int pllreg)
18777b55e8cSThomas Abraham {
18877b55e8cSThomas Abraham 	struct exynos4_clock *clk =
18977b55e8cSThomas Abraham 		(struct exynos4_clock *)samsung_get_base_clock();
19077b55e8cSThomas Abraham 	unsigned long r, k = 0;
19177b55e8cSThomas Abraham 
19277b55e8cSThomas Abraham 	switch (pllreg) {
19377b55e8cSThomas Abraham 	case APLL:
19477b55e8cSThomas Abraham 		r = readl(&clk->apll_con0);
19577b55e8cSThomas Abraham 		break;
19677b55e8cSThomas Abraham 	case MPLL:
19777b55e8cSThomas Abraham 		r = readl(&clk->mpll_con0);
19877b55e8cSThomas Abraham 		break;
19977b55e8cSThomas Abraham 	case EPLL:
20077b55e8cSThomas Abraham 		r = readl(&clk->epll_con0);
20177b55e8cSThomas Abraham 		k = readl(&clk->epll_con1);
20277b55e8cSThomas Abraham 		break;
20377b55e8cSThomas Abraham 	case VPLL:
20477b55e8cSThomas Abraham 		r = readl(&clk->vpll_con0);
20577b55e8cSThomas Abraham 		k = readl(&clk->vpll_con1);
20677b55e8cSThomas Abraham 		break;
20777b55e8cSThomas Abraham 	default:
20877b55e8cSThomas Abraham 		printf("Unsupported PLL (%d)\n", pllreg);
20977b55e8cSThomas Abraham 		return 0;
21077b55e8cSThomas Abraham 	}
21177b55e8cSThomas Abraham 
21277b55e8cSThomas Abraham 	return exynos_get_pll_clk(pllreg, r, k);
21377b55e8cSThomas Abraham }
21477b55e8cSThomas Abraham 
21577b55e8cSThomas Abraham /* exynos4x12: return pll clock frequency */
exynos4x12_get_pll_clk(int pllreg)21677b55e8cSThomas Abraham static unsigned long exynos4x12_get_pll_clk(int pllreg)
21777b55e8cSThomas Abraham {
21877b55e8cSThomas Abraham 	struct exynos4x12_clock *clk =
21977b55e8cSThomas Abraham 		(struct exynos4x12_clock *)samsung_get_base_clock();
22077b55e8cSThomas Abraham 	unsigned long r, k = 0;
22177b55e8cSThomas Abraham 
22277b55e8cSThomas Abraham 	switch (pllreg) {
22377b55e8cSThomas Abraham 	case APLL:
22477b55e8cSThomas Abraham 		r = readl(&clk->apll_con0);
22577b55e8cSThomas Abraham 		break;
22677b55e8cSThomas Abraham 	case MPLL:
22777b55e8cSThomas Abraham 		r = readl(&clk->mpll_con0);
22877b55e8cSThomas Abraham 		break;
22977b55e8cSThomas Abraham 	case EPLL:
23077b55e8cSThomas Abraham 		r = readl(&clk->epll_con0);
23177b55e8cSThomas Abraham 		k = readl(&clk->epll_con1);
23277b55e8cSThomas Abraham 		break;
23377b55e8cSThomas Abraham 	case VPLL:
23477b55e8cSThomas Abraham 		r = readl(&clk->vpll_con0);
23577b55e8cSThomas Abraham 		k = readl(&clk->vpll_con1);
23677b55e8cSThomas Abraham 		break;
23777b55e8cSThomas Abraham 	default:
23877b55e8cSThomas Abraham 		printf("Unsupported PLL (%d)\n", pllreg);
23977b55e8cSThomas Abraham 		return 0;
24077b55e8cSThomas Abraham 	}
24177b55e8cSThomas Abraham 
24277b55e8cSThomas Abraham 	return exynos_get_pll_clk(pllreg, r, k);
24377b55e8cSThomas Abraham }
24477b55e8cSThomas Abraham 
24577b55e8cSThomas Abraham /* exynos5: return pll clock frequency */
exynos5_get_pll_clk(int pllreg)24677b55e8cSThomas Abraham static unsigned long exynos5_get_pll_clk(int pllreg)
24777b55e8cSThomas Abraham {
24877b55e8cSThomas Abraham 	struct exynos5_clock *clk =
24977b55e8cSThomas Abraham 		(struct exynos5_clock *)samsung_get_base_clock();
25077b55e8cSThomas Abraham 	unsigned long r, k = 0, fout;
25177b55e8cSThomas Abraham 	unsigned int pll_div2_sel, fout_sel;
25277b55e8cSThomas Abraham 
25377b55e8cSThomas Abraham 	switch (pllreg) {
25477b55e8cSThomas Abraham 	case APLL:
25577b55e8cSThomas Abraham 		r = readl(&clk->apll_con0);
25677b55e8cSThomas Abraham 		break;
25777b55e8cSThomas Abraham 	case MPLL:
25877b55e8cSThomas Abraham 		r = readl(&clk->mpll_con0);
25977b55e8cSThomas Abraham 		break;
26077b55e8cSThomas Abraham 	case EPLL:
26177b55e8cSThomas Abraham 		r = readl(&clk->epll_con0);
26277b55e8cSThomas Abraham 		k = readl(&clk->epll_con1);
26377b55e8cSThomas Abraham 		break;
26477b55e8cSThomas Abraham 	case VPLL:
26577b55e8cSThomas Abraham 		r = readl(&clk->vpll_con0);
26677b55e8cSThomas Abraham 		k = readl(&clk->vpll_con1);
26777b55e8cSThomas Abraham 		break;
26877b55e8cSThomas Abraham 	case BPLL:
26977b55e8cSThomas Abraham 		r = readl(&clk->bpll_con0);
27077b55e8cSThomas Abraham 		break;
27177b55e8cSThomas Abraham 	default:
27277b55e8cSThomas Abraham 		printf("Unsupported PLL (%d)\n", pllreg);
27377b55e8cSThomas Abraham 		return 0;
27477b55e8cSThomas Abraham 	}
27577b55e8cSThomas Abraham 
27677b55e8cSThomas Abraham 	fout = exynos_get_pll_clk(pllreg, r, k);
27777b55e8cSThomas Abraham 
27877b55e8cSThomas Abraham 	/* According to the user manual, in EVT1 MPLL and BPLL always gives
27977b55e8cSThomas Abraham 	 * 1.6GHz clock, so divide by 2 to get 800MHz MPLL clock.*/
28077b55e8cSThomas Abraham 	if (pllreg == MPLL || pllreg == BPLL) {
28177b55e8cSThomas Abraham 		pll_div2_sel = readl(&clk->pll_div2_sel);
28277b55e8cSThomas Abraham 
28377b55e8cSThomas Abraham 		switch (pllreg) {
28477b55e8cSThomas Abraham 		case MPLL:
28577b55e8cSThomas Abraham 			fout_sel = (pll_div2_sel >> MPLL_FOUT_SEL_SHIFT)
28677b55e8cSThomas Abraham 					& MPLL_FOUT_SEL_MASK;
28777b55e8cSThomas Abraham 			break;
28877b55e8cSThomas Abraham 		case BPLL:
28977b55e8cSThomas Abraham 			fout_sel = (pll_div2_sel >> BPLL_FOUT_SEL_SHIFT)
29077b55e8cSThomas Abraham 					& BPLL_FOUT_SEL_MASK;
29177b55e8cSThomas Abraham 			break;
29277b55e8cSThomas Abraham 		default:
29377b55e8cSThomas Abraham 			fout_sel = -1;
29477b55e8cSThomas Abraham 			break;
29577b55e8cSThomas Abraham 		}
29677b55e8cSThomas Abraham 
29777b55e8cSThomas Abraham 		if (fout_sel == 0)
29877b55e8cSThomas Abraham 			fout /= 2;
29977b55e8cSThomas Abraham 	}
30077b55e8cSThomas Abraham 
30177b55e8cSThomas Abraham 	return fout;
30277b55e8cSThomas Abraham }
30377b55e8cSThomas Abraham 
30477b55e8cSThomas Abraham /* exynos542x: return pll clock frequency */
exynos542x_get_pll_clk(int pllreg)30577b55e8cSThomas Abraham static unsigned long exynos542x_get_pll_clk(int pllreg)
30677b55e8cSThomas Abraham {
30777b55e8cSThomas Abraham 	struct exynos5420_clock *clk =
30877b55e8cSThomas Abraham 		(struct exynos5420_clock *)samsung_get_base_clock();
30977b55e8cSThomas Abraham 	unsigned long r, k = 0;
31077b55e8cSThomas Abraham 
31177b55e8cSThomas Abraham 	switch (pllreg) {
31277b55e8cSThomas Abraham 	case APLL:
31377b55e8cSThomas Abraham 		r = readl(&clk->apll_con0);
31477b55e8cSThomas Abraham 		break;
31577b55e8cSThomas Abraham 	case MPLL:
31677b55e8cSThomas Abraham 		r = readl(&clk->mpll_con0);
31777b55e8cSThomas Abraham 		break;
31877b55e8cSThomas Abraham 	case EPLL:
31977b55e8cSThomas Abraham 		r = readl(&clk->epll_con0);
32077b55e8cSThomas Abraham 		k = readl(&clk->epll_con1);
32177b55e8cSThomas Abraham 		break;
32277b55e8cSThomas Abraham 	case VPLL:
32377b55e8cSThomas Abraham 		r = readl(&clk->vpll_con0);
32477b55e8cSThomas Abraham 		k = readl(&clk->vpll_con1);
32577b55e8cSThomas Abraham 		break;
32677b55e8cSThomas Abraham 	case BPLL:
32777b55e8cSThomas Abraham 		r = readl(&clk->bpll_con0);
32877b55e8cSThomas Abraham 		break;
32977b55e8cSThomas Abraham 	case RPLL:
33077b55e8cSThomas Abraham 		r = readl(&clk->rpll_con0);
33177b55e8cSThomas Abraham 		k = readl(&clk->rpll_con1);
33277b55e8cSThomas Abraham 		break;
33377b55e8cSThomas Abraham 	case SPLL:
33477b55e8cSThomas Abraham 		r = readl(&clk->spll_con0);
33577b55e8cSThomas Abraham 		break;
33677b55e8cSThomas Abraham 	default:
33777b55e8cSThomas Abraham 		printf("Unsupported PLL (%d)\n", pllreg);
33877b55e8cSThomas Abraham 		return 0;
33977b55e8cSThomas Abraham 	}
34077b55e8cSThomas Abraham 
34177b55e8cSThomas Abraham 	return exynos_get_pll_clk(pllreg, r, k);
34277b55e8cSThomas Abraham }
34377b55e8cSThomas Abraham 
get_clk_bit_info(int peripheral)34477b55e8cSThomas Abraham static struct clk_bit_info *get_clk_bit_info(int peripheral)
34577b55e8cSThomas Abraham {
34677b55e8cSThomas Abraham 	int i;
34777b55e8cSThomas Abraham 	struct clk_bit_info *info;
34877b55e8cSThomas Abraham 
349*d64c8adeSPrzemyslaw Marczak 	if (proid_is_exynos5420() || proid_is_exynos5422())
35077b55e8cSThomas Abraham 		info = exynos542x_bit_info;
35177b55e8cSThomas Abraham 	else
35277b55e8cSThomas Abraham 		info = exynos5_bit_info;
35377b55e8cSThomas Abraham 
35477b55e8cSThomas Abraham 	for (i = 0; info[i].id != PERIPH_ID_NONE; i++) {
35577b55e8cSThomas Abraham 		if (info[i].id == peripheral)
35677b55e8cSThomas Abraham 			break;
35777b55e8cSThomas Abraham 	}
35877b55e8cSThomas Abraham 
35977b55e8cSThomas Abraham 	if (info[i].id == PERIPH_ID_NONE)
36077b55e8cSThomas Abraham 		debug("ERROR: Peripheral ID %d not found\n", peripheral);
36177b55e8cSThomas Abraham 
36277b55e8cSThomas Abraham 	return &info[i];
36377b55e8cSThomas Abraham }
36477b55e8cSThomas Abraham 
exynos5_get_periph_rate(int peripheral)36577b55e8cSThomas Abraham static unsigned long exynos5_get_periph_rate(int peripheral)
36677b55e8cSThomas Abraham {
36777b55e8cSThomas Abraham 	struct clk_bit_info *bit_info = get_clk_bit_info(peripheral);
36877b55e8cSThomas Abraham 	unsigned long sclk = 0;
36977b55e8cSThomas Abraham 	unsigned int src = 0, div = 0, sub_div = 0;
37077b55e8cSThomas Abraham 	struct exynos5_clock *clk =
37177b55e8cSThomas Abraham 			(struct exynos5_clock *)samsung_get_base_clock();
37277b55e8cSThomas Abraham 
37377b55e8cSThomas Abraham 	switch (peripheral) {
37477b55e8cSThomas Abraham 	case PERIPH_ID_UART0:
37577b55e8cSThomas Abraham 	case PERIPH_ID_UART1:
37677b55e8cSThomas Abraham 	case PERIPH_ID_UART2:
37777b55e8cSThomas Abraham 	case PERIPH_ID_UART3:
37877b55e8cSThomas Abraham 		src = readl(&clk->src_peric0);
37977b55e8cSThomas Abraham 		div = readl(&clk->div_peric0);
38077b55e8cSThomas Abraham 		break;
38177b55e8cSThomas Abraham 	case PERIPH_ID_PWM0:
38277b55e8cSThomas Abraham 	case PERIPH_ID_PWM1:
38377b55e8cSThomas Abraham 	case PERIPH_ID_PWM2:
38477b55e8cSThomas Abraham 	case PERIPH_ID_PWM3:
38577b55e8cSThomas Abraham 	case PERIPH_ID_PWM4:
38677b55e8cSThomas Abraham 		src = readl(&clk->src_peric0);
38777b55e8cSThomas Abraham 		div = readl(&clk->div_peric3);
38877b55e8cSThomas Abraham 		break;
38977b55e8cSThomas Abraham 	case PERIPH_ID_I2S0:
39077b55e8cSThomas Abraham 		src = readl(&clk->src_mau);
39177b55e8cSThomas Abraham 		div = sub_div = readl(&clk->div_mau);
39277b55e8cSThomas Abraham 	case PERIPH_ID_SPI0:
39377b55e8cSThomas Abraham 	case PERIPH_ID_SPI1:
39477b55e8cSThomas Abraham 		src = readl(&clk->src_peric1);
39577b55e8cSThomas Abraham 		div = sub_div = readl(&clk->div_peric1);
39677b55e8cSThomas Abraham 		break;
39777b55e8cSThomas Abraham 	case PERIPH_ID_SPI2:
39877b55e8cSThomas Abraham 		src = readl(&clk->src_peric1);
39977b55e8cSThomas Abraham 		div = sub_div = readl(&clk->div_peric2);
40077b55e8cSThomas Abraham 		break;
40177b55e8cSThomas Abraham 	case PERIPH_ID_SPI3:
40277b55e8cSThomas Abraham 	case PERIPH_ID_SPI4:
40377b55e8cSThomas Abraham 		src = readl(&clk->sclk_src_isp);
40477b55e8cSThomas Abraham 		div = sub_div = readl(&clk->sclk_div_isp);
40577b55e8cSThomas Abraham 		break;
40677b55e8cSThomas Abraham 	case PERIPH_ID_SDMMC0:
40777b55e8cSThomas Abraham 	case PERIPH_ID_SDMMC1:
40877b55e8cSThomas Abraham 		src = readl(&clk->src_fsys);
40977b55e8cSThomas Abraham 		div = sub_div = readl(&clk->div_fsys1);
41077b55e8cSThomas Abraham 		break;
41177b55e8cSThomas Abraham 	case PERIPH_ID_SDMMC2:
41277b55e8cSThomas Abraham 	case PERIPH_ID_SDMMC3:
41377b55e8cSThomas Abraham 		src = readl(&clk->src_fsys);
41477b55e8cSThomas Abraham 		div = sub_div = readl(&clk->div_fsys2);
41577b55e8cSThomas Abraham 		break;
41677b55e8cSThomas Abraham 	case PERIPH_ID_I2C0:
41777b55e8cSThomas Abraham 	case PERIPH_ID_I2C1:
41877b55e8cSThomas Abraham 	case PERIPH_ID_I2C2:
41977b55e8cSThomas Abraham 	case PERIPH_ID_I2C3:
42077b55e8cSThomas Abraham 	case PERIPH_ID_I2C4:
42177b55e8cSThomas Abraham 	case PERIPH_ID_I2C5:
42277b55e8cSThomas Abraham 	case PERIPH_ID_I2C6:
42377b55e8cSThomas Abraham 	case PERIPH_ID_I2C7:
42477b55e8cSThomas Abraham 		src = EXYNOS_SRC_MPLL;
42577b55e8cSThomas Abraham 		div = readl(&clk->div_top1);
42677b55e8cSThomas Abraham 		sub_div = readl(&clk->div_top0);
42777b55e8cSThomas Abraham 		break;
42877b55e8cSThomas Abraham 	default:
42977b55e8cSThomas Abraham 		debug("%s: invalid peripheral %d", __func__, peripheral);
43077b55e8cSThomas Abraham 		return -1;
43177b55e8cSThomas Abraham 	};
43277b55e8cSThomas Abraham 
43377b55e8cSThomas Abraham 	if (bit_info->src_bit >= 0)
43477b55e8cSThomas Abraham 		src = (src >> bit_info->src_bit) & bit_info->src_mask;
43577b55e8cSThomas Abraham 
43677b55e8cSThomas Abraham 	switch (src) {
43777b55e8cSThomas Abraham 	case EXYNOS_SRC_MPLL:
43877b55e8cSThomas Abraham 		sclk = exynos5_get_pll_clk(MPLL);
43977b55e8cSThomas Abraham 		break;
44077b55e8cSThomas Abraham 	case EXYNOS_SRC_EPLL:
44177b55e8cSThomas Abraham 		sclk = exynos5_get_pll_clk(EPLL);
44277b55e8cSThomas Abraham 		break;
44377b55e8cSThomas Abraham 	case EXYNOS_SRC_VPLL:
44477b55e8cSThomas Abraham 		sclk = exynos5_get_pll_clk(VPLL);
44577b55e8cSThomas Abraham 		break;
44677b55e8cSThomas Abraham 	default:
44777b55e8cSThomas Abraham 		debug("%s: EXYNOS_SRC %d not supported\n", __func__, src);
44877b55e8cSThomas Abraham 		return 0;
44977b55e8cSThomas Abraham 	}
45077b55e8cSThomas Abraham 
45177b55e8cSThomas Abraham 	/* Clock divider ratio for this peripheral */
45277b55e8cSThomas Abraham 	if (bit_info->div_bit >= 0)
45377b55e8cSThomas Abraham 		div = (div >> bit_info->div_bit) & bit_info->div_mask;
45477b55e8cSThomas Abraham 
45577b55e8cSThomas Abraham 	/* Clock pre-divider ratio for this peripheral */
45677b55e8cSThomas Abraham 	if (bit_info->prediv_bit >= 0)
45777b55e8cSThomas Abraham 		sub_div = (sub_div >> bit_info->prediv_bit)
45877b55e8cSThomas Abraham 			  & bit_info->prediv_mask;
45977b55e8cSThomas Abraham 
46077b55e8cSThomas Abraham 	/* Calculate and return required clock rate */
46177b55e8cSThomas Abraham 	return (sclk / (div + 1)) / (sub_div + 1);
46277b55e8cSThomas Abraham }
46377b55e8cSThomas Abraham 
exynos542x_get_periph_rate(int peripheral)46477b55e8cSThomas Abraham static unsigned long exynos542x_get_periph_rate(int peripheral)
46577b55e8cSThomas Abraham {
46677b55e8cSThomas Abraham 	struct clk_bit_info *bit_info = get_clk_bit_info(peripheral);
46777b55e8cSThomas Abraham 	unsigned long sclk = 0;
46877b55e8cSThomas Abraham 	unsigned int src = 0, div = 0, sub_div = 0;
46977b55e8cSThomas Abraham 	struct exynos5420_clock *clk =
47077b55e8cSThomas Abraham 			(struct exynos5420_clock *)samsung_get_base_clock();
47177b55e8cSThomas Abraham 
47277b55e8cSThomas Abraham 	switch (peripheral) {
47377b55e8cSThomas Abraham 	case PERIPH_ID_UART0:
47477b55e8cSThomas Abraham 	case PERIPH_ID_UART1:
47577b55e8cSThomas Abraham 	case PERIPH_ID_UART2:
47677b55e8cSThomas Abraham 	case PERIPH_ID_UART3:
47777b55e8cSThomas Abraham 	case PERIPH_ID_PWM0:
47877b55e8cSThomas Abraham 	case PERIPH_ID_PWM1:
47977b55e8cSThomas Abraham 	case PERIPH_ID_PWM2:
48077b55e8cSThomas Abraham 	case PERIPH_ID_PWM3:
48177b55e8cSThomas Abraham 	case PERIPH_ID_PWM4:
48277b55e8cSThomas Abraham 		src = readl(&clk->src_peric0);
48377b55e8cSThomas Abraham 		div = readl(&clk->div_peric0);
48477b55e8cSThomas Abraham 		break;
48577b55e8cSThomas Abraham 	case PERIPH_ID_SPI0:
48677b55e8cSThomas Abraham 	case PERIPH_ID_SPI1:
48777b55e8cSThomas Abraham 	case PERIPH_ID_SPI2:
48877b55e8cSThomas Abraham 		src = readl(&clk->src_peric1);
48977b55e8cSThomas Abraham 		div = readl(&clk->div_peric1);
49077b55e8cSThomas Abraham 		sub_div = readl(&clk->div_peric4);
49177b55e8cSThomas Abraham 		break;
49277b55e8cSThomas Abraham 	case PERIPH_ID_SPI3:
49377b55e8cSThomas Abraham 	case PERIPH_ID_SPI4:
49477b55e8cSThomas Abraham 		src = readl(&clk->src_isp);
49577b55e8cSThomas Abraham 		div = readl(&clk->div_isp1);
49677b55e8cSThomas Abraham 		sub_div = readl(&clk->div_isp1);
49777b55e8cSThomas Abraham 		break;
49877b55e8cSThomas Abraham 	case PERIPH_ID_SDMMC0:
49977b55e8cSThomas Abraham 	case PERIPH_ID_SDMMC1:
50077b55e8cSThomas Abraham 	case PERIPH_ID_SDMMC2:
50177b55e8cSThomas Abraham 	case PERIPH_ID_SDMMC3:
50277b55e8cSThomas Abraham 		src = readl(&clk->src_fsys);
50377b55e8cSThomas Abraham 		div = readl(&clk->div_fsys1);
50477b55e8cSThomas Abraham 		break;
50577b55e8cSThomas Abraham 	case PERIPH_ID_I2C0:
50677b55e8cSThomas Abraham 	case PERIPH_ID_I2C1:
50777b55e8cSThomas Abraham 	case PERIPH_ID_I2C2:
50877b55e8cSThomas Abraham 	case PERIPH_ID_I2C3:
50977b55e8cSThomas Abraham 	case PERIPH_ID_I2C4:
51077b55e8cSThomas Abraham 	case PERIPH_ID_I2C5:
51177b55e8cSThomas Abraham 	case PERIPH_ID_I2C6:
51277b55e8cSThomas Abraham 	case PERIPH_ID_I2C7:
51377b55e8cSThomas Abraham 	case PERIPH_ID_I2C8:
51477b55e8cSThomas Abraham 	case PERIPH_ID_I2C9:
51577b55e8cSThomas Abraham 	case PERIPH_ID_I2C10:
51677b55e8cSThomas Abraham 		src = EXYNOS542X_SRC_MPLL;
51777b55e8cSThomas Abraham 		div = readl(&clk->div_top1);
51877b55e8cSThomas Abraham 		break;
51977b55e8cSThomas Abraham 	default:
52077b55e8cSThomas Abraham 		debug("%s: invalid peripheral %d", __func__, peripheral);
52177b55e8cSThomas Abraham 		return -1;
52277b55e8cSThomas Abraham 	};
52377b55e8cSThomas Abraham 
52477b55e8cSThomas Abraham 	if (bit_info->src_bit >= 0)
52577b55e8cSThomas Abraham 		src = (src >> bit_info->src_bit) & bit_info->src_mask;
52677b55e8cSThomas Abraham 
52777b55e8cSThomas Abraham 	switch (src) {
52877b55e8cSThomas Abraham 	case EXYNOS542X_SRC_MPLL:
52977b55e8cSThomas Abraham 		sclk = exynos542x_get_pll_clk(MPLL);
53077b55e8cSThomas Abraham 		break;
53177b55e8cSThomas Abraham 	case EXYNOS542X_SRC_SPLL:
53277b55e8cSThomas Abraham 		sclk = exynos542x_get_pll_clk(SPLL);
53377b55e8cSThomas Abraham 		break;
53477b55e8cSThomas Abraham 	case EXYNOS542X_SRC_EPLL:
53577b55e8cSThomas Abraham 		sclk = exynos542x_get_pll_clk(EPLL);
53677b55e8cSThomas Abraham 		break;
53777b55e8cSThomas Abraham 	case EXYNOS542X_SRC_RPLL:
53877b55e8cSThomas Abraham 		sclk = exynos542x_get_pll_clk(RPLL);
53977b55e8cSThomas Abraham 		break;
54077b55e8cSThomas Abraham 	default:
54177b55e8cSThomas Abraham 		debug("%s: EXYNOS542X_SRC %d not supported", __func__, src);
54277b55e8cSThomas Abraham 		return 0;
54377b55e8cSThomas Abraham 	}
54477b55e8cSThomas Abraham 
54577b55e8cSThomas Abraham 	/* Clock divider ratio for this peripheral */
54677b55e8cSThomas Abraham 	if (bit_info->div_bit >= 0)
54777b55e8cSThomas Abraham 		div = (div >> bit_info->div_bit) & bit_info->div_mask;
54877b55e8cSThomas Abraham 
54977b55e8cSThomas Abraham 	/* Clock pre-divider ratio for this peripheral */
55077b55e8cSThomas Abraham 	if (bit_info->prediv_bit >= 0)
55177b55e8cSThomas Abraham 		sub_div = (sub_div >> bit_info->prediv_bit)
55277b55e8cSThomas Abraham 			  & bit_info->prediv_mask;
55377b55e8cSThomas Abraham 
55477b55e8cSThomas Abraham 	/* Calculate and return required clock rate */
55577b55e8cSThomas Abraham 	return (sclk / (div + 1)) / (sub_div + 1);
55677b55e8cSThomas Abraham }
55777b55e8cSThomas Abraham 
clock_get_periph_rate(int peripheral)55877b55e8cSThomas Abraham unsigned long clock_get_periph_rate(int peripheral)
55977b55e8cSThomas Abraham {
56077b55e8cSThomas Abraham 	if (cpu_is_exynos5()) {
561*d64c8adeSPrzemyslaw Marczak 		if (proid_is_exynos5420() || proid_is_exynos5422())
56277b55e8cSThomas Abraham 			return exynos542x_get_periph_rate(peripheral);
56377b55e8cSThomas Abraham 		return exynos5_get_periph_rate(peripheral);
56477b55e8cSThomas Abraham 	} else {
56577b55e8cSThomas Abraham 		return 0;
56677b55e8cSThomas Abraham 	}
56777b55e8cSThomas Abraham }
56877b55e8cSThomas Abraham 
56977b55e8cSThomas Abraham /* exynos4: return ARM clock frequency */
exynos4_get_arm_clk(void)57077b55e8cSThomas Abraham static unsigned long exynos4_get_arm_clk(void)
57177b55e8cSThomas Abraham {
57277b55e8cSThomas Abraham 	struct exynos4_clock *clk =
57377b55e8cSThomas Abraham 		(struct exynos4_clock *)samsung_get_base_clock();
57477b55e8cSThomas Abraham 	unsigned long div;
57577b55e8cSThomas Abraham 	unsigned long armclk;
57677b55e8cSThomas Abraham 	unsigned int core_ratio;
57777b55e8cSThomas Abraham 	unsigned int core2_ratio;
57877b55e8cSThomas Abraham 
57977b55e8cSThomas Abraham 	div = readl(&clk->div_cpu0);
58077b55e8cSThomas Abraham 
58177b55e8cSThomas Abraham 	/* CORE_RATIO: [2:0], CORE2_RATIO: [30:28] */
58277b55e8cSThomas Abraham 	core_ratio = (div >> 0) & 0x7;
58377b55e8cSThomas Abraham 	core2_ratio = (div >> 28) & 0x7;
58477b55e8cSThomas Abraham 
58577b55e8cSThomas Abraham 	armclk = get_pll_clk(APLL) / (core_ratio + 1);
58677b55e8cSThomas Abraham 	armclk /= (core2_ratio + 1);
58777b55e8cSThomas Abraham 
58877b55e8cSThomas Abraham 	return armclk;
58977b55e8cSThomas Abraham }
59077b55e8cSThomas Abraham 
59177b55e8cSThomas Abraham /* exynos4x12: return ARM clock frequency */
exynos4x12_get_arm_clk(void)59277b55e8cSThomas Abraham static unsigned long exynos4x12_get_arm_clk(void)
59377b55e8cSThomas Abraham {
59477b55e8cSThomas Abraham 	struct exynos4x12_clock *clk =
59577b55e8cSThomas Abraham 		(struct exynos4x12_clock *)samsung_get_base_clock();
59677b55e8cSThomas Abraham 	unsigned long div;
59777b55e8cSThomas Abraham 	unsigned long armclk;
59877b55e8cSThomas Abraham 	unsigned int core_ratio;
59977b55e8cSThomas Abraham 	unsigned int core2_ratio;
60077b55e8cSThomas Abraham 
60177b55e8cSThomas Abraham 	div = readl(&clk->div_cpu0);
60277b55e8cSThomas Abraham 
60377b55e8cSThomas Abraham 	/* CORE_RATIO: [2:0], CORE2_RATIO: [30:28] */
60477b55e8cSThomas Abraham 	core_ratio = (div >> 0) & 0x7;
60577b55e8cSThomas Abraham 	core2_ratio = (div >> 28) & 0x7;
60677b55e8cSThomas Abraham 
60777b55e8cSThomas Abraham 	armclk = get_pll_clk(APLL) / (core_ratio + 1);
60877b55e8cSThomas Abraham 	armclk /= (core2_ratio + 1);
60977b55e8cSThomas Abraham 
61077b55e8cSThomas Abraham 	return armclk;
61177b55e8cSThomas Abraham }
61277b55e8cSThomas Abraham 
61377b55e8cSThomas Abraham /* exynos5: return ARM clock frequency */
exynos5_get_arm_clk(void)61477b55e8cSThomas Abraham static unsigned long exynos5_get_arm_clk(void)
61577b55e8cSThomas Abraham {
61677b55e8cSThomas Abraham 	struct exynos5_clock *clk =
61777b55e8cSThomas Abraham 		(struct exynos5_clock *)samsung_get_base_clock();
61877b55e8cSThomas Abraham 	unsigned long div;
61977b55e8cSThomas Abraham 	unsigned long armclk;
62077b55e8cSThomas Abraham 	unsigned int arm_ratio;
62177b55e8cSThomas Abraham 	unsigned int arm2_ratio;
62277b55e8cSThomas Abraham 
62377b55e8cSThomas Abraham 	div = readl(&clk->div_cpu0);
62477b55e8cSThomas Abraham 
62577b55e8cSThomas Abraham 	/* ARM_RATIO: [2:0], ARM2_RATIO: [30:28] */
62677b55e8cSThomas Abraham 	arm_ratio = (div >> 0) & 0x7;
62777b55e8cSThomas Abraham 	arm2_ratio = (div >> 28) & 0x7;
62877b55e8cSThomas Abraham 
62977b55e8cSThomas Abraham 	armclk = get_pll_clk(APLL) / (arm_ratio + 1);
63077b55e8cSThomas Abraham 	armclk /= (arm2_ratio + 1);
63177b55e8cSThomas Abraham 
63277b55e8cSThomas Abraham 	return armclk;
63377b55e8cSThomas Abraham }
63477b55e8cSThomas Abraham 
63577b55e8cSThomas Abraham /* exynos4: return pwm clock frequency */
exynos4_get_pwm_clk(void)63677b55e8cSThomas Abraham static unsigned long exynos4_get_pwm_clk(void)
63777b55e8cSThomas Abraham {
63877b55e8cSThomas Abraham 	struct exynos4_clock *clk =
63977b55e8cSThomas Abraham 		(struct exynos4_clock *)samsung_get_base_clock();
64077b55e8cSThomas Abraham 	unsigned long pclk, sclk;
64177b55e8cSThomas Abraham 	unsigned int sel;
64277b55e8cSThomas Abraham 	unsigned int ratio;
64377b55e8cSThomas Abraham 
64477b55e8cSThomas Abraham 	if (s5p_get_cpu_rev() == 0) {
64577b55e8cSThomas Abraham 		/*
64677b55e8cSThomas Abraham 		 * CLK_SRC_PERIL0
64777b55e8cSThomas Abraham 		 * PWM_SEL [27:24]
64877b55e8cSThomas Abraham 		 */
64977b55e8cSThomas Abraham 		sel = readl(&clk->src_peril0);
65077b55e8cSThomas Abraham 		sel = (sel >> 24) & 0xf;
65177b55e8cSThomas Abraham 
65277b55e8cSThomas Abraham 		if (sel == 0x6)
65377b55e8cSThomas Abraham 			sclk = get_pll_clk(MPLL);
65477b55e8cSThomas Abraham 		else if (sel == 0x7)
65577b55e8cSThomas Abraham 			sclk = get_pll_clk(EPLL);
65677b55e8cSThomas Abraham 		else if (sel == 0x8)
65777b55e8cSThomas Abraham 			sclk = get_pll_clk(VPLL);
65877b55e8cSThomas Abraham 		else
65977b55e8cSThomas Abraham 			return 0;
66077b55e8cSThomas Abraham 
66177b55e8cSThomas Abraham 		/*
66277b55e8cSThomas Abraham 		 * CLK_DIV_PERIL3
66377b55e8cSThomas Abraham 		 * PWM_RATIO [3:0]
66477b55e8cSThomas Abraham 		 */
66577b55e8cSThomas Abraham 		ratio = readl(&clk->div_peril3);
66677b55e8cSThomas Abraham 		ratio = ratio & 0xf;
66777b55e8cSThomas Abraham 	} else if (s5p_get_cpu_rev() == 1) {
66877b55e8cSThomas Abraham 		sclk = get_pll_clk(MPLL);
66977b55e8cSThomas Abraham 		ratio = 8;
67077b55e8cSThomas Abraham 	} else
67177b55e8cSThomas Abraham 		return 0;
67277b55e8cSThomas Abraham 
67377b55e8cSThomas Abraham 	pclk = sclk / (ratio + 1);
67477b55e8cSThomas Abraham 
67577b55e8cSThomas Abraham 	return pclk;
67677b55e8cSThomas Abraham }
67777b55e8cSThomas Abraham 
67877b55e8cSThomas Abraham /* exynos4x12: return pwm clock frequency */
exynos4x12_get_pwm_clk(void)67977b55e8cSThomas Abraham static unsigned long exynos4x12_get_pwm_clk(void)
68077b55e8cSThomas Abraham {
68177b55e8cSThomas Abraham 	unsigned long pclk, sclk;
68277b55e8cSThomas Abraham 	unsigned int ratio;
68377b55e8cSThomas Abraham 
68477b55e8cSThomas Abraham 	sclk = get_pll_clk(MPLL);
68577b55e8cSThomas Abraham 	ratio = 8;
68677b55e8cSThomas Abraham 
68777b55e8cSThomas Abraham 	pclk = sclk / (ratio + 1);
68877b55e8cSThomas Abraham 
68977b55e8cSThomas Abraham 	return pclk;
69077b55e8cSThomas Abraham }
69177b55e8cSThomas Abraham 
69277b55e8cSThomas Abraham /* exynos4: return uart clock frequency */
exynos4_get_uart_clk(int dev_index)69377b55e8cSThomas Abraham static unsigned long exynos4_get_uart_clk(int dev_index)
69477b55e8cSThomas Abraham {
69577b55e8cSThomas Abraham 	struct exynos4_clock *clk =
69677b55e8cSThomas Abraham 		(struct exynos4_clock *)samsung_get_base_clock();
69777b55e8cSThomas Abraham 	unsigned long uclk, sclk;
69877b55e8cSThomas Abraham 	unsigned int sel;
69977b55e8cSThomas Abraham 	unsigned int ratio;
70077b55e8cSThomas Abraham 
70177b55e8cSThomas Abraham 	/*
70277b55e8cSThomas Abraham 	 * CLK_SRC_PERIL0
70377b55e8cSThomas Abraham 	 * UART0_SEL [3:0]
70477b55e8cSThomas Abraham 	 * UART1_SEL [7:4]
70577b55e8cSThomas Abraham 	 * UART2_SEL [8:11]
70677b55e8cSThomas Abraham 	 * UART3_SEL [12:15]
70777b55e8cSThomas Abraham 	 * UART4_SEL [16:19]
70877b55e8cSThomas Abraham 	 * UART5_SEL [23:20]
70977b55e8cSThomas Abraham 	 */
71077b55e8cSThomas Abraham 	sel = readl(&clk->src_peril0);
71177b55e8cSThomas Abraham 	sel = (sel >> (dev_index << 2)) & 0xf;
71277b55e8cSThomas Abraham 
71377b55e8cSThomas Abraham 	if (sel == 0x6)
71477b55e8cSThomas Abraham 		sclk = get_pll_clk(MPLL);
71577b55e8cSThomas Abraham 	else if (sel == 0x7)
71677b55e8cSThomas Abraham 		sclk = get_pll_clk(EPLL);
71777b55e8cSThomas Abraham 	else if (sel == 0x8)
71877b55e8cSThomas Abraham 		sclk = get_pll_clk(VPLL);
71977b55e8cSThomas Abraham 	else
72077b55e8cSThomas Abraham 		return 0;
72177b55e8cSThomas Abraham 
72277b55e8cSThomas Abraham 	/*
72377b55e8cSThomas Abraham 	 * CLK_DIV_PERIL0
72477b55e8cSThomas Abraham 	 * UART0_RATIO [3:0]
72577b55e8cSThomas Abraham 	 * UART1_RATIO [7:4]
72677b55e8cSThomas Abraham 	 * UART2_RATIO [8:11]
72777b55e8cSThomas Abraham 	 * UART3_RATIO [12:15]
72877b55e8cSThomas Abraham 	 * UART4_RATIO [16:19]
72977b55e8cSThomas Abraham 	 * UART5_RATIO [23:20]
73077b55e8cSThomas Abraham 	 */
73177b55e8cSThomas Abraham 	ratio = readl(&clk->div_peril0);
73277b55e8cSThomas Abraham 	ratio = (ratio >> (dev_index << 2)) & 0xf;
73377b55e8cSThomas Abraham 
73477b55e8cSThomas Abraham 	uclk = sclk / (ratio + 1);
73577b55e8cSThomas Abraham 
73677b55e8cSThomas Abraham 	return uclk;
73777b55e8cSThomas Abraham }
73877b55e8cSThomas Abraham 
73977b55e8cSThomas Abraham /* exynos4x12: return uart clock frequency */
exynos4x12_get_uart_clk(int dev_index)74077b55e8cSThomas Abraham static unsigned long exynos4x12_get_uart_clk(int dev_index)
74177b55e8cSThomas Abraham {
74277b55e8cSThomas Abraham 	struct exynos4x12_clock *clk =
74377b55e8cSThomas Abraham 		(struct exynos4x12_clock *)samsung_get_base_clock();
74477b55e8cSThomas Abraham 	unsigned long uclk, sclk;
74577b55e8cSThomas Abraham 	unsigned int sel;
74677b55e8cSThomas Abraham 	unsigned int ratio;
74777b55e8cSThomas Abraham 
74877b55e8cSThomas Abraham 	/*
74977b55e8cSThomas Abraham 	 * CLK_SRC_PERIL0
75077b55e8cSThomas Abraham 	 * UART0_SEL [3:0]
75177b55e8cSThomas Abraham 	 * UART1_SEL [7:4]
75277b55e8cSThomas Abraham 	 * UART2_SEL [8:11]
75377b55e8cSThomas Abraham 	 * UART3_SEL [12:15]
75477b55e8cSThomas Abraham 	 * UART4_SEL [16:19]
75577b55e8cSThomas Abraham 	 */
75677b55e8cSThomas Abraham 	sel = readl(&clk->src_peril0);
75777b55e8cSThomas Abraham 	sel = (sel >> (dev_index << 2)) & 0xf;
75877b55e8cSThomas Abraham 
75977b55e8cSThomas Abraham 	if (sel == 0x6)
76077b55e8cSThomas Abraham 		sclk = get_pll_clk(MPLL);
76177b55e8cSThomas Abraham 	else if (sel == 0x7)
76277b55e8cSThomas Abraham 		sclk = get_pll_clk(EPLL);
76377b55e8cSThomas Abraham 	else if (sel == 0x8)
76477b55e8cSThomas Abraham 		sclk = get_pll_clk(VPLL);
76577b55e8cSThomas Abraham 	else
76677b55e8cSThomas Abraham 		return 0;
76777b55e8cSThomas Abraham 
76877b55e8cSThomas Abraham 	/*
76977b55e8cSThomas Abraham 	 * CLK_DIV_PERIL0
77077b55e8cSThomas Abraham 	 * UART0_RATIO [3:0]
77177b55e8cSThomas Abraham 	 * UART1_RATIO [7:4]
77277b55e8cSThomas Abraham 	 * UART2_RATIO [8:11]
77377b55e8cSThomas Abraham 	 * UART3_RATIO [12:15]
77477b55e8cSThomas Abraham 	 * UART4_RATIO [16:19]
77577b55e8cSThomas Abraham 	 */
77677b55e8cSThomas Abraham 	ratio = readl(&clk->div_peril0);
77777b55e8cSThomas Abraham 	ratio = (ratio >> (dev_index << 2)) & 0xf;
77877b55e8cSThomas Abraham 
77977b55e8cSThomas Abraham 	uclk = sclk / (ratio + 1);
78077b55e8cSThomas Abraham 
78177b55e8cSThomas Abraham 	return uclk;
78277b55e8cSThomas Abraham }
78377b55e8cSThomas Abraham 
exynos4_get_mmc_clk(int dev_index)78477b55e8cSThomas Abraham static unsigned long exynos4_get_mmc_clk(int dev_index)
78577b55e8cSThomas Abraham {
78677b55e8cSThomas Abraham 	struct exynos4_clock *clk =
78777b55e8cSThomas Abraham 		(struct exynos4_clock *)samsung_get_base_clock();
78877b55e8cSThomas Abraham 	unsigned long uclk, sclk;
78977b55e8cSThomas Abraham 	unsigned int sel, ratio, pre_ratio;
79077b55e8cSThomas Abraham 	int shift = 0;
79177b55e8cSThomas Abraham 
79277b55e8cSThomas Abraham 	sel = readl(&clk->src_fsys);
79377b55e8cSThomas Abraham 	sel = (sel >> (dev_index << 2)) & 0xf;
79477b55e8cSThomas Abraham 
79577b55e8cSThomas Abraham 	if (sel == 0x6)
79677b55e8cSThomas Abraham 		sclk = get_pll_clk(MPLL);
79777b55e8cSThomas Abraham 	else if (sel == 0x7)
79877b55e8cSThomas Abraham 		sclk = get_pll_clk(EPLL);
79977b55e8cSThomas Abraham 	else if (sel == 0x8)
80077b55e8cSThomas Abraham 		sclk = get_pll_clk(VPLL);
80177b55e8cSThomas Abraham 	else
80277b55e8cSThomas Abraham 		return 0;
80377b55e8cSThomas Abraham 
80477b55e8cSThomas Abraham 	switch (dev_index) {
80577b55e8cSThomas Abraham 	case 0:
80677b55e8cSThomas Abraham 	case 1:
80777b55e8cSThomas Abraham 		ratio = readl(&clk->div_fsys1);
80877b55e8cSThomas Abraham 		pre_ratio = readl(&clk->div_fsys1);
80977b55e8cSThomas Abraham 		break;
81077b55e8cSThomas Abraham 	case 2:
81177b55e8cSThomas Abraham 	case 3:
81277b55e8cSThomas Abraham 		ratio = readl(&clk->div_fsys2);
81377b55e8cSThomas Abraham 		pre_ratio = readl(&clk->div_fsys2);
81477b55e8cSThomas Abraham 		break;
81577b55e8cSThomas Abraham 	case 4:
81677b55e8cSThomas Abraham 		ratio = readl(&clk->div_fsys3);
81777b55e8cSThomas Abraham 		pre_ratio = readl(&clk->div_fsys3);
81877b55e8cSThomas Abraham 		break;
81977b55e8cSThomas Abraham 	default:
82077b55e8cSThomas Abraham 		return 0;
82177b55e8cSThomas Abraham 	}
82277b55e8cSThomas Abraham 
82377b55e8cSThomas Abraham 	if (dev_index == 1 || dev_index == 3)
82477b55e8cSThomas Abraham 		shift = 16;
82577b55e8cSThomas Abraham 
82677b55e8cSThomas Abraham 	ratio = (ratio >> shift) & 0xf;
82777b55e8cSThomas Abraham 	pre_ratio = (pre_ratio >> (shift + 8)) & 0xff;
82877b55e8cSThomas Abraham 	uclk = (sclk / (ratio + 1)) / (pre_ratio + 1);
82977b55e8cSThomas Abraham 
83077b55e8cSThomas Abraham 	return uclk;
83177b55e8cSThomas Abraham }
83277b55e8cSThomas Abraham 
83377b55e8cSThomas Abraham /* exynos4: set the mmc clock */
exynos4_set_mmc_clk(int dev_index,unsigned int div)83477b55e8cSThomas Abraham static void exynos4_set_mmc_clk(int dev_index, unsigned int div)
83577b55e8cSThomas Abraham {
83677b55e8cSThomas Abraham 	struct exynos4_clock *clk =
83777b55e8cSThomas Abraham 		(struct exynos4_clock *)samsung_get_base_clock();
83877b55e8cSThomas Abraham 	unsigned int addr, clear_bit, set_bit;
83977b55e8cSThomas Abraham 
84077b55e8cSThomas Abraham 	/*
84177b55e8cSThomas Abraham 	 * CLK_DIV_FSYS1
84277b55e8cSThomas Abraham 	 * MMC0_PRE_RATIO [15:8], MMC1_PRE_RATIO [31:24]
84377b55e8cSThomas Abraham 	 * CLK_DIV_FSYS2
84477b55e8cSThomas Abraham 	 * MMC2_PRE_RATIO [15:8], MMC3_PRE_RATIO [31:24]
84577b55e8cSThomas Abraham 	 * CLK_DIV_FSYS3
84677b55e8cSThomas Abraham 	 * MMC4_RATIO [3:0]
84777b55e8cSThomas Abraham 	 */
84877b55e8cSThomas Abraham 	if (dev_index < 2) {
84977b55e8cSThomas Abraham 		addr = (unsigned int)&clk->div_fsys1;
85077b55e8cSThomas Abraham 		clear_bit = MASK_PRE_RATIO(dev_index);
85177b55e8cSThomas Abraham 		set_bit = SET_PRE_RATIO(dev_index, div);
85277b55e8cSThomas Abraham 	} else if (dev_index == 4) {
85377b55e8cSThomas Abraham 		addr = (unsigned int)&clk->div_fsys3;
85477b55e8cSThomas Abraham 		dev_index -= 4;
85577b55e8cSThomas Abraham 		/* MMC4 is controlled with the MMC4_RATIO value */
85677b55e8cSThomas Abraham 		clear_bit = MASK_RATIO(dev_index);
85777b55e8cSThomas Abraham 		set_bit = SET_RATIO(dev_index, div);
85877b55e8cSThomas Abraham 	} else {
85977b55e8cSThomas Abraham 		addr = (unsigned int)&clk->div_fsys2;
86077b55e8cSThomas Abraham 		dev_index -= 2;
86177b55e8cSThomas Abraham 		clear_bit = MASK_PRE_RATIO(dev_index);
86277b55e8cSThomas Abraham 		set_bit = SET_PRE_RATIO(dev_index, div);
86377b55e8cSThomas Abraham 	}
86477b55e8cSThomas Abraham 
86577b55e8cSThomas Abraham 	clrsetbits_le32(addr, clear_bit, set_bit);
86677b55e8cSThomas Abraham }
86777b55e8cSThomas Abraham 
86877b55e8cSThomas Abraham /* exynos5: set the mmc clock */
exynos5_set_mmc_clk(int dev_index,unsigned int div)86977b55e8cSThomas Abraham static void exynos5_set_mmc_clk(int dev_index, unsigned int div)
87077b55e8cSThomas Abraham {
87177b55e8cSThomas Abraham 	struct exynos5_clock *clk =
87277b55e8cSThomas Abraham 		(struct exynos5_clock *)samsung_get_base_clock();
87377b55e8cSThomas Abraham 	unsigned int addr;
87477b55e8cSThomas Abraham 
87577b55e8cSThomas Abraham 	/*
87677b55e8cSThomas Abraham 	 * CLK_DIV_FSYS1
87777b55e8cSThomas Abraham 	 * MMC0_PRE_RATIO [15:8], MMC1_PRE_RATIO [31:24]
87877b55e8cSThomas Abraham 	 * CLK_DIV_FSYS2
87977b55e8cSThomas Abraham 	 * MMC2_PRE_RATIO [15:8], MMC3_PRE_RATIO [31:24]
88077b55e8cSThomas Abraham 	 */
88177b55e8cSThomas Abraham 	if (dev_index < 2) {
88277b55e8cSThomas Abraham 		addr = (unsigned int)&clk->div_fsys1;
88377b55e8cSThomas Abraham 	} else {
88477b55e8cSThomas Abraham 		addr = (unsigned int)&clk->div_fsys2;
88577b55e8cSThomas Abraham 		dev_index -= 2;
88677b55e8cSThomas Abraham 	}
88777b55e8cSThomas Abraham 
88877b55e8cSThomas Abraham 	clrsetbits_le32(addr, 0xff << ((dev_index << 4) + 8),
88977b55e8cSThomas Abraham 			(div & 0xff) << ((dev_index << 4) + 8));
89077b55e8cSThomas Abraham }
89177b55e8cSThomas Abraham 
89277b55e8cSThomas Abraham /* exynos5: set the mmc clock */
exynos5420_set_mmc_clk(int dev_index,unsigned int div)89377b55e8cSThomas Abraham static void exynos5420_set_mmc_clk(int dev_index, unsigned int div)
89477b55e8cSThomas Abraham {
89577b55e8cSThomas Abraham 	struct exynos5420_clock *clk =
89677b55e8cSThomas Abraham 		(struct exynos5420_clock *)samsung_get_base_clock();
89777b55e8cSThomas Abraham 	unsigned int addr;
89877b55e8cSThomas Abraham 	unsigned int shift;
89977b55e8cSThomas Abraham 
90077b55e8cSThomas Abraham 	/*
90177b55e8cSThomas Abraham 	 * CLK_DIV_FSYS1
90277b55e8cSThomas Abraham 	 * MMC0_RATIO [9:0]
90377b55e8cSThomas Abraham 	 * MMC1_RATIO [19:10]
90477b55e8cSThomas Abraham 	 * MMC2_RATIO [29:20]
90577b55e8cSThomas Abraham 	 */
90677b55e8cSThomas Abraham 	addr = (unsigned int)&clk->div_fsys1;
90777b55e8cSThomas Abraham 	shift = dev_index * 10;
90877b55e8cSThomas Abraham 
90977b55e8cSThomas Abraham 	clrsetbits_le32(addr, 0x3ff << shift, (div & 0x3ff) << shift);
91077b55e8cSThomas Abraham }
91177b55e8cSThomas Abraham 
91277b55e8cSThomas Abraham /* get_lcd_clk: return lcd clock frequency */
exynos4_get_lcd_clk(void)91377b55e8cSThomas Abraham static unsigned long exynos4_get_lcd_clk(void)
91477b55e8cSThomas Abraham {
91577b55e8cSThomas Abraham 	struct exynos4_clock *clk =
91677b55e8cSThomas Abraham 		(struct exynos4_clock *)samsung_get_base_clock();
91777b55e8cSThomas Abraham 	unsigned long pclk, sclk;
91877b55e8cSThomas Abraham 	unsigned int sel;
91977b55e8cSThomas Abraham 	unsigned int ratio;
92077b55e8cSThomas Abraham 
92177b55e8cSThomas Abraham 	/*
92277b55e8cSThomas Abraham 	 * CLK_SRC_LCD0
92377b55e8cSThomas Abraham 	 * FIMD0_SEL [3:0]
92477b55e8cSThomas Abraham 	 */
92577b55e8cSThomas Abraham 	sel = readl(&clk->src_lcd0);
92677b55e8cSThomas Abraham 	sel = sel & 0xf;
92777b55e8cSThomas Abraham 
92877b55e8cSThomas Abraham 	/*
92977b55e8cSThomas Abraham 	 * 0x6: SCLK_MPLL
93077b55e8cSThomas Abraham 	 * 0x7: SCLK_EPLL
93177b55e8cSThomas Abraham 	 * 0x8: SCLK_VPLL
93277b55e8cSThomas Abraham 	 */
93377b55e8cSThomas Abraham 	if (sel == 0x6)
93477b55e8cSThomas Abraham 		sclk = get_pll_clk(MPLL);
93577b55e8cSThomas Abraham 	else if (sel == 0x7)
93677b55e8cSThomas Abraham 		sclk = get_pll_clk(EPLL);
93777b55e8cSThomas Abraham 	else if (sel == 0x8)
93877b55e8cSThomas Abraham 		sclk = get_pll_clk(VPLL);
93977b55e8cSThomas Abraham 	else
94077b55e8cSThomas Abraham 		return 0;
94177b55e8cSThomas Abraham 
94277b55e8cSThomas Abraham 	/*
94377b55e8cSThomas Abraham 	 * CLK_DIV_LCD0
94477b55e8cSThomas Abraham 	 * FIMD0_RATIO [3:0]
94577b55e8cSThomas Abraham 	 */
94677b55e8cSThomas Abraham 	ratio = readl(&clk->div_lcd0);
94777b55e8cSThomas Abraham 	ratio = ratio & 0xf;
94877b55e8cSThomas Abraham 
94977b55e8cSThomas Abraham 	pclk = sclk / (ratio + 1);
95077b55e8cSThomas Abraham 
95177b55e8cSThomas Abraham 	return pclk;
95277b55e8cSThomas Abraham }
95377b55e8cSThomas Abraham 
95477b55e8cSThomas Abraham /* get_lcd_clk: return lcd clock frequency */
exynos5_get_lcd_clk(void)95577b55e8cSThomas Abraham static unsigned long exynos5_get_lcd_clk(void)
95677b55e8cSThomas Abraham {
95777b55e8cSThomas Abraham 	struct exynos5_clock *clk =
95877b55e8cSThomas Abraham 		(struct exynos5_clock *)samsung_get_base_clock();
95977b55e8cSThomas Abraham 	unsigned long pclk, sclk;
96077b55e8cSThomas Abraham 	unsigned int sel;
96177b55e8cSThomas Abraham 	unsigned int ratio;
96277b55e8cSThomas Abraham 
96377b55e8cSThomas Abraham 	/*
96477b55e8cSThomas Abraham 	 * CLK_SRC_LCD0
96577b55e8cSThomas Abraham 	 * FIMD0_SEL [3:0]
96677b55e8cSThomas Abraham 	 */
96777b55e8cSThomas Abraham 	sel = readl(&clk->src_disp1_0);
96877b55e8cSThomas Abraham 	sel = sel & 0xf;
96977b55e8cSThomas Abraham 
97077b55e8cSThomas Abraham 	/*
97177b55e8cSThomas Abraham 	 * 0x6: SCLK_MPLL
97277b55e8cSThomas Abraham 	 * 0x7: SCLK_EPLL
97377b55e8cSThomas Abraham 	 * 0x8: SCLK_VPLL
97477b55e8cSThomas Abraham 	 */
97577b55e8cSThomas Abraham 	if (sel == 0x6)
97677b55e8cSThomas Abraham 		sclk = get_pll_clk(MPLL);
97777b55e8cSThomas Abraham 	else if (sel == 0x7)
97877b55e8cSThomas Abraham 		sclk = get_pll_clk(EPLL);
97977b55e8cSThomas Abraham 	else if (sel == 0x8)
98077b55e8cSThomas Abraham 		sclk = get_pll_clk(VPLL);
98177b55e8cSThomas Abraham 	else
98277b55e8cSThomas Abraham 		return 0;
98377b55e8cSThomas Abraham 
98477b55e8cSThomas Abraham 	/*
98577b55e8cSThomas Abraham 	 * CLK_DIV_LCD0
98677b55e8cSThomas Abraham 	 * FIMD0_RATIO [3:0]
98777b55e8cSThomas Abraham 	 */
98877b55e8cSThomas Abraham 	ratio = readl(&clk->div_disp1_0);
98977b55e8cSThomas Abraham 	ratio = ratio & 0xf;
99077b55e8cSThomas Abraham 
99177b55e8cSThomas Abraham 	pclk = sclk / (ratio + 1);
99277b55e8cSThomas Abraham 
99377b55e8cSThomas Abraham 	return pclk;
99477b55e8cSThomas Abraham }
99577b55e8cSThomas Abraham 
exynos5420_get_lcd_clk(void)99677b55e8cSThomas Abraham static unsigned long exynos5420_get_lcd_clk(void)
99777b55e8cSThomas Abraham {
99877b55e8cSThomas Abraham 	struct exynos5420_clock *clk =
99977b55e8cSThomas Abraham 		(struct exynos5420_clock *)samsung_get_base_clock();
100077b55e8cSThomas Abraham 	unsigned long pclk, sclk;
100177b55e8cSThomas Abraham 	unsigned int sel;
100277b55e8cSThomas Abraham 	unsigned int ratio;
100377b55e8cSThomas Abraham 
100477b55e8cSThomas Abraham 	/*
100577b55e8cSThomas Abraham 	 * CLK_SRC_DISP10
100677b55e8cSThomas Abraham 	 * FIMD1_SEL [4]
100777b55e8cSThomas Abraham 	 * 0: SCLK_RPLL
100877b55e8cSThomas Abraham 	 * 1: SCLK_SPLL
100977b55e8cSThomas Abraham 	 */
101077b55e8cSThomas Abraham 	sel = readl(&clk->src_disp10);
101177b55e8cSThomas Abraham 	sel &= (1 << 4);
101277b55e8cSThomas Abraham 
101377b55e8cSThomas Abraham 	if (sel)
101477b55e8cSThomas Abraham 		sclk = get_pll_clk(SPLL);
101577b55e8cSThomas Abraham 	else
101677b55e8cSThomas Abraham 		sclk = get_pll_clk(RPLL);
101777b55e8cSThomas Abraham 
101877b55e8cSThomas Abraham 	/*
101977b55e8cSThomas Abraham 	 * CLK_DIV_DISP10
102077b55e8cSThomas Abraham 	 * FIMD1_RATIO [3:0]
102177b55e8cSThomas Abraham 	 */
102277b55e8cSThomas Abraham 	ratio = readl(&clk->div_disp10);
102377b55e8cSThomas Abraham 	ratio = ratio & 0xf;
102477b55e8cSThomas Abraham 
102577b55e8cSThomas Abraham 	pclk = sclk / (ratio + 1);
102677b55e8cSThomas Abraham 
102777b55e8cSThomas Abraham 	return pclk;
102877b55e8cSThomas Abraham }
102977b55e8cSThomas Abraham 
exynos5800_get_lcd_clk(void)103077b55e8cSThomas Abraham static unsigned long exynos5800_get_lcd_clk(void)
103177b55e8cSThomas Abraham {
103277b55e8cSThomas Abraham 	struct exynos5420_clock *clk =
103377b55e8cSThomas Abraham 		(struct exynos5420_clock *)samsung_get_base_clock();
103477b55e8cSThomas Abraham 	unsigned long sclk;
103577b55e8cSThomas Abraham 	unsigned int sel;
103677b55e8cSThomas Abraham 	unsigned int ratio;
103777b55e8cSThomas Abraham 
103877b55e8cSThomas Abraham 	/*
103977b55e8cSThomas Abraham 	 * CLK_SRC_DISP10
104077b55e8cSThomas Abraham 	 * CLKMUX_FIMD1 [6:4]
104177b55e8cSThomas Abraham 	 */
104277b55e8cSThomas Abraham 	sel = (readl(&clk->src_disp10) >> 4) & 0x7;
104377b55e8cSThomas Abraham 
104477b55e8cSThomas Abraham 	if (sel) {
104577b55e8cSThomas Abraham 		/*
104677b55e8cSThomas Abraham 		 * Mapping of CLK_SRC_DISP10 CLKMUX_FIMD1 [6:4] values into
104777b55e8cSThomas Abraham 		 * PLLs. The first element is a placeholder to bypass the
104877b55e8cSThomas Abraham 		 * default settig.
104977b55e8cSThomas Abraham 		 */
105077b55e8cSThomas Abraham 		const int reg_map[] = {0, CPLL, DPLL, MPLL, SPLL, IPLL, EPLL,
105177b55e8cSThomas Abraham 									RPLL};
105277b55e8cSThomas Abraham 		sclk = get_pll_clk(reg_map[sel]);
105377b55e8cSThomas Abraham 	} else
105477b55e8cSThomas Abraham 		sclk = CONFIG_SYS_CLK_FREQ;
105577b55e8cSThomas Abraham 	/*
105677b55e8cSThomas Abraham 	 * CLK_DIV_DISP10
105777b55e8cSThomas Abraham 	 * FIMD1_RATIO [3:0]
105877b55e8cSThomas Abraham 	 */
105977b55e8cSThomas Abraham 	ratio = readl(&clk->div_disp10) & 0xf;
106077b55e8cSThomas Abraham 
106177b55e8cSThomas Abraham 	return sclk / (ratio + 1);
106277b55e8cSThomas Abraham }
106377b55e8cSThomas Abraham 
exynos4_set_lcd_clk(void)106477b55e8cSThomas Abraham void exynos4_set_lcd_clk(void)
106577b55e8cSThomas Abraham {
106677b55e8cSThomas Abraham 	struct exynos4_clock *clk =
106777b55e8cSThomas Abraham 	    (struct exynos4_clock *)samsung_get_base_clock();
106877b55e8cSThomas Abraham 
106977b55e8cSThomas Abraham 	/*
107077b55e8cSThomas Abraham 	 * CLK_GATE_BLOCK
107177b55e8cSThomas Abraham 	 * CLK_CAM	[0]
107277b55e8cSThomas Abraham 	 * CLK_TV	[1]
107377b55e8cSThomas Abraham 	 * CLK_MFC	[2]
107477b55e8cSThomas Abraham 	 * CLK_G3D	[3]
107577b55e8cSThomas Abraham 	 * CLK_LCD0	[4]
107677b55e8cSThomas Abraham 	 * CLK_LCD1	[5]
107777b55e8cSThomas Abraham 	 * CLK_GPS	[7]
107877b55e8cSThomas Abraham 	 */
107977b55e8cSThomas Abraham 	setbits_le32(&clk->gate_block, 1 << 4);
108077b55e8cSThomas Abraham 
108177b55e8cSThomas Abraham 	/*
108277b55e8cSThomas Abraham 	 * CLK_SRC_LCD0
108377b55e8cSThomas Abraham 	 * FIMD0_SEL		[3:0]
108477b55e8cSThomas Abraham 	 * MDNIE0_SEL		[7:4]
108577b55e8cSThomas Abraham 	 * MDNIE_PWM0_SEL	[8:11]
108677b55e8cSThomas Abraham 	 * MIPI0_SEL		[12:15]
108777b55e8cSThomas Abraham 	 * set lcd0 src clock 0x6: SCLK_MPLL
108877b55e8cSThomas Abraham 	 */
108977b55e8cSThomas Abraham 	clrsetbits_le32(&clk->src_lcd0, 0xf, 0x6);
109077b55e8cSThomas Abraham 
109177b55e8cSThomas Abraham 	/*
109277b55e8cSThomas Abraham 	 * CLK_GATE_IP_LCD0
109377b55e8cSThomas Abraham 	 * CLK_FIMD0		[0]
109477b55e8cSThomas Abraham 	 * CLK_MIE0		[1]
109577b55e8cSThomas Abraham 	 * CLK_MDNIE0		[2]
109677b55e8cSThomas Abraham 	 * CLK_DSIM0		[3]
109777b55e8cSThomas Abraham 	 * CLK_SMMUFIMD0	[4]
109877b55e8cSThomas Abraham 	 * CLK_PPMULCD0		[5]
109977b55e8cSThomas Abraham 	 * Gating all clocks for FIMD0
110077b55e8cSThomas Abraham 	 */
110177b55e8cSThomas Abraham 	setbits_le32(&clk->gate_ip_lcd0, 1 << 0);
110277b55e8cSThomas Abraham 
110377b55e8cSThomas Abraham 	/*
110477b55e8cSThomas Abraham 	 * CLK_DIV_LCD0
110577b55e8cSThomas Abraham 	 * FIMD0_RATIO		[3:0]
110677b55e8cSThomas Abraham 	 * MDNIE0_RATIO		[7:4]
110777b55e8cSThomas Abraham 	 * MDNIE_PWM0_RATIO	[11:8]
110877b55e8cSThomas Abraham 	 * MDNIE_PWM_PRE_RATIO	[15:12]
110977b55e8cSThomas Abraham 	 * MIPI0_RATIO		[19:16]
111077b55e8cSThomas Abraham 	 * MIPI0_PRE_RATIO	[23:20]
111177b55e8cSThomas Abraham 	 * set fimd ratio
111277b55e8cSThomas Abraham 	 */
111377b55e8cSThomas Abraham 	clrsetbits_le32(&clk->div_lcd0, 0xf, 0x1);
111477b55e8cSThomas Abraham }
111577b55e8cSThomas Abraham 
exynos5_set_lcd_clk(void)111677b55e8cSThomas Abraham void exynos5_set_lcd_clk(void)
111777b55e8cSThomas Abraham {
111877b55e8cSThomas Abraham 	struct exynos5_clock *clk =
111977b55e8cSThomas Abraham 	    (struct exynos5_clock *)samsung_get_base_clock();
112077b55e8cSThomas Abraham 
112177b55e8cSThomas Abraham 	/*
112277b55e8cSThomas Abraham 	 * CLK_GATE_BLOCK
112377b55e8cSThomas Abraham 	 * CLK_CAM	[0]
112477b55e8cSThomas Abraham 	 * CLK_TV	[1]
112577b55e8cSThomas Abraham 	 * CLK_MFC	[2]
112677b55e8cSThomas Abraham 	 * CLK_G3D	[3]
112777b55e8cSThomas Abraham 	 * CLK_LCD0	[4]
112877b55e8cSThomas Abraham 	 * CLK_LCD1	[5]
112977b55e8cSThomas Abraham 	 * CLK_GPS	[7]
113077b55e8cSThomas Abraham 	 */
113177b55e8cSThomas Abraham 	setbits_le32(&clk->gate_block, 1 << 4);
113277b55e8cSThomas Abraham 
113377b55e8cSThomas Abraham 	/*
113477b55e8cSThomas Abraham 	 * CLK_SRC_LCD0
113577b55e8cSThomas Abraham 	 * FIMD0_SEL		[3:0]
113677b55e8cSThomas Abraham 	 * MDNIE0_SEL		[7:4]
113777b55e8cSThomas Abraham 	 * MDNIE_PWM0_SEL	[8:11]
113877b55e8cSThomas Abraham 	 * MIPI0_SEL		[12:15]
113977b55e8cSThomas Abraham 	 * set lcd0 src clock 0x6: SCLK_MPLL
114077b55e8cSThomas Abraham 	 */
114177b55e8cSThomas Abraham 	clrsetbits_le32(&clk->src_disp1_0, 0xf, 0x6);
114277b55e8cSThomas Abraham 
114377b55e8cSThomas Abraham 	/*
114477b55e8cSThomas Abraham 	 * CLK_GATE_IP_LCD0
114577b55e8cSThomas Abraham 	 * CLK_FIMD0		[0]
114677b55e8cSThomas Abraham 	 * CLK_MIE0		[1]
114777b55e8cSThomas Abraham 	 * CLK_MDNIE0		[2]
114877b55e8cSThomas Abraham 	 * CLK_DSIM0		[3]
114977b55e8cSThomas Abraham 	 * CLK_SMMUFIMD0	[4]
115077b55e8cSThomas Abraham 	 * CLK_PPMULCD0		[5]
115177b55e8cSThomas Abraham 	 * Gating all clocks for FIMD0
115277b55e8cSThomas Abraham 	 */
115377b55e8cSThomas Abraham 	setbits_le32(&clk->gate_ip_disp1, 1 << 0);
115477b55e8cSThomas Abraham 
115577b55e8cSThomas Abraham 	/*
115677b55e8cSThomas Abraham 	 * CLK_DIV_LCD0
115777b55e8cSThomas Abraham 	 * FIMD0_RATIO		[3:0]
115877b55e8cSThomas Abraham 	 * MDNIE0_RATIO		[7:4]
115977b55e8cSThomas Abraham 	 * MDNIE_PWM0_RATIO	[11:8]
116077b55e8cSThomas Abraham 	 * MDNIE_PWM_PRE_RATIO	[15:12]
116177b55e8cSThomas Abraham 	 * MIPI0_RATIO		[19:16]
116277b55e8cSThomas Abraham 	 * MIPI0_PRE_RATIO	[23:20]
116377b55e8cSThomas Abraham 	 * set fimd ratio
116477b55e8cSThomas Abraham 	 */
116577b55e8cSThomas Abraham 	clrsetbits_le32(&clk->div_disp1_0, 0xf, 0x0);
116677b55e8cSThomas Abraham }
116777b55e8cSThomas Abraham 
exynos5420_set_lcd_clk(void)116877b55e8cSThomas Abraham void exynos5420_set_lcd_clk(void)
116977b55e8cSThomas Abraham {
117077b55e8cSThomas Abraham 	struct exynos5420_clock *clk =
117177b55e8cSThomas Abraham 		(struct exynos5420_clock *)samsung_get_base_clock();
117277b55e8cSThomas Abraham 	unsigned int cfg;
117377b55e8cSThomas Abraham 
117477b55e8cSThomas Abraham 	/*
117577b55e8cSThomas Abraham 	 * CLK_SRC_DISP10
117677b55e8cSThomas Abraham 	 * FIMD1_SEL [4]
117777b55e8cSThomas Abraham 	 * 0: SCLK_RPLL
117877b55e8cSThomas Abraham 	 * 1: SCLK_SPLL
117977b55e8cSThomas Abraham 	 */
118077b55e8cSThomas Abraham 	cfg = readl(&clk->src_disp10);
118177b55e8cSThomas Abraham 	cfg &= ~(0x1 << 4);
118277b55e8cSThomas Abraham 	cfg |= (0 << 4);
118377b55e8cSThomas Abraham 	writel(cfg, &clk->src_disp10);
118477b55e8cSThomas Abraham 
118577b55e8cSThomas Abraham 	/*
118677b55e8cSThomas Abraham 	 * CLK_DIV_DISP10
118777b55e8cSThomas Abraham 	 * FIMD1_RATIO		[3:0]
118877b55e8cSThomas Abraham 	 */
118977b55e8cSThomas Abraham 	cfg = readl(&clk->div_disp10);
119077b55e8cSThomas Abraham 	cfg &= ~(0xf << 0);
119177b55e8cSThomas Abraham 	cfg |= (0 << 0);
119277b55e8cSThomas Abraham 	writel(cfg, &clk->div_disp10);
119377b55e8cSThomas Abraham }
119477b55e8cSThomas Abraham 
exynos5800_set_lcd_clk(void)119577b55e8cSThomas Abraham void exynos5800_set_lcd_clk(void)
119677b55e8cSThomas Abraham {
119777b55e8cSThomas Abraham 	struct exynos5420_clock *clk =
119877b55e8cSThomas Abraham 		(struct exynos5420_clock *)samsung_get_base_clock();
119977b55e8cSThomas Abraham 	unsigned int cfg;
120077b55e8cSThomas Abraham 
120177b55e8cSThomas Abraham 	/*
120277b55e8cSThomas Abraham 	 * Use RPLL for pixel clock
120377b55e8cSThomas Abraham 	 * CLK_SRC_DISP10 CLKMUX_FIMD1 [6:4]
120477b55e8cSThomas Abraham 	 * ==================
120577b55e8cSThomas Abraham 	 * 111: SCLK_RPLL
120677b55e8cSThomas Abraham 	 */
120777b55e8cSThomas Abraham 	cfg = readl(&clk->src_disp10) | (0x7 << 4);
120877b55e8cSThomas Abraham 	writel(cfg, &clk->src_disp10);
120977b55e8cSThomas Abraham 
121077b55e8cSThomas Abraham 	/*
121177b55e8cSThomas Abraham 	 * CLK_DIV_DISP10
121277b55e8cSThomas Abraham 	 * FIMD1_RATIO		[3:0]
121377b55e8cSThomas Abraham 	 */
121477b55e8cSThomas Abraham 	clrsetbits_le32(&clk->div_disp10, 0xf << 0, 0x0 << 0);
121577b55e8cSThomas Abraham }
121677b55e8cSThomas Abraham 
exynos4_set_mipi_clk(void)121777b55e8cSThomas Abraham void exynos4_set_mipi_clk(void)
121877b55e8cSThomas Abraham {
121977b55e8cSThomas Abraham 	struct exynos4_clock *clk =
122077b55e8cSThomas Abraham 	    (struct exynos4_clock *)samsung_get_base_clock();
122177b55e8cSThomas Abraham 
122277b55e8cSThomas Abraham 	/*
122377b55e8cSThomas Abraham 	 * CLK_SRC_LCD0
122477b55e8cSThomas Abraham 	 * FIMD0_SEL		[3:0]
122577b55e8cSThomas Abraham 	 * MDNIE0_SEL		[7:4]
122677b55e8cSThomas Abraham 	 * MDNIE_PWM0_SEL	[8:11]
122777b55e8cSThomas Abraham 	 * MIPI0_SEL		[12:15]
122877b55e8cSThomas Abraham 	 * set mipi0 src clock 0x6: SCLK_MPLL
122977b55e8cSThomas Abraham 	 */
123077b55e8cSThomas Abraham 	clrsetbits_le32(&clk->src_lcd0, 0xf << 12, 0x6 << 12);
123177b55e8cSThomas Abraham 
123277b55e8cSThomas Abraham 	/*
123377b55e8cSThomas Abraham 	 * CLK_SRC_MASK_LCD0
123477b55e8cSThomas Abraham 	 * FIMD0_MASK		[0]
123577b55e8cSThomas Abraham 	 * MDNIE0_MASK		[4]
123677b55e8cSThomas Abraham 	 * MDNIE_PWM0_MASK	[8]
123777b55e8cSThomas Abraham 	 * MIPI0_MASK		[12]
123877b55e8cSThomas Abraham 	 * set src mask mipi0 0x1: Unmask
123977b55e8cSThomas Abraham 	 */
124077b55e8cSThomas Abraham 	setbits_le32(&clk->src_mask_lcd0, 0x1 << 12);
124177b55e8cSThomas Abraham 
124277b55e8cSThomas Abraham 	/*
124377b55e8cSThomas Abraham 	 * CLK_GATE_IP_LCD0
124477b55e8cSThomas Abraham 	 * CLK_FIMD0		[0]
124577b55e8cSThomas Abraham 	 * CLK_MIE0		[1]
124677b55e8cSThomas Abraham 	 * CLK_MDNIE0		[2]
124777b55e8cSThomas Abraham 	 * CLK_DSIM0		[3]
124877b55e8cSThomas Abraham 	 * CLK_SMMUFIMD0	[4]
124977b55e8cSThomas Abraham 	 * CLK_PPMULCD0		[5]
125077b55e8cSThomas Abraham 	 * Gating all clocks for MIPI0
125177b55e8cSThomas Abraham 	 */
125277b55e8cSThomas Abraham 	setbits_le32(&clk->gate_ip_lcd0, 1 << 3);
125377b55e8cSThomas Abraham 
125477b55e8cSThomas Abraham 	/*
125577b55e8cSThomas Abraham 	 * CLK_DIV_LCD0
125677b55e8cSThomas Abraham 	 * FIMD0_RATIO		[3:0]
125777b55e8cSThomas Abraham 	 * MDNIE0_RATIO		[7:4]
125877b55e8cSThomas Abraham 	 * MDNIE_PWM0_RATIO	[11:8]
125977b55e8cSThomas Abraham 	 * MDNIE_PWM_PRE_RATIO	[15:12]
126077b55e8cSThomas Abraham 	 * MIPI0_RATIO		[19:16]
126177b55e8cSThomas Abraham 	 * MIPI0_PRE_RATIO	[23:20]
126277b55e8cSThomas Abraham 	 * set mipi ratio
126377b55e8cSThomas Abraham 	 */
126477b55e8cSThomas Abraham 	clrsetbits_le32(&clk->div_lcd0, 0xf << 16, 0x1 << 16);
126577b55e8cSThomas Abraham }
126677b55e8cSThomas Abraham 
exynos5_set_epll_clk(unsigned long rate)126777b55e8cSThomas Abraham int exynos5_set_epll_clk(unsigned long rate)
126877b55e8cSThomas Abraham {
126977b55e8cSThomas Abraham 	unsigned int epll_con, epll_con_k;
127077b55e8cSThomas Abraham 	unsigned int i;
127177b55e8cSThomas Abraham 	unsigned int lockcnt;
127277b55e8cSThomas Abraham 	unsigned int start;
127377b55e8cSThomas Abraham 	struct exynos5_clock *clk =
127477b55e8cSThomas Abraham 		(struct exynos5_clock *)samsung_get_base_clock();
127577b55e8cSThomas Abraham 
127677b55e8cSThomas Abraham 	epll_con = readl(&clk->epll_con0);
127777b55e8cSThomas Abraham 	epll_con &= ~((EPLL_CON0_LOCK_DET_EN_MASK <<
127877b55e8cSThomas Abraham 			EPLL_CON0_LOCK_DET_EN_SHIFT) |
127977b55e8cSThomas Abraham 		EPLL_CON0_MDIV_MASK << EPLL_CON0_MDIV_SHIFT |
128077b55e8cSThomas Abraham 		EPLL_CON0_PDIV_MASK << EPLL_CON0_PDIV_SHIFT |
128177b55e8cSThomas Abraham 		EPLL_CON0_SDIV_MASK << EPLL_CON0_SDIV_SHIFT);
128277b55e8cSThomas Abraham 
128377b55e8cSThomas Abraham 	for (i = 0; i < ARRAY_SIZE(exynos5_epll_div); i++) {
128477b55e8cSThomas Abraham 		if (exynos5_epll_div[i].freq_out == rate)
128577b55e8cSThomas Abraham 			break;
128677b55e8cSThomas Abraham 	}
128777b55e8cSThomas Abraham 
128877b55e8cSThomas Abraham 	if (i == ARRAY_SIZE(exynos5_epll_div))
128977b55e8cSThomas Abraham 		return -1;
129077b55e8cSThomas Abraham 
129177b55e8cSThomas Abraham 	epll_con_k = exynos5_epll_div[i].k_dsm << 0;
129277b55e8cSThomas Abraham 	epll_con |= exynos5_epll_div[i].en_lock_det <<
129377b55e8cSThomas Abraham 				EPLL_CON0_LOCK_DET_EN_SHIFT;
129477b55e8cSThomas Abraham 	epll_con |= exynos5_epll_div[i].m_div << EPLL_CON0_MDIV_SHIFT;
129577b55e8cSThomas Abraham 	epll_con |= exynos5_epll_div[i].p_div << EPLL_CON0_PDIV_SHIFT;
129677b55e8cSThomas Abraham 	epll_con |= exynos5_epll_div[i].s_div << EPLL_CON0_SDIV_SHIFT;
129777b55e8cSThomas Abraham 
129877b55e8cSThomas Abraham 	/*
129977b55e8cSThomas Abraham 	 * Required period ( in cycles) to genarate a stable clock output.
130077b55e8cSThomas Abraham 	 * The maximum clock time can be up to 3000 * PDIV cycles of PLLs
130177b55e8cSThomas Abraham 	 * frequency input (as per spec)
130277b55e8cSThomas Abraham 	 */
130377b55e8cSThomas Abraham 	lockcnt = 3000 * exynos5_epll_div[i].p_div;
130477b55e8cSThomas Abraham 
130577b55e8cSThomas Abraham 	writel(lockcnt, &clk->epll_lock);
130677b55e8cSThomas Abraham 	writel(epll_con, &clk->epll_con0);
130777b55e8cSThomas Abraham 	writel(epll_con_k, &clk->epll_con1);
130877b55e8cSThomas Abraham 
130977b55e8cSThomas Abraham 	start = get_timer(0);
131077b55e8cSThomas Abraham 
131177b55e8cSThomas Abraham 	 while (!(readl(&clk->epll_con0) &
131277b55e8cSThomas Abraham 			(0x1 << EXYNOS5_EPLLCON0_LOCKED_SHIFT))) {
131377b55e8cSThomas Abraham 		if (get_timer(start) > TIMEOUT_EPLL_LOCK) {
131477b55e8cSThomas Abraham 			debug("%s: Timeout waiting for EPLL lock\n", __func__);
131577b55e8cSThomas Abraham 			return -1;
131677b55e8cSThomas Abraham 		}
131777b55e8cSThomas Abraham 	}
131877b55e8cSThomas Abraham 	return 0;
131977b55e8cSThomas Abraham }
132077b55e8cSThomas Abraham 
exynos5_set_i2s_clk_source(unsigned int i2s_id)132177b55e8cSThomas Abraham int exynos5_set_i2s_clk_source(unsigned int i2s_id)
132277b55e8cSThomas Abraham {
132377b55e8cSThomas Abraham 	struct exynos5_clock *clk =
132477b55e8cSThomas Abraham 		(struct exynos5_clock *)samsung_get_base_clock();
132577b55e8cSThomas Abraham 	unsigned int *audio_ass = (unsigned int *)samsung_get_base_audio_ass();
132677b55e8cSThomas Abraham 
132777b55e8cSThomas Abraham 	if (i2s_id == 0) {
132877b55e8cSThomas Abraham 		setbits_le32(&clk->src_top2, CLK_SRC_MOUT_EPLL);
132977b55e8cSThomas Abraham 		clrsetbits_le32(&clk->src_mau, AUDIO0_SEL_MASK,
133077b55e8cSThomas Abraham 				(CLK_SRC_SCLK_EPLL));
133177b55e8cSThomas Abraham 		setbits_le32(audio_ass, AUDIO_CLKMUX_ASS);
133277b55e8cSThomas Abraham 	} else if (i2s_id == 1) {
133377b55e8cSThomas Abraham 		clrsetbits_le32(&clk->src_peric1, AUDIO1_SEL_MASK,
133477b55e8cSThomas Abraham 				(CLK_SRC_SCLK_EPLL));
133577b55e8cSThomas Abraham 	} else {
133677b55e8cSThomas Abraham 		return -1;
133777b55e8cSThomas Abraham 	}
133877b55e8cSThomas Abraham 	return 0;
133977b55e8cSThomas Abraham }
134077b55e8cSThomas Abraham 
exynos5_set_i2s_clk_prescaler(unsigned int src_frq,unsigned int dst_frq,unsigned int i2s_id)134177b55e8cSThomas Abraham int exynos5_set_i2s_clk_prescaler(unsigned int src_frq,
134277b55e8cSThomas Abraham 				  unsigned int dst_frq,
134377b55e8cSThomas Abraham 				  unsigned int i2s_id)
134477b55e8cSThomas Abraham {
134577b55e8cSThomas Abraham 	struct exynos5_clock *clk =
134677b55e8cSThomas Abraham 		(struct exynos5_clock *)samsung_get_base_clock();
134777b55e8cSThomas Abraham 	unsigned int div;
134877b55e8cSThomas Abraham 
134977b55e8cSThomas Abraham 	if ((dst_frq == 0) || (src_frq == 0)) {
135077b55e8cSThomas Abraham 		debug("%s: Invalid requency input for prescaler\n", __func__);
135177b55e8cSThomas Abraham 		debug("src frq = %d des frq = %d ", src_frq, dst_frq);
135277b55e8cSThomas Abraham 		return -1;
135377b55e8cSThomas Abraham 	}
135477b55e8cSThomas Abraham 
135577b55e8cSThomas Abraham 	div = (src_frq / dst_frq);
135677b55e8cSThomas Abraham 	if (i2s_id == 0) {
135777b55e8cSThomas Abraham 		if (div > AUDIO_0_RATIO_MASK) {
135877b55e8cSThomas Abraham 			debug("%s: Frequency ratio is out of range\n",
135977b55e8cSThomas Abraham 			      __func__);
136077b55e8cSThomas Abraham 			debug("src frq = %d des frq = %d ", src_frq, dst_frq);
136177b55e8cSThomas Abraham 			return -1;
136277b55e8cSThomas Abraham 		}
136377b55e8cSThomas Abraham 		clrsetbits_le32(&clk->div_mau, AUDIO_0_RATIO_MASK,
136477b55e8cSThomas Abraham 				(div & AUDIO_0_RATIO_MASK));
136577b55e8cSThomas Abraham 	} else if (i2s_id == 1) {
136677b55e8cSThomas Abraham 		if (div > AUDIO_1_RATIO_MASK) {
136777b55e8cSThomas Abraham 			debug("%s: Frequency ratio is out of range\n",
136877b55e8cSThomas Abraham 			      __func__);
136977b55e8cSThomas Abraham 			debug("src frq = %d des frq = %d ", src_frq, dst_frq);
137077b55e8cSThomas Abraham 			return -1;
137177b55e8cSThomas Abraham 		}
137277b55e8cSThomas Abraham 		clrsetbits_le32(&clk->div_peric4, AUDIO_1_RATIO_MASK,
137377b55e8cSThomas Abraham 				(div & AUDIO_1_RATIO_MASK));
137477b55e8cSThomas Abraham 	} else {
137577b55e8cSThomas Abraham 		return -1;
137677b55e8cSThomas Abraham 	}
137777b55e8cSThomas Abraham 	return 0;
137877b55e8cSThomas Abraham }
137977b55e8cSThomas Abraham 
138077b55e8cSThomas Abraham /**
138177b55e8cSThomas Abraham  * Linearly searches for the most accurate main and fine stage clock scalars
138277b55e8cSThomas Abraham  * (divisors) for a specified target frequency and scalar bit sizes by checking
138377b55e8cSThomas Abraham  * all multiples of main_scalar_bits values. Will always return scalars up to or
138477b55e8cSThomas Abraham  * slower than target.
138577b55e8cSThomas Abraham  *
138677b55e8cSThomas Abraham  * @param main_scalar_bits	Number of main scalar bits, must be > 0 and < 32
138777b55e8cSThomas Abraham  * @param fine_scalar_bits	Number of fine scalar bits, must be > 0 and < 32
138877b55e8cSThomas Abraham  * @param input_freq		Clock frequency to be scaled in Hz
138977b55e8cSThomas Abraham  * @param target_freq		Desired clock frequency in Hz
139077b55e8cSThomas Abraham  * @param best_fine_scalar	Pointer to store the fine stage divisor
139177b55e8cSThomas Abraham  *
139277b55e8cSThomas Abraham  * @return best_main_scalar	Main scalar for desired frequency or -1 if none
139377b55e8cSThomas Abraham  * found
139477b55e8cSThomas Abraham  */
clock_calc_best_scalar(unsigned int main_scaler_bits,unsigned int fine_scalar_bits,unsigned int input_rate,unsigned int target_rate,unsigned int * best_fine_scalar)139577b55e8cSThomas Abraham static int clock_calc_best_scalar(unsigned int main_scaler_bits,
139677b55e8cSThomas Abraham 	unsigned int fine_scalar_bits, unsigned int input_rate,
139777b55e8cSThomas Abraham 	unsigned int target_rate, unsigned int *best_fine_scalar)
139877b55e8cSThomas Abraham {
139977b55e8cSThomas Abraham 	int i;
140077b55e8cSThomas Abraham 	int best_main_scalar = -1;
140177b55e8cSThomas Abraham 	unsigned int best_error = target_rate;
140277b55e8cSThomas Abraham 	const unsigned int cap = (1 << fine_scalar_bits) - 1;
140377b55e8cSThomas Abraham 	const unsigned int loops = 1 << main_scaler_bits;
140477b55e8cSThomas Abraham 
140577b55e8cSThomas Abraham 	debug("Input Rate is %u, Target is %u, Cap is %u\n", input_rate,
140677b55e8cSThomas Abraham 			target_rate, cap);
140777b55e8cSThomas Abraham 
140877b55e8cSThomas Abraham 	assert(best_fine_scalar != NULL);
140977b55e8cSThomas Abraham 	assert(main_scaler_bits <= fine_scalar_bits);
141077b55e8cSThomas Abraham 
141177b55e8cSThomas Abraham 	*best_fine_scalar = 1;
141277b55e8cSThomas Abraham 
141377b55e8cSThomas Abraham 	if (input_rate == 0 || target_rate == 0)
141477b55e8cSThomas Abraham 		return -1;
141577b55e8cSThomas Abraham 
141677b55e8cSThomas Abraham 	if (target_rate >= input_rate)
141777b55e8cSThomas Abraham 		return 1;
141877b55e8cSThomas Abraham 
141977b55e8cSThomas Abraham 	for (i = 1; i <= loops; i++) {
142077b55e8cSThomas Abraham 		const unsigned int effective_div =
142177b55e8cSThomas Abraham 			max(min(input_rate / i / target_rate, cap), 1U);
142277b55e8cSThomas Abraham 		const unsigned int effective_rate = input_rate / i /
142377b55e8cSThomas Abraham 							effective_div;
142477b55e8cSThomas Abraham 		const int error = target_rate - effective_rate;
142577b55e8cSThomas Abraham 
142677b55e8cSThomas Abraham 		debug("%d|effdiv:%u, effrate:%u, error:%d\n", i, effective_div,
142777b55e8cSThomas Abraham 				effective_rate, error);
142877b55e8cSThomas Abraham 
142977b55e8cSThomas Abraham 		if (error >= 0 && error <= best_error) {
143077b55e8cSThomas Abraham 			best_error = error;
143177b55e8cSThomas Abraham 			best_main_scalar = i;
143277b55e8cSThomas Abraham 			*best_fine_scalar = effective_div;
143377b55e8cSThomas Abraham 		}
143477b55e8cSThomas Abraham 	}
143577b55e8cSThomas Abraham 
143677b55e8cSThomas Abraham 	return best_main_scalar;
143777b55e8cSThomas Abraham }
143877b55e8cSThomas Abraham 
exynos5_set_spi_clk(enum periph_id periph_id,unsigned int rate)143977b55e8cSThomas Abraham static int exynos5_set_spi_clk(enum periph_id periph_id,
144077b55e8cSThomas Abraham 					unsigned int rate)
144177b55e8cSThomas Abraham {
144277b55e8cSThomas Abraham 	struct exynos5_clock *clk =
144377b55e8cSThomas Abraham 		(struct exynos5_clock *)samsung_get_base_clock();
144477b55e8cSThomas Abraham 	int main;
144577b55e8cSThomas Abraham 	unsigned int fine;
144677b55e8cSThomas Abraham 	unsigned shift, pre_shift;
144777b55e8cSThomas Abraham 	unsigned mask = 0xff;
144877b55e8cSThomas Abraham 	u32 *reg;
144977b55e8cSThomas Abraham 
145077b55e8cSThomas Abraham 	main = clock_calc_best_scalar(4, 8, 400000000, rate, &fine);
145177b55e8cSThomas Abraham 	if (main < 0) {
145277b55e8cSThomas Abraham 		debug("%s: Cannot set clock rate for periph %d",
145377b55e8cSThomas Abraham 				__func__, periph_id);
145477b55e8cSThomas Abraham 		return -1;
145577b55e8cSThomas Abraham 	}
145677b55e8cSThomas Abraham 	main = main - 1;
145777b55e8cSThomas Abraham 	fine = fine - 1;
145877b55e8cSThomas Abraham 
145977b55e8cSThomas Abraham 	switch (periph_id) {
146077b55e8cSThomas Abraham 	case PERIPH_ID_SPI0:
146177b55e8cSThomas Abraham 		reg = &clk->div_peric1;
146277b55e8cSThomas Abraham 		shift = 0;
146377b55e8cSThomas Abraham 		pre_shift = 8;
146477b55e8cSThomas Abraham 		break;
146577b55e8cSThomas Abraham 	case PERIPH_ID_SPI1:
146677b55e8cSThomas Abraham 		reg = &clk->div_peric1;
146777b55e8cSThomas Abraham 		shift = 16;
146877b55e8cSThomas Abraham 		pre_shift = 24;
146977b55e8cSThomas Abraham 		break;
147077b55e8cSThomas Abraham 	case PERIPH_ID_SPI2:
147177b55e8cSThomas Abraham 		reg = &clk->div_peric2;
147277b55e8cSThomas Abraham 		shift = 0;
147377b55e8cSThomas Abraham 		pre_shift = 8;
147477b55e8cSThomas Abraham 		break;
147577b55e8cSThomas Abraham 	case PERIPH_ID_SPI3:
147677b55e8cSThomas Abraham 		reg = &clk->sclk_div_isp;
147777b55e8cSThomas Abraham 		shift = 0;
147877b55e8cSThomas Abraham 		pre_shift = 4;
147977b55e8cSThomas Abraham 		break;
148077b55e8cSThomas Abraham 	case PERIPH_ID_SPI4:
148177b55e8cSThomas Abraham 		reg = &clk->sclk_div_isp;
148277b55e8cSThomas Abraham 		shift = 12;
148377b55e8cSThomas Abraham 		pre_shift = 16;
148477b55e8cSThomas Abraham 		break;
148577b55e8cSThomas Abraham 	default:
148677b55e8cSThomas Abraham 		debug("%s: Unsupported peripheral ID %d\n", __func__,
148777b55e8cSThomas Abraham 		      periph_id);
148877b55e8cSThomas Abraham 		return -1;
148977b55e8cSThomas Abraham 	}
149077b55e8cSThomas Abraham 	clrsetbits_le32(reg, mask << shift, (main & mask) << shift);
149177b55e8cSThomas Abraham 	clrsetbits_le32(reg, mask << pre_shift, (fine & mask) << pre_shift);
149277b55e8cSThomas Abraham 
149377b55e8cSThomas Abraham 	return 0;
149477b55e8cSThomas Abraham }
149577b55e8cSThomas Abraham 
exynos5420_set_spi_clk(enum periph_id periph_id,unsigned int rate)149677b55e8cSThomas Abraham static int exynos5420_set_spi_clk(enum periph_id periph_id,
149777b55e8cSThomas Abraham 					unsigned int rate)
149877b55e8cSThomas Abraham {
149977b55e8cSThomas Abraham 	struct exynos5420_clock *clk =
150077b55e8cSThomas Abraham 		(struct exynos5420_clock *)samsung_get_base_clock();
150177b55e8cSThomas Abraham 	int main;
150277b55e8cSThomas Abraham 	unsigned int fine;
150377b55e8cSThomas Abraham 	unsigned shift, pre_shift;
150477b55e8cSThomas Abraham 	unsigned div_mask = 0xf, pre_div_mask = 0xff;
150577b55e8cSThomas Abraham 	u32 *reg;
150677b55e8cSThomas Abraham 	u32 *pre_reg;
150777b55e8cSThomas Abraham 
150877b55e8cSThomas Abraham 	main = clock_calc_best_scalar(4, 8, 400000000, rate, &fine);
150977b55e8cSThomas Abraham 	if (main < 0) {
151077b55e8cSThomas Abraham 		debug("%s: Cannot set clock rate for periph %d",
151177b55e8cSThomas Abraham 		      __func__, periph_id);
151277b55e8cSThomas Abraham 		return -1;
151377b55e8cSThomas Abraham 	}
151477b55e8cSThomas Abraham 	main = main - 1;
151577b55e8cSThomas Abraham 	fine = fine - 1;
151677b55e8cSThomas Abraham 
151777b55e8cSThomas Abraham 	switch (periph_id) {
151877b55e8cSThomas Abraham 	case PERIPH_ID_SPI0:
151977b55e8cSThomas Abraham 		reg = &clk->div_peric1;
152077b55e8cSThomas Abraham 		shift = 20;
152177b55e8cSThomas Abraham 		pre_reg = &clk->div_peric4;
152277b55e8cSThomas Abraham 		pre_shift = 8;
152377b55e8cSThomas Abraham 		break;
152477b55e8cSThomas Abraham 	case PERIPH_ID_SPI1:
152577b55e8cSThomas Abraham 		reg = &clk->div_peric1;
152677b55e8cSThomas Abraham 		shift = 24;
152777b55e8cSThomas Abraham 		pre_reg = &clk->div_peric4;
152877b55e8cSThomas Abraham 		pre_shift = 16;
152977b55e8cSThomas Abraham 		break;
153077b55e8cSThomas Abraham 	case PERIPH_ID_SPI2:
153177b55e8cSThomas Abraham 		reg = &clk->div_peric1;
153277b55e8cSThomas Abraham 		shift = 28;
153377b55e8cSThomas Abraham 		pre_reg = &clk->div_peric4;
153477b55e8cSThomas Abraham 		pre_shift = 24;
153577b55e8cSThomas Abraham 		break;
153677b55e8cSThomas Abraham 	case PERIPH_ID_SPI3:
153777b55e8cSThomas Abraham 		reg = &clk->div_isp1;
153877b55e8cSThomas Abraham 		shift = 16;
153977b55e8cSThomas Abraham 		pre_reg = &clk->div_isp1;
154077b55e8cSThomas Abraham 		pre_shift = 0;
154177b55e8cSThomas Abraham 		break;
154277b55e8cSThomas Abraham 	case PERIPH_ID_SPI4:
154377b55e8cSThomas Abraham 		reg = &clk->div_isp1;
154477b55e8cSThomas Abraham 		shift = 20;
154577b55e8cSThomas Abraham 		pre_reg = &clk->div_isp1;
154677b55e8cSThomas Abraham 		pre_shift = 8;
154777b55e8cSThomas Abraham 		break;
154877b55e8cSThomas Abraham 	default:
154977b55e8cSThomas Abraham 		debug("%s: Unsupported peripheral ID %d\n", __func__,
155077b55e8cSThomas Abraham 		      periph_id);
155177b55e8cSThomas Abraham 		return -1;
155277b55e8cSThomas Abraham 	}
155377b55e8cSThomas Abraham 
155477b55e8cSThomas Abraham 	clrsetbits_le32(reg, div_mask << shift, (main & div_mask) << shift);
155577b55e8cSThomas Abraham 	clrsetbits_le32(pre_reg, pre_div_mask << pre_shift,
155677b55e8cSThomas Abraham 			(fine & pre_div_mask) << pre_shift);
155777b55e8cSThomas Abraham 
155877b55e8cSThomas Abraham 	return 0;
155977b55e8cSThomas Abraham }
156077b55e8cSThomas Abraham 
exynos4_get_i2c_clk(void)156177b55e8cSThomas Abraham static unsigned long exynos4_get_i2c_clk(void)
156277b55e8cSThomas Abraham {
156377b55e8cSThomas Abraham 	struct exynos4_clock *clk =
156477b55e8cSThomas Abraham 		(struct exynos4_clock *)samsung_get_base_clock();
156577b55e8cSThomas Abraham 	unsigned long sclk, aclk_100;
156677b55e8cSThomas Abraham 	unsigned int ratio;
156777b55e8cSThomas Abraham 
156877b55e8cSThomas Abraham 	sclk = get_pll_clk(APLL);
156977b55e8cSThomas Abraham 
157077b55e8cSThomas Abraham 	ratio = (readl(&clk->div_top)) >> 4;
157177b55e8cSThomas Abraham 	ratio &= 0xf;
157277b55e8cSThomas Abraham 	aclk_100 = sclk / (ratio + 1);
157377b55e8cSThomas Abraham 	return aclk_100;
157477b55e8cSThomas Abraham }
157577b55e8cSThomas Abraham 
get_pll_clk(int pllreg)157677b55e8cSThomas Abraham unsigned long get_pll_clk(int pllreg)
157777b55e8cSThomas Abraham {
157877b55e8cSThomas Abraham 	if (cpu_is_exynos5()) {
1579*d64c8adeSPrzemyslaw Marczak 		if (proid_is_exynos5420() || proid_is_exynos5422())
158077b55e8cSThomas Abraham 			return exynos542x_get_pll_clk(pllreg);
158177b55e8cSThomas Abraham 		return exynos5_get_pll_clk(pllreg);
158277b55e8cSThomas Abraham 	} else if (cpu_is_exynos4()) {
158377b55e8cSThomas Abraham 		if (proid_is_exynos4412())
158477b55e8cSThomas Abraham 			return exynos4x12_get_pll_clk(pllreg);
158577b55e8cSThomas Abraham 		return exynos4_get_pll_clk(pllreg);
158677b55e8cSThomas Abraham 	}
158777b55e8cSThomas Abraham 
158877b55e8cSThomas Abraham 	return 0;
158977b55e8cSThomas Abraham }
159077b55e8cSThomas Abraham 
get_arm_clk(void)159177b55e8cSThomas Abraham unsigned long get_arm_clk(void)
159277b55e8cSThomas Abraham {
159377b55e8cSThomas Abraham 	if (cpu_is_exynos5()) {
159477b55e8cSThomas Abraham 		return exynos5_get_arm_clk();
159577b55e8cSThomas Abraham 	} else if (cpu_is_exynos4()) {
159677b55e8cSThomas Abraham 		if (proid_is_exynos4412())
159777b55e8cSThomas Abraham 			return exynos4x12_get_arm_clk();
159877b55e8cSThomas Abraham 		return exynos4_get_arm_clk();
159977b55e8cSThomas Abraham 	}
160077b55e8cSThomas Abraham 
160177b55e8cSThomas Abraham 	return 0;
160277b55e8cSThomas Abraham }
160377b55e8cSThomas Abraham 
get_i2c_clk(void)160477b55e8cSThomas Abraham unsigned long get_i2c_clk(void)
160577b55e8cSThomas Abraham {
160677b55e8cSThomas Abraham 	if (cpu_is_exynos5())
160777b55e8cSThomas Abraham 		return clock_get_periph_rate(PERIPH_ID_I2C0);
160877b55e8cSThomas Abraham 	else if (cpu_is_exynos4())
160977b55e8cSThomas Abraham 		return exynos4_get_i2c_clk();
161077b55e8cSThomas Abraham 
161177b55e8cSThomas Abraham 	return 0;
161277b55e8cSThomas Abraham }
161377b55e8cSThomas Abraham 
get_pwm_clk(void)161477b55e8cSThomas Abraham unsigned long get_pwm_clk(void)
161577b55e8cSThomas Abraham {
161677b55e8cSThomas Abraham 	if (cpu_is_exynos5()) {
161777b55e8cSThomas Abraham 		return clock_get_periph_rate(PERIPH_ID_PWM0);
161877b55e8cSThomas Abraham 	} else if (cpu_is_exynos4()) {
161977b55e8cSThomas Abraham 		if (proid_is_exynos4412())
162077b55e8cSThomas Abraham 			return exynos4x12_get_pwm_clk();
162177b55e8cSThomas Abraham 		return exynos4_get_pwm_clk();
162277b55e8cSThomas Abraham 	}
162377b55e8cSThomas Abraham 
162477b55e8cSThomas Abraham 	return 0;
162577b55e8cSThomas Abraham }
162677b55e8cSThomas Abraham 
get_uart_clk(int dev_index)162777b55e8cSThomas Abraham unsigned long get_uart_clk(int dev_index)
162877b55e8cSThomas Abraham {
162977b55e8cSThomas Abraham 	enum periph_id id;
163077b55e8cSThomas Abraham 
163177b55e8cSThomas Abraham 	switch (dev_index) {
163277b55e8cSThomas Abraham 	case 0:
163377b55e8cSThomas Abraham 		id = PERIPH_ID_UART0;
163477b55e8cSThomas Abraham 		break;
163577b55e8cSThomas Abraham 	case 1:
163677b55e8cSThomas Abraham 		id = PERIPH_ID_UART1;
163777b55e8cSThomas Abraham 		break;
163877b55e8cSThomas Abraham 	case 2:
163977b55e8cSThomas Abraham 		id = PERIPH_ID_UART2;
164077b55e8cSThomas Abraham 		break;
164177b55e8cSThomas Abraham 	case 3:
164277b55e8cSThomas Abraham 		id = PERIPH_ID_UART3;
164377b55e8cSThomas Abraham 		break;
164477b55e8cSThomas Abraham 	default:
164577b55e8cSThomas Abraham 		debug("%s: invalid UART index %d", __func__, dev_index);
164677b55e8cSThomas Abraham 		return -1;
164777b55e8cSThomas Abraham 	}
164877b55e8cSThomas Abraham 
164977b55e8cSThomas Abraham 	if (cpu_is_exynos5()) {
165077b55e8cSThomas Abraham 		return clock_get_periph_rate(id);
165177b55e8cSThomas Abraham 	} else if (cpu_is_exynos4()) {
165277b55e8cSThomas Abraham 		if (proid_is_exynos4412())
165377b55e8cSThomas Abraham 			return exynos4x12_get_uart_clk(dev_index);
165477b55e8cSThomas Abraham 		return exynos4_get_uart_clk(dev_index);
165577b55e8cSThomas Abraham 	}
165677b55e8cSThomas Abraham 
165777b55e8cSThomas Abraham 	return 0;
165877b55e8cSThomas Abraham }
165977b55e8cSThomas Abraham 
get_mmc_clk(int dev_index)166077b55e8cSThomas Abraham unsigned long get_mmc_clk(int dev_index)
166177b55e8cSThomas Abraham {
166277b55e8cSThomas Abraham 	enum periph_id id;
166377b55e8cSThomas Abraham 
16647241df1cSPrzemyslaw Marczak 	if (cpu_is_exynos4())
16657241df1cSPrzemyslaw Marczak 		return exynos4_get_mmc_clk(dev_index);
16667241df1cSPrzemyslaw Marczak 
166777b55e8cSThomas Abraham 	switch (dev_index) {
166877b55e8cSThomas Abraham 	case 0:
166977b55e8cSThomas Abraham 		id = PERIPH_ID_SDMMC0;
167077b55e8cSThomas Abraham 		break;
167177b55e8cSThomas Abraham 	case 1:
167277b55e8cSThomas Abraham 		id = PERIPH_ID_SDMMC1;
167377b55e8cSThomas Abraham 		break;
167477b55e8cSThomas Abraham 	case 2:
167577b55e8cSThomas Abraham 		id = PERIPH_ID_SDMMC2;
167677b55e8cSThomas Abraham 		break;
167777b55e8cSThomas Abraham 	case 3:
167877b55e8cSThomas Abraham 		id = PERIPH_ID_SDMMC3;
167977b55e8cSThomas Abraham 		break;
168077b55e8cSThomas Abraham 	default:
168177b55e8cSThomas Abraham 		debug("%s: invalid MMC index %d", __func__, dev_index);
168277b55e8cSThomas Abraham 		return -1;
168377b55e8cSThomas Abraham 	}
168477b55e8cSThomas Abraham 
168577b55e8cSThomas Abraham 	return clock_get_periph_rate(id);
168677b55e8cSThomas Abraham }
168777b55e8cSThomas Abraham 
set_mmc_clk(int dev_index,unsigned int div)168877b55e8cSThomas Abraham void set_mmc_clk(int dev_index, unsigned int div)
168977b55e8cSThomas Abraham {
169077b55e8cSThomas Abraham 	/* If want to set correct value, it needs to substract one from div.*/
169177b55e8cSThomas Abraham 	if (div > 0)
169277b55e8cSThomas Abraham 		div -= 1;
169377b55e8cSThomas Abraham 
169477b55e8cSThomas Abraham 	if (cpu_is_exynos5()) {
1695*d64c8adeSPrzemyslaw Marczak 		if (proid_is_exynos5420() || proid_is_exynos5422())
169677b55e8cSThomas Abraham 			exynos5420_set_mmc_clk(dev_index, div);
169777b55e8cSThomas Abraham 		else
169877b55e8cSThomas Abraham 			exynos5_set_mmc_clk(dev_index, div);
169977b55e8cSThomas Abraham 	} else if (cpu_is_exynos4()) {
170077b55e8cSThomas Abraham 		exynos4_set_mmc_clk(dev_index, div);
170177b55e8cSThomas Abraham 	}
170277b55e8cSThomas Abraham }
170377b55e8cSThomas Abraham 
get_lcd_clk(void)170477b55e8cSThomas Abraham unsigned long get_lcd_clk(void)
170577b55e8cSThomas Abraham {
170677b55e8cSThomas Abraham 	if (cpu_is_exynos4()) {
170777b55e8cSThomas Abraham 		return exynos4_get_lcd_clk();
170877b55e8cSThomas Abraham 	} else if (cpu_is_exynos5()) {
170977b55e8cSThomas Abraham 		if (proid_is_exynos5420())
171077b55e8cSThomas Abraham 			return exynos5420_get_lcd_clk();
1711*d64c8adeSPrzemyslaw Marczak 		else if (proid_is_exynos5422())
171277b55e8cSThomas Abraham 			return exynos5800_get_lcd_clk();
171377b55e8cSThomas Abraham 		else
171477b55e8cSThomas Abraham 			return exynos5_get_lcd_clk();
171577b55e8cSThomas Abraham 	}
171677b55e8cSThomas Abraham 
171777b55e8cSThomas Abraham 	return 0;
171877b55e8cSThomas Abraham }
171977b55e8cSThomas Abraham 
set_lcd_clk(void)172077b55e8cSThomas Abraham void set_lcd_clk(void)
172177b55e8cSThomas Abraham {
172277b55e8cSThomas Abraham 	if (cpu_is_exynos4()) {
172377b55e8cSThomas Abraham 		exynos4_set_lcd_clk();
172477b55e8cSThomas Abraham 	} else if (cpu_is_exynos5()) {
172577b55e8cSThomas Abraham 		if (proid_is_exynos5250())
172677b55e8cSThomas Abraham 			exynos5_set_lcd_clk();
172777b55e8cSThomas Abraham 		else if (proid_is_exynos5420())
172877b55e8cSThomas Abraham 			exynos5420_set_lcd_clk();
172977b55e8cSThomas Abraham 		else
173077b55e8cSThomas Abraham 			exynos5800_set_lcd_clk();
173177b55e8cSThomas Abraham 	}
173277b55e8cSThomas Abraham }
173377b55e8cSThomas Abraham 
set_mipi_clk(void)173477b55e8cSThomas Abraham void set_mipi_clk(void)
173577b55e8cSThomas Abraham {
173677b55e8cSThomas Abraham 	if (cpu_is_exynos4())
173777b55e8cSThomas Abraham 		exynos4_set_mipi_clk();
173877b55e8cSThomas Abraham }
173977b55e8cSThomas Abraham 
set_spi_clk(int periph_id,unsigned int rate)174077b55e8cSThomas Abraham int set_spi_clk(int periph_id, unsigned int rate)
174177b55e8cSThomas Abraham {
174277b55e8cSThomas Abraham 	if (cpu_is_exynos5()) {
1743*d64c8adeSPrzemyslaw Marczak 		if (proid_is_exynos5420() || proid_is_exynos5422())
174477b55e8cSThomas Abraham 			return exynos5420_set_spi_clk(periph_id, rate);
174577b55e8cSThomas Abraham 		return exynos5_set_spi_clk(periph_id, rate);
174677b55e8cSThomas Abraham 	}
174777b55e8cSThomas Abraham 
174877b55e8cSThomas Abraham 	return 0;
174977b55e8cSThomas Abraham }
175077b55e8cSThomas Abraham 
set_i2s_clk_prescaler(unsigned int src_frq,unsigned int dst_frq,unsigned int i2s_id)175177b55e8cSThomas Abraham int set_i2s_clk_prescaler(unsigned int src_frq, unsigned int dst_frq,
175277b55e8cSThomas Abraham 			  unsigned int i2s_id)
175377b55e8cSThomas Abraham {
175477b55e8cSThomas Abraham 	if (cpu_is_exynos5())
175577b55e8cSThomas Abraham 		return exynos5_set_i2s_clk_prescaler(src_frq, dst_frq, i2s_id);
175677b55e8cSThomas Abraham 
175777b55e8cSThomas Abraham 	return 0;
175877b55e8cSThomas Abraham }
175977b55e8cSThomas Abraham 
set_i2s_clk_source(unsigned int i2s_id)176077b55e8cSThomas Abraham int set_i2s_clk_source(unsigned int i2s_id)
176177b55e8cSThomas Abraham {
176277b55e8cSThomas Abraham 	if (cpu_is_exynos5())
176377b55e8cSThomas Abraham 		return exynos5_set_i2s_clk_source(i2s_id);
176477b55e8cSThomas Abraham 
176577b55e8cSThomas Abraham 	return 0;
176677b55e8cSThomas Abraham }
176777b55e8cSThomas Abraham 
set_epll_clk(unsigned long rate)176877b55e8cSThomas Abraham int set_epll_clk(unsigned long rate)
176977b55e8cSThomas Abraham {
177077b55e8cSThomas Abraham 	if (cpu_is_exynos5())
177177b55e8cSThomas Abraham 		return exynos5_set_epll_clk(rate);
177277b55e8cSThomas Abraham 
177377b55e8cSThomas Abraham 	return 0;
177477b55e8cSThomas Abraham }
1775