blob: d852f5ebed8feb80117acf02552a3f9fefeb6aa2 [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>
10#include <asm/blackfin.h>
11
Sonic Zhanga2979dc2012-08-16 11:56:14 +080012#ifdef PLL_CTL
13# include <asm/mach-common/bits/pll.h>
14# define pll_is_bypassed() (bfin_read_PLL_STAT() & DF)
15#else
16# include <asm/mach-common/bits/cgu.h>
17# define pll_is_bypassed() (bfin_read_CGU_STAT() & PLLBP)
18# define bfin_read_PLL_CTL() bfin_read_CGU_CTL()
19# define bfin_read_PLL_DIV() bfin_read_CGU_DIV()
20#endif
21
Mike Frysinger40599232008-10-24 22:48:47 -040022/* Get the voltage input multiplier */
Mike Frysinger40599232008-10-24 22:48:47 -040023u_long get_vco(void)
24{
Sonic Zhanga2979dc2012-08-16 11:56:14 +080025 static u_long cached_vco_pll_ctl, cached_vco;
Mike Frysinger40599232008-10-24 22:48:47 -040026
Sonic Zhanga2979dc2012-08-16 11:56:14 +080027 u_long msel, pll_ctl;
28
29 pll_ctl = bfin_read_PLL_CTL();
Mike Frysinger40599232008-10-24 22:48:47 -040030 if (pll_ctl == cached_vco_pll_ctl)
31 return cached_vco;
32 else
33 cached_vco_pll_ctl = pll_ctl;
34
Sonic Zhanga2979dc2012-08-16 11:56:14 +080035 msel = (pll_ctl & MSEL) >> MSEL_P;
Mike Frysinger40599232008-10-24 22:48:47 -040036 if (0 == msel)
Sonic Zhanga2979dc2012-08-16 11:56:14 +080037 msel = (MSEL >> MSEL_P) + 1;
Mike Frysinger40599232008-10-24 22:48:47 -040038
39 cached_vco = CONFIG_CLKIN_HZ;
Sonic Zhanga2979dc2012-08-16 11:56:14 +080040 cached_vco >>= (pll_ctl & DF);
Mike Frysinger40599232008-10-24 22:48:47 -040041 cached_vco *= msel;
42 return cached_vco;
43}
44
45/* Get the Core clock */
Mike Frysinger40599232008-10-24 22:48:47 -040046u_long get_cclk(void)
47{
Sonic Zhanga2979dc2012-08-16 11:56:14 +080048 static u_long cached_cclk_pll_div, cached_cclk;
49 u_long div, csel, ssel;
Mike Frysinger40599232008-10-24 22:48:47 -040050
Sonic Zhanga2979dc2012-08-16 11:56:14 +080051 if (pll_is_bypassed())
Mike Frysinger40599232008-10-24 22:48:47 -040052 return CONFIG_CLKIN_HZ;
53
Sonic Zhanga2979dc2012-08-16 11:56:14 +080054 div = bfin_read_PLL_DIV();
55 if (div == cached_cclk_pll_div)
Mike Frysinger40599232008-10-24 22:48:47 -040056 return cached_cclk;
57 else
Sonic Zhanga2979dc2012-08-16 11:56:14 +080058 cached_cclk_pll_div = div;
Mike Frysinger40599232008-10-24 22:48:47 -040059
Sonic Zhanga2979dc2012-08-16 11:56:14 +080060 csel = (div & CSEL) >> CSEL_P;
61#ifndef CGU_DIV
62 ssel = (div & SSEL) >> SSEL_P;
Mike Frysinger40599232008-10-24 22:48:47 -040063 if (ssel && ssel < (1 << csel)) /* SCLK > CCLK */
64 cached_cclk = get_vco() / ssel;
65 else
66 cached_cclk = get_vco() >> csel;
Sonic Zhanga2979dc2012-08-16 11:56:14 +080067#else
68 cached_cclk = get_vco() / csel;
69#endif
Mike Frysinger40599232008-10-24 22:48:47 -040070 return cached_cclk;
71}
72
73/* Get the System clock */
Sonic Zhanga2979dc2012-08-16 11:56:14 +080074#ifdef CGU_DIV
Mike Frysinger40599232008-10-24 22:48:47 -040075
Sonic Zhanga2979dc2012-08-16 11:56:14 +080076static u_long cached_sclk_pll_div, cached_sclk;
77static u_long cached_sclk0, cached_sclk1, cached_dclk;
78static u_long _get_sclk(u_long *cache)
79{
80 u_long div, ssel;
81
82 if (pll_is_bypassed())
Mike Frysinger40599232008-10-24 22:48:47 -040083 return CONFIG_CLKIN_HZ;
84
Sonic Zhanga2979dc2012-08-16 11:56:14 +080085 div = bfin_read_PLL_DIV();
86 if (div == cached_sclk_pll_div)
87 return *cache;
88 else
89 cached_sclk_pll_div = div;
90
91 ssel = (div & SYSSEL) >> SYSSEL_P;
92 cached_sclk = get_vco() / ssel;
93
94 ssel = (div & S0SEL) >> S0SEL_P;
95 cached_sclk0 = cached_sclk / ssel;
96
97 ssel = (div & S1SEL) >> S1SEL_P;
98 cached_sclk1 = cached_sclk / ssel;
99
100 ssel = (div & DSEL) >> DSEL_P;
101 cached_dclk = get_vco() / ssel;
102
103 return *cache;
104}
105
106u_long get_sclk(void)
107{
108 return _get_sclk(&cached_sclk);
109}
110
111u_long get_sclk0(void)
112{
113 return _get_sclk(&cached_sclk0);
114}
115
116u_long get_sclk1(void)
117{
118 return _get_sclk(&cached_sclk1);
119}
120
121u_long get_dclk(void)
122{
123 return _get_sclk(&cached_dclk);
124}
125#else
126
127u_long get_sclk(void)
128{
129 static u_long cached_sclk_pll_div, cached_sclk;
130 u_long div, ssel;
131
132 if (pll_is_bypassed())
133 return CONFIG_CLKIN_HZ;
134
135 div = bfin_read_PLL_DIV();
136 if (div == cached_sclk_pll_div)
Mike Frysinger40599232008-10-24 22:48:47 -0400137 return cached_sclk;
138 else
Sonic Zhanga2979dc2012-08-16 11:56:14 +0800139 cached_sclk_pll_div = div;
Mike Frysinger40599232008-10-24 22:48:47 -0400140
Sonic Zhanga2979dc2012-08-16 11:56:14 +0800141 ssel = (div & SSEL) >> SSEL_P;
Mike Frysinger40599232008-10-24 22:48:47 -0400142 cached_sclk = get_vco() / ssel;
Sonic Zhanga2979dc2012-08-16 11:56:14 +0800143
Mike Frysinger40599232008-10-24 22:48:47 -0400144 return cached_sclk;
145}
Sonic Zhanga2979dc2012-08-16 11:56:14 +0800146
147#endif