ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc / platforms / pmac_cpufreq.c
1 /*
2  *  arch/ppc/platforms/pmac_cpufreq.c
3  *
4  *  Copyright (C) 2002 - 2003 Benjamin Herrenschmidt <benh@kernel.crashing.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  */
11
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/errno.h>
16 #include <linux/kernel.h>
17 #include <linux/delay.h>
18 #include <linux/sched.h>
19 #include <linux/adb.h>
20 #include <linux/pmu.h>
21 #include <linux/slab.h>
22 #include <linux/cpufreq.h>
23 #include <linux/init.h>
24 #include <linux/sysdev.h>
25 #include <linux/i2c.h>
26 #include <asm/prom.h>
27 #include <asm/machdep.h>
28 #include <asm/irq.h>
29 #include <asm/hardirq.h>
30 #include <asm/pmac_feature.h>
31 #include <asm/mmu_context.h>
32 #include <asm/sections.h>
33 #include <asm/cputable.h>
34 #include <asm/time.h>
35
36 /* WARNING !!! This will cause calibrate_delay() to be called,
37  * but this is an __init function ! So you MUST go edit
38  * init/main.c to make it non-init before enabling DEBUG_FREQ
39  */
40 #undef DEBUG_FREQ
41
42 /*
43  * There is a problem with the core cpufreq code on SMP kernels,
44  * it won't recalculate the Bogomips properly
45  */
46 #ifdef CONFIG_SMP
47 #warning "WARNING, CPUFREQ not recommended on SMP kernels"
48 #endif
49
50 extern void low_choose_750fx_pll(int pll);
51 extern void low_sleep_handler(void);
52 extern void openpic_suspend(struct sys_device *sysdev, u32 state);
53 extern void openpic_resume(struct sys_device *sysdev);
54 extern void enable_kernel_altivec(void);
55 extern void enable_kernel_fp(void);
56
57 static unsigned int low_freq;
58 static unsigned int hi_freq;
59 static unsigned int cur_freq;
60
61 /* Clean that up some day ... use a func ptr or at least an enum... */
62 static int cpufreq_uses_pmu;
63 static int cpufreq_uses_gpios;
64
65 static u32 voltage_gpio;
66 static u32 frequency_gpio;
67 static u32 slew_done_gpio;
68
69 #define PMAC_CPU_LOW_SPEED      1
70 #define PMAC_CPU_HIGH_SPEED     0
71
72 /* There are only two frequency states for each processor. Values
73  * are in kHz for the time being.
74  */
75 #define CPUFREQ_HIGH                  PMAC_CPU_HIGH_SPEED
76 #define CPUFREQ_LOW                   PMAC_CPU_LOW_SPEED
77
78 static struct cpufreq_frequency_table pmac_cpu_freqs[] = {
79         {CPUFREQ_HIGH,          0},
80         {CPUFREQ_LOW,           0},
81         {0,                     CPUFREQ_TABLE_END},
82 };
83
84 static inline void wakeup_decrementer(void)
85 {
86         set_dec(tb_ticks_per_jiffy);
87         /* No currently-supported powerbook has a 601,
88          * so use get_tbl, not native
89          */
90         last_jiffy_stamp(0) = tb_last_stamp = get_tbl();
91 }
92
93 #ifdef DEBUG_FREQ
94 static inline void debug_calc_bogomips(void)
95 {
96         /* This will cause a recalc of bogomips and display the
97          * result. We backup/restore the value to avoid affecting the
98          * core cpufreq framework's own calculation.
99          */
100         extern void calibrate_delay(void);
101
102         unsigned long save_lpj = loops_per_jiffy;
103         calibrate_delay();
104         loops_per_jiffy = save_lpj;
105 }
106 #endif /* DEBUG_FREQ */
107
108 /* Switch CPU speed under 750FX CPU control
109  */
110 static int __pmac cpu_750fx_cpu_speed(int low_speed)
111 {
112 #ifdef DEBUG_FREQ
113         printk(KERN_DEBUG "HID1, before: %x\n", mfspr(SPRN_HID1));
114 #endif
115 #ifdef CONFIG_6xx
116         low_choose_750fx_pll(low_speed);
117 #endif
118 #ifdef DEBUG_FREQ
119         printk(KERN_DEBUG "HID1, after: %x\n", mfspr(SPRN_HID1));
120         debug_calc_bogomips();
121 #endif
122
123         return 0;
124 }
125
126 /* Switch CPU speed using slewing GPIOs
127  */
128 static int __pmac gpios_set_cpu_speed(unsigned int low_speed)
129 {
130         int gpio;
131
132         /* If ramping up, set voltage first */
133         if (low_speed == 0) {
134                 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, voltage_gpio, 0x05);
135                 /* Delay is way too big but it's ok, we schedule */
136                 set_current_state(TASK_UNINTERRUPTIBLE);
137                 schedule_timeout(HZ/100);
138         }
139
140         /* Set frequency */
141         pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, frequency_gpio, low_speed ? 0x04 : 0x05);
142         udelay(200);
143         do {
144                 set_current_state(TASK_UNINTERRUPTIBLE);
145                 schedule_timeout(1);
146                 gpio = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, slew_done_gpio, 0);
147         } while((gpio & 0x02) == 0);
148
149         /* If ramping down, set voltage last */
150         if (low_speed == 1) {
151                 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, voltage_gpio, 0x04);
152                 /* Delay is way too big but it's ok, we schedule */
153                 set_current_state(TASK_UNINTERRUPTIBLE);
154                 schedule_timeout(HZ/100);
155         }
156
157 #ifdef DEBUG_FREQ
158         debug_calc_bogomips();
159 #endif
160
161         return 0;
162 }
163
164 /* Switch CPU speed under PMU control
165  */
166 static int __pmac pmu_set_cpu_speed(unsigned int low_speed)
167 {
168         struct adb_request req;
169         unsigned long save_l2cr;
170         unsigned long save_l3cr;
171
172         preempt_disable();
173
174 #ifdef DEBUG_FREQ
175         printk(KERN_DEBUG "HID1, before: %x\n", mfspr(SPRN_HID1));
176 #endif
177         /* Disable all interrupt sources on openpic */
178         openpic_suspend(NULL, 1);
179
180         /* Make sure the decrementer won't interrupt us */
181         asm volatile("mtdec %0" : : "r" (0x7fffffff));
182         /* Make sure any pending DEC interrupt occuring while we did
183          * the above didn't re-enable the DEC */
184         mb();
185         asm volatile("mtdec %0" : : "r" (0x7fffffff));
186
187         /* We can now disable MSR_EE */
188         local_irq_disable();
189
190         /* Giveup the FPU & vec */
191         enable_kernel_fp();
192
193 #ifdef CONFIG_ALTIVEC
194         if (cur_cpu_spec[0]->cpu_features & CPU_FTR_ALTIVEC)
195                 enable_kernel_altivec();
196 #endif /* CONFIG_ALTIVEC */
197
198         /* Save & disable L2 and L3 caches */
199         save_l3cr = _get_L3CR();        /* (returns -1 if not available) */
200         save_l2cr = _get_L2CR();        /* (returns -1 if not available) */
201         if (save_l3cr != 0xffffffff && (save_l3cr & L3CR_L3E) != 0)
202                 _set_L3CR(save_l3cr & 0x7fffffff);
203         if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
204                 _set_L2CR(save_l2cr & 0x7fffffff);
205
206         /* Send the new speed command. My assumption is that this command
207          * will cause PLL_CFG[0..3] to be changed next time CPU goes to sleep
208          */
209         pmu_request(&req, NULL, 6, PMU_CPU_SPEED, 'W', 'O', 'O', 'F', low_speed);
210         while (!req.complete)
211                 pmu_poll();
212
213         /* Prepare the northbridge for the speed transition */
214         pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,1,1);
215
216         /* Call low level code to backup CPU state and recover from
217          * hardware reset
218          */
219         low_sleep_handler();
220
221         /* Restore the northbridge */
222         pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,1,0);
223
224         /* Restore L2 cache */
225         if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
226                 _set_L2CR(save_l2cr);
227         /* Restore L3 cache */
228         if (save_l3cr != 0xffffffff && (save_l3cr & L3CR_L3E) != 0)
229                 _set_L3CR(save_l3cr);
230
231         /* Restore userland MMU context */
232         set_context(current->active_mm->context, current->active_mm->pgd);
233
234 #ifdef DEBUG_FREQ
235         printk(KERN_DEBUG "HID1, after: %x\n", mfspr(SPRN_HID1));
236 #endif
237
238         /* Restore low level PMU operations */
239         pmu_unlock();
240
241         /* Restore decrementer */
242         wakeup_decrementer();
243
244         /* Restore interrupts */
245         openpic_resume(NULL);
246
247         /* Let interrupts flow again ... */
248         local_irq_enable();
249
250 #ifdef DEBUG_FREQ
251         debug_calc_bogomips();
252 #endif
253
254         preempt_enable();
255
256         return 0;
257 }
258
259 static int __pmac do_set_cpu_speed(int speed_mode)
260 {
261         struct cpufreq_freqs freqs;
262         int rc;
263
264         freqs.old = cur_freq;
265         freqs.new = (speed_mode == PMAC_CPU_HIGH_SPEED) ? hi_freq : low_freq;
266         freqs.cpu = smp_processor_id();
267
268         if (freqs.old == freqs.new)
269                 return 0;
270
271         cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
272         if (cpufreq_uses_pmu)
273                 rc = pmu_set_cpu_speed(speed_mode);
274         else if (cpufreq_uses_gpios)
275                 rc = gpios_set_cpu_speed(speed_mode);
276         else
277                 rc = cpu_750fx_cpu_speed(speed_mode);
278         cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
279         cur_freq = (speed_mode == PMAC_CPU_HIGH_SPEED) ? hi_freq : low_freq;
280
281         return rc;
282 }
283
284 static int __pmac pmac_cpufreq_verify(struct cpufreq_policy *policy)
285 {
286         return cpufreq_frequency_table_verify(policy, pmac_cpu_freqs);
287 }
288
289 static int __pmac pmac_cpufreq_target(  struct cpufreq_policy *policy,
290                                         unsigned int target_freq,
291                                         unsigned int relation)
292 {
293         unsigned int    newstate = 0;
294
295         if (cpufreq_frequency_table_target(policy, pmac_cpu_freqs,
296                         target_freq, relation, &newstate))
297                 return -EINVAL;
298
299         return do_set_cpu_speed(newstate);
300 }
301
302 unsigned int __pmac pmac_get_one_cpufreq(int i)
303 {
304         /* Supports only one CPU for now */
305         return (i == 0) ? cur_freq : 0;
306 }
307
308 static int __pmac pmac_cpufreq_cpu_init(struct cpufreq_policy *policy)
309 {
310         if (policy->cpu != 0)
311                 return -ENODEV;
312
313         policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
314         policy->cpuinfo.transition_latency      = CPUFREQ_ETERNAL;
315         policy->cur = cur_freq;
316
317         return cpufreq_frequency_table_cpuinfo(policy, &pmac_cpu_freqs[0]);
318 }
319
320 static u32 __pmac read_gpio(struct device_node *np)
321 {
322         u32 *reg = (u32 *)get_property(np, "reg", NULL);
323
324         if (reg == NULL)
325                 return 0;
326         /* That works for all keylargos but shall be fixed properly
327          * some day...
328          */
329         return 0x50 + (*reg);
330 }
331
332 static struct cpufreq_driver pmac_cpufreq_driver = {
333         .verify         = pmac_cpufreq_verify,
334         .target         = pmac_cpufreq_target,
335         .init           = pmac_cpufreq_cpu_init,
336         .name           = "powermac",
337         .owner          = THIS_MODULE,
338 };
339
340
341 /* Currently, we support the following machines:
342  *
343  *  - Titanium PowerBook 1Ghz (PMU based, 667Mhz & 1Ghz)
344  *  - Titanium PowerBook 800 (PMU based, 667Mhz & 800Mhz)
345  *  - Titanium PowerBook 500 (PMU based, 300Mhz & 500Mhz)
346  *  - iBook2 500 (PMU based, 400Mhz & 500Mhz)
347  *  - iBook2 700 (CPU based, 400Mhz & 700Mhz, support low voltage)
348  *  - Recent MacRISC3 machines
349  */
350 static int __init pmac_cpufreq_setup(void)
351 {
352         struct device_node      *cpunode;
353         u32                     *value;
354         int                     has_freq_ctl = 0;
355
356         if (strstr(cmd_line, "nocpufreq"))
357                 return 0;
358
359         /* Assume only one CPU */
360         cpunode = find_type_devices("cpu");
361         if (!cpunode)
362                 goto out;
363
364         /* Get current cpu clock freq */
365         value = (u32 *)get_property(cpunode, "clock-frequency", NULL);
366         if (!value)
367                 goto out;
368         cur_freq = (*value) / 1000;
369
370         /* Check for newer machines */
371         if (machine_is_compatible("PowerBook3,4") ||
372             machine_is_compatible("PowerBook3,5") ||
373             machine_is_compatible("MacRISC3")) {
374                 struct device_node *volt_gpio_np = of_find_node_by_name(NULL, "voltage-gpio");
375                 struct device_node *freq_gpio_np = of_find_node_by_name(NULL, "frequency-gpio");
376                 struct device_node *slew_done_gpio_np = of_find_node_by_name(NULL, "slewing-done");
377
378                 /*
379                  * Check to see if it's GPIO driven or PMU only
380                  *
381                  * The way we extract the GPIO address is slightly hackish, but it
382                  * works well enough for now. We need to abstract the whole GPIO
383                  * stuff sooner or later anyway
384                  */
385
386                 if (volt_gpio_np)
387                         voltage_gpio = read_gpio(volt_gpio_np);
388                 if (freq_gpio_np)
389                         frequency_gpio = read_gpio(freq_gpio_np);
390                 if (slew_done_gpio_np)
391                         slew_done_gpio = read_gpio(slew_done_gpio_np);
392
393                 /* If we use the frequency GPIOs, calculate the min/max speeds based
394                  * on the bus frequencies
395                  */
396                 if (frequency_gpio && slew_done_gpio) {
397                         int lenp, rc;
398                         u32 *freqs, *ratio;
399
400                         freqs = (u32 *)get_property(cpunode, "bus-frequencies", &lenp);
401                         lenp /= sizeof(u32);
402                         if (freqs == NULL || lenp != 2) {
403                                 printk(KERN_ERR "cpufreq: bus-frequencies incorrect or missing\n");
404                                 goto out;
405                         }
406                         ratio = (u32 *)get_property(cpunode, "processor-to-bus-ratio*2", NULL);
407                         if (ratio == NULL) {
408                                 printk(KERN_ERR "cpufreq: processor-to-bus-ratio*2 missing\n");
409                                 goto out;
410                         }
411
412                         /* Get the min/max bus frequencies */
413                         low_freq = min(freqs[0], freqs[1]);
414                         hi_freq = max(freqs[0], freqs[1]);
415
416                         /* Grrrr.. It _seems_ that the device-tree is lying on the low bus
417                          * frequency, it claims it to be around 84Mhz on some models while
418                          * it appears to be approx. 101Mhz on all. Let's hack around here...
419                          * fortunately, we don't need to be too precise
420                          */
421                         if (low_freq < 98000000)
422                                 low_freq = 101000000;
423                         
424                         /* Convert those to CPU core clocks */
425                         low_freq = (low_freq * (*ratio)) / 2000;
426                         hi_freq = (hi_freq * (*ratio)) / 2000;
427
428                         /* Now we get the frequencies, we read the GPIO to see what is out current
429                          * speed
430                          */
431                         rc = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, frequency_gpio, 0);
432                         cur_freq = (rc & 0x01) ? hi_freq : low_freq;
433
434                         has_freq_ctl = 1;
435                         cpufreq_uses_gpios = 1;
436                         goto out;
437                 }
438
439                 /* If we use the PMU, look for the min & max frequencies in the
440                  * device-tree
441                  */
442                 value = (u32 *)get_property(cpunode, "min-clock-frequency", NULL);
443                 if (!value)
444                         goto out;
445                 low_freq = (*value) / 1000;
446                 /* The PowerBook G4 12" (PowerBook6,1) has an error in the device-tree
447                  * here */
448                 if (low_freq < 100000)
449                         low_freq *= 10;
450
451                 value = (u32 *)get_property(cpunode, "max-clock-frequency", NULL);
452                 if (!value)
453                         goto out;
454                 hi_freq = (*value) / 1000;
455                 has_freq_ctl = 1;
456                 cpufreq_uses_pmu = 1;
457         }
458         /* Else check for iBook2 500 */
459         else if (machine_is_compatible("PowerBook4,1")) {
460                 /* We only know about 500Mhz model */
461                 if (cur_freq < 450000 || cur_freq > 550000)
462                         goto out;
463                 hi_freq = cur_freq;
464                 low_freq = 400000;
465                 has_freq_ctl = 1;
466                 cpufreq_uses_pmu = 1;
467         }
468         /* Else check for TiPb 500 */
469         else if (machine_is_compatible("PowerBook3,2")) {
470                 /* We only know about 500Mhz model */
471                 if (cur_freq < 450000 || cur_freq > 550000)
472                         goto out;
473                 hi_freq = cur_freq;
474                 low_freq = 300000;
475                 has_freq_ctl = 1;
476                 cpufreq_uses_pmu = 1;
477         }
478         /* Else check for 750FX */
479         else if (PVR_VER(mfspr(PVR)) == 0x7000) {
480                 if (get_property(cpunode, "dynamic-power-step", NULL) == NULL)
481                         goto out;
482                 hi_freq = cur_freq;
483                 value = (u32 *)get_property(cpunode, "reduced-clock-frequency", NULL);
484                 if (!value)
485                         goto out;
486                 low_freq = (*value) / 1000;
487                 cpufreq_uses_pmu = 0;
488                 has_freq_ctl = 1;
489         }
490 out:
491         if (!has_freq_ctl)
492                 return -ENODEV;
493
494         pmac_cpu_freqs[CPUFREQ_LOW].frequency = low_freq;
495         pmac_cpu_freqs[CPUFREQ_HIGH].frequency = hi_freq;
496
497         printk(KERN_INFO "Registering PowerMac CPU frequency driver\n");
498         printk(KERN_INFO "Low: %d Mhz, High: %d Mhz, Boot: %d Mhz, switch method: %s\n",
499                low_freq/1000, hi_freq/1000, cur_freq/1000,
500                cpufreq_uses_pmu ? "PMU" : (cpufreq_uses_gpios ? "GPIOs" : "CPU"));
501
502         return cpufreq_register_driver(&pmac_cpufreq_driver);
503 }
504
505 module_init(pmac_cpufreq_setup);
506