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