http://downloads.netgear.com/files/GPL/DM111PSP_v3.61d_GPL.tar.gz
[bcm963xx.git] / kernel / linux / 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 <asm/prom.h>
28 #include <asm/machdep.h>
29 #include <asm/irq.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>
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         int rc;
305
306         freqs.old = cur_freq;
307         freqs.new = (speed_mode == PMAC_CPU_HIGH_SPEED) ? hi_freq : low_freq;
308         freqs.cpu = smp_processor_id();
309
310         if (freqs.old == freqs.new)
311                 return 0;
312
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;
317
318         return rc;
319 }
320
321 static int __pmac pmac_cpufreq_verify(struct cpufreq_policy *policy)
322 {
323         return cpufreq_frequency_table_verify(policy, pmac_cpu_freqs);
324 }
325
326 static int __pmac pmac_cpufreq_target(  struct cpufreq_policy *policy,
327                                         unsigned int target_freq,
328                                         unsigned int relation)
329 {
330         unsigned int    newstate = 0;
331
332         if (cpufreq_frequency_table_target(policy, pmac_cpu_freqs,
333                         target_freq, relation, &newstate))
334                 return -EINVAL;
335
336         return do_set_cpu_speed(newstate);
337 }
338
339 unsigned int __pmac pmac_get_one_cpufreq(int i)
340 {
341         /* Supports only one CPU for now */
342         return (i == 0) ? cur_freq : 0;
343 }
344
345 static int __pmac pmac_cpufreq_cpu_init(struct cpufreq_policy *policy)
346 {
347         if (policy->cpu != 0)
348                 return -ENODEV;
349
350         policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
351         policy->cpuinfo.transition_latency      = CPUFREQ_ETERNAL;
352         policy->cur = cur_freq;
353
354         return cpufreq_frequency_table_cpuinfo(policy, &pmac_cpu_freqs[0]);
355 }
356
357 static u32 __pmac read_gpio(struct device_node *np)
358 {
359         u32 *reg = (u32 *)get_property(np, "reg", NULL);
360
361         if (reg == NULL)
362                 return 0;
363         /* That works for all keylargos but shall be fixed properly
364          * some day...
365          */
366         return 0x50 + (*reg);
367 }
368
369 static struct cpufreq_driver pmac_cpufreq_driver = {
370         .verify         = pmac_cpufreq_verify,
371         .target         = pmac_cpufreq_target,
372         .init           = pmac_cpufreq_cpu_init,
373         .name           = "powermac",
374         .owner          = THIS_MODULE,
375 };
376
377
378 static int __pmac pmac_cpufreq_init_MacRISC3(struct device_node *cpunode)
379 {
380         struct device_node *volt_gpio_np = of_find_node_by_name(NULL,
381                                                                 "voltage-gpio");
382         struct device_node *freq_gpio_np = of_find_node_by_name(NULL,
383                                                                 "frequency-gpio");
384         struct device_node *slew_done_gpio_np = of_find_node_by_name(NULL,
385                                                                      "slewing-done");
386         u32 *value;
387
388         /*
389          * Check to see if it's GPIO driven or PMU only
390          *
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
394          */
395
396         if (volt_gpio_np)
397                 voltage_gpio = read_gpio(volt_gpio_np);
398         if (freq_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);
402
403         /* If we use the frequency GPIOs, calculate the min/max speeds based
404          * on the bus frequencies
405          */
406         if (frequency_gpio && slew_done_gpio) {
407                 int lenp, rc;
408                 u32 *freqs, *ratio;
409
410                 freqs = (u32 *)get_property(cpunode, "bus-frequencies", &lenp);
411                 lenp /= sizeof(u32);
412                 if (freqs == NULL || lenp != 2) {
413                         printk(KERN_ERR "cpufreq: bus-frequencies incorrect or missing\n");
414                         return 1;
415                 }
416                 ratio = (u32 *)get_property(cpunode, "processor-to-bus-ratio*2", NULL);
417                 if (ratio == NULL) {
418                         printk(KERN_ERR "cpufreq: processor-to-bus-ratio*2 missing\n");
419                         return 1;
420                 }
421
422                 /* Get the min/max bus frequencies */
423                 low_freq = min(freqs[0], freqs[1]);
424                 hi_freq = max(freqs[0], freqs[1]);
425
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
430                  */
431                 if (low_freq < 98000000)
432                         low_freq = 101000000;
433                         
434                 /* Convert those to CPU core clocks */
435                 low_freq = (low_freq * (*ratio)) / 2000;
436                 hi_freq = (hi_freq * (*ratio)) / 2000;
437
438                 /* Now we get the frequencies, we read the GPIO to see what is out current
439                  * speed
440                  */
441                 rc = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, frequency_gpio, 0);
442                 cur_freq = (rc & 0x01) ? hi_freq : low_freq;
443
444                 set_speed_proc = gpios_set_cpu_speed;
445                 return 1;
446         }
447
448         /* If we use the PMU, look for the min & max frequencies in the
449          * device-tree
450          */
451         value = (u32 *)get_property(cpunode, "min-clock-frequency", NULL);
452         if (!value)
453                 return 1;
454         low_freq = (*value) / 1000;
455         /* The PowerBook G4 12" (PowerBook6,1) has an error in the device-tree
456          * here */
457         if (low_freq < 100000)
458                 low_freq *= 10;
459
460         value = (u32 *)get_property(cpunode, "max-clock-frequency", NULL);
461         if (!value)
462                 return 1;
463         hi_freq = (*value) / 1000;
464         set_speed_proc = pmu_set_cpu_speed;
465
466         return 0;
467 }
468
469 static int __pmac pmac_cpufreq_init_7447A(struct device_node *cpunode)
470 {
471         struct device_node *volt_gpio_np;
472         u32 *reg;
473
474         /* OF only reports the high frequency */
475         hi_freq = cur_freq;
476         low_freq = cur_freq/2;
477         if (mfspr(HID1) & HID1_DFS)
478                 cur_freq = low_freq;
479         else
480                 cur_freq = hi_freq;
481
482         volt_gpio_np = of_find_node_by_name(NULL, "cpu-vcore-select");
483         if (!volt_gpio_np){
484                 printk(KERN_ERR "cpufreq: missing cpu-vcore-select gpio\n");
485                 return 1;
486         }
487
488         reg = (u32 *)get_property(volt_gpio_np, "reg", NULL);
489         voltage_gpio = *reg;
490         set_speed_proc = dfs_set_cpu_speed;
491
492         return 0;
493 }
494
495 /* Currently, we support the following machines:
496  *
497  *  - Titanium PowerBook 1Ghz (PMU based, 667Mhz & 1Ghz)
498  *  - Titanium PowerBook 800 (PMU based, 667Mhz & 800Mhz)
499  *  - Titanium PowerBook 400 (PMU based, 300Mhz & 400Mhz)
500  *  - Titanium PowerBook 500 (PMU based, 300Mhz & 500Mhz)
501  *  - iBook2 500 (PMU based, 400Mhz & 500Mhz)
502  *  - iBook2 700 (CPU based, 400Mhz & 700Mhz, support low voltage)
503  *  - Recent MacRISC3 laptops
504  *  - iBook G4s and PowerBook G4s with 7447A CPUs
505  */
506 static int __init pmac_cpufreq_setup(void)
507 {
508         struct device_node      *cpunode;
509         u32                     *value;
510
511         if (strstr(cmd_line, "nocpufreq"))
512                 return 0;
513
514         /* Assume only one CPU */
515         cpunode = find_type_devices("cpu");
516         if (!cpunode)
517                 goto out;
518
519         /* Get current cpu clock freq */
520         value = (u32 *)get_property(cpunode, "clock-frequency", NULL);
521         if (!value)
522                 goto out;
523         cur_freq = (*value) / 1000;
524
525         /*  Check for 7447A based iBook G4 or PowerBook */
526         if (machine_is_compatible("PowerBook6,5") ||
527             machine_is_compatible("PowerBook6,4") ||
528             machine_is_compatible("PowerBook5,5") ||
529             machine_is_compatible("PowerBook5,4")) {
530                 pmac_cpufreq_init_7447A(cpunode);
531         /* Check for other MacRISC3 machines */
532         } else if (machine_is_compatible("PowerBook3,4") ||
533                    machine_is_compatible("PowerBook3,5") ||
534                    machine_is_compatible("MacRISC3")) {
535                 pmac_cpufreq_init_MacRISC3(cpunode);
536         /* Else check for iBook2 500 */
537         } else if (machine_is_compatible("PowerBook4,1")) {
538                 /* We only know about 500Mhz model */
539                 if (cur_freq < 450000 || cur_freq > 550000)
540                         goto out;
541                 hi_freq = cur_freq;
542                 low_freq = 400000;
543                 set_speed_proc = pmu_set_cpu_speed;
544         }
545         /* Else check for TiPb 400 & 500 */
546         else if (machine_is_compatible("PowerBook3,2")) {
547                 /* We only know about the 400 MHz and the 500Mhz model
548                  * they both have 300 MHz as low frequency
549                  */
550                 if (cur_freq < 350000 || cur_freq > 550000)
551                         goto out;
552                 hi_freq = cur_freq;
553                 low_freq = 300000;
554                 set_speed_proc = pmu_set_cpu_speed;
555         }
556         /* Else check for 750FX */
557         else if (PVR_VER(mfspr(PVR)) == 0x7000) {
558                 if (get_property(cpunode, "dynamic-power-step", NULL) == NULL)
559                         goto out;
560                 hi_freq = cur_freq;
561                 value = (u32 *)get_property(cpunode, "reduced-clock-frequency", NULL);
562                 if (!value)
563                         goto out;
564                 low_freq = (*value) / 1000;             
565                 set_speed_proc = cpu_750fx_cpu_speed;
566         }
567 out:
568         if (set_speed_proc == NULL)
569                 return -ENODEV;
570
571         pmac_cpu_freqs[CPUFREQ_LOW].frequency = low_freq;
572         pmac_cpu_freqs[CPUFREQ_HIGH].frequency = hi_freq;
573
574         printk(KERN_INFO "Registering PowerMac CPU frequency driver\n");
575         printk(KERN_INFO "Low: %d Mhz, High: %d Mhz, Boot: %d Mhz\n",
576                low_freq/1000, hi_freq/1000, cur_freq/1000);
577
578         return cpufreq_register_driver(&pmac_cpufreq_driver);
579 }
580
581 module_init(pmac_cpufreq_setup);
582