Merge remote-tracking branch 'asoc/topic/88pm860x' into asoc-next
[deliverable/linux.git] / drivers / cpufreq / blackfin-cpufreq.c
CommitLineData
e6c91b64 1/*
96f1050d 2 * Blackfin core clock scaling
e6c91b64 3 *
8944b5a2 4 * Copyright 2008-2011 Analog Devices Inc.
e6c91b64 5 *
96f1050d 6 * Licensed under the GPL-2 or later.
e6c91b64
MH
7 */
8
9#include <linux/kernel.h>
6a550b99 10#include <linux/module.h>
e6c91b64
MH
11#include <linux/types.h>
12#include <linux/init.h>
96900315 13#include <linux/clk.h>
e6c91b64
MH
14#include <linux/cpufreq.h>
15#include <linux/fs.h>
7998a878 16#include <linux/delay.h>
e6c91b64
MH
17#include <asm/blackfin.h>
18#include <asm/time.h>
761ec44a 19#include <asm/dpmc.h>
e6c91b64 20
96900315 21
e6c91b64 22/* this is the table of CCLK frequencies, in Hz */
50701588 23/* .driver_data is the entry in the auxiliary dpm_state_table[] */
e6c91b64
MH
24static struct cpufreq_frequency_table bfin_freq_table[] = {
25 {
26 .frequency = CPUFREQ_TABLE_END,
50701588 27 .driver_data = 0,
e6c91b64
MH
28 },
29 {
30 .frequency = CPUFREQ_TABLE_END,
50701588 31 .driver_data = 1,
e6c91b64
MH
32 },
33 {
34 .frequency = CPUFREQ_TABLE_END,
50701588 35 .driver_data = 2,
e6c91b64
MH
36 },
37 {
38 .frequency = CPUFREQ_TABLE_END,
50701588 39 .driver_data = 0,
e6c91b64
MH
40 },
41};
42
43static struct bfin_dpm_state {
44 unsigned int csel; /* system clock divider */
45 unsigned int tscale; /* change the divider on the core timer interrupt */
46} dpm_state_table[3];
47
6c2b7072 48#if defined(CONFIG_CYCLES_CLOCKSOURCE)
1bfb4b21 49/*
8944b5a2 50 * normalized to maximum frequency offset for CYCLES,
6c2b7072
GY
51 * used in time-ts cycles clock source, but could be used
52 * somewhere also.
1bfb4b21
VM
53 */
54unsigned long long __bfin_cycles_off;
55unsigned int __bfin_cycles_mod;
6c2b7072 56#endif
1bfb4b21 57
e6c91b64 58/**************************************************************************/
6c2b7072
GY
59static void __init bfin_init_tables(unsigned long cclk, unsigned long sclk)
60{
e6c91b64 61
6c2b7072
GY
62 unsigned long csel, min_cclk;
63 int index;
64
65 /* Anomaly 273 seems to still exist on non-BF54x w/dcache turned on */
66#if ANOMALY_05000273 || ANOMALY_05000274 || \
7c7d0277
SZ
67 (!(defined(CONFIG_BF54x) || defined(CONFIG_BF60x)) \
68 && defined(CONFIG_BFIN_EXTMEM_DCACHEABLE))
6c2b7072
GY
69 min_cclk = sclk * 2;
70#else
71 min_cclk = sclk;
72#endif
96900315
SM
73
74#ifndef CONFIG_BF60x
6c2b7072 75 csel = ((bfin_read_PLL_DIV() & CSEL) >> 4);
96900315
SM
76#else
77 csel = bfin_read32(CGU0_DIV) & 0x1F;
78#endif
6c2b7072 79
810f1512 80 for (index = 0; (cclk >> index) >= min_cclk && csel <= 3 && index < 3; index++, csel++) {
6c2b7072 81 bfin_freq_table[index].frequency = cclk >> index;
96900315 82#ifndef CONFIG_BF60x
6c2b7072 83 dpm_state_table[index].csel = csel << 4; /* Shift now into PLL_DIV bitpos */
96900315
SM
84#else
85 dpm_state_table[index].csel = csel;
96900315 86#endif
810f1512 87 dpm_state_table[index].tscale = (TIME_SCALE >> index) - 1;
6c2b7072
GY
88
89 pr_debug("cpufreq: freq:%d csel:0x%x tscale:%d\n",
90 bfin_freq_table[index].frequency,
91 dpm_state_table[index].csel,
92 dpm_state_table[index].tscale);
93 }
94 return;
95}
96
97static void bfin_adjust_core_timer(void *info)
e6c91b64 98{
6c2b7072
GY
99 unsigned int tscale;
100 unsigned int index = *(unsigned int *)info;
101
102 /* we have to adjust the core timer, because it is using cclk */
103 tscale = dpm_state_table[index].tscale;
104 bfin_write_TSCALE(tscale);
105 return;
106}
e6c91b64 107
6c2b7072
GY
108static unsigned int bfin_getfreq_khz(unsigned int cpu)
109{
110 /* Both CoreA/B have the same core clock */
a10101d5 111 return get_cclk() / 1000;
e6c91b64
MH
112}
113
3a3cf0d7 114#ifdef CONFIG_BF60x
96900315
SM
115unsigned long cpu_set_cclk(int cpu, unsigned long new)
116{
117 struct clk *clk;
118 int ret;
119
120 clk = clk_get(NULL, "CCLK");
121 if (IS_ERR(clk))
122 return -ENODEV;
123
124 ret = clk_set_rate(clk, new);
125 clk_put(clk);
126 return ret;
127}
3a3cf0d7 128#endif
96900315 129
b43a7ffb 130static int bfin_target(struct cpufreq_policy *policy,
e6c91b64
MH
131 unsigned int target_freq, unsigned int relation)
132{
3a3cf0d7
BL
133#ifndef CONFIG_BF60x
134 unsigned int plldiv;
135#endif
b43a7ffb 136 unsigned int index;
5204e478 137 unsigned long cclk_hz;
e6c91b64 138 struct cpufreq_freqs freqs;
7998a878
GY
139 static unsigned long lpj_ref;
140 static unsigned int lpj_ref_freq;
96900315 141 int ret = 0;
7998a878 142
6c2b7072 143#if defined(CONFIG_CYCLES_CLOCKSOURCE)
1bfb4b21 144 cycles_t cycles;
6c2b7072 145#endif
e6c91b64 146
b43a7ffb
VK
147 if (cpufreq_frequency_table_target(policy, bfin_freq_table, target_freq,
148 relation, &index))
149 return -EINVAL;
6c2b7072 150
b43a7ffb 151 cclk_hz = bfin_freq_table[index].frequency;
6c2b7072 152
b43a7ffb
VK
153 freqs.old = bfin_getfreq_khz(0);
154 freqs.new = cclk_hz;
6c2b7072 155
b43a7ffb
VK
156 pr_debug("cpufreq: changing cclk to %lu; target = %u, oldfreq = %u\n",
157 cclk_hz, target_freq, freqs.old);
6c2b7072 158
b43a7ffb 159 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
96900315 160#ifndef CONFIG_BF60x
b43a7ffb
VK
161 plldiv = (bfin_read_PLL_DIV() & SSEL) | dpm_state_table[index].csel;
162 bfin_write_PLL_DIV(plldiv);
96900315 163#else
b43a7ffb
VK
164 ret = cpu_set_cclk(policy->cpu, freqs.new * 1000);
165 if (ret != 0) {
166 WARN_ONCE(ret, "cpufreq set freq failed %d\n", ret);
167 return ret;
168 }
96900315 169#endif
b43a7ffb 170 on_each_cpu(bfin_adjust_core_timer, &index, 1);
6c2b7072 171#if defined(CONFIG_CYCLES_CLOCKSOURCE)
b43a7ffb
VK
172 cycles = get_cycles();
173 SSYNC();
174 cycles += 10; /* ~10 cycles we lose after get_cycles() */
175 __bfin_cycles_off += (cycles << __bfin_cycles_mod) - (cycles << index);
176 __bfin_cycles_mod = index;
6c2b7072 177#endif
b43a7ffb
VK
178 if (!lpj_ref_freq) {
179 lpj_ref = loops_per_jiffy;
180 lpj_ref_freq = freqs.old;
6c2b7072 181 }
b43a7ffb
VK
182 if (freqs.new != freqs.old) {
183 loops_per_jiffy = cpufreq_scale(lpj_ref,
184 lpj_ref_freq, freqs.new);
185 }
186
187 /* TODO: just test case for cycles clock source, remove later */
188 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
e6c91b64 189
6c2b7072 190 pr_debug("cpufreq: done\n");
96900315 191 return ret;
e6c91b64
MH
192}
193
194static int bfin_verify_speed(struct cpufreq_policy *policy)
195{
196 return cpufreq_frequency_table_verify(policy, bfin_freq_table);
197}
198
96900315 199static int __bfin_cpu_init(struct cpufreq_policy *policy)
e6c91b64
MH
200{
201
6c2b7072 202 unsigned long cclk, sclk;
e6c91b64 203
a10101d5
MH
204 cclk = get_cclk() / 1000;
205 sclk = get_sclk() / 1000;
e6c91b64 206
6c2b7072
GY
207 if (policy->cpu == CPUFREQ_CPU)
208 bfin_init_tables(cclk, sclk);
e6c91b64 209
d887a1ce
MH
210 policy->cpuinfo.transition_latency = 50000; /* 50us assumed */
211
e6c91b64
MH
212 policy->cur = cclk;
213 cpufreq_frequency_table_get_attr(bfin_freq_table, policy->cpu);
214 return cpufreq_frequency_table_cpuinfo(policy, bfin_freq_table);
215}
216
217static struct freq_attr *bfin_freq_attr[] = {
218 &cpufreq_freq_attr_scaling_available_freqs,
219 NULL,
220};
221
222static struct cpufreq_driver bfin_driver = {
223 .verify = bfin_verify_speed,
224 .target = bfin_target,
a10101d5 225 .get = bfin_getfreq_khz,
e6c91b64
MH
226 .init = __bfin_cpu_init,
227 .name = "bfin cpufreq",
e6c91b64
MH
228 .attr = bfin_freq_attr,
229};
230
231static int __init bfin_cpu_init(void)
232{
233 return cpufreq_register_driver(&bfin_driver);
234}
235
236static void __exit bfin_cpu_exit(void)
237{
238 cpufreq_unregister_driver(&bfin_driver);
239}
240
241MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
242MODULE_DESCRIPTION("cpufreq driver for Blackfin");
243MODULE_LICENSE("GPL");
244
245module_init(bfin_cpu_init);
246module_exit(bfin_cpu_exit);
This page took 0.349595 seconds and 5 git commands to generate.