1
2
3
4
5
6
7
8
9
10
11
12
13
14#undef DEBUG
15
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.h>
28
29#define DBG(fmt...) pr_debug(fmt)
30
31
32
33#define SCOM_PCR 0x0aa001
34
35#define PCR_HILO_SELECT 0x80000000U
36#define PCR_SPEED_FULL 0x00000000U
37#define PCR_SPEED_HALF 0x00020000U
38#define PCR_SPEED_QUARTER 0x00040000U
39#define PCR_SPEED_MASK 0x000e0000U
40#define PCR_SPEED_SHIFT 17
41#define PCR_FREQ_REQ_VALID 0x00010000U
42#define PCR_VOLT_REQ_VALID 0x00008000U
43#define PCR_TARGET_TIME_MASK 0x00006000U
44#define PCR_STATLAT_MASK 0x00001f00U
45#define PCR_SNOOPLAT_MASK 0x000000f0U
46#define PCR_SNOOPACC_MASK 0x0000000fU
47
48#define SCOM_PSR 0x408001
49
50#define PSR_CMD_RECEIVED 0x2000000000000000U
51#define PSR_CMD_COMPLETED 0x1000000000000000U
52#define PSR_CUR_SPEED_MASK 0x0300000000000000U
53#define PSR_CUR_SPEED_SHIFT (56)
54
55
56
57
58#define CPUFREQ_HIGH 0
59#define CPUFREQ_LOW 1
60
61static struct cpufreq_frequency_table maple_cpu_freqs[] = {
62 {CPUFREQ_HIGH, 0},
63 {CPUFREQ_LOW, 0},
64 {0, CPUFREQ_TABLE_END},
65};
66
67static struct freq_attr *maple_cpu_freqs_attr[] = {
68 &cpufreq_freq_attr_scaling_available_freqs,
69 NULL,
70};
71
72
73
74
75static int maple_pmode_cur;
76
77static DEFINE_MUTEX(maple_switch_mutex);
78
79static const u32 *maple_pmode_data;
80static int maple_pmode_max;
81
82
83
84
85static int maple_scom_switch_freq(int speed_mode)
86{
87 unsigned long flags;
88 int to;
89
90 local_irq_save(flags);
91
92
93 scom970_write(SCOM_PCR, 0);
94
95 scom970_write(SCOM_PCR, PCR_HILO_SELECT | 0);
96
97 scom970_write(SCOM_PCR, PCR_HILO_SELECT |
98 maple_pmode_data[speed_mode]);
99
100
101 for (to = 0; to < 10; to++) {
102 unsigned long psr = scom970_read(SCOM_PSR);
103
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);
112 }
113
114 local_irq_restore(flags);
115
116 maple_pmode_cur = speed_mode;
117 ppc_proc_freq = maple_cpu_freqs[speed_mode].frequency * 1000ul;
118
119 return 0;
120}
121
122static int maple_scom_query_freq(void)
123{
124 unsigned long psr = scom970_read(SCOM_PSR);
125 int i;
126
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;
132}
133
134
135
136
137
138static int maple_cpufreq_verify(struct cpufreq_policy *policy)
139{
140 return cpufreq_frequency_table_verify(policy, maple_cpu_freqs);
141}
142
143static int maple_cpufreq_target(struct cpufreq_policy *policy,
144 unsigned int target_freq, unsigned int relation)
145{
146 unsigned int newstate = 0;
147 struct cpufreq_freqs freqs;
148 int rc;
149
150 if (cpufreq_frequency_table_target(policy, maple_cpu_freqs,
151 target_freq, relation, &newstate))
152 return -EINVAL;
153
154 if (maple_pmode_cur == newstate)
155 return 0;
156
157 mutex_lock(&maple_switch_mutex);
158
159 freqs.old = maple_cpu_freqs[maple_pmode_cur].frequency;
160 freqs.new = maple_cpu_freqs[newstate].frequency;
161
162 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
163 rc = maple_scom_switch_freq(newstate);
164 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
165
166 mutex_unlock(&maple_switch_mutex);
167
168 return rc;
169}
170
171static unsigned int maple_cpufreq_get_speed(unsigned int cpu)
172{
173 return maple_cpu_freqs[maple_pmode_cur].frequency;
174}
175
176static int maple_cpufreq_cpu_init(struct cpufreq_policy *policy)
177{
178 policy->cpuinfo.transition_latency = 12000;
179 policy->cur = maple_cpu_freqs[maple_scom_query_freq()].frequency;
180
181
182
183 cpumask_setall(policy->cpus);
184 cpufreq_frequency_table_get_attr(maple_cpu_freqs, policy->cpu);
185
186 return cpufreq_frequency_table_cpuinfo(policy,
187 maple_cpu_freqs);
188}
189
190
191static struct cpufreq_driver maple_cpufreq_driver = {
192 .name = "maple",
193 .owner = THIS_MODULE,
194 .flags = CPUFREQ_CONST_LOOPS,
195 .init = maple_cpufreq_cpu_init,
196 .verify = maple_cpufreq_verify,
197 .target = maple_cpufreq_target,
198 .get = maple_cpufreq_get_speed,
199 .attr = maple_cpu_freqs_attr,
200};
201
202static int __init maple_cpufreq_init(void)
203{
204 struct device_node *cpus;
205 struct device_node *cpunode;
206 unsigned int psize;
207 unsigned long max_freq;
208 const u32 *valp;
209 u32 pvr_hi;
210 int rc = -ENODEV;
211
212
213
214
215
216 if (!of_machine_is_compatible("Momentum,Maple") &&
217 !of_machine_is_compatible("Momentum,Apache"))
218 return 0;
219
220 cpus = of_find_node_by_path("/cpus");
221 if (cpus == NULL) {
222 DBG("No /cpus node !\n");
223 return -ENODEV;
224 }
225
226
227 for (cpunode = NULL;
228 (cpunode = of_get_next_child(cpus, cpunode)) != NULL;) {
229 const u32 *reg = of_get_property(cpunode, "reg", NULL);
230 if (reg == NULL || (*reg) != 0)
231 continue;
232 if (!strcmp(cpunode->type, "cpu"))
233 break;
234 }
235 if (cpunode == NULL) {
236 printk(KERN_ERR "cpufreq: Can't find any CPU 0 node\n");
237 goto bail_cpus;
238 }
239
240
241
242 pvr_hi = PVR_VER(mfspr(SPRN_PVR));
243 if (pvr_hi != 0x3c && pvr_hi != 0x44) {
244 printk(KERN_ERR "cpufreq: Unsupported CPU version (%x)\n",
245 pvr_hi);
246 goto bail_noprops;
247 }
248
249
250
251
252
253
254
255 maple_pmode_data = of_get_property(cpunode, "power-mode-data", &psize);
256 if (!maple_pmode_data) {
257 DBG("No power-mode-data !\n");
258 goto bail_noprops;
259 }
260 maple_pmode_max = psize / sizeof(u32) - 1;
261
262
263
264
265
266
267
268
269 valp = of_get_property(cpunode, "clock-frequency", NULL);
270 if (!valp)
271 return -ENODEV;
272 max_freq = (*valp)/1000;
273 maple_cpu_freqs[0].frequency = max_freq;
274 maple_cpu_freqs[1].frequency = max_freq/2;
275
276
277
278
279
280 msleep(10);
281 maple_pmode_cur = -1;
282 maple_scom_switch_freq(maple_scom_query_freq());
283
284 printk(KERN_INFO "Registering Maple CPU frequency driver\n");
285 printk(KERN_INFO "Low: %d Mhz, High: %d Mhz, Cur: %d MHz\n",
286 maple_cpu_freqs[1].frequency/1000,
287 maple_cpu_freqs[0].frequency/1000,
288 maple_cpu_freqs[maple_pmode_cur].frequency/1000);
289
290 rc = cpufreq_register_driver(&maple_cpufreq_driver);
291
292 of_node_put(cpunode);
293 of_node_put(cpus);
294
295 return rc;
296
297bail_noprops:
298 of_node_put(cpunode);
299bail_cpus:
300 of_node_put(cpus);
301
302 return rc;
303}
304
305module_init(maple_cpufreq_init);
306
307
308MODULE_LICENSE("GPL");
309