1 /* us3_cpufreq.c: UltraSPARC-III cpu frequency support
3 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
5 * Many thanks to Dominik Brodowski for fixing up the cpufreq
6 * infrastructure in order to make this driver easier to implement.
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/sched.h>
12 #include <linux/smp.h>
13 #include <linux/cpufreq.h>
14 #include <linux/threads.h>
15 #include <linux/slab.h>
16 #include <linux/init.h>
19 #include <asm/timer.h>
21 static struct cpufreq_driver
*cpufreq_us3_driver
;
23 struct us3_freq_percpu_info
{
24 struct cpufreq_frequency_table table
[4];
27 /* Indexed by cpu number. */
28 static struct us3_freq_percpu_info
*us3_freq_table
;
30 /* UltraSPARC-III has three dividers: 1, 2, and 32. These are controlled
31 * in the Safari config register.
33 #define SAFARI_CFG_DIV_1 0x0000000000000000UL
34 #define SAFARI_CFG_DIV_2 0x0000000040000000UL
35 #define SAFARI_CFG_DIV_32 0x0000000080000000UL
36 #define SAFARI_CFG_DIV_MASK 0x00000000C0000000UL
38 static unsigned long read_safari_cfg(void)
42 __asm__
__volatile__("ldxa [%%g0] %1, %0"
44 : "i" (ASI_SAFARI_CONFIG
));
48 static void write_safari_cfg(unsigned long val
)
50 __asm__
__volatile__("stxa %0, [%%g0] %1\n\t"
53 : "r" (val
), "i" (ASI_SAFARI_CONFIG
)
57 static unsigned long get_current_freq(unsigned int cpu
, unsigned long safari_cfg
)
59 unsigned long clock_tick
= sparc64_get_clock_tick(cpu
) / 1000;
62 switch (safari_cfg
& SAFARI_CFG_DIV_MASK
) {
63 case SAFARI_CFG_DIV_1
:
66 case SAFARI_CFG_DIV_2
:
69 case SAFARI_CFG_DIV_32
:
70 ret
= clock_tick
/ 32;
79 static unsigned int us3_freq_get(unsigned int cpu
)
81 cpumask_t cpus_allowed
;
85 cpumask_copy(&cpus_allowed
, tsk_cpus_allowed(current
));
86 set_cpus_allowed_ptr(current
, cpumask_of(cpu
));
88 reg
= read_safari_cfg();
89 ret
= get_current_freq(cpu
, reg
);
91 set_cpus_allowed_ptr(current
, &cpus_allowed
);
96 static void us3_set_cpu_divider_index(struct cpufreq_policy
*policy
,
99 unsigned int cpu
= policy
->cpu
;
100 unsigned long new_bits
, new_freq
, reg
;
101 cpumask_t cpus_allowed
;
102 struct cpufreq_freqs freqs
;
104 cpumask_copy(&cpus_allowed
, tsk_cpus_allowed(current
));
105 set_cpus_allowed_ptr(current
, cpumask_of(cpu
));
107 new_freq
= sparc64_get_clock_tick(cpu
) / 1000;
110 new_bits
= SAFARI_CFG_DIV_1
;
114 new_bits
= SAFARI_CFG_DIV_2
;
118 new_bits
= SAFARI_CFG_DIV_32
;
126 reg
= read_safari_cfg();
128 freqs
.old
= get_current_freq(cpu
, reg
);
129 freqs
.new = new_freq
;
130 cpufreq_notify_transition(policy
, &freqs
, CPUFREQ_PRECHANGE
);
132 reg
&= ~SAFARI_CFG_DIV_MASK
;
134 write_safari_cfg(reg
);
136 cpufreq_notify_transition(policy
, &freqs
, CPUFREQ_POSTCHANGE
);
138 set_cpus_allowed_ptr(current
, &cpus_allowed
);
141 static int us3_freq_target(struct cpufreq_policy
*policy
,
142 unsigned int target_freq
,
143 unsigned int relation
)
145 unsigned int new_index
= 0;
147 if (cpufreq_frequency_table_target(policy
,
148 &us3_freq_table
[policy
->cpu
].table
[0],
154 us3_set_cpu_divider_index(policy
, new_index
);
159 static int us3_freq_verify(struct cpufreq_policy
*policy
)
161 return cpufreq_frequency_table_verify(policy
,
162 &us3_freq_table
[policy
->cpu
].table
[0]);
165 static int __init
us3_freq_cpu_init(struct cpufreq_policy
*policy
)
167 unsigned int cpu
= policy
->cpu
;
168 unsigned long clock_tick
= sparc64_get_clock_tick(cpu
) / 1000;
169 struct cpufreq_frequency_table
*table
=
170 &us3_freq_table
[cpu
].table
[0];
172 table
[0].driver_data
= 0;
173 table
[0].frequency
= clock_tick
/ 1;
174 table
[1].driver_data
= 1;
175 table
[1].frequency
= clock_tick
/ 2;
176 table
[2].driver_data
= 2;
177 table
[2].frequency
= clock_tick
/ 32;
178 table
[3].driver_data
= 0;
179 table
[3].frequency
= CPUFREQ_TABLE_END
;
181 policy
->cpuinfo
.transition_latency
= 0;
182 policy
->cur
= clock_tick
;
184 return cpufreq_frequency_table_cpuinfo(policy
, table
);
187 static int us3_freq_cpu_exit(struct cpufreq_policy
*policy
)
189 if (cpufreq_us3_driver
)
190 us3_set_cpu_divider_index(policy
, 0);
195 static int __init
us3_freq_init(void)
197 unsigned long manuf
, impl
, ver
;
200 if (tlb_type
!= cheetah
&& tlb_type
!= cheetah_plus
)
203 __asm__("rdpr %%ver, %0" : "=r" (ver
));
204 manuf
= ((ver
>> 48) & 0xffff);
205 impl
= ((ver
>> 32) & 0xffff);
207 if (manuf
== CHEETAH_MANUF
&&
208 (impl
== CHEETAH_IMPL
||
209 impl
== CHEETAH_PLUS_IMPL
||
210 impl
== JAGUAR_IMPL
||
211 impl
== PANTHER_IMPL
)) {
212 struct cpufreq_driver
*driver
;
215 driver
= kzalloc(sizeof(*driver
), GFP_KERNEL
);
219 us3_freq_table
= kzalloc((NR_CPUS
* sizeof(*us3_freq_table
)),
224 driver
->init
= us3_freq_cpu_init
;
225 driver
->verify
= us3_freq_verify
;
226 driver
->target
= us3_freq_target
;
227 driver
->get
= us3_freq_get
;
228 driver
->exit
= us3_freq_cpu_exit
;
229 strcpy(driver
->name
, "UltraSPARC-III");
231 cpufreq_us3_driver
= driver
;
232 ret
= cpufreq_register_driver(driver
);
241 cpufreq_us3_driver
= NULL
;
243 kfree(us3_freq_table
);
244 us3_freq_table
= NULL
;
251 static void __exit
us3_freq_exit(void)
253 if (cpufreq_us3_driver
) {
254 cpufreq_unregister_driver(cpufreq_us3_driver
);
255 kfree(cpufreq_us3_driver
);
256 cpufreq_us3_driver
= NULL
;
257 kfree(us3_freq_table
);
258 us3_freq_table
= NULL
;
262 MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
263 MODULE_DESCRIPTION("cpufreq driver for UltraSPARC-III");
264 MODULE_LICENSE("GPL");
266 module_init(us3_freq_init
);
267 module_exit(us3_freq_exit
);