Pull platform-drivers into test branch
[linux-2.6] / arch / powerpc / platforms / cell / cbe_cpufreq.c
1 /*
2  * cpufreq driver for the cell processor
3  *
4  * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
5  *
6  * Author: Christian Krafft <krafft@de.ibm.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #include <linux/cpufreq.h>
24 #include <linux/timer.h>
25
26 #include <asm/hw_irq.h>
27 #include <asm/io.h>
28 #include <asm/processor.h>
29 #include <asm/prom.h>
30 #include <asm/time.h>
31
32 #include "cbe_regs.h"
33
34 static DEFINE_MUTEX(cbe_switch_mutex);
35
36
37 /* the CBE supports an 8 step frequency scaling */
38 static struct cpufreq_frequency_table cbe_freqs[] = {
39         {1,     0},
40         {2,     0},
41         {3,     0},
42         {4,     0},
43         {5,     0},
44         {6,     0},
45         {8,     0},
46         {10,    0},
47         {0,     CPUFREQ_TABLE_END},
48 };
49
50 /* to write to MIC register */
51 static u64 MIC_Slow_Fast_Timer_table[] = {
52         [0 ... 7] = 0x007fc00000000000ull,
53 };
54
55 /* more values for the MIC */
56 static u64 MIC_Slow_Next_Timer_table[] = {
57         0x0000240000000000ull,
58         0x0000268000000000ull,
59         0x000029C000000000ull,
60         0x00002D0000000000ull,
61         0x0000300000000000ull,
62         0x0000334000000000ull,
63         0x000039C000000000ull,
64         0x00003FC000000000ull,
65 };
66
67 /*
68  * hardware specific functions
69  */
70
71 static int get_pmode(int cpu)
72 {
73         int ret;
74         struct cbe_pmd_regs __iomem *pmd_regs;
75
76         pmd_regs = cbe_get_cpu_pmd_regs(cpu);
77         ret = in_be64(&pmd_regs->pmsr) & 0x07;
78
79         return ret;
80 }
81
82 static int set_pmode(int cpu, unsigned int pmode)
83 {
84         struct cbe_pmd_regs __iomem *pmd_regs;
85         struct cbe_mic_tm_regs __iomem *mic_tm_regs;
86         u64 flags;
87         u64 value;
88
89         local_irq_save(flags);
90
91         mic_tm_regs = cbe_get_cpu_mic_tm_regs(cpu);
92         pmd_regs = cbe_get_cpu_pmd_regs(cpu);
93
94         pr_debug("pm register is mapped at %p\n", &pmd_regs->pmcr);
95         pr_debug("mic register is mapped at %p\n", &mic_tm_regs->slow_fast_timer_0);
96
97         out_be64(&mic_tm_regs->slow_fast_timer_0, MIC_Slow_Fast_Timer_table[pmode]);
98         out_be64(&mic_tm_regs->slow_fast_timer_1, MIC_Slow_Fast_Timer_table[pmode]);
99
100         out_be64(&mic_tm_regs->slow_next_timer_0, MIC_Slow_Next_Timer_table[pmode]);
101         out_be64(&mic_tm_regs->slow_next_timer_1, MIC_Slow_Next_Timer_table[pmode]);
102
103         value = in_be64(&pmd_regs->pmcr);
104         /* set bits to zero */
105         value &= 0xFFFFFFFFFFFFFFF8ull;
106         /* set bits to next pmode */
107         value |= pmode;
108
109         out_be64(&pmd_regs->pmcr, value);
110
111         /* wait until new pmode appears in status register */
112         value = in_be64(&pmd_regs->pmsr) & 0x07;
113         while(value != pmode) {
114                 cpu_relax();
115                 value = in_be64(&pmd_regs->pmsr) & 0x07;
116         }
117
118         local_irq_restore(flags);
119
120         return 0;
121 }
122
123 /*
124  * cpufreq functions
125  */
126
127 static int cbe_cpufreq_cpu_init (struct cpufreq_policy *policy)
128 {
129         u32 *max_freq;
130         int i, cur_pmode;
131         struct device_node *cpu;
132
133         cpu = of_get_cpu_node(policy->cpu, NULL);
134
135         if(!cpu)
136                 return -ENODEV;
137
138         pr_debug("init cpufreq on CPU %d\n", policy->cpu);
139
140         max_freq = (u32*) get_property(cpu, "clock-frequency", NULL);
141
142         if(!max_freq)
143                 return -EINVAL;
144
145         // we need the freq in kHz
146         *max_freq /= 1000;
147
148         pr_debug("max clock-frequency is at %u kHz\n", *max_freq);
149         pr_debug("initializing frequency table\n");
150
151         // initialize frequency table
152         for (i=0; cbe_freqs[i].frequency!=CPUFREQ_TABLE_END; i++) {
153                 cbe_freqs[i].frequency = *max_freq / cbe_freqs[i].index;
154                 pr_debug("%d: %d\n", i, cbe_freqs[i].frequency);
155         }
156
157         policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
158         /* if DEBUG is enabled set_pmode() measures the correct latency of a transition */
159         policy->cpuinfo.transition_latency = 25000;
160
161         cur_pmode = get_pmode(policy->cpu);
162         pr_debug("current pmode is at %d\n",cur_pmode);
163
164         policy->cur = cbe_freqs[cur_pmode].frequency;
165
166 #ifdef CONFIG_SMP
167         policy->cpus = cpu_sibling_map[policy->cpu];
168 #endif
169
170         cpufreq_frequency_table_get_attr (cbe_freqs, policy->cpu);
171
172         /* this ensures that policy->cpuinfo_min and policy->cpuinfo_max are set correctly */
173         return cpufreq_frequency_table_cpuinfo (policy, cbe_freqs);
174 }
175
176 static int cbe_cpufreq_cpu_exit(struct cpufreq_policy *policy)
177 {
178         cpufreq_frequency_table_put_attr(policy->cpu);
179         return 0;
180 }
181
182 static int cbe_cpufreq_verify(struct cpufreq_policy *policy)
183 {
184         return cpufreq_frequency_table_verify(policy, cbe_freqs);
185 }
186
187
188 static int cbe_cpufreq_target(struct cpufreq_policy *policy, unsigned int target_freq,
189                             unsigned int relation)
190 {
191         int rc;
192         struct cpufreq_freqs freqs;
193         int cbe_pmode_new;
194
195         cpufreq_frequency_table_target(policy,
196                                        cbe_freqs,
197                                        target_freq,
198                                        relation,
199                                        &cbe_pmode_new);
200
201         freqs.old = policy->cur;
202         freqs.new = cbe_freqs[cbe_pmode_new].frequency;
203         freqs.cpu = policy->cpu;
204
205         mutex_lock (&cbe_switch_mutex);
206         cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
207
208         pr_debug("setting frequency for cpu %d to %d kHz, 1/%d of max frequency\n",
209                  policy->cpu,
210                  cbe_freqs[cbe_pmode_new].frequency,
211                  cbe_freqs[cbe_pmode_new].index);
212
213         rc = set_pmode(policy->cpu, cbe_pmode_new);
214         cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
215         mutex_unlock(&cbe_switch_mutex);
216
217         return rc;
218 }
219
220 static struct cpufreq_driver cbe_cpufreq_driver = {
221         .verify         = cbe_cpufreq_verify,
222         .target         = cbe_cpufreq_target,
223         .init           = cbe_cpufreq_cpu_init,
224         .exit           = cbe_cpufreq_cpu_exit,
225         .name           = "cbe-cpufreq",
226         .owner          = THIS_MODULE,
227         .flags          = CPUFREQ_CONST_LOOPS,
228 };
229
230 /*
231  * module init and destoy
232  */
233
234 static int __init cbe_cpufreq_init(void)
235 {
236         return cpufreq_register_driver(&cbe_cpufreq_driver);
237 }
238
239 static void __exit cbe_cpufreq_exit(void)
240 {
241         cpufreq_unregister_driver(&cbe_cpufreq_driver);
242 }
243
244 module_init(cbe_cpufreq_init);
245 module_exit(cbe_cpufreq_exit);
246
247 MODULE_LICENSE("GPL");
248 MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");