2 * arch/ppc/platforms/pmac_cpufreq.c
4 * Copyright (C) 2002 - 2003 Benjamin Herrenschmidt <benh@kernel.crashing.org>
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.
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>
27 #include <asm/machdep.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>
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
43 * There is a problem with the core cpufreq code on SMP kernels,
44 * it won't recalculate the Bogomips properly
47 #warning "WARNING, CPUFREQ not recommended on SMP kernels"
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);
57 static unsigned int low_freq;
58 static unsigned int hi_freq;
59 static unsigned int cur_freq;
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;
65 static u32 voltage_gpio;
66 static u32 frequency_gpio;
67 static u32 slew_done_gpio;
69 #define PMAC_CPU_LOW_SPEED 1
70 #define PMAC_CPU_HIGH_SPEED 0
72 /* There are only two frequency states for each processor. Values
73 * are in kHz for the time being.
75 #define CPUFREQ_HIGH PMAC_CPU_HIGH_SPEED
76 #define CPUFREQ_LOW PMAC_CPU_LOW_SPEED
78 static struct cpufreq_frequency_table pmac_cpu_freqs[] = {
81 {0, CPUFREQ_TABLE_END},
84 static inline void wakeup_decrementer(void)
86 set_dec(tb_ticks_per_jiffy);
87 /* No currently-supported powerbook has a 601,
88 * so use get_tbl, not native
90 last_jiffy_stamp(0) = tb_last_stamp = get_tbl();
94 static inline void debug_calc_bogomips(void)
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.
100 extern void calibrate_delay(void);
102 unsigned long save_lpj = loops_per_jiffy;
104 loops_per_jiffy = save_lpj;
106 #endif /* DEBUG_FREQ */
108 /* Switch CPU speed under 750FX CPU control
110 static int __pmac cpu_750fx_cpu_speed(int low_speed)
113 printk(KERN_DEBUG "HID1, before: %x\n", mfspr(SPRN_HID1));
116 low_choose_750fx_pll(low_speed);
119 printk(KERN_DEBUG "HID1, after: %x\n", mfspr(SPRN_HID1));
120 debug_calc_bogomips();
126 /* Switch CPU speed using slewing GPIOs
128 static int __pmac gpios_set_cpu_speed(unsigned int low_speed)
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);
141 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, frequency_gpio, low_speed ? 0x04 : 0x05);
144 set_current_state(TASK_UNINTERRUPTIBLE);
146 gpio = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, slew_done_gpio, 0);
147 } while((gpio & 0x02) == 0);
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);
158 debug_calc_bogomips();
164 /* Switch CPU speed under PMU control
166 static int __pmac pmu_set_cpu_speed(unsigned int low_speed)
168 struct adb_request req;
169 unsigned long save_l2cr;
170 unsigned long save_l3cr;
175 printk(KERN_DEBUG "HID1, before: %x\n", mfspr(SPRN_HID1));
177 /* Disable all interrupt sources on openpic */
178 openpic_suspend(NULL, 1);
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 */
185 asm volatile("mtdec %0" : : "r" (0x7fffffff));
187 /* We can now disable MSR_EE */
190 /* Giveup the FPU & vec */
193 #ifdef CONFIG_ALTIVEC
194 if (cur_cpu_spec[0]->cpu_features & CPU_FTR_ALTIVEC)
195 enable_kernel_altivec();
196 #endif /* CONFIG_ALTIVEC */
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);
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
209 pmu_request(&req, NULL, 6, PMU_CPU_SPEED, 'W', 'O', 'O', 'F', low_speed);
210 while (!req.complete)
213 /* Prepare the northbridge for the speed transition */
214 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,1,1);
216 /* Call low level code to backup CPU state and recover from
221 /* Restore the northbridge */
222 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,1,0);
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);
231 /* Restore userland MMU context */
232 set_context(current->active_mm->context, current->active_mm->pgd);
235 printk(KERN_DEBUG "HID1, after: %x\n", mfspr(SPRN_HID1));
238 /* Restore low level PMU operations */
241 /* Restore decrementer */
242 wakeup_decrementer();
244 /* Restore interrupts */
245 openpic_resume(NULL);
247 /* Let interrupts flow again ... */
251 debug_calc_bogomips();
259 static int __pmac do_set_cpu_speed(int speed_mode)
261 struct cpufreq_freqs freqs;
264 freqs.old = cur_freq;
265 freqs.new = (speed_mode == PMAC_CPU_HIGH_SPEED) ? hi_freq : low_freq;
266 freqs.cpu = smp_processor_id();
268 if (freqs.old == freqs.new)
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);
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;
284 static int __pmac pmac_cpufreq_verify(struct cpufreq_policy *policy)
286 return cpufreq_frequency_table_verify(policy, pmac_cpu_freqs);
289 static int __pmac pmac_cpufreq_target( struct cpufreq_policy *policy,
290 unsigned int target_freq,
291 unsigned int relation)
293 unsigned int newstate = 0;
295 if (cpufreq_frequency_table_target(policy, pmac_cpu_freqs,
296 target_freq, relation, &newstate))
299 return do_set_cpu_speed(newstate);
302 unsigned int __pmac pmac_get_one_cpufreq(int i)
304 /* Supports only one CPU for now */
305 return (i == 0) ? cur_freq : 0;
308 static int __pmac pmac_cpufreq_cpu_init(struct cpufreq_policy *policy)
310 if (policy->cpu != 0)
313 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
314 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
315 policy->cur = cur_freq;
317 return cpufreq_frequency_table_cpuinfo(policy, &pmac_cpu_freqs[0]);
320 static u32 __pmac read_gpio(struct device_node *np)
322 u32 *reg = (u32 *)get_property(np, "reg", NULL);
326 /* That works for all keylargos but shall be fixed properly
329 return 0x50 + (*reg);
332 static struct cpufreq_driver pmac_cpufreq_driver = {
333 .verify = pmac_cpufreq_verify,
334 .target = pmac_cpufreq_target,
335 .init = pmac_cpufreq_cpu_init,
337 .owner = THIS_MODULE,
341 /* Currently, we support the following machines:
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
350 static int __init pmac_cpufreq_setup(void)
352 struct device_node *cpunode;
354 int has_freq_ctl = 0;
356 if (strstr(cmd_line, "nocpufreq"))
359 /* Assume only one CPU */
360 cpunode = find_type_devices("cpu");
364 /* Get current cpu clock freq */
365 value = (u32 *)get_property(cpunode, "clock-frequency", NULL);
368 cur_freq = (*value) / 1000;
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");
379 * Check to see if it's GPIO driven or PMU only
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
387 voltage_gpio = read_gpio(volt_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);
393 /* If we use the frequency GPIOs, calculate the min/max speeds based
394 * on the bus frequencies
396 if (frequency_gpio && slew_done_gpio) {
400 freqs = (u32 *)get_property(cpunode, "bus-frequencies", &lenp);
402 if (freqs == NULL || lenp != 2) {
403 printk(KERN_ERR "cpufreq: bus-frequencies incorrect or missing\n");
406 ratio = (u32 *)get_property(cpunode, "processor-to-bus-ratio*2", NULL);
408 printk(KERN_ERR "cpufreq: processor-to-bus-ratio*2 missing\n");
412 /* Get the min/max bus frequencies */
413 low_freq = min(freqs[0], freqs[1]);
414 hi_freq = max(freqs[0], freqs[1]);
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
421 if (low_freq < 98000000)
422 low_freq = 101000000;
424 /* Convert those to CPU core clocks */
425 low_freq = (low_freq * (*ratio)) / 2000;
426 hi_freq = (hi_freq * (*ratio)) / 2000;
428 /* Now we get the frequencies, we read the GPIO to see what is out current
431 rc = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, frequency_gpio, 0);
432 cur_freq = (rc & 0x01) ? hi_freq : low_freq;
435 cpufreq_uses_gpios = 1;
439 /* If we use the PMU, look for the min & max frequencies in the
442 value = (u32 *)get_property(cpunode, "min-clock-frequency", NULL);
445 low_freq = (*value) / 1000;
446 /* The PowerBook G4 12" (PowerBook6,1) has an error in the device-tree
448 if (low_freq < 100000)
451 value = (u32 *)get_property(cpunode, "max-clock-frequency", NULL);
454 hi_freq = (*value) / 1000;
456 cpufreq_uses_pmu = 1;
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)
466 cpufreq_uses_pmu = 1;
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)
476 cpufreq_uses_pmu = 1;
478 /* Else check for 750FX */
479 else if (PVR_VER(mfspr(PVR)) == 0x7000) {
480 if (get_property(cpunode, "dynamic-power-step", NULL) == NULL)
483 value = (u32 *)get_property(cpunode, "reduced-clock-frequency", NULL);
486 low_freq = (*value) / 1000;
487 cpufreq_uses_pmu = 0;
494 pmac_cpu_freqs[CPUFREQ_LOW].frequency = low_freq;
495 pmac_cpu_freqs[CPUFREQ_HIGH].frequency = hi_freq;
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"));
502 return cpufreq_register_driver(&pmac_cpufreq_driver);
505 module_init(pmac_cpufreq_setup);