2 * arch/ppc/platforms/pmac_cpufreq.c
4 * Copyright (C) 2002 - 2004 Benjamin Herrenschmidt <benh@kernel.crashing.org>
5 * Copyright (C) 2004 John Steele Scott <toojays@toojays.net>
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.
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>
28 #include <asm/machdep.h>
30 #include <asm/hardirq.h>
31 #include <asm/pmac_feature.h>
32 #include <asm/mmu_context.h>
33 #include <asm/sections.h>
34 #include <asm/cputable.h>
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
44 * There is a problem with the core cpufreq code on SMP kernels,
45 * it won't recalculate the Bogomips properly
48 #warning "WARNING, CPUFREQ not recommended on SMP kernels"
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);
60 * Currently, PowerMac cpufreq supports only high & low frequencies
61 * that are set by the firmware
63 static unsigned int low_freq;
64 static unsigned int hi_freq;
65 static unsigned int cur_freq;
68 * Different models uses different mecanisms to switch the frequency
70 static int (*set_speed_proc)(int low_speed);
73 * Some definitions used by the various speedprocs
75 static u32 voltage_gpio;
76 static u32 frequency_gpio;
77 static u32 slew_done_gpio;
80 #define PMAC_CPU_LOW_SPEED 1
81 #define PMAC_CPU_HIGH_SPEED 0
83 /* There are only two frequency states for each processor. Values
84 * are in kHz for the time being.
86 #define CPUFREQ_HIGH PMAC_CPU_HIGH_SPEED
87 #define CPUFREQ_LOW PMAC_CPU_LOW_SPEED
89 static struct cpufreq_frequency_table pmac_cpu_freqs[] = {
92 {0, CPUFREQ_TABLE_END},
95 static inline void wakeup_decrementer(void)
97 set_dec(tb_ticks_per_jiffy);
98 /* No currently-supported powerbook has a 601,
99 * so use get_tbl, not native
101 last_jiffy_stamp(0) = tb_last_stamp = get_tbl();
105 static inline void debug_calc_bogomips(void)
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.
111 extern void calibrate_delay(void);
113 unsigned long save_lpj = loops_per_jiffy;
115 loops_per_jiffy = save_lpj;
117 #endif /* DEBUG_FREQ */
119 /* Switch CPU speed under 750FX CPU control
121 static int __pmac cpu_750fx_cpu_speed(int low_speed)
124 printk(KERN_DEBUG "HID1, before: %x\n", mfspr(SPRN_HID1));
127 low_choose_750fx_pll(low_speed);
130 printk(KERN_DEBUG "HID1, after: %x\n", mfspr(SPRN_HID1));
131 debug_calc_bogomips();
137 /* Switch CPU speed using DFS */
138 static int __pmac dfs_set_cpu_speed(int low_speed)
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);
146 /* ramping down, enable aack delay first */
147 pmac_call_feature(PMAC_FTR_AACK_DELAY_ENABLE, NULL, 1, 0);
151 low_choose_7447a_dfs(low_speed);
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);
159 /* ramping up, disable aack delay last */
160 pmac_call_feature(PMAC_FTR_AACK_DELAY_ENABLE, NULL, 0, 0);
167 /* Switch CPU speed using slewing GPIOs
169 static int __pmac gpios_set_cpu_speed(int low_speed)
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);
182 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, frequency_gpio,
183 low_speed ? 0x04 : 0x05);
186 set_current_state(TASK_UNINTERRUPTIBLE);
188 gpio = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, slew_done_gpio, 0);
189 } while((gpio & 0x02) == 0);
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);
200 debug_calc_bogomips();
206 /* Switch CPU speed under PMU control
208 static int __pmac pmu_set_cpu_speed(int low_speed)
210 struct adb_request req;
211 unsigned long save_l2cr;
212 unsigned long save_l3cr;
217 printk(KERN_DEBUG "HID1, before: %x\n", mfspr(SPRN_HID1));
219 /* Disable all interrupt sources on openpic */
220 openpic_suspend(NULL, 1);
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 */
227 asm volatile("mtdec %0" : : "r" (0x7fffffff));
229 /* We can now disable MSR_EE */
232 /* Giveup the FPU & vec */
235 #ifdef CONFIG_ALTIVEC
236 if (cur_cpu_spec[0]->cpu_features & CPU_FTR_ALTIVEC)
237 enable_kernel_altivec();
238 #endif /* CONFIG_ALTIVEC */
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);
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
251 pmu_request(&req, NULL, 6, PMU_CPU_SPEED, 'W', 'O', 'O', 'F', low_speed);
252 while (!req.complete)
255 /* Prepare the northbridge for the speed transition */
256 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,1,1);
258 /* Call low level code to backup CPU state and recover from
263 /* Restore the northbridge */
264 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,1,0);
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);
273 /* Restore userland MMU context */
274 set_context(current->active_mm->context, current->active_mm->pgd);
277 printk(KERN_DEBUG "HID1, after: %x\n", mfspr(SPRN_HID1));
280 /* Restore low level PMU operations */
283 /* Restore decrementer */
284 wakeup_decrementer();
286 /* Restore interrupts */
287 openpic_resume(NULL);
289 /* Let interrupts flow again ... */
293 debug_calc_bogomips();
301 static int __pmac do_set_cpu_speed(int speed_mode)
303 struct cpufreq_freqs freqs;
306 freqs.old = cur_freq;
307 freqs.new = (speed_mode == PMAC_CPU_HIGH_SPEED) ? hi_freq : low_freq;
308 freqs.cpu = smp_processor_id();
310 if (freqs.old == freqs.new)
313 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
314 set_speed_proc(speed_mode == PMAC_CPU_LOW_SPEED);
315 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
316 cur_freq = (speed_mode == PMAC_CPU_HIGH_SPEED) ? hi_freq : low_freq;
321 static int __pmac pmac_cpufreq_verify(struct cpufreq_policy *policy)
323 return cpufreq_frequency_table_verify(policy, pmac_cpu_freqs);
326 static int __pmac pmac_cpufreq_target( struct cpufreq_policy *policy,
327 unsigned int target_freq,
328 unsigned int relation)
330 unsigned int newstate = 0;
332 if (cpufreq_frequency_table_target(policy, pmac_cpu_freqs,
333 target_freq, relation, &newstate))
336 return do_set_cpu_speed(newstate);
339 unsigned int __pmac pmac_get_one_cpufreq(int i)
341 /* Supports only one CPU for now */
342 return (i == 0) ? cur_freq : 0;
345 static int __pmac pmac_cpufreq_cpu_init(struct cpufreq_policy *policy)
347 if (policy->cpu != 0)
350 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
351 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
352 policy->cur = cur_freq;
354 return cpufreq_frequency_table_cpuinfo(policy, &pmac_cpu_freqs[0]);
357 static u32 __pmac read_gpio(struct device_node *np)
359 u32 *reg = (u32 *)get_property(np, "reg", NULL);
363 /* That works for all keylargos but shall be fixed properly
366 return 0x50 + (*reg);
369 static struct cpufreq_driver pmac_cpufreq_driver = {
370 .verify = pmac_cpufreq_verify,
371 .target = pmac_cpufreq_target,
372 .init = pmac_cpufreq_cpu_init,
374 .owner = THIS_MODULE,
378 static int __pmac pmac_cpufreq_init_MacRISC3(struct device_node *cpunode)
380 struct device_node *volt_gpio_np = of_find_node_by_name(NULL,
382 struct device_node *freq_gpio_np = of_find_node_by_name(NULL,
384 struct device_node *slew_done_gpio_np = of_find_node_by_name(NULL,
389 * Check to see if it's GPIO driven or PMU only
391 * The way we extract the GPIO address is slightly hackish, but it
392 * works well enough for now. We need to abstract the whole GPIO
393 * stuff sooner or later anyway
397 voltage_gpio = read_gpio(volt_gpio_np);
399 frequency_gpio = read_gpio(freq_gpio_np);
400 if (slew_done_gpio_np)
401 slew_done_gpio = read_gpio(slew_done_gpio_np);
403 /* If we use the frequency GPIOs, calculate the min/max speeds based
404 * on the bus frequencies
406 if (frequency_gpio && slew_done_gpio) {
410 freqs = (u32 *)get_property(cpunode, "bus-frequencies", &lenp);
412 if (freqs == NULL || lenp != 2) {
413 printk(KERN_ERR "cpufreq: bus-frequencies incorrect or missing\n");
416 ratio = (u32 *)get_property(cpunode, "processor-to-bus-ratio*2", NULL);
418 printk(KERN_ERR "cpufreq: processor-to-bus-ratio*2 missing\n");
422 /* Get the min/max bus frequencies */
423 low_freq = min(freqs[0], freqs[1]);
424 hi_freq = max(freqs[0], freqs[1]);
426 /* Grrrr.. It _seems_ that the device-tree is lying on the low bus
427 * frequency, it claims it to be around 84Mhz on some models while
428 * it appears to be approx. 101Mhz on all. Let's hack around here...
429 * fortunately, we don't need to be too precise
431 if (low_freq < 98000000)
432 low_freq = 101000000;
434 /* Convert those to CPU core clocks */
435 low_freq = (low_freq * (*ratio)) / 2000;
436 hi_freq = (hi_freq * (*ratio)) / 2000;
438 /* Now we get the frequencies, we read the GPIO to see what is out current
441 rc = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, frequency_gpio, 0);
442 cur_freq = (rc & 0x01) ? hi_freq : low_freq;
444 set_speed_proc = gpios_set_cpu_speed;
448 /* If we use the PMU, look for the min & max frequencies in the
451 value = (u32 *)get_property(cpunode, "min-clock-frequency", NULL);
454 low_freq = (*value) / 1000;
455 /* The PowerBook G4 12" (PowerBook6,1) has an error in the device-tree
457 if (low_freq < 100000)
460 value = (u32 *)get_property(cpunode, "max-clock-frequency", NULL);
463 hi_freq = (*value) / 1000;
464 set_speed_proc = pmu_set_cpu_speed;
469 static int __pmac pmac_cpufreq_init_7447A(struct device_node *cpunode)
471 struct device_node *volt_gpio_np;
473 /* OF only reports the high frequency */
475 low_freq = cur_freq/2;
476 if (mfspr(HID1) & HID1_DFS)
481 volt_gpio_np = of_find_node_by_name(NULL, "cpu-vcore-select");
483 printk(KERN_ERR "cpufreq: missing cpu-vcore-select gpio\n");
487 u32 *reg = (u32 *)get_property(volt_gpio_np, "reg", NULL);
489 set_speed_proc = dfs_set_cpu_speed;
494 /* Currently, we support the following machines:
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 (PMU based, 400Mhz & 500Mhz)
501 * - iBook2 700 (CPU based, 400Mhz & 700Mhz, support low voltage)
502 * - Recent MacRISC3 laptops
503 * - iBook G4s and PowerBook G4s with 7447A CPUs
505 static int __init pmac_cpufreq_setup(void)
507 struct device_node *cpunode;
510 if (strstr(cmd_line, "nocpufreq"))
513 /* Assume only one CPU */
514 cpunode = find_type_devices("cpu");
518 /* Get current cpu clock freq */
519 value = (u32 *)get_property(cpunode, "clock-frequency", NULL);
522 cur_freq = (*value) / 1000;
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 */
536 } else if (machine_is_compatible("PowerBook4,1")) {
537 /* We only know about 500Mhz model */
538 if (cur_freq < 450000 || cur_freq > 550000)
542 set_speed_proc = pmu_set_cpu_speed;
544 /* Else check for TiPb 400 & 500 */
545 else if (machine_is_compatible("PowerBook3,2")) {
546 /* We only know about the 400 MHz and the 500Mhz model
547 * they both have 300 MHz as low frequency
549 if (cur_freq < 350000 || cur_freq > 550000)
553 set_speed_proc = pmu_set_cpu_speed;
555 /* Else check for 750FX */
556 else if (PVR_VER(mfspr(PVR)) == 0x7000) {
557 if (get_property(cpunode, "dynamic-power-step", NULL) == NULL)
560 value = (u32 *)get_property(cpunode, "reduced-clock-frequency", NULL);
563 low_freq = (*value) / 1000;
564 set_speed_proc = cpu_750fx_cpu_speed;
567 if (set_speed_proc == NULL)
570 pmac_cpu_freqs[CPUFREQ_LOW].frequency = low_freq;
571 pmac_cpu_freqs[CPUFREQ_HIGH].frequency = hi_freq;
573 printk(KERN_INFO "Registering PowerMac CPU frequency driver\n");
574 printk(KERN_INFO "Low: %d Mhz, High: %d Mhz, Boot: %d Mhz\n",
575 low_freq/1000, hi_freq/1000, cur_freq/1000);
577 return cpufreq_register_driver(&pmac_cpufreq_driver);
580 module_init(pmac_cpufreq_setup);