blob: 97795e11ac993498d60848ecda562df08d625872 [file] [log] [blame]
Mike Frysinger40599232008-10-24 22:48:47 -04001/*
2 * clocks.c - figure out sclk/cclk/vco and such
3 *
4 * Copyright (c) 2005-2008 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
9#include <common.h>
Sonic Zhang79f2b392013-02-05 19:10:34 +080010#include <asm/clock.h>
Sonic Zhanga2979dc2012-08-16 11:56:14 +080011
Mike Frysinger40599232008-10-24 22:48:47 -040012/* Get the voltage input multiplier */
Mike Frysinger40599232008-10-24 22:48:47 -040013u_long get_vco(void)
14{
Sonic Zhanga2979dc2012-08-16 11:56:14 +080015 static u_long cached_vco_pll_ctl, cached_vco;
Mike Frysinger40599232008-10-24 22:48:47 -040016
Sonic Zhanga2979dc2012-08-16 11:56:14 +080017 u_long msel, pll_ctl;
18
19 pll_ctl = bfin_read_PLL_CTL();
Mike Frysinger40599232008-10-24 22:48:47 -040020 if (pll_ctl == cached_vco_pll_ctl)
21 return cached_vco;
22 else
23 cached_vco_pll_ctl = pll_ctl;
24
Sonic Zhanga2979dc2012-08-16 11:56:14 +080025 msel = (pll_ctl & MSEL) >> MSEL_P;
Mike Frysinger40599232008-10-24 22:48:47 -040026 if (0 == msel)
Sonic Zhanga2979dc2012-08-16 11:56:14 +080027 msel = (MSEL >> MSEL_P) + 1;
Mike Frysinger40599232008-10-24 22:48:47 -040028
29 cached_vco = CONFIG_CLKIN_HZ;
Sonic Zhanga2979dc2012-08-16 11:56:14 +080030 cached_vco >>= (pll_ctl & DF);
Mike Frysinger40599232008-10-24 22:48:47 -040031 cached_vco *= msel;
32 return cached_vco;
33}
34
35/* Get the Core clock */
Mike Frysinger40599232008-10-24 22:48:47 -040036u_long get_cclk(void)
37{
Sonic Zhanga2979dc2012-08-16 11:56:14 +080038 static u_long cached_cclk_pll_div, cached_cclk;
39 u_long div, csel, ssel;
Mike Frysinger40599232008-10-24 22:48:47 -040040
Sonic Zhanga2979dc2012-08-16 11:56:14 +080041 if (pll_is_bypassed())
Mike Frysinger40599232008-10-24 22:48:47 -040042 return CONFIG_CLKIN_HZ;
43
Sonic Zhanga2979dc2012-08-16 11:56:14 +080044 div = bfin_read_PLL_DIV();
45 if (div == cached_cclk_pll_div)
Mike Frysinger40599232008-10-24 22:48:47 -040046 return cached_cclk;
47 else
Sonic Zhanga2979dc2012-08-16 11:56:14 +080048 cached_cclk_pll_div = div;
Mike Frysinger40599232008-10-24 22:48:47 -040049
Sonic Zhanga2979dc2012-08-16 11:56:14 +080050 csel = (div & CSEL) >> CSEL_P;
51#ifndef CGU_DIV
52 ssel = (div & SSEL) >> SSEL_P;
Mike Frysinger40599232008-10-24 22:48:47 -040053 if (ssel && ssel < (1 << csel)) /* SCLK > CCLK */
54 cached_cclk = get_vco() / ssel;
55 else
56 cached_cclk = get_vco() >> csel;
Sonic Zhanga2979dc2012-08-16 11:56:14 +080057#else
58 cached_cclk = get_vco() / csel;
59#endif
Mike Frysinger40599232008-10-24 22:48:47 -040060 return cached_cclk;
61}
62
63/* Get the System clock */
Sonic Zhanga2979dc2012-08-16 11:56:14 +080064#ifdef CGU_DIV
Mike Frysinger40599232008-10-24 22:48:47 -040065
Sonic Zhanga2979dc2012-08-16 11:56:14 +080066static u_long cached_sclk_pll_div, cached_sclk;
67static u_long cached_sclk0, cached_sclk1, cached_dclk;
68static u_long _get_sclk(u_long *cache)
69{
70 u_long div, ssel;
71
72 if (pll_is_bypassed())
Mike Frysinger40599232008-10-24 22:48:47 -040073 return CONFIG_CLKIN_HZ;
74
Sonic Zhanga2979dc2012-08-16 11:56:14 +080075 div = bfin_read_PLL_DIV();
76 if (div == cached_sclk_pll_div)
77 return *cache;
78 else
79 cached_sclk_pll_div = div;
80
81 ssel = (div & SYSSEL) >> SYSSEL_P;
82 cached_sclk = get_vco() / ssel;
83
84 ssel = (div & S0SEL) >> S0SEL_P;
85 cached_sclk0 = cached_sclk / ssel;
86
87 ssel = (div & S1SEL) >> S1SEL_P;
88 cached_sclk1 = cached_sclk / ssel;
89
90 ssel = (div & DSEL) >> DSEL_P;
91 cached_dclk = get_vco() / ssel;
92
93 return *cache;
94}
95
96u_long get_sclk(void)
97{
98 return _get_sclk(&cached_sclk);
99}
100
101u_long get_sclk0(void)
102{
103 return _get_sclk(&cached_sclk0);
104}
105
106u_long get_sclk1(void)
107{
108 return _get_sclk(&cached_sclk1);
109}
110
111u_long get_dclk(void)
112{
113 return _get_sclk(&cached_dclk);
114}
115#else
116
117u_long get_sclk(void)
118{
119 static u_long cached_sclk_pll_div, cached_sclk;
120 u_long div, ssel;
121
122 if (pll_is_bypassed())
123 return CONFIG_CLKIN_HZ;
124
125 div = bfin_read_PLL_DIV();
126 if (div == cached_sclk_pll_div)
Mike Frysinger40599232008-10-24 22:48:47 -0400127 return cached_sclk;
128 else
Sonic Zhanga2979dc2012-08-16 11:56:14 +0800129 cached_sclk_pll_div = div;
Mike Frysinger40599232008-10-24 22:48:47 -0400130
Sonic Zhanga2979dc2012-08-16 11:56:14 +0800131 ssel = (div & SSEL) >> SSEL_P;
Mike Frysinger40599232008-10-24 22:48:47 -0400132 cached_sclk = get_vco() / ssel;
Sonic Zhanga2979dc2012-08-16 11:56:14 +0800133
Mike Frysinger40599232008-10-24 22:48:47 -0400134 return cached_sclk;
135}
Sonic Zhanga2979dc2012-08-16 11:56:14 +0800136
137#endif