2 * cpufreq driver for the cell processor
4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
6 * Author: Christian Krafft <krafft@de.ibm.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/cpufreq.h>
24 #include <linux/timer.h>
26 #include <asm/hw_irq.h>
28 #include <asm/processor.h>
34 static DEFINE_MUTEX(cbe_switch_mutex);
37 /* the CBE supports an 8 step frequency scaling */
38 static struct cpufreq_frequency_table cbe_freqs[] = {
47 {0, CPUFREQ_TABLE_END},
50 /* to write to MIC register */
51 static u64 MIC_Slow_Fast_Timer_table[] = {
52 [0 ... 7] = 0x007fc00000000000ull,
55 /* more values for the MIC */
56 static u64 MIC_Slow_Next_Timer_table[] = {
57 0x0000240000000000ull,
58 0x0000268000000000ull,
59 0x000029C000000000ull,
60 0x00002D0000000000ull,
61 0x0000300000000000ull,
62 0x0000334000000000ull,
63 0x000039C000000000ull,
64 0x00003FC000000000ull,
68 * hardware specific functions
71 static int get_pmode(int cpu)
74 struct cbe_pmd_regs __iomem *pmd_regs;
76 pmd_regs = cbe_get_cpu_pmd_regs(cpu);
77 ret = in_be64(&pmd_regs->pmsr) & 0x07;
82 static int set_pmode(int cpu, unsigned int pmode)
84 struct cbe_pmd_regs __iomem *pmd_regs;
85 struct cbe_mic_tm_regs __iomem *mic_tm_regs;
89 local_irq_save(flags);
91 mic_tm_regs = cbe_get_cpu_mic_tm_regs(cpu);
92 pmd_regs = cbe_get_cpu_pmd_regs(cpu);
94 pr_debug("pm register is mapped at %p\n", &pmd_regs->pmcr);
95 pr_debug("mic register is mapped at %p\n", &mic_tm_regs->slow_fast_timer_0);
97 out_be64(&mic_tm_regs->slow_fast_timer_0, MIC_Slow_Fast_Timer_table[pmode]);
98 out_be64(&mic_tm_regs->slow_fast_timer_1, MIC_Slow_Fast_Timer_table[pmode]);
100 out_be64(&mic_tm_regs->slow_next_timer_0, MIC_Slow_Next_Timer_table[pmode]);
101 out_be64(&mic_tm_regs->slow_next_timer_1, MIC_Slow_Next_Timer_table[pmode]);
103 value = in_be64(&pmd_regs->pmcr);
104 /* set bits to zero */
105 value &= 0xFFFFFFFFFFFFFFF8ull;
106 /* set bits to next pmode */
109 out_be64(&pmd_regs->pmcr, value);
111 /* wait until new pmode appears in status register */
112 value = in_be64(&pmd_regs->pmsr) & 0x07;
113 while(value != pmode) {
115 value = in_be64(&pmd_regs->pmsr) & 0x07;
118 local_irq_restore(flags);
127 static int cbe_cpufreq_cpu_init (struct cpufreq_policy *policy)
131 struct device_node *cpu;
133 cpu = of_get_cpu_node(policy->cpu, NULL);
138 pr_debug("init cpufreq on CPU %d\n", policy->cpu);
140 max_freq = (u32*) get_property(cpu, "clock-frequency", NULL);
145 // we need the freq in kHz
148 pr_debug("max clock-frequency is at %u kHz\n", *max_freq);
149 pr_debug("initializing frequency table\n");
151 // initialize frequency table
152 for (i=0; cbe_freqs[i].frequency!=CPUFREQ_TABLE_END; i++) {
153 cbe_freqs[i].frequency = *max_freq / cbe_freqs[i].index;
154 pr_debug("%d: %d\n", i, cbe_freqs[i].frequency);
157 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
158 /* if DEBUG is enabled set_pmode() measures the correct latency of a transition */
159 policy->cpuinfo.transition_latency = 25000;
161 cur_pmode = get_pmode(policy->cpu);
162 pr_debug("current pmode is at %d\n",cur_pmode);
164 policy->cur = cbe_freqs[cur_pmode].frequency;
167 policy->cpus = cpu_sibling_map[policy->cpu];
170 cpufreq_frequency_table_get_attr (cbe_freqs, policy->cpu);
172 /* this ensures that policy->cpuinfo_min and policy->cpuinfo_max are set correctly */
173 return cpufreq_frequency_table_cpuinfo (policy, cbe_freqs);
176 static int cbe_cpufreq_cpu_exit(struct cpufreq_policy *policy)
178 cpufreq_frequency_table_put_attr(policy->cpu);
182 static int cbe_cpufreq_verify(struct cpufreq_policy *policy)
184 return cpufreq_frequency_table_verify(policy, cbe_freqs);
188 static int cbe_cpufreq_target(struct cpufreq_policy *policy, unsigned int target_freq,
189 unsigned int relation)
192 struct cpufreq_freqs freqs;
195 cpufreq_frequency_table_target(policy,
201 freqs.old = policy->cur;
202 freqs.new = cbe_freqs[cbe_pmode_new].frequency;
203 freqs.cpu = policy->cpu;
205 mutex_lock (&cbe_switch_mutex);
206 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
208 pr_debug("setting frequency for cpu %d to %d kHz, 1/%d of max frequency\n",
210 cbe_freqs[cbe_pmode_new].frequency,
211 cbe_freqs[cbe_pmode_new].index);
213 rc = set_pmode(policy->cpu, cbe_pmode_new);
214 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
215 mutex_unlock(&cbe_switch_mutex);
220 static struct cpufreq_driver cbe_cpufreq_driver = {
221 .verify = cbe_cpufreq_verify,
222 .target = cbe_cpufreq_target,
223 .init = cbe_cpufreq_cpu_init,
224 .exit = cbe_cpufreq_cpu_exit,
225 .name = "cbe-cpufreq",
226 .owner = THIS_MODULE,
227 .flags = CPUFREQ_CONST_LOOPS,
231 * module init and destoy
234 static int __init cbe_cpufreq_init(void)
236 return cpufreq_register_driver(&cbe_cpufreq_driver);
239 static void __exit cbe_cpufreq_exit(void)
241 cpufreq_unregister_driver(&cbe_cpufreq_driver);
244 module_init(cbe_cpufreq_init);
245 module_exit(cbe_cpufreq_exit);
247 MODULE_LICENSE("GPL");
248 MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");