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