2 * arch/sh/kernel/cpufreq.c
4 * cpufreq driver for the SuperH processors.
6 * Copyright (C) 2002, 2003 Paul Mundt
7 * Copyright (C) 2002 M. R. Brown
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
14 #include <linux/types.h>
15 #include <linux/cpufreq.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include <linux/delay.h>
21 #include <linux/smp.h>
23 #include <asm/processor.h>
24 #include <asm/watchdog.h>
29 * For SuperH, each policy change requires that we change the IFC, BFC, and
30 * PFC at the same time. Here we define sane values that won't trash the
33 * Note the max set is computed at runtime, we use the divisors that we booted
34 * with to setup our maximum operating frequencies.
41 #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH2)
42 { 0, 0, 0 }, /* not implemented yet */
43 #elif defined(CONFIG_CPU_SH4)
44 { 4, 8, 8 }, /* min - IFC: 1/4, BFC: 1/8, PFC: 1/8 */
45 { 1, 2, 2 }, /* max - IFC: 1, BFC: 1/2, PFC: 1/2 */
49 #define NR_CLOCK_SETS (sizeof(clock_sets) / sizeof(struct clock_set))
50 #define MIN_CLOCK_SET 0
51 #define MAX_CLOCK_SET (NR_CLOCK_SETS - 1)
54 * For the time being, we only support two frequencies, which in turn are
55 * aimed at the POWERSAVE and PERFORMANCE policies, which in turn are derived
56 * directly from the respective min/max clock sets. Technically we could
57 * support a wider range of frequencies, but these vary far too much for each
58 * CPU subtype (and we'd have to construct a frequency table for each subtype).
60 * Maybe something to implement in the future..
65 static struct cpufreq_frequency_table sh_freqs[] = {
68 { 0, CPUFREQ_TABLE_END },
71 static void sh_cpufreq_update_clocks(unsigned int set)
73 current_cpu_data.cpu_clock = current_cpu_data.master_clock / clock_sets[set].ifc;
74 current_cpu_data.bus_clock = current_cpu_data.master_clock / clock_sets[set].bfc;
75 current_cpu_data.module_clock = current_cpu_data.master_clock / clock_sets[set].pfc;
76 current_cpu_data.loops_per_jiffy = loops_per_jiffy;
79 /* XXX: This needs to be split out per CPU and CPU subtype. */
81 * Here we notify other drivers of the proposed change and the final change.
83 static int sh_cpufreq_setstate(unsigned int cpu, unsigned int set)
85 unsigned short frqcr = ctrl_inw(FRQCR);
86 unsigned long cpus_allowed;
87 struct cpufreq_freqs freqs;
88 int allowable_cpu_map;
93 cpus_allowed = current->cpus_allowed;
94 allowable_cpu_map = 1 << cpu;
95 set_cpus_allowed(current, allowable_cpu_map);
97 BUG_ON(!(allowable_cpu_map & (1 << smp_processor_id())));
100 freqs.old = current_cpu_data.cpu_clock / 1000;
101 freqs.new = (current_cpu_data.master_clock / clock_sets[set].ifc) / 1000;
103 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
104 #if defined(CONFIG_CPU_SH3)
105 frqcr |= (newstate & 0x4000) << 14;
106 frqcr |= (newstate & 0x000c) << 2;
107 #elif defined(CONFIG_CPU_SH4)
109 * FRQCR.PLL2EN is 1, we need to allow the PLL to stabilize by
110 * initializing the WDT.
112 if (frqcr & (1 << 9)) {
116 * Set the overflow period to the highest available,
117 * in this case a 1/4096 division ratio yields a 5.25ms
118 * overflow period. See asm-sh/watchdog.h for more
119 * information and a range of other divisors.
121 csr = sh_wdt_read_csr();
122 csr |= WTCSR_CKS_4096;
123 sh_wdt_write_csr(csr);
127 frqcr &= 0x0e00; /* Clear ifc, bfc, pfc */
128 frqcr |= get_ifc_value(clock_sets[set].ifc) << 6;
129 frqcr |= get_bfc_value(clock_sets[set].bfc) << 3;
130 frqcr |= get_pfc_value(clock_sets[set].pfc);
132 ctrl_outw(frqcr, FRQCR);
133 sh_cpufreq_update_clocks(set);
135 set_cpus_allowed(current, cpus_allowed);
136 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
141 static int sh_cpufreq_cpu_init(struct cpufreq_policy *policy)
143 unsigned int min_freq, max_freq;
144 unsigned int ifc, bfc, pfc;
146 if (!cpu_online(policy->cpu))
149 /* Update our maximum clock set */
150 get_current_frequency_divisors(&ifc, &bfc, &pfc);
151 clock_sets[MAX_CLOCK_SET].ifc = ifc;
152 clock_sets[MAX_CLOCK_SET].bfc = bfc;
153 clock_sets[MAX_CLOCK_SET].pfc = pfc;
155 /* Convert from Hz to kHz */
156 max_freq = current_cpu_data.cpu_clock / 1000;
157 min_freq = (current_cpu_data.master_clock / clock_sets[MIN_CLOCK_SET].ifc) / 1000;
159 sh_freqs[SH_FREQ_MAX].frequency = max_freq;
160 sh_freqs[SH_FREQ_MIN].frequency = min_freq;
162 /* cpuinfo and default policy values */
163 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
164 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
165 policy->cur = max_freq;
167 return cpufreq_frequency_table_cpuinfo(policy, &sh_freqs[0]);
170 static int sh_cpufreq_verify(struct cpufreq_policy *policy)
172 return cpufreq_frequency_table_verify(policy, &sh_freqs[0]);
175 static int sh_cpufreq_target(struct cpufreq_policy *policy,
176 unsigned int target_freq,
177 unsigned int relation)
179 unsigned int set, idx = 0;
181 if (cpufreq_frequency_table_target(policy, &sh_freqs[0], target_freq, relation, &idx))
184 set = (idx == SH_FREQ_MIN) ? MIN_CLOCK_SET : MAX_CLOCK_SET;
186 sh_cpufreq_setstate(policy->cpu, set);
191 static struct cpufreq_driver sh_cpufreq_driver = {
192 .owner = THIS_MODULE,
193 .name = "SH cpufreq",
194 .init = sh_cpufreq_cpu_init,
195 .verify = sh_cpufreq_verify,
196 .target = sh_cpufreq_target,
199 static int __init sh_cpufreq_init(void)
201 if (!current_cpu_data.cpu_clock)
203 if (cpufreq_register_driver(&sh_cpufreq_driver))
209 static void __exit sh_cpufreq_exit(void)
211 cpufreq_unregister_driver(&sh_cpufreq_driver);
214 module_init(sh_cpufreq_init);
215 module_exit(sh_cpufreq_exit);
217 MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
218 MODULE_DESCRIPTION("cpufreq driver for SuperH");
219 MODULE_LICENSE("GPL");