PageRenderTime 40ms CodeModel.GetById 17ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/ppc/platforms/pmac_cpufreq.c

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