📄 cpufreq_64.c
字号:
/* * Copyright (C) 2002 - 2005 Benjamin Herrenschmidt <benh@kernel.crashing.org> * and Markus Demleitner <msdemlei@cl.uni-heidelberg.de> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * This driver adds basic cpufreq support for SMU & 970FX based G5 Macs, * that is iMac G5 and latest single CPU desktop. */#undef DEBUG#include <linux/module.h>#include <linux/types.h>#include <linux/errno.h>#include <linux/kernel.h>#include <linux/delay.h>#include <linux/sched.h>#include <linux/slab.h>#include <linux/cpufreq.h>#include <linux/init.h>#include <linux/completion.h>#include <linux/mutex.h>#include <asm/prom.h>#include <asm/machdep.h>#include <asm/irq.h>#include <asm/sections.h>#include <asm/cputable.h>#include <asm/time.h>#include <asm/smu.h>#include <asm/pmac_pfunc.h>#define DBG(fmt...) pr_debug(fmt)/* see 970FX user manual */#define SCOM_PCR 0x0aa001 /* PCR scom addr */#define PCR_HILO_SELECT 0x80000000U /* 1 = PCR, 0 = PCRH */#define PCR_SPEED_FULL 0x00000000U /* 1:1 speed value */#define PCR_SPEED_HALF 0x00020000U /* 1:2 speed value */#define PCR_SPEED_QUARTER 0x00040000U /* 1:4 speed value */#define PCR_SPEED_MASK 0x000e0000U /* speed mask */#define PCR_SPEED_SHIFT 17#define PCR_FREQ_REQ_VALID 0x00010000U /* freq request valid */#define PCR_VOLT_REQ_VALID 0x00008000U /* volt request valid */#define PCR_TARGET_TIME_MASK 0x00006000U /* target time */#define PCR_STATLAT_MASK 0x00001f00U /* STATLAT value */#define PCR_SNOOPLAT_MASK 0x000000f0U /* SNOOPLAT value */#define PCR_SNOOPACC_MASK 0x0000000fU /* SNOOPACC value */#define SCOM_PSR 0x408001 /* PSR scom addr *//* warning: PSR is a 64 bits register */#define PSR_CMD_RECEIVED 0x2000000000000000U /* command received */#define PSR_CMD_COMPLETED 0x1000000000000000U /* command completed */#define PSR_CUR_SPEED_MASK 0x0300000000000000U /* current speed */#define PSR_CUR_SPEED_SHIFT (56)/* * The G5 only supports two frequencies (Quarter speed is not supported) */#define CPUFREQ_HIGH 0#define CPUFREQ_LOW 1static struct cpufreq_frequency_table g5_cpu_freqs[] = { {CPUFREQ_HIGH, 0}, {CPUFREQ_LOW, 0}, {0, CPUFREQ_TABLE_END},};static struct freq_attr* g5_cpu_freqs_attr[] = { &cpufreq_freq_attr_scaling_available_freqs, NULL,};/* Power mode data is an array of the 32 bits PCR values to use for * the various frequencies, retrieved from the device-tree */static int g5_pmode_cur;static void (*g5_switch_volt)(int speed_mode);static int (*g5_switch_freq)(int speed_mode);static int (*g5_query_freq)(void);static DEFINE_MUTEX(g5_switch_mutex);#ifdef CONFIG_PMAC_SMUstatic const u32 *g5_pmode_data;static int g5_pmode_max;static struct smu_sdbp_fvt *g5_fvt_table; /* table of op. points */static int g5_fvt_count; /* number of op. points */static int g5_fvt_cur; /* current op. point *//* * SMU based voltage switching for Neo2 platforms */static void g5_smu_switch_volt(int speed_mode){ struct smu_simple_cmd cmd; DECLARE_COMPLETION_ONSTACK(comp); smu_queue_simple(&cmd, SMU_CMD_POWER_COMMAND, 8, smu_done_complete, &comp, 'V', 'S', 'L', 'E', 'W', 0xff, g5_fvt_cur+1, speed_mode); wait_for_completion(&comp);}/* * Platform function based voltage/vdnap switching for Neo2 */static struct pmf_function *pfunc_set_vdnap0;static struct pmf_function *pfunc_vdnap0_complete;static void g5_vdnap_switch_volt(int speed_mode){ struct pmf_args args; u32 slew, done = 0; unsigned long timeout; slew = (speed_mode == CPUFREQ_LOW) ? 1 : 0; args.count = 1; args.u[0].p = &slew; pmf_call_one(pfunc_set_vdnap0, &args); /* It's an irq GPIO so we should be able to just block here, * I'll do that later after I've properly tested the IRQ code for * platform functions */ timeout = jiffies + HZ/10; while(!time_after(jiffies, timeout)) { args.count = 1; args.u[0].p = &done; pmf_call_one(pfunc_vdnap0_complete, &args); if (done) break; msleep(1); } if (done == 0) printk(KERN_WARNING "cpufreq: Timeout in clock slewing !\n");}/* * SCOM based frequency switching for 970FX rev3 */static int g5_scom_switch_freq(int speed_mode){ unsigned long flags; int to; /* If frequency is going up, first ramp up the voltage */ if (speed_mode < g5_pmode_cur) g5_switch_volt(speed_mode); local_irq_save(flags); /* Clear PCR high */ scom970_write(SCOM_PCR, 0); /* Clear PCR low */ scom970_write(SCOM_PCR, PCR_HILO_SELECT | 0); /* Set PCR low */ scom970_write(SCOM_PCR, PCR_HILO_SELECT | g5_pmode_data[speed_mode]); /* Wait for completion */ for (to = 0; to < 10; to++) { unsigned long psr = scom970_read(SCOM_PSR); if ((psr & PSR_CMD_RECEIVED) == 0 && (((psr >> PSR_CUR_SPEED_SHIFT) ^ (g5_pmode_data[speed_mode] >> PCR_SPEED_SHIFT)) & 0x3) == 0) break; if (psr & PSR_CMD_COMPLETED) break; udelay(100); } local_irq_restore(flags); /* If frequency is going down, last ramp the voltage */ if (speed_mode > g5_pmode_cur) g5_switch_volt(speed_mode); g5_pmode_cur = speed_mode; ppc_proc_freq = g5_cpu_freqs[speed_mode].frequency * 1000ul; return 0;}static int g5_scom_query_freq(void){ unsigned long psr = scom970_read(SCOM_PSR); int i; for (i = 0; i <= g5_pmode_max; i++) if ((((psr >> PSR_CUR_SPEED_SHIFT) ^ (g5_pmode_data[i] >> PCR_SPEED_SHIFT)) & 0x3) == 0) break; return i;}/* * Fake voltage switching for platforms with missing support */static void g5_dummy_switch_volt(int speed_mode){}#endif /* CONFIG_PMAC_SMU *//* * Platform function based voltage switching for PowerMac7,2 & 7,3 */static struct pmf_function *pfunc_cpu0_volt_high;static struct pmf_function *pfunc_cpu0_volt_low;static struct pmf_function *pfunc_cpu1_volt_high;static struct pmf_function *pfunc_cpu1_volt_low;static void g5_pfunc_switch_volt(int speed_mode){ if (speed_mode == CPUFREQ_HIGH) { if (pfunc_cpu0_volt_high) pmf_call_one(pfunc_cpu0_volt_high, NULL); if (pfunc_cpu1_volt_high) pmf_call_one(pfunc_cpu1_volt_high, NULL); } else { if (pfunc_cpu0_volt_low) pmf_call_one(pfunc_cpu0_volt_low, NULL); if (pfunc_cpu1_volt_low) pmf_call_one(pfunc_cpu1_volt_low, NULL); } msleep(10); /* should be faster , to fix */}/* * Platform function based frequency switching for PowerMac7,2 & 7,3 */static struct pmf_function *pfunc_cpu_setfreq_high;static struct pmf_function *pfunc_cpu_setfreq_low;static struct pmf_function *pfunc_cpu_getfreq;static struct pmf_function *pfunc_slewing_done;;static int g5_pfunc_switch_freq(int speed_mode){ struct pmf_args args; u32 done = 0; unsigned long timeout; int rc; DBG("g5_pfunc_switch_freq(%d)\n", speed_mode); /* If frequency is going up, first ramp up the voltage */ if (speed_mode < g5_pmode_cur) g5_switch_volt(speed_mode); /* Do it */ if (speed_mode == CPUFREQ_HIGH) rc = pmf_call_one(pfunc_cpu_setfreq_high, NULL); else rc = pmf_call_one(pfunc_cpu_setfreq_low, NULL); if (rc) printk(KERN_WARNING "cpufreq: pfunc switch error %d\n", rc); /* It's an irq GPIO so we should be able to just block here, * I'll do that later after I've properly tested the IRQ code for * platform functions */ timeout = jiffies + HZ/10; while(!time_after(jiffies, timeout)) { args.count = 1; args.u[0].p = &done; pmf_call_one(pfunc_slewing_done, &args); if (done) break; msleep(1); } if (done == 0) printk(KERN_WARNING "cpufreq: Timeout in clock slewing !\n"); /* If frequency is going down, last ramp the voltage */ if (speed_mode > g5_pmode_cur) g5_switch_volt(speed_mode); g5_pmode_cur = speed_mode; ppc_proc_freq = g5_cpu_freqs[speed_mode].frequency * 1000ul; return 0;}static int g5_pfunc_query_freq(void){ struct pmf_args args; u32 val = 0; args.count = 1; args.u[0].p = &val; pmf_call_one(pfunc_cpu_getfreq, &args); return val ? CPUFREQ_HIGH : CPUFREQ_LOW;}/* * Common interface to the cpufreq core */static int g5_cpufreq_verify(struct cpufreq_policy *policy){ return cpufreq_frequency_table_verify(policy, g5_cpu_freqs);}static int g5_cpufreq_target(struct cpufreq_policy *policy, unsigned int target_freq, unsigned int relation){ unsigned int newstate = 0; struct cpufreq_freqs freqs; int rc; if (cpufreq_frequency_table_target(policy, g5_cpu_freqs, target_freq, relation, &newstate)) return -EINVAL; if (g5_pmode_cur == newstate) return 0; mutex_lock(&g5_switch_mutex); freqs.old = g5_cpu_freqs[g5_pmode_cur].frequency; freqs.new = g5_cpu_freqs[newstate].frequency; freqs.cpu = 0; cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE); rc = g5_switch_freq(newstate); cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE); mutex_unlock(&g5_switch_mutex); return rc;}static unsigned int g5_cpufreq_get_speed(unsigned int cpu){ return g5_cpu_freqs[g5_pmode_cur].frequency;}static int g5_cpufreq_cpu_init(struct cpufreq_policy *policy){ policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL; policy->cur = g5_cpu_freqs[g5_query_freq()].frequency; /* secondary CPUs are tied to the primary one by the * cpufreq core if in the secondary policy we tell it that * it actually must be one policy together with all others. */ policy->cpus = cpu_online_map; cpufreq_frequency_table_get_attr(g5_cpu_freqs, policy->cpu); return cpufreq_frequency_table_cpuinfo(policy, g5_cpu_freqs);}static struct cpufreq_driver g5_cpufreq_driver = {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -