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