blob: 7ed56a72742759a3b7f8ac8b0f7683da586e2b49 [file] [log] [blame]
Mike Frysinger9d93a622008-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 Zhangc15c4032013-02-05 19:10:34 +080010#include <asm/clock.h>
Sonic Zhanga99f03e2012-08-16 11:56:14 +080011
Mike Frysinger9d93a622008-10-24 22:48:47 -040012/* Get the voltage input multiplier */
Mike Frysinger9d93a622008-10-24 22:48:47 -040013u_long get_vco(void)
14{
Sonic Zhanga99f03e2012-08-16 11:56:14 +080015 static u_long cached_vco_pll_ctl, cached_vco;
16
17 u_long msel, pll_ctl;
Mike Frysinger9d93a622008-10-24 22:48:47 -040018
Sonic Zhanga99f03e2012-08-16 11:56:14 +080019 pll_ctl = bfin_read_PLL_CTL();
Mike Frysinger9d93a622008-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 Zhanga99f03e2012-08-16 11:56:14 +080025 msel = (pll_ctl & MSEL) >> MSEL_P;
Mike Frysinger9d93a622008-10-24 22:48:47 -040026 if (0 == msel)
Sonic Zhanga99f03e2012-08-16 11:56:14 +080027 msel = (MSEL >> MSEL_P) + 1;
Mike Frysinger9d93a622008-10-24 22:48:47 -040028
29 cached_vco = CONFIG_CLKIN_HZ;
Sonic Zhanga99f03e2012-08-16 11:56:14 +080030 cached_vco >>= (pll_ctl & DF);
Mike Frysinger9d93a622008-10-24 22:48:47 -040031 cached_vco *= msel;
32 return cached_vco;
33}
34
35/* Get the Core clock */
Mike Frysinger9d93a622008-10-24 22:48:47 -040036u_long get_cclk(void)
37{
Sonic Zhanga99f03e2012-08-16 11:56:14 +080038 static u_long cached_cclk_pll_div, cached_cclk;
Sonic Zhang70b86bc2013-12-09 12:56:27 +080039 u_long div, csel;
40#ifndef CGU_DIV
41 u_long ssel;
42#endif
Mike Frysinger9d93a622008-10-24 22:48:47 -040043
Sonic Zhanga99f03e2012-08-16 11:56:14 +080044 if (pll_is_bypassed())
Mike Frysinger9d93a622008-10-24 22:48:47 -040045 return CONFIG_CLKIN_HZ;
46
Sonic Zhanga99f03e2012-08-16 11:56:14 +080047 div = bfin_read_PLL_DIV();
48 if (div == cached_cclk_pll_div)
Mike Frysinger9d93a622008-10-24 22:48:47 -040049 return cached_cclk;
50 else
Sonic Zhanga99f03e2012-08-16 11:56:14 +080051 cached_cclk_pll_div = div;
Mike Frysinger9d93a622008-10-24 22:48:47 -040052
Sonic Zhanga99f03e2012-08-16 11:56:14 +080053 csel = (div & CSEL) >> CSEL_P;
54#ifndef CGU_DIV
55 ssel = (div & SSEL) >> SSEL_P;
Mike Frysinger9d93a622008-10-24 22:48:47 -040056 if (ssel && ssel < (1 << csel)) /* SCLK > CCLK */
57 cached_cclk = get_vco() / ssel;
58 else
59 cached_cclk = get_vco() >> csel;
Sonic Zhanga99f03e2012-08-16 11:56:14 +080060#else
61 cached_cclk = get_vco() / csel;
62#endif
Mike Frysinger9d93a622008-10-24 22:48:47 -040063 return cached_cclk;
64}
65
66/* Get the System clock */
Sonic Zhanga99f03e2012-08-16 11:56:14 +080067#ifdef CGU_DIV
68
Mike Frysinger9d93a622008-10-24 22:48:47 -040069static u_long cached_sclk_pll_div, cached_sclk;
Sonic Zhanga99f03e2012-08-16 11:56:14 +080070static u_long cached_sclk0, cached_sclk1, cached_dclk;
71static u_long _get_sclk(u_long *cache)
72{
73 u_long div, ssel;
74
75 if (pll_is_bypassed())
76 return CONFIG_CLKIN_HZ;
77
78 div = bfin_read_PLL_DIV();
79 if (div == cached_sclk_pll_div)
80 return *cache;
81 else
82 cached_sclk_pll_div = div;
83
84 ssel = (div & SYSSEL) >> SYSSEL_P;
85 cached_sclk = get_vco() / ssel;
86
87 ssel = (div & S0SEL) >> S0SEL_P;
88 cached_sclk0 = cached_sclk / ssel;
89
90 ssel = (div & S1SEL) >> S1SEL_P;
91 cached_sclk1 = cached_sclk / ssel;
92
93 ssel = (div & DSEL) >> DSEL_P;
94 cached_dclk = get_vco() / ssel;
95
96 return *cache;
97}
98
Mike Frysinger9d93a622008-10-24 22:48:47 -040099u_long get_sclk(void)
100{
Sonic Zhanga99f03e2012-08-16 11:56:14 +0800101 return _get_sclk(&cached_sclk);
102}
103
104u_long get_sclk0(void)
105{
106 return _get_sclk(&cached_sclk0);
107}
108
109u_long get_sclk1(void)
110{
111 return _get_sclk(&cached_sclk1);
112}
113
114u_long get_dclk(void)
115{
116 return _get_sclk(&cached_dclk);
117}
118#else
119
120u_long get_sclk(void)
121{
122 static u_long cached_sclk_pll_div, cached_sclk;
123 u_long div, ssel;
Mike Frysinger9d93a622008-10-24 22:48:47 -0400124
Sonic Zhanga99f03e2012-08-16 11:56:14 +0800125 if (pll_is_bypassed())
Mike Frysinger9d93a622008-10-24 22:48:47 -0400126 return CONFIG_CLKIN_HZ;
127
Sonic Zhanga99f03e2012-08-16 11:56:14 +0800128 div = bfin_read_PLL_DIV();
129 if (div == cached_sclk_pll_div)
Mike Frysinger9d93a622008-10-24 22:48:47 -0400130 return cached_sclk;
131 else
Sonic Zhanga99f03e2012-08-16 11:56:14 +0800132 cached_sclk_pll_div = div;
Mike Frysinger9d93a622008-10-24 22:48:47 -0400133
Sonic Zhanga99f03e2012-08-16 11:56:14 +0800134 ssel = (div & SSEL) >> SSEL_P;
Mike Frysinger9d93a622008-10-24 22:48:47 -0400135 cached_sclk = get_vco() / ssel;
Sonic Zhanga99f03e2012-08-16 11:56:14 +0800136
Mike Frysinger9d93a622008-10-24 22:48:47 -0400137 return cached_sclk;
138}
Sonic Zhanga99f03e2012-08-16 11:56:14 +0800139
140#endif