1 /* us2e_cpufreq.c: UltraSPARC-IIe 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/delay.h>
17 #include <linux/init.h>
20 #include <asm/timer.h>
22 static struct cpufreq_driver
*cpufreq_us2e_driver
;
24 struct us2e_freq_percpu_info
{
25 struct cpufreq_frequency_table table
[6];
28 /* Indexed by cpu number. */
29 static struct us2e_freq_percpu_info
*us2e_freq_table
;
31 #define HBIRD_MEM_CNTL0_ADDR 0x1fe0000f010UL
32 #define HBIRD_ESTAR_MODE_ADDR 0x1fe0000f080UL
34 /* UltraSPARC-IIe has five dividers: 1, 2, 4, 6, and 8. These are controlled
35 * in the ESTAR mode control register.
37 #define ESTAR_MODE_DIV_1 0x0000000000000000UL
38 #define ESTAR_MODE_DIV_2 0x0000000000000001UL
39 #define ESTAR_MODE_DIV_4 0x0000000000000003UL
40 #define ESTAR_MODE_DIV_6 0x0000000000000002UL
41 #define ESTAR_MODE_DIV_8 0x0000000000000004UL
42 #define ESTAR_MODE_DIV_MASK 0x0000000000000007UL
44 #define MCTRL0_SREFRESH_ENAB 0x0000000000010000UL
45 #define MCTRL0_REFR_COUNT_MASK 0x0000000000007f00UL
46 #define MCTRL0_REFR_COUNT_SHIFT 8
47 #define MCTRL0_REFR_INTERVAL 7800
48 #define MCTRL0_REFR_CLKS_P_CNT 64
50 static unsigned long read_hbreg(unsigned long addr
)
54 __asm__
__volatile__("ldxa [%1] %2, %0"
56 : "r" (addr
), "i" (ASI_PHYS_BYPASS_EC_E
));
60 static void write_hbreg(unsigned long addr
, unsigned long val
)
62 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
65 : "r" (val
), "r" (addr
), "i" (ASI_PHYS_BYPASS_EC_E
)
67 if (addr
== HBIRD_ESTAR_MODE_ADDR
) {
68 /* Need to wait 16 clock cycles for the PLL to lock. */
73 static void self_refresh_ctl(int enable
)
75 unsigned long mctrl
= read_hbreg(HBIRD_MEM_CNTL0_ADDR
);
78 mctrl
|= MCTRL0_SREFRESH_ENAB
;
80 mctrl
&= ~MCTRL0_SREFRESH_ENAB
;
81 write_hbreg(HBIRD_MEM_CNTL0_ADDR
, mctrl
);
82 (void) read_hbreg(HBIRD_MEM_CNTL0_ADDR
);
85 static void frob_mem_refresh(int cpu_slowing_down
,
86 unsigned long clock_tick
,
87 unsigned long old_divisor
, unsigned long divisor
)
89 unsigned long old_refr_count
, refr_count
, mctrl
;
91 refr_count
= (clock_tick
* MCTRL0_REFR_INTERVAL
);
92 refr_count
/= (MCTRL0_REFR_CLKS_P_CNT
* divisor
* 1000000000UL);
94 mctrl
= read_hbreg(HBIRD_MEM_CNTL0_ADDR
);
95 old_refr_count
= (mctrl
& MCTRL0_REFR_COUNT_MASK
)
96 >> MCTRL0_REFR_COUNT_SHIFT
;
98 mctrl
&= ~MCTRL0_REFR_COUNT_MASK
;
99 mctrl
|= refr_count
<< MCTRL0_REFR_COUNT_SHIFT
;
100 write_hbreg(HBIRD_MEM_CNTL0_ADDR
, mctrl
);
101 mctrl
= read_hbreg(HBIRD_MEM_CNTL0_ADDR
);
103 if (cpu_slowing_down
&& !(mctrl
& MCTRL0_SREFRESH_ENAB
)) {
106 /* We have to wait for both refresh counts (old
107 * and new) to go to zero.
109 usecs
= (MCTRL0_REFR_CLKS_P_CNT
*
110 (refr_count
+ old_refr_count
) *
112 old_divisor
) / clock_tick
;
117 static void us2e_transition(unsigned long estar
, unsigned long new_bits
,
118 unsigned long clock_tick
,
119 unsigned long old_divisor
, unsigned long divisor
)
121 estar
&= ~ESTAR_MODE_DIV_MASK
;
123 /* This is based upon the state transition diagram in the IIe manual. */
124 if (old_divisor
== 2 && divisor
== 1) {
126 write_hbreg(HBIRD_ESTAR_MODE_ADDR
, estar
| new_bits
);
127 frob_mem_refresh(0, clock_tick
, old_divisor
, divisor
);
128 } else if (old_divisor
== 1 && divisor
== 2) {
129 frob_mem_refresh(1, clock_tick
, old_divisor
, divisor
);
130 write_hbreg(HBIRD_ESTAR_MODE_ADDR
, estar
| new_bits
);
132 } else if (old_divisor
== 1 && divisor
> 2) {
133 us2e_transition(estar
, ESTAR_MODE_DIV_2
, clock_tick
,
135 us2e_transition(estar
, new_bits
, clock_tick
,
137 } else if (old_divisor
> 2 && divisor
== 1) {
138 us2e_transition(estar
, ESTAR_MODE_DIV_2
, clock_tick
,
140 us2e_transition(estar
, new_bits
, clock_tick
,
142 } else if (old_divisor
< divisor
) {
143 frob_mem_refresh(0, clock_tick
, old_divisor
, divisor
);
144 write_hbreg(HBIRD_ESTAR_MODE_ADDR
, estar
| new_bits
);
145 } else if (old_divisor
> divisor
) {
146 write_hbreg(HBIRD_ESTAR_MODE_ADDR
, estar
| new_bits
);
147 frob_mem_refresh(1, clock_tick
, old_divisor
, divisor
);
153 static unsigned long index_to_estar_mode(unsigned int index
)
157 return ESTAR_MODE_DIV_1
;
160 return ESTAR_MODE_DIV_2
;
163 return ESTAR_MODE_DIV_4
;
166 return ESTAR_MODE_DIV_6
;
169 return ESTAR_MODE_DIV_8
;
176 static unsigned long index_to_divisor(unsigned int index
)
199 static unsigned long estar_to_divisor(unsigned long estar
)
203 switch (estar
& ESTAR_MODE_DIV_MASK
) {
204 case ESTAR_MODE_DIV_1
:
207 case ESTAR_MODE_DIV_2
:
210 case ESTAR_MODE_DIV_4
:
213 case ESTAR_MODE_DIV_6
:
216 case ESTAR_MODE_DIV_8
:
226 static void __us2e_freq_get(void *arg
)
228 unsigned long *estar
= arg
;
230 *estar
= read_hbreg(HBIRD_ESTAR_MODE_ADDR
);
233 static unsigned int us2e_freq_get(unsigned int cpu
)
235 unsigned long clock_tick
, estar
;
237 clock_tick
= sparc64_get_clock_tick(cpu
) / 1000;
238 if (smp_call_function_single(cpu
, __us2e_freq_get
, &estar
, 1))
241 return clock_tick
/ estar_to_divisor(estar
);
244 static void __us2e_freq_target(void *arg
)
246 unsigned int cpu
= smp_processor_id();
247 unsigned int *index
= arg
;
248 unsigned long new_bits
, new_freq
;
249 unsigned long clock_tick
, divisor
, old_divisor
, estar
;
251 new_freq
= clock_tick
= sparc64_get_clock_tick(cpu
) / 1000;
252 new_bits
= index_to_estar_mode(*index
);
253 divisor
= index_to_divisor(*index
);
256 estar
= read_hbreg(HBIRD_ESTAR_MODE_ADDR
);
258 old_divisor
= estar_to_divisor(estar
);
260 if (old_divisor
!= divisor
) {
261 us2e_transition(estar
, new_bits
, clock_tick
* 1000,
262 old_divisor
, divisor
);
266 static int us2e_freq_target(struct cpufreq_policy
*policy
, unsigned int index
)
268 unsigned int cpu
= policy
->cpu
;
270 return smp_call_function_single(cpu
, __us2e_freq_target
, &index
, 1);
273 static int __init
us2e_freq_cpu_init(struct cpufreq_policy
*policy
)
275 unsigned int cpu
= policy
->cpu
;
276 unsigned long clock_tick
= sparc64_get_clock_tick(cpu
) / 1000;
277 struct cpufreq_frequency_table
*table
=
278 &us2e_freq_table
[cpu
].table
[0];
280 table
[0].driver_data
= 0;
281 table
[0].frequency
= clock_tick
/ 1;
282 table
[1].driver_data
= 1;
283 table
[1].frequency
= clock_tick
/ 2;
284 table
[2].driver_data
= 2;
285 table
[2].frequency
= clock_tick
/ 4;
286 table
[2].driver_data
= 3;
287 table
[2].frequency
= clock_tick
/ 6;
288 table
[2].driver_data
= 4;
289 table
[2].frequency
= clock_tick
/ 8;
290 table
[2].driver_data
= 5;
291 table
[3].frequency
= CPUFREQ_TABLE_END
;
293 policy
->cpuinfo
.transition_latency
= 0;
294 policy
->cur
= clock_tick
;
295 policy
->freq_table
= table
;
300 static int us2e_freq_cpu_exit(struct cpufreq_policy
*policy
)
302 if (cpufreq_us2e_driver
)
303 us2e_freq_target(policy
, 0);
308 static int __init
us2e_freq_init(void)
310 unsigned long manuf
, impl
, ver
;
313 if (tlb_type
!= spitfire
)
316 __asm__("rdpr %%ver, %0" : "=r" (ver
));
317 manuf
= ((ver
>> 48) & 0xffff);
318 impl
= ((ver
>> 32) & 0xffff);
320 if (manuf
== 0x17 && impl
== 0x13) {
321 struct cpufreq_driver
*driver
;
324 driver
= kzalloc(sizeof(*driver
), GFP_KERNEL
);
328 us2e_freq_table
= kzalloc((NR_CPUS
* sizeof(*us2e_freq_table
)),
330 if (!us2e_freq_table
)
333 driver
->init
= us2e_freq_cpu_init
;
334 driver
->verify
= cpufreq_generic_frequency_table_verify
;
335 driver
->target_index
= us2e_freq_target
;
336 driver
->get
= us2e_freq_get
;
337 driver
->exit
= us2e_freq_cpu_exit
;
338 strcpy(driver
->name
, "UltraSPARC-IIe");
340 cpufreq_us2e_driver
= driver
;
341 ret
= cpufreq_register_driver(driver
);
350 cpufreq_us2e_driver
= NULL
;
352 kfree(us2e_freq_table
);
353 us2e_freq_table
= NULL
;
360 static void __exit
us2e_freq_exit(void)
362 if (cpufreq_us2e_driver
) {
363 cpufreq_unregister_driver(cpufreq_us2e_driver
);
364 kfree(cpufreq_us2e_driver
);
365 cpufreq_us2e_driver
= NULL
;
366 kfree(us2e_freq_table
);
367 us2e_freq_table
= NULL
;
371 MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
372 MODULE_DESCRIPTION("cpufreq driver for UltraSPARC-IIe");
373 MODULE_LICENSE("GPL");
375 module_init(us2e_freq_init
);
376 module_exit(us2e_freq_exit
);