1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <device/mmio.h>
5 #include <console/console.h>
7 #include <soc/periph.h>
10 /* input clock of PLL: SMDK5250 has 24MHz input clock */
11 #define CONF_SYS_CLK_FREQ 24000000
13 static struct arm_clk_ratios arm_clk_ratios
[] = {
23 .pclk_dbg_ratio
= 0x1,
38 .pclk_dbg_ratio
= 0x1,
53 .pclk_dbg_ratio
= 0x1,
68 .pclk_dbg_ratio
= 0x1,
83 .pclk_dbg_ratio
= 0x1,
98 .pclk_dbg_ratio
= 0x1,
107 /* src_bit div_bit prediv_bit */
108 static struct clk_bit_info clk_bit_info
[PERIPH_ID_COUNT
] = {
118 {16, 4, 0, 8}, /* PERIPH_ID_SROMC */
140 {-1, -1, -1, -1}, /* PERIPH_ID_I2S1 */
141 {24, 1, 20, -1}, /* PERIPH_ID_SATA */
144 /* Epll Clock division values to achieve different frequency output */
145 static struct st_epll_con_val epll_div
[] = {
146 { 192000000, 0, 48, 3, 1, 0 },
147 { 180000000, 0, 45, 3, 1, 0 },
148 { 73728000, 1, 73, 3, 3, 47710 },
149 { 67737600, 1, 90, 4, 3, 20762 },
150 { 49152000, 0, 49, 3, 3, 9961 },
151 { 45158400, 0, 45, 3, 3, 10381 },
152 { 180633600, 0, 45, 3, 1, 10381 }
155 /* exynos5: return pll clock frequency */
156 unsigned long get_pll_clk(int pllreg
)
158 unsigned long r
, m
, p
, s
, k
= 0, mask
, fout
;
163 r
= read32(&exynos_clock
->apll_con0
);
166 r
= read32(&exynos_clock
->bpll_con0
);
169 r
= read32(&exynos_clock
->mpll_con0
);
172 r
= read32(&exynos_clock
->epll_con0
);
173 k
= read32(&exynos_clock
->epll_con1
);
176 r
= read32(&exynos_clock
->vpll_con0
);
177 k
= read32(&exynos_clock
->vpll_con1
);
180 printk(BIOS_DEBUG
, "Unsupported PLL (%d)\n", pllreg
);
185 * APLL_CON: MIDV [25:16]
186 * MPLL_CON: MIDV [25:16]
187 * EPLL_CON: MIDV [24:16]
188 * VPLL_CON: MIDV [24:16]
190 if (pllreg
== APLL
|| pllreg
== BPLL
|| pllreg
== MPLL
)
195 m
= (r
>> 16) & mask
;
202 freq
= CONF_SYS_CLK_FREQ
;
204 if (pllreg
== EPLL
) {
206 /* FOUT = (MDIV + K / 65536) * FIN / (PDIV * 2^SDIV) */
207 fout
= (m
+ k
/ 65536) * (freq
/ (p
* (1 << s
)));
208 } else if (pllreg
== VPLL
) {
210 /* FOUT = (MDIV + K / 1024) * FIN / (PDIV * 2^SDIV) */
211 fout
= (m
+ k
/ 1024) * (freq
/ (p
* (1 << s
)));
213 /* FOUT = MDIV * FIN / (PDIV * 2^SDIV) */
214 fout
= m
* (freq
/ (p
* (1 << s
)));
220 unsigned long clock_get_periph_rate(enum periph_id peripheral
)
222 struct clk_bit_info
*bit_info
= &clk_bit_info
[peripheral
];
223 unsigned long sclk
, sub_clk
;
224 unsigned int src
, div
, sub_div
;
226 switch (peripheral
) {
227 case PERIPH_ID_UART0
:
228 case PERIPH_ID_UART1
:
229 case PERIPH_ID_UART2
:
230 case PERIPH_ID_UART3
:
231 src
= read32(&exynos_clock
->src_peric0
);
232 div
= read32(&exynos_clock
->div_peric0
);
239 src
= read32(&exynos_clock
->src_peric0
);
240 div
= read32(&exynos_clock
->div_peric3
);
244 src
= read32(&exynos_clock
->src_peric1
);
245 div
= read32(&exynos_clock
->div_peric1
);
248 src
= read32(&exynos_clock
->src_peric1
);
249 div
= read32(&exynos_clock
->div_peric2
);
253 src
= read32(&exynos_clock
->sclk_src_isp
);
254 div
= read32(&exynos_clock
->sclk_div_isp
);
257 src
= read32(&exynos_clock
->src_fsys
);
258 div
= read32(&exynos_clock
->div_fsys0
);
260 case PERIPH_ID_SDMMC0
:
261 case PERIPH_ID_SDMMC1
:
262 case PERIPH_ID_SDMMC2
:
263 case PERIPH_ID_SDMMC3
:
264 src
= read32(&exynos_clock
->src_fsys
);
265 div
= read32(&exynos_clock
->div_fsys1
);
275 sclk
= get_pll_clk(MPLL
);
276 sub_div
= ((read32(&exynos_clock
->div_top1
)
277 >> bit_info
->div_bit
) & 0x7) + 1;
278 div
= ((read32(&exynos_clock
->div_top0
)
279 >> bit_info
->prediv_bit
) & 0x7) + 1;
280 return (sclk
/ sub_div
) / div
;
282 printk(BIOS_DEBUG
, "%s: invalid peripheral %d", __func__
, peripheral
);
286 src
= (src
>> bit_info
->src_bit
) & ((1 << bit_info
->n_src_bits
) - 1);
287 if (peripheral
== PERIPH_ID_SATA
) {
289 sclk
= get_pll_clk(BPLL
);
291 sclk
= get_pll_clk(MPLL
);
294 sclk
= get_pll_clk(MPLL
);
295 else if (src
== SRC_EPLL
)
296 sclk
= get_pll_clk(EPLL
);
297 else if (src
== SRC_VPLL
)
298 sclk
= get_pll_clk(VPLL
);
303 sub_div
= (div
>> bit_info
->div_bit
) & 0xf;
304 sub_clk
= sclk
/ (sub_div
+ 1);
306 if (peripheral
== PERIPH_ID_SDMMC0
|| peripheral
== PERIPH_ID_SDMMC2
) {
307 div
= (div
>> bit_info
->prediv_bit
) & 0xff;
308 return sub_clk
/ (div
+ 1);
314 /* exynos5: return ARM clock frequency */
315 unsigned long get_arm_clk(void)
318 unsigned long armclk
;
319 unsigned int arm_ratio
;
320 unsigned int arm2_ratio
;
322 div
= read32(&exynos_clock
->div_cpu0
);
324 /* ARM_RATIO: [2:0], ARM2_RATIO: [30:28] */
325 arm_ratio
= (div
>> 0) & 0x7;
326 arm2_ratio
= (div
>> 28) & 0x7;
328 armclk
= get_pll_clk(APLL
) / (arm_ratio
+ 1);
329 armclk
/= (arm2_ratio
+ 1);
334 struct arm_clk_ratios
*get_arm_clk_ratios(void)
336 struct arm_clk_ratios
*arm_ratio
;
337 unsigned long arm_freq
= 1700; /* FIXME: use get_arm_clk() */
340 for (i
= 0, arm_ratio
= arm_clk_ratios
; i
< ARRAY_SIZE(arm_clk_ratios
);
342 if (arm_ratio
->arm_freq_mhz
== arm_freq
)
349 /* exynos5: set the mmc clock */
350 void set_mmc_clk(int dev_index
, unsigned int div
)
357 * MMC0_PRE_RATIO [15:8], MMC1_PRE_RATIO [31:24]
359 * MMC2_PRE_RATIO [15:8], MMC3_PRE_RATIO [31:24]
362 addr
= &exynos_clock
->div_fsys1
;
364 addr
= &exynos_clock
->div_fsys2
;
369 val
&= ~(0xff << ((dev_index
<< 4) + 8));
370 val
|= (div
& 0xff) << ((dev_index
<< 4) + 8);
374 void clock_ll_set_pre_ratio(enum periph_id periph_id
, unsigned int divisor
)
377 unsigned int mask
= 0xff;
381 * For now we only handle a very small subset of peripherals here.
382 * Others will need to (and do) mangle the clock registers
383 * themselves, At some point it is hoped that this function can work
384 * from a table or calculated register offset / mask. For now this
385 * is at least better than spreading clock control code around
390 reg
= &exynos_clock
->div_peric1
;
394 reg
= &exynos_clock
->div_peric1
;
398 reg
= &exynos_clock
->div_peric2
;
402 reg
= &exynos_clock
->sclk_div_isp
;
406 reg
= &exynos_clock
->sclk_div_isp
;
410 printk(BIOS_DEBUG
, "%s: Unsupported peripheral ID %d\n", __func__
,
414 clrsetbits32(reg
, mask
<< shift
, (divisor
& mask
) << shift
);
417 void clock_ll_set_ratio(enum periph_id periph_id
, unsigned int divisor
)
420 unsigned int mask
= 0xff;
425 reg
= &exynos_clock
->div_peric1
;
429 reg
= &exynos_clock
->div_peric1
;
433 reg
= &exynos_clock
->div_peric2
;
437 reg
= &exynos_clock
->sclk_div_isp
;
441 reg
= &exynos_clock
->sclk_div_isp
;
445 printk(BIOS_DEBUG
, "%s: Unsupported peripheral ID %d\n", __func__
,
449 clrsetbits32(reg
, mask
<< shift
, (divisor
& mask
) << shift
);
453 * Linearly searches for the most accurate main and fine stage clock scalars
454 * (divisors) for a specified target frequency and scalar bit sizes by checking
455 * all multiples of main_scalar_bits values. Will always return scalars up to or
456 * slower than target.
458 * @param main_scalar_bits Number of main scalar bits, must be > 0 and < 32
459 * @param fine_scalar_bits Number of fine scalar bits, must be > 0 and < 32
460 * @param input_rate Clock frequency to be scaled in Hz
461 * @param target_rate Desired clock frequency in Hz
462 * @param best_fine_scalar Pointer to store the fine stage divisor
464 * @return best_main_scalar Main scalar for desired frequency or -1 if none
467 static int clock_calc_best_scalar(unsigned int main_scaler_bits
,
468 unsigned int fine_scalar_bits
, unsigned int input_rate
,
469 unsigned int target_rate
, unsigned int *best_fine_scalar
)
472 int best_main_scalar
= -1;
473 unsigned int best_error
= target_rate
;
474 const unsigned int cap
= (1 << fine_scalar_bits
) - 1;
475 const unsigned int loops
= 1 << main_scaler_bits
;
477 printk(BIOS_DEBUG
, "Input Rate is %u, Target is %u, Cap is %u\n", input_rate
,
480 ASSERT(best_fine_scalar
!= NULL
);
481 ASSERT(main_scaler_bits
<= fine_scalar_bits
);
483 *best_fine_scalar
= 1;
485 if (input_rate
== 0 || target_rate
== 0)
488 if (target_rate
>= input_rate
)
491 for (i
= 1; i
<= loops
; i
++) {
492 const unsigned int effective_div
= MAX(MIN(input_rate
/ i
/
493 target_rate
, cap
), 1);
494 const unsigned int effective_rate
= input_rate
/ i
/
496 const int error
= target_rate
- effective_rate
;
498 printk(BIOS_DEBUG
, "%d|effdiv:%u, effrate:%u, error:%d\n", i
, effective_div
,
499 effective_rate
, error
);
501 if (error
>= 0 && error
<= best_error
) {
503 best_main_scalar
= i
;
504 *best_fine_scalar
= effective_div
;
508 return best_main_scalar
;
511 int clock_set_rate(enum periph_id periph_id
, unsigned int rate
)
522 main_scalar
= clock_calc_best_scalar(4, 8, 400000000, rate
, &fine
);
523 if (main_scalar
< 0) {
524 printk(BIOS_DEBUG
, "%s: Cannot set clock rate for periph %d",
525 __func__
, periph_id
);
528 clock_ll_set_ratio(periph_id
, main_scalar
- 1);
529 clock_ll_set_pre_ratio(periph_id
, fine
- 1);
532 printk(BIOS_DEBUG
, "%s: Unsupported peripheral ID %d\n", __func__
,
540 int clock_set_mshci(enum periph_id peripheral
)
548 clock
= get_pll_clk(MPLL
) / 1000000;
552 * MMC0_PRE_RATIO [15:8], MMC0_RATIO [3:0]
554 * MMC2_PRE_RATIO [15:8], MMC2_RATIO [3:0]
556 switch (peripheral
) {
557 case PERIPH_ID_SDMMC0
:
558 addr
= &exynos_clock
->div_fsys1
;
560 case PERIPH_ID_SDMMC2
:
561 addr
= &exynos_clock
->div_fsys2
;
564 printk(BIOS_DEBUG
, "invalid peripheral\n");
567 tmp
= read32(addr
) & ~0xff0f;
568 for (i
= 0; i
<= 0xf; i
++) {
569 if ((clock
/ (i
+ 1)) <= 400) {
570 write32(addr
, tmp
| i
<< 0);
577 int clock_epll_set_rate(unsigned long rate
)
579 unsigned int epll_con
, epll_con_k
;
581 unsigned int lockcnt
;
584 epll_con
= read32(&exynos_clock
->epll_con0
);
585 epll_con
&= ~((EPLL_CON0_LOCK_DET_EN_MASK
<<
586 EPLL_CON0_LOCK_DET_EN_SHIFT
) |
587 EPLL_CON0_MDIV_MASK
<< EPLL_CON0_MDIV_SHIFT
|
588 EPLL_CON0_PDIV_MASK
<< EPLL_CON0_PDIV_SHIFT
|
589 EPLL_CON0_SDIV_MASK
<< EPLL_CON0_SDIV_SHIFT
);
591 for (i
= 0; i
< ARRAY_SIZE(epll_div
); i
++) {
592 if (epll_div
[i
].freq_out
== rate
)
596 if (i
== ARRAY_SIZE(epll_div
))
599 epll_con_k
= epll_div
[i
].k_dsm
<< 0;
600 epll_con
|= epll_div
[i
].en_lock_det
<< EPLL_CON0_LOCK_DET_EN_SHIFT
;
601 epll_con
|= epll_div
[i
].m_div
<< EPLL_CON0_MDIV_SHIFT
;
602 epll_con
|= epll_div
[i
].p_div
<< EPLL_CON0_PDIV_SHIFT
;
603 epll_con
|= epll_div
[i
].s_div
<< EPLL_CON0_SDIV_SHIFT
;
606 * Required period (in cycles) to generate a stable clock output.
607 * The maximum clock time can be up to 3000 * PDIV cycles of PLLs
608 * frequency input (as per spec)
610 lockcnt
= 3000 * epll_div
[i
].p_div
;
612 write32(&exynos_clock
->epll_lock
, lockcnt
);
613 write32(&exynos_clock
->epll_con0
, epll_con
);
614 write32(&exynos_clock
->epll_con1
, epll_con_k
);
616 stopwatch_init_msecs_expire(&sw
, TIMEOUT_EPLL_LOCK
);
618 while (!(read32(&exynos_clock
->epll_con0
) &
619 (0x1 << EXYNOS5_EPLLCON0_LOCKED_SHIFT
))) {
620 if (stopwatch_expired(&sw
)) {
622 "%s: Timeout waiting for EPLL lock\n",
631 void clock_select_i2s_clk_source(void)
633 clrsetbits32(&exynos_clock
->src_peric1
, AUDIO1_SEL_MASK
,
634 (CLK_SRC_SCLK_EPLL
));
637 int clock_set_i2s_clk_prescaler(unsigned int src_frq
, unsigned int dst_frq
)
641 if ((dst_frq
== 0) || (src_frq
== 0)) {
642 printk(BIOS_DEBUG
, "%s: Invalid frequency input for prescaler\n", __func__
);
643 printk(BIOS_DEBUG
, "src frq = %d des frq = %d ", src_frq
, dst_frq
);
647 div
= (src_frq
/ dst_frq
);
648 if (div
> AUDIO_1_RATIO_MASK
) {
649 printk(BIOS_DEBUG
, "%s: Frequency ratio is out of range\n", __func__
);
650 printk(BIOS_DEBUG
, "src frq = %d des frq = %d ", src_frq
, dst_frq
);
653 clrsetbits32(&exynos_clock
->div_peric4
, AUDIO_1_RATIO_MASK
,
654 (div
& AUDIO_1_RATIO_MASK
));