Linux 3.12.39
[linux/fpc-iii.git] / drivers / cpufreq / maple-cpufreq.c
blob6168d77b296d78b7d1a452bae6f3ae34c7e036c3
1 /*
2 * Copyright (C) 2011 Dmitry Eremin-Solenikov
3 * Copyright (C) 2002 - 2005 Benjamin Herrenschmidt <benh@kernel.crashing.org>
4 * and Markus Demleitner <msdemlei@cl.uni-heidelberg.de>
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.
10 * This driver adds basic cpufreq support for SMU & 970FX based G5 Macs,
11 * that is iMac G5 and latest single CPU desktop.
14 #undef DEBUG
16 #include <linux/module.h>
17 #include <linux/types.h>
18 #include <linux/errno.h>
19 #include <linux/kernel.h>
20 #include <linux/delay.h>
21 #include <linux/sched.h>
22 #include <linux/cpufreq.h>
23 #include <linux/init.h>
24 #include <linux/completion.h>
25 #include <linux/mutex.h>
26 #include <linux/time.h>
27 #include <linux/of_device.h>
29 #define DBG(fmt...) pr_debug(fmt)
31 /* see 970FX user manual */
33 #define SCOM_PCR 0x0aa001 /* PCR scom addr */
35 #define PCR_HILO_SELECT 0x80000000U /* 1 = PCR, 0 = PCRH */
36 #define PCR_SPEED_FULL 0x00000000U /* 1:1 speed value */
37 #define PCR_SPEED_HALF 0x00020000U /* 1:2 speed value */
38 #define PCR_SPEED_QUARTER 0x00040000U /* 1:4 speed value */
39 #define PCR_SPEED_MASK 0x000e0000U /* speed mask */
40 #define PCR_SPEED_SHIFT 17
41 #define PCR_FREQ_REQ_VALID 0x00010000U /* freq request valid */
42 #define PCR_VOLT_REQ_VALID 0x00008000U /* volt request valid */
43 #define PCR_TARGET_TIME_MASK 0x00006000U /* target time */
44 #define PCR_STATLAT_MASK 0x00001f00U /* STATLAT value */
45 #define PCR_SNOOPLAT_MASK 0x000000f0U /* SNOOPLAT value */
46 #define PCR_SNOOPACC_MASK 0x0000000fU /* SNOOPACC value */
48 #define SCOM_PSR 0x408001 /* PSR scom addr */
49 /* warning: PSR is a 64 bits register */
50 #define PSR_CMD_RECEIVED 0x2000000000000000U /* command received */
51 #define PSR_CMD_COMPLETED 0x1000000000000000U /* command completed */
52 #define PSR_CUR_SPEED_MASK 0x0300000000000000U /* current speed */
53 #define PSR_CUR_SPEED_SHIFT (56)
56 * The G5 only supports two frequencies (Quarter speed is not supported)
58 #define CPUFREQ_HIGH 0
59 #define CPUFREQ_LOW 1
61 static struct cpufreq_frequency_table maple_cpu_freqs[] = {
62 {CPUFREQ_HIGH, 0},
63 {CPUFREQ_LOW, 0},
64 {0, CPUFREQ_TABLE_END},
67 static struct freq_attr *maple_cpu_freqs_attr[] = {
68 &cpufreq_freq_attr_scaling_available_freqs,
69 NULL,
72 /* Power mode data is an array of the 32 bits PCR values to use for
73 * the various frequencies, retrieved from the device-tree
75 static int maple_pmode_cur;
77 static DEFINE_MUTEX(maple_switch_mutex);
79 static const u32 *maple_pmode_data;
80 static int maple_pmode_max;
83 * SCOM based frequency switching for 970FX rev3
85 static int maple_scom_switch_freq(int speed_mode)
87 unsigned long flags;
88 int to;
90 local_irq_save(flags);
92 /* Clear PCR high */
93 scom970_write(SCOM_PCR, 0);
94 /* Clear PCR low */
95 scom970_write(SCOM_PCR, PCR_HILO_SELECT | 0);
96 /* Set PCR low */
97 scom970_write(SCOM_PCR, PCR_HILO_SELECT |
98 maple_pmode_data[speed_mode]);
100 /* Wait for completion */
101 for (to = 0; to < 10; to++) {
102 unsigned long psr = scom970_read(SCOM_PSR);
104 if ((psr & PSR_CMD_RECEIVED) == 0 &&
105 (((psr >> PSR_CUR_SPEED_SHIFT) ^
106 (maple_pmode_data[speed_mode] >> PCR_SPEED_SHIFT)) & 0x3)
107 == 0)
108 break;
109 if (psr & PSR_CMD_COMPLETED)
110 break;
111 udelay(100);
114 local_irq_restore(flags);
116 maple_pmode_cur = speed_mode;
117 ppc_proc_freq = maple_cpu_freqs[speed_mode].frequency * 1000ul;
119 return 0;
122 static int maple_scom_query_freq(void)
124 unsigned long psr = scom970_read(SCOM_PSR);
125 int i;
127 for (i = 0; i <= maple_pmode_max; i++)
128 if ((((psr >> PSR_CUR_SPEED_SHIFT) ^
129 (maple_pmode_data[i] >> PCR_SPEED_SHIFT)) & 0x3) == 0)
130 break;
131 return i;
135 * Common interface to the cpufreq core
138 static int maple_cpufreq_verify(struct cpufreq_policy *policy)
140 return cpufreq_frequency_table_verify(policy, maple_cpu_freqs);
143 static int maple_cpufreq_target(struct cpufreq_policy *policy,
144 unsigned int target_freq, unsigned int relation)
146 unsigned int newstate = 0;
147 struct cpufreq_freqs freqs;
148 int rc;
150 if (cpufreq_frequency_table_target(policy, maple_cpu_freqs,
151 target_freq, relation, &newstate))
152 return -EINVAL;
154 if (maple_pmode_cur == newstate)
155 return 0;
157 mutex_lock(&maple_switch_mutex);
159 freqs.old = maple_cpu_freqs[maple_pmode_cur].frequency;
160 freqs.new = maple_cpu_freqs[newstate].frequency;
162 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
163 rc = maple_scom_switch_freq(newstate);
164 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
166 mutex_unlock(&maple_switch_mutex);
168 return rc;
171 static unsigned int maple_cpufreq_get_speed(unsigned int cpu)
173 return maple_cpu_freqs[maple_pmode_cur].frequency;
176 static int maple_cpufreq_cpu_init(struct cpufreq_policy *policy)
178 policy->cpuinfo.transition_latency = 12000;
179 policy->cur = maple_cpu_freqs[maple_scom_query_freq()].frequency;
180 /* secondary CPUs are tied to the primary one by the
181 * cpufreq core if in the secondary policy we tell it that
182 * it actually must be one policy together with all others. */
183 cpumask_setall(policy->cpus);
184 cpufreq_frequency_table_get_attr(maple_cpu_freqs, policy->cpu);
186 return cpufreq_frequency_table_cpuinfo(policy,
187 maple_cpu_freqs);
191 static struct cpufreq_driver maple_cpufreq_driver = {
192 .name = "maple",
193 .flags = CPUFREQ_CONST_LOOPS,
194 .init = maple_cpufreq_cpu_init,
195 .verify = maple_cpufreq_verify,
196 .target = maple_cpufreq_target,
197 .get = maple_cpufreq_get_speed,
198 .attr = maple_cpu_freqs_attr,
201 static int __init maple_cpufreq_init(void)
203 struct device_node *cpunode;
204 unsigned int psize;
205 unsigned long max_freq;
206 const u32 *valp;
207 u32 pvr_hi;
208 int rc = -ENODEV;
211 * Behave here like powermac driver which checks machine compatibility
212 * to ease merging of two drivers in future.
214 if (!of_machine_is_compatible("Momentum,Maple") &&
215 !of_machine_is_compatible("Momentum,Apache"))
216 return 0;
218 /* Get first CPU node */
219 cpunode = of_cpu_device_node_get(0);
220 if (cpunode == NULL) {
221 printk(KERN_ERR "cpufreq: Can't find any CPU 0 node\n");
222 goto bail_noprops;
225 /* Check 970FX for now */
226 /* we actually don't care on which CPU to access PVR */
227 pvr_hi = PVR_VER(mfspr(SPRN_PVR));
228 if (pvr_hi != 0x3c && pvr_hi != 0x44) {
229 printk(KERN_ERR "cpufreq: Unsupported CPU version (%x)\n",
230 pvr_hi);
231 goto bail_noprops;
234 /* Look for the powertune data in the device-tree */
236 * On Maple this property is provided by PIBS in dual-processor config,
237 * not provided by PIBS in CPU0 config and also not provided by SLOF,
238 * so YMMV
240 maple_pmode_data = of_get_property(cpunode, "power-mode-data", &psize);
241 if (!maple_pmode_data) {
242 DBG("No power-mode-data !\n");
243 goto bail_noprops;
245 maple_pmode_max = psize / sizeof(u32) - 1;
248 * From what I see, clock-frequency is always the maximal frequency.
249 * The current driver can not slew sysclk yet, so we really only deal
250 * with powertune steps for now. We also only implement full freq and
251 * half freq in this version. So far, I haven't yet seen a machine
252 * supporting anything else.
254 valp = of_get_property(cpunode, "clock-frequency", NULL);
255 if (!valp)
256 return -ENODEV;
257 max_freq = (*valp)/1000;
258 maple_cpu_freqs[0].frequency = max_freq;
259 maple_cpu_freqs[1].frequency = max_freq/2;
261 /* Force apply current frequency to make sure everything is in
262 * sync (voltage is right for example). Firmware may leave us with
263 * a strange setting ...
265 msleep(10);
266 maple_pmode_cur = -1;
267 maple_scom_switch_freq(maple_scom_query_freq());
269 printk(KERN_INFO "Registering Maple CPU frequency driver\n");
270 printk(KERN_INFO "Low: %d Mhz, High: %d Mhz, Cur: %d MHz\n",
271 maple_cpu_freqs[1].frequency/1000,
272 maple_cpu_freqs[0].frequency/1000,
273 maple_cpu_freqs[maple_pmode_cur].frequency/1000);
275 rc = cpufreq_register_driver(&maple_cpufreq_driver);
277 of_node_put(cpunode);
279 return rc;
281 bail_noprops:
282 of_node_put(cpunode);
284 return rc;
287 module_init(maple_cpufreq_init);
290 MODULE_LICENSE("GPL");