]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/powerpc/platforms/cell/cbe_cpufreq.c
Merge branch 'for-2.6.22' of master.kernel.org:/pub/scm/linux/kernel/git/arnd/cell...
[linux-2.6-omap-h63xx.git] / arch / powerpc / platforms / cell / cbe_cpufreq.c
1 /*
2  * cpufreq driver for the cell processor
3  *
4  * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
5  *
6  * Author: Christian Krafft <krafft@de.ibm.com>
7  *
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)
11  * any later version.
12  *
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.
17  *
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.
21  */
22
23 #include <linux/cpufreq.h>
24 #include <linux/timer.h>
25
26 #include <asm/hw_irq.h>
27 #include <asm/io.h>
28 #include <asm/processor.h>
29 #include <asm/prom.h>
30 #include <asm/time.h>
31 #include <asm/pmi.h>
32 #include <asm/of_platform.h>
33
34 #include "cbe_regs.h"
35
36 static DEFINE_MUTEX(cbe_switch_mutex);
37
38
39 /* the CBE supports an 8 step frequency scaling */
40 static struct cpufreq_frequency_table cbe_freqs[] = {
41         {1,     0},
42         {2,     0},
43         {3,     0},
44         {4,     0},
45         {5,     0},
46         {6,     0},
47         {8,     0},
48         {10,    0},
49         {0,     CPUFREQ_TABLE_END},
50 };
51
52 /* to write to MIC register */
53 static u64 MIC_Slow_Fast_Timer_table[] = {
54         [0 ... 7] = 0x007fc00000000000ull,
55 };
56
57 /* more values for the MIC */
58 static u64 MIC_Slow_Next_Timer_table[] = {
59         0x0000240000000000ull,
60         0x0000268000000000ull,
61         0x000029C000000000ull,
62         0x00002D0000000000ull,
63         0x0000300000000000ull,
64         0x0000334000000000ull,
65         0x000039C000000000ull,
66         0x00003FC000000000ull,
67 };
68
69 /*
70  * hardware specific functions
71  */
72
73 static struct of_device *pmi_dev;
74
75 static int set_pmode_pmi(int cpu, unsigned int pmode)
76 {
77         int ret;
78         pmi_message_t pmi_msg;
79 #ifdef DEBUG
80         u64 time;
81 #endif
82
83         pmi_msg.type = PMI_TYPE_FREQ_CHANGE;
84         pmi_msg.data1 = cbe_cpu_to_node(cpu);
85         pmi_msg.data2 = pmode;
86
87 #ifdef DEBUG
88         time = (u64) get_cycles();
89 #endif
90
91         pmi_send_message(pmi_dev, pmi_msg);
92         ret = pmi_msg.data2;
93
94         pr_debug("PMI returned slow mode %d\n", ret);
95
96 #ifdef DEBUG
97         time = (u64) get_cycles() - time; /* actual cycles (not cpu cycles!) */
98         time = 1000000000 * time / CLOCK_TICK_RATE; /* time in ns (10^-9) */
99         pr_debug("had to wait %lu ns for a transition\n", time);
100 #endif
101         return ret;
102 }
103
104
105 static int get_pmode(int cpu)
106 {
107         int ret;
108         struct cbe_pmd_regs __iomem *pmd_regs;
109
110         pmd_regs = cbe_get_cpu_pmd_regs(cpu);
111         ret = in_be64(&pmd_regs->pmsr) & 0x07;
112
113         return ret;
114 }
115
116 static int set_pmode_reg(int cpu, unsigned int pmode)
117 {
118         struct cbe_pmd_regs __iomem *pmd_regs;
119         struct cbe_mic_tm_regs __iomem *mic_tm_regs;
120         u64 flags;
121         u64 value;
122
123         local_irq_save(flags);
124
125         mic_tm_regs = cbe_get_cpu_mic_tm_regs(cpu);
126         pmd_regs = cbe_get_cpu_pmd_regs(cpu);
127
128         pr_debug("pm register is mapped at %p\n", &pmd_regs->pmcr);
129         pr_debug("mic register is mapped at %p\n", &mic_tm_regs->slow_fast_timer_0);
130
131         out_be64(&mic_tm_regs->slow_fast_timer_0, MIC_Slow_Fast_Timer_table[pmode]);
132         out_be64(&mic_tm_regs->slow_fast_timer_1, MIC_Slow_Fast_Timer_table[pmode]);
133
134         out_be64(&mic_tm_regs->slow_next_timer_0, MIC_Slow_Next_Timer_table[pmode]);
135         out_be64(&mic_tm_regs->slow_next_timer_1, MIC_Slow_Next_Timer_table[pmode]);
136
137         value = in_be64(&pmd_regs->pmcr);
138         /* set bits to zero */
139         value &= 0xFFFFFFFFFFFFFFF8ull;
140         /* set bits to next pmode */
141         value |= pmode;
142
143         out_be64(&pmd_regs->pmcr, value);
144
145         /* wait until new pmode appears in status register */
146         value = in_be64(&pmd_regs->pmsr) & 0x07;
147         while(value != pmode) {
148                 cpu_relax();
149                 value = in_be64(&pmd_regs->pmsr) & 0x07;
150         }
151
152         local_irq_restore(flags);
153
154         return 0;
155 }
156
157 static int set_pmode(int cpu, unsigned int slow_mode) {
158         if(pmi_dev)
159                 return set_pmode_pmi(cpu, slow_mode);
160         else
161                 return set_pmode_reg(cpu, slow_mode);
162 }
163
164 static void cbe_cpufreq_handle_pmi(struct of_device *dev, pmi_message_t pmi_msg)
165 {
166         struct cpufreq_policy policy;
167         u8 cpu;
168         u8 cbe_pmode_new;
169
170         BUG_ON (pmi_msg.type != PMI_TYPE_FREQ_CHANGE);
171
172         cpu = cbe_node_to_cpu(pmi_msg.data1);
173         cbe_pmode_new = pmi_msg.data2;
174
175         cpufreq_get_policy(&policy, cpu);
176
177         policy.max = min(policy.max, cbe_freqs[cbe_pmode_new].frequency);
178         policy.min = min(policy.min, policy.max);
179
180         pr_debug("cbe_handle_pmi: new policy.min=%d policy.max=%d\n", policy.min, policy.max);
181         cpufreq_set_policy(&policy);
182 }
183
184 static struct pmi_handler cbe_pmi_handler = {
185         .type                   = PMI_TYPE_FREQ_CHANGE,
186         .handle_pmi_message     = cbe_cpufreq_handle_pmi,
187 };
188
189
190 /*
191  * cpufreq functions
192  */
193
194 static int cbe_cpufreq_cpu_init (struct cpufreq_policy *policy)
195 {
196         const u32 *max_freqp;
197         u32 max_freq;
198         int i, cur_pmode;
199         struct device_node *cpu;
200
201         cpu = of_get_cpu_node(policy->cpu, NULL);
202
203         if(!cpu)
204                 return -ENODEV;
205
206         pr_debug("init cpufreq on CPU %d\n", policy->cpu);
207
208         max_freqp = of_get_property(cpu, "clock-frequency", NULL);
209
210         if (!max_freqp)
211                 return -EINVAL;
212
213         // we need the freq in kHz
214         max_freq = *max_freqp / 1000;
215
216         pr_debug("max clock-frequency is at %u kHz\n", max_freq);
217         pr_debug("initializing frequency table\n");
218
219         // initialize frequency table
220         for (i=0; cbe_freqs[i].frequency!=CPUFREQ_TABLE_END; i++) {
221                 cbe_freqs[i].frequency = max_freq / cbe_freqs[i].index;
222                 pr_debug("%d: %d\n", i, cbe_freqs[i].frequency);
223         }
224
225         policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
226         /* if DEBUG is enabled set_pmode() measures the correct latency of a transition */
227         policy->cpuinfo.transition_latency = 25000;
228
229         cur_pmode = get_pmode(policy->cpu);
230         pr_debug("current pmode is at %d\n",cur_pmode);
231
232         policy->cur = cbe_freqs[cur_pmode].frequency;
233
234 #ifdef CONFIG_SMP
235         policy->cpus = cpu_sibling_map[policy->cpu];
236 #endif
237
238         cpufreq_frequency_table_get_attr (cbe_freqs, policy->cpu);
239
240         /* this ensures that policy->cpuinfo_min and policy->cpuinfo_max are set correctly */
241         return cpufreq_frequency_table_cpuinfo (policy, cbe_freqs);
242 }
243
244 static int cbe_cpufreq_cpu_exit(struct cpufreq_policy *policy)
245 {
246         cpufreq_frequency_table_put_attr(policy->cpu);
247         return 0;
248 }
249
250 static int cbe_cpufreq_verify(struct cpufreq_policy *policy)
251 {
252         return cpufreq_frequency_table_verify(policy, cbe_freqs);
253 }
254
255
256 static int cbe_cpufreq_target(struct cpufreq_policy *policy, unsigned int target_freq,
257                             unsigned int relation)
258 {
259         int rc;
260         struct cpufreq_freqs freqs;
261         int cbe_pmode_new;
262
263         cpufreq_frequency_table_target(policy,
264                                        cbe_freqs,
265                                        target_freq,
266                                        relation,
267                                        &cbe_pmode_new);
268
269         freqs.old = policy->cur;
270         freqs.new = cbe_freqs[cbe_pmode_new].frequency;
271         freqs.cpu = policy->cpu;
272
273         mutex_lock (&cbe_switch_mutex);
274         cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
275
276         pr_debug("setting frequency for cpu %d to %d kHz, 1/%d of max frequency\n",
277                  policy->cpu,
278                  cbe_freqs[cbe_pmode_new].frequency,
279                  cbe_freqs[cbe_pmode_new].index);
280
281         rc = set_pmode(policy->cpu, cbe_pmode_new);
282         cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
283         mutex_unlock(&cbe_switch_mutex);
284
285         return rc;
286 }
287
288 static struct cpufreq_driver cbe_cpufreq_driver = {
289         .verify         = cbe_cpufreq_verify,
290         .target         = cbe_cpufreq_target,
291         .init           = cbe_cpufreq_cpu_init,
292         .exit           = cbe_cpufreq_cpu_exit,
293         .name           = "cbe-cpufreq",
294         .owner          = THIS_MODULE,
295         .flags          = CPUFREQ_CONST_LOOPS,
296 };
297
298 /*
299  * module init and destoy
300  */
301
302 static int __init cbe_cpufreq_init(void)
303 {
304         struct device_node *np;
305
306         np = of_find_node_by_type(NULL, "ibm,pmi");
307
308         pmi_dev = of_find_device_by_node(np);
309
310         if (pmi_dev)
311                 pmi_register_handler(pmi_dev, &cbe_pmi_handler);
312
313         return cpufreq_register_driver(&cbe_cpufreq_driver);
314 }
315
316 static void __exit cbe_cpufreq_exit(void)
317 {
318         if(pmi_dev)
319                 pmi_unregister_handler(pmi_dev, &cbe_pmi_handler);
320
321         cpufreq_unregister_driver(&cbe_cpufreq_driver);
322 }
323
324 module_init(cbe_cpufreq_init);
325 module_exit(cbe_cpufreq_exit);
326
327 MODULE_LICENSE("GPL");
328 MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");