blob: 9c5d63b7e76c4215bc2735c503d7f30ff21032a7 [file] [log] [blame]
Christian Krafft36ca4ba2006-10-24 18:39:45 +02001/*
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
34static DEFINE_MUTEX(cbe_switch_mutex);
35
36
37/* the CBE supports an 8 step frequency scaling */
38static 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 */
51static u64 MIC_Slow_Fast_Timer_table[] = {
52 [0 ... 7] = 0x007fc00000000000ull,
53};
54
55/* more values for the MIC */
56static 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
71static 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
82static 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
127static int cbe_cpufreq_cpu_init (struct cpufreq_policy *policy)
128{
Stephen Rothwell9c1a2ba2007-04-03 22:50:59 +1000129 const u32 *max_freqp;
130 u32 max_freq;
Christian Krafft36ca4ba2006-10-24 18:39:45 +0200131 int i, cur_pmode;
132 struct device_node *cpu;
133
134 cpu = of_get_cpu_node(policy->cpu, NULL);
135
136 if(!cpu)
137 return -ENODEV;
138
139 pr_debug("init cpufreq on CPU %d\n", policy->cpu);
140
Stephen Rothwell9c1a2ba2007-04-03 22:50:59 +1000141 max_freqp = of_get_property(cpu, "clock-frequency", NULL);
Christian Krafft36ca4ba2006-10-24 18:39:45 +0200142
Stephen Rothwell9c1a2ba2007-04-03 22:50:59 +1000143 if (!max_freqp)
Christian Krafft36ca4ba2006-10-24 18:39:45 +0200144 return -EINVAL;
145
146 // we need the freq in kHz
Stephen Rothwell9c1a2ba2007-04-03 22:50:59 +1000147 max_freq = *max_freqp / 1000;
Christian Krafft36ca4ba2006-10-24 18:39:45 +0200148
Stephen Rothwell9c1a2ba2007-04-03 22:50:59 +1000149 pr_debug("max clock-frequency is at %u kHz\n", max_freq);
Christian Krafft36ca4ba2006-10-24 18:39:45 +0200150 pr_debug("initializing frequency table\n");
151
152 // initialize frequency table
153 for (i=0; cbe_freqs[i].frequency!=CPUFREQ_TABLE_END; i++) {
Stephen Rothwell9c1a2ba2007-04-03 22:50:59 +1000154 cbe_freqs[i].frequency = max_freq / cbe_freqs[i].index;
Christian Krafft36ca4ba2006-10-24 18:39:45 +0200155 pr_debug("%d: %d\n", i, cbe_freqs[i].frequency);
156 }
157
158 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
159 /* if DEBUG is enabled set_pmode() measures the correct latency of a transition */
160 policy->cpuinfo.transition_latency = 25000;
161
162 cur_pmode = get_pmode(policy->cpu);
163 pr_debug("current pmode is at %d\n",cur_pmode);
164
165 policy->cur = cbe_freqs[cur_pmode].frequency;
166
167#ifdef CONFIG_SMP
168 policy->cpus = cpu_sibling_map[policy->cpu];
169#endif
170
171 cpufreq_frequency_table_get_attr (cbe_freqs, policy->cpu);
172
173 /* this ensures that policy->cpuinfo_min and policy->cpuinfo_max are set correctly */
174 return cpufreq_frequency_table_cpuinfo (policy, cbe_freqs);
175}
176
177static int cbe_cpufreq_cpu_exit(struct cpufreq_policy *policy)
178{
179 cpufreq_frequency_table_put_attr(policy->cpu);
180 return 0;
181}
182
183static int cbe_cpufreq_verify(struct cpufreq_policy *policy)
184{
185 return cpufreq_frequency_table_verify(policy, cbe_freqs);
186}
187
188
189static int cbe_cpufreq_target(struct cpufreq_policy *policy, unsigned int target_freq,
190 unsigned int relation)
191{
192 int rc;
193 struct cpufreq_freqs freqs;
194 int cbe_pmode_new;
195
196 cpufreq_frequency_table_target(policy,
197 cbe_freqs,
198 target_freq,
199 relation,
200 &cbe_pmode_new);
201
202 freqs.old = policy->cur;
203 freqs.new = cbe_freqs[cbe_pmode_new].frequency;
204 freqs.cpu = policy->cpu;
205
206 mutex_lock (&cbe_switch_mutex);
207 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
208
209 pr_debug("setting frequency for cpu %d to %d kHz, 1/%d of max frequency\n",
210 policy->cpu,
211 cbe_freqs[cbe_pmode_new].frequency,
212 cbe_freqs[cbe_pmode_new].index);
213
214 rc = set_pmode(policy->cpu, cbe_pmode_new);
215 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
216 mutex_unlock(&cbe_switch_mutex);
217
218 return rc;
219}
220
221static struct cpufreq_driver cbe_cpufreq_driver = {
222 .verify = cbe_cpufreq_verify,
223 .target = cbe_cpufreq_target,
224 .init = cbe_cpufreq_cpu_init,
225 .exit = cbe_cpufreq_cpu_exit,
226 .name = "cbe-cpufreq",
227 .owner = THIS_MODULE,
228 .flags = CPUFREQ_CONST_LOOPS,
229};
230
231/*
232 * module init and destoy
233 */
234
235static int __init cbe_cpufreq_init(void)
236{
237 return cpufreq_register_driver(&cbe_cpufreq_driver);
238}
239
240static void __exit cbe_cpufreq_exit(void)
241{
242 cpufreq_unregister_driver(&cbe_cpufreq_driver);
243}
244
245module_init(cbe_cpufreq_init);
246module_exit(cbe_cpufreq_exit);
247
248MODULE_LICENSE("GPL");
249MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");