aboutsummaryrefslogtreecommitdiffstats
path: root/arch/blackfin
diff options
context:
space:
mode:
authorMichael Hennerich <michael.hennerich@analog.com>2008-04-24 16:58:29 -0400
committerBryan Wu <cooloney@kernel.org>2008-04-24 16:58:29 -0400
commite6c91b64dd6e4c3adf39483c85a936eef9465e19 (patch)
treefb21af3166c55866dd587dd30c3807e9218054a9 /arch/blackfin
parentfe44193c55e26b9b835722b5ee2519972f59c540 (diff)
[Blackfin] arch: Functional power management support: Add support for cpu frequency scaling
Signed-off-by: Michael Hennerich <michael.hennerich@analog.com> Signed-off-by: Bryan Wu <cooloney@kernel.org>
Diffstat (limited to 'arch/blackfin')
-rw-r--r--arch/blackfin/kernel/time-ts.c30
-rw-r--r--arch/blackfin/kernel/time.c19
-rw-r--r--arch/blackfin/mach-common/Makefile3
-rw-r--r--arch/blackfin/mach-common/cpufreq.c194
4 files changed, 227 insertions, 19 deletions
diff --git a/arch/blackfin/kernel/time-ts.c b/arch/blackfin/kernel/time-ts.c
index 1ce8cb1e4982..4482c47c09e5 100644
--- a/arch/blackfin/kernel/time-ts.c
+++ b/arch/blackfin/kernel/time-ts.c
@@ -16,11 +16,35 @@
16#include <linux/irq.h> 16#include <linux/irq.h>
17#include <linux/clocksource.h> 17#include <linux/clocksource.h>
18#include <linux/clockchips.h> 18#include <linux/clockchips.h>
19#include <linux/cpufreq.h>
19 20
20#include <asm/blackfin.h> 21#include <asm/blackfin.h>
22#include <asm/time.h>
21 23
22#ifdef CONFIG_CYCLES_CLOCKSOURCE 24#ifdef CONFIG_CYCLES_CLOCKSOURCE
23 25
26/* Accelerators for sched_clock()
27 * convert from cycles(64bits) => nanoseconds (64bits)
28 * basic equation:
29 * ns = cycles / (freq / ns_per_sec)
30 * ns = cycles * (ns_per_sec / freq)
31 * ns = cycles * (10^9 / (cpu_khz * 10^3))
32 * ns = cycles * (10^6 / cpu_khz)
33 *
34 * Then we use scaling math (suggested by george@mvista.com) to get:
35 * ns = cycles * (10^6 * SC / cpu_khz) / SC
36 * ns = cycles * cyc2ns_scale / SC
37 *
38 * And since SC is a constant power of two, we can convert the div
39 * into a shift.
40 *
41 * We can use khz divisor instead of mhz to keep a better precision, since
42 * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
43 * (mathieu.desnoyers@polymtl.ca)
44 *
45 * -johnstul@us.ibm.com "math is hard, lets go shopping!"
46 */
47
24static unsigned long cyc2ns_scale; 48static unsigned long cyc2ns_scale;
25#define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */ 49#define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
26 50
@@ -82,8 +106,9 @@ static void bfin_timer_set_mode(enum clock_event_mode mode,
82{ 106{
83 switch (mode) { 107 switch (mode) {
84 case CLOCK_EVT_MODE_PERIODIC: { 108 case CLOCK_EVT_MODE_PERIODIC: {
85 unsigned long tcount = ((get_cclk() / (HZ * 1)) - 1); 109 unsigned long tcount = ((get_cclk() / (HZ * TIME_SCALE)) - 1);
86 bfin_write_TCNTL(TMPWR); 110 bfin_write_TCNTL(TMPWR);
111 bfin_write_TSCALE(TIME_SCALE - 1);
87 CSYNC(); 112 CSYNC();
88 bfin_write_TPERIOD(tcount); 113 bfin_write_TPERIOD(tcount);
89 bfin_write_TCOUNT(tcount); 114 bfin_write_TCOUNT(tcount);
@@ -92,6 +117,7 @@ static void bfin_timer_set_mode(enum clock_event_mode mode,
92 break; 117 break;
93 } 118 }
94 case CLOCK_EVT_MODE_ONESHOT: 119 case CLOCK_EVT_MODE_ONESHOT:
120 bfin_write_TSCALE(0);
95 bfin_write_TCOUNT(0); 121 bfin_write_TCOUNT(0);
96 bfin_write_TCNTL(TMPWR | TMREN); 122 bfin_write_TCNTL(TMPWR | TMREN);
97 CSYNC(); 123 CSYNC();
@@ -115,7 +141,7 @@ static void __init bfin_timer_init(void)
115 /* 141 /*
116 * the TSCALE prescaler counter. 142 * the TSCALE prescaler counter.
117 */ 143 */
118 bfin_write_TSCALE(0); 144 bfin_write_TSCALE(TIME_SCALE - 1);
119 bfin_write_TPERIOD(0); 145 bfin_write_TPERIOD(0);
120 bfin_write_TCOUNT(0); 146 bfin_write_TCOUNT(0);
121 147
diff --git a/arch/blackfin/kernel/time.c b/arch/blackfin/kernel/time.c
index 715b3945e4c7..eb2352320454 100644
--- a/arch/blackfin/kernel/time.c
+++ b/arch/blackfin/kernel/time.c
@@ -6,9 +6,10 @@
6 * Created: 6 * Created:
7 * Description: This file contains the bfin-specific time handling details. 7 * Description: This file contains the bfin-specific time handling details.
8 * Most of the stuff is located in the machine specific files. 8 * Most of the stuff is located in the machine specific files.
9 * FIXME: (This file is subject for removal)
9 * 10 *
10 * Modified: 11 * Modified:
11 * Copyright 2004-2006 Analog Devices Inc. 12 * Copyright 2004-2008 Analog Devices Inc.
12 * 13 *
13 * Bugs: Enter bugs at http://blackfin.uclinux.org/ 14 * Bugs: Enter bugs at http://blackfin.uclinux.org/
14 * 15 *
@@ -35,6 +36,7 @@
35#include <linux/irq.h> 36#include <linux/irq.h>
36 37
37#include <asm/blackfin.h> 38#include <asm/blackfin.h>
39#include <asm/time.h>
38 40
39/* This is an NTP setting */ 41/* This is an NTP setting */
40#define TICK_SIZE (tick_nsec / 1000) 42#define TICK_SIZE (tick_nsec / 1000)
@@ -47,21 +49,6 @@ static struct irqaction bfin_timer_irq = {
47 .flags = IRQF_DISABLED 49 .flags = IRQF_DISABLED
48}; 50};
49 51
50/*
51 * The way that the Blackfin core timer works is:
52 * - CCLK is divided by a programmable 8-bit pre-scaler (TSCALE)
53 * - Every time TSCALE ticks, a 32bit is counted down (TCOUNT)
54 *
55 * If you take the fastest clock (1ns, or 1GHz to make the math work easier)
56 * 10ms is 10,000,000 clock ticks, which fits easy into a 32-bit counter
57 * (32 bit counter is 4,294,967,296ns or 4.2 seconds) so, we don't need
58 * to use TSCALE, and program it to zero (which is pass CCLK through).
59 * If you feel like using it, try to keep HZ * TIMESCALE to some
60 * value that divides easy (like power of 2).
61 */
62
63#define TIME_SCALE 1
64
65static void 52static void
66time_sched_init(irq_handler_t timer_routine) 53time_sched_init(irq_handler_t timer_routine)
67{ 54{
diff --git a/arch/blackfin/mach-common/Makefile b/arch/blackfin/mach-common/Makefile
index 15e33ca1ce80..393081e9b680 100644
--- a/arch/blackfin/mach-common/Makefile
+++ b/arch/blackfin/mach-common/Makefile
@@ -6,4 +6,5 @@ obj-y := \
6 cache.o cacheinit.o entry.o \ 6 cache.o cacheinit.o entry.o \
7 interrupt.o lock.o irqpanic.o arch_checks.o ints-priority.o 7 interrupt.o lock.o irqpanic.o arch_checks.o ints-priority.o
8 8
9obj-$(CONFIG_PM) += pm.o dpmc.o 9obj-$(CONFIG_PM) += pm.o dpmc.o
10obj-$(CONFIG_CPU_FREQ) += cpufreq.o
diff --git a/arch/blackfin/mach-common/cpufreq.c b/arch/blackfin/mach-common/cpufreq.c
new file mode 100644
index 000000000000..ed81e00d20e1
--- /dev/null
+++ b/arch/blackfin/mach-common/cpufreq.c
@@ -0,0 +1,194 @@
1/*
2 * File: arch/blackfin/mach-common/cpufreq.c
3 * Based on:
4 * Author:
5 *
6 * Created:
7 * Description: Blackfin core clock scaling
8 *
9 * Modified:
10 * Copyright 2004-2008 Analog Devices Inc.
11 *
12 * Bugs: Enter bugs at http://blackfin.uclinux.org/
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see the file COPYING, or write
26 * to the Free Software Foundation, Inc.,
27 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 */
29
30#include <linux/kernel.h>
31#include <linux/types.h>
32#include <linux/init.h>
33#include <linux/cpufreq.h>
34#include <linux/fs.h>
35#include <asm/blackfin.h>
36#include <asm/time.h>
37
38
39/* this is the table of CCLK frequencies, in Hz */
40/* .index is the entry in the auxillary dpm_state_table[] */
41static struct cpufreq_frequency_table bfin_freq_table[] = {
42 {
43 .frequency = CPUFREQ_TABLE_END,
44 .index = 0,
45 },
46 {
47 .frequency = CPUFREQ_TABLE_END,
48 .index = 1,
49 },
50 {
51 .frequency = CPUFREQ_TABLE_END,
52 .index = 2,
53 },
54 {
55 .frequency = CPUFREQ_TABLE_END,
56 .index = 0,
57 },
58};
59
60static struct bfin_dpm_state {
61 unsigned int csel; /* system clock divider */
62 unsigned int tscale; /* change the divider on the core timer interrupt */
63} dpm_state_table[3];
64
65/**************************************************************************/
66
67static unsigned int bfin_getfreq(unsigned int cpu)
68{
69 /* The driver only support single cpu */
70 if (cpu != 0)
71 return -1;
72
73 return get_cclk();
74}
75
76
77static int bfin_target(struct cpufreq_policy *policy,
78 unsigned int target_freq, unsigned int relation)
79{
80 unsigned int index, plldiv, tscale;
81 unsigned long flags, cclk_hz;
82 struct cpufreq_freqs freqs;
83
84 if (cpufreq_frequency_table_target(policy, bfin_freq_table,
85 target_freq, relation, &index))
86 return -EINVAL;
87
88 cclk_hz = bfin_freq_table[index].frequency;
89
90 freqs.old = bfin_getfreq(0);
91 freqs.new = cclk_hz;
92 freqs.cpu = 0;
93
94 pr_debug("cpufreq: changing cclk to %lu; target = %u, oldfreq = %u\n",
95 cclk_hz, target_freq, freqs.old);
96
97 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
98 local_irq_save(flags);
99 plldiv = (bfin_read_PLL_DIV() & SSEL) | dpm_state_table[index].csel;
100 tscale = dpm_state_table[index].tscale;
101 bfin_write_PLL_DIV(plldiv);
102 /* we have to adjust the core timer, because it is using cclk */
103 bfin_write_TSCALE(tscale);
104 SSYNC();
105 local_irq_restore(flags);
106 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
107
108 return 0;
109}
110
111static int bfin_verify_speed(struct cpufreq_policy *policy)
112{
113 return cpufreq_frequency_table_verify(policy, bfin_freq_table);
114}
115
116static int __init __bfin_cpu_init(struct cpufreq_policy *policy)
117{
118
119 unsigned long cclk, sclk, csel, min_cclk;
120 int index;
121
122#ifdef CONFIG_CYCLES_CLOCKSOURCE
123/*
124 * Clocksource CYCLES is still CONTINUOUS but not longer MONOTONIC in case we enable
125 * CPU frequency scaling, since CYCLES runs off Core Clock.
126 */
127 printk(KERN_WARNING "CPU frequency scaling not supported: Clocksource not suitable\n"
128 return -ENODEV;
129#endif
130
131 if (policy->cpu != 0)
132 return -EINVAL;
133
134 cclk = get_cclk();
135 sclk = get_sclk();
136
137#if ANOMALY_05000273
138 min_cclk = sclk * 2;
139#else
140 min_cclk = sclk;
141#endif
142 csel = ((bfin_read_PLL_DIV() & CSEL) >> 4);
143
144 for (index = 0; (cclk >> index) >= min_cclk && csel <= 3; index++, csel++) {
145 bfin_freq_table[index].frequency = cclk >> index;
146 dpm_state_table[index].csel = csel << 4; /* Shift now into PLL_DIV bitpos */
147 dpm_state_table[index].tscale = (TIME_SCALE / (1 << csel)) - 1;
148
149 pr_debug("cpufreq: freq:%d csel:%d tscale:%d\n",
150 bfin_freq_table[index].frequency,
151 dpm_state_table[index].csel,
152 dpm_state_table[index].tscale);
153 }
154
155 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
156
157 policy->cpuinfo.transition_latency = (bfin_read_PLL_LOCKCNT() / (sclk / 1000000)) * 1000;
158 /*Now ,only support one cpu */
159 policy->cur = cclk;
160 cpufreq_frequency_table_get_attr(bfin_freq_table, policy->cpu);
161 return cpufreq_frequency_table_cpuinfo(policy, bfin_freq_table);
162}
163
164static struct freq_attr *bfin_freq_attr[] = {
165 &cpufreq_freq_attr_scaling_available_freqs,
166 NULL,
167};
168
169static struct cpufreq_driver bfin_driver = {
170 .verify = bfin_verify_speed,
171 .target = bfin_target,
172 .get = bfin_getfreq,
173 .init = __bfin_cpu_init,
174 .name = "bfin cpufreq",
175 .owner = THIS_MODULE,
176 .attr = bfin_freq_attr,
177};
178
179static int __init bfin_cpu_init(void)
180{
181 return cpufreq_register_driver(&bfin_driver);
182}
183
184static void __exit bfin_cpu_exit(void)
185{
186 cpufreq_unregister_driver(&bfin_driver);
187}
188
189MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
190MODULE_DESCRIPTION("cpufreq driver for Blackfin");
191MODULE_LICENSE("GPL");
192
193module_init(bfin_cpu_init);
194module_exit(bfin_cpu_exit);