]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - arch/sh/kernel/cpufreq.c
sh: Support rate rounding for SH7722 FRQCR clocks.
[mirror_ubuntu-zesty-kernel.git] / arch / sh / kernel / cpufreq.c
CommitLineData
1da177e4
LT
1/*
2 * arch/sh/kernel/cpufreq.c
3 *
4 * cpufreq driver for the SuperH processors.
5 *
6 * Copyright (C) 2002, 2003, 2004, 2005 Paul Mundt
7 * Copyright (C) 2002 M. R. Brown
8 *
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.
13 */
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/cpumask.h>
22#include <linux/smp.h>
4e57b681 23#include <linux/sched.h> /* set_cpus_allowed() */
1da177e4
LT
24
25#include <asm/processor.h>
26#include <asm/watchdog.h>
27#include <asm/freq.h>
28#include <asm/io.h>
29
30/*
31 * For SuperH, each policy change requires that we change the IFC, BFC, and
32 * PFC at the same time. Here we define sane values that won't trash the
33 * system.
34 *
35 * Note the max set is computed at runtime, we use the divisors that we booted
36 * with to setup our maximum operating frequencies.
37 */
38struct clock_set {
39 unsigned int ifc;
40 unsigned int bfc;
41 unsigned int pfc;
42} clock_sets[] = {
43#if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH2)
44 { 0, 0, 0 }, /* not implemented yet */
45#elif defined(CONFIG_CPU_SH4)
46 { 4, 8, 8 }, /* min - IFC: 1/4, BFC: 1/8, PFC: 1/8 */
47 { 1, 2, 2 }, /* max - IFC: 1, BFC: 1/2, PFC: 1/2 */
48#endif
49};
50
51#define MIN_CLOCK_SET 0
52#define MAX_CLOCK_SET (ARRAY_SIZE(clock_sets) - 1)
53
54/*
55 * For the time being, we only support two frequencies, which in turn are
56 * aimed at the POWERSAVE and PERFORMANCE policies, which in turn are derived
57 * directly from the respective min/max clock sets. Technically we could
58 * support a wider range of frequencies, but these vary far too much for each
59 * CPU subtype (and we'd have to construct a frequency table for each subtype).
60 *
61 * Maybe something to implement in the future..
62 */
63#define SH_FREQ_MAX 0
64#define SH_FREQ_MIN 1
65
66static struct cpufreq_frequency_table sh_freqs[] = {
67 { SH_FREQ_MAX, 0 },
68 { SH_FREQ_MIN, 0 },
69 { 0, CPUFREQ_TABLE_END },
70};
71
72static void sh_cpufreq_update_clocks(unsigned int set)
73{
74 current_cpu_data.cpu_clock = current_cpu_data.master_clock / clock_sets[set].ifc;
75 current_cpu_data.bus_clock = current_cpu_data.master_clock / clock_sets[set].bfc;
76 current_cpu_data.module_clock = current_cpu_data.master_clock / clock_sets[set].pfc;
77 current_cpu_data.loops_per_jiffy = loops_per_jiffy;
78}
79
80/* XXX: This needs to be split out per CPU and CPU subtype. */
81/*
82 * Here we notify other drivers of the proposed change and the final change.
83 */
84static int sh_cpufreq_setstate(unsigned int cpu, unsigned int set)
85{
86 unsigned short frqcr = ctrl_inw(FRQCR);
87 cpumask_t cpus_allowed;
88 struct cpufreq_freqs freqs;
89
90 if (!cpu_online(cpu))
91 return -ENODEV;
92
93 cpus_allowed = current->cpus_allowed;
94 set_cpus_allowed(current, cpumask_of_cpu(cpu));
95
96 BUG_ON(smp_processor_id() != cpu);
97
98 freqs.cpu = cpu;
99 freqs.old = current_cpu_data.cpu_clock / 1000;
100 freqs.new = (current_cpu_data.master_clock / clock_sets[set].ifc) / 1000;
101
102 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
103#if defined(CONFIG_CPU_SH3)
104 frqcr |= (newstate & 0x4000) << 14;
105 frqcr |= (newstate & 0x000c) << 2;
106#elif defined(CONFIG_CPU_SH4)
107 /*
108 * FRQCR.PLL2EN is 1, we need to allow the PLL to stabilize by
109 * initializing the WDT.
110 */
111 if (frqcr & (1 << 9)) {
112 __u8 csr;
113
114 /*
115 * Set the overflow period to the highest available,
116 * in this case a 1/4096 division ratio yields a 5.25ms
117 * overflow period. See asm-sh/watchdog.h for more
118 * information and a range of other divisors.
119 */
120 csr = sh_wdt_read_csr();
121 csr |= WTCSR_CKS_4096;
122 sh_wdt_write_csr(csr);
123
124 sh_wdt_write_cnt(0);
125 }
126 frqcr &= 0x0e00; /* Clear ifc, bfc, pfc */
127 frqcr |= get_ifc_value(clock_sets[set].ifc) << 6;
128 frqcr |= get_bfc_value(clock_sets[set].bfc) << 3;
129 frqcr |= get_pfc_value(clock_sets[set].pfc);
130#endif
131 ctrl_outw(frqcr, FRQCR);
132 sh_cpufreq_update_clocks(set);
133
134 set_cpus_allowed(current, cpus_allowed);
135 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
136
137 return 0;
138}
139
140static int sh_cpufreq_cpu_init(struct cpufreq_policy *policy)
141{
142 unsigned int min_freq, max_freq;
143 unsigned int ifc, bfc, pfc;
144
145 if (!cpu_online(policy->cpu))
146 return -ENODEV;
147
148 /* Update our maximum clock set */
149 get_current_frequency_divisors(&ifc, &bfc, &pfc);
150 clock_sets[MAX_CLOCK_SET].ifc = ifc;
151 clock_sets[MAX_CLOCK_SET].bfc = bfc;
152 clock_sets[MAX_CLOCK_SET].pfc = pfc;
153
154 /* Convert from Hz to kHz */
155 max_freq = current_cpu_data.cpu_clock / 1000;
156 min_freq = (current_cpu_data.master_clock / clock_sets[MIN_CLOCK_SET].ifc) / 1000;
157
158 sh_freqs[SH_FREQ_MAX].frequency = max_freq;
159 sh_freqs[SH_FREQ_MIN].frequency = min_freq;
160
161 /* cpuinfo and default policy values */
162 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
163 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
164 policy->cur = max_freq;
165
166 return cpufreq_frequency_table_cpuinfo(policy, &sh_freqs[0]);
167}
168
169static int sh_cpufreq_verify(struct cpufreq_policy *policy)
170{
171 return cpufreq_frequency_table_verify(policy, &sh_freqs[0]);
172}
173
174static int sh_cpufreq_target(struct cpufreq_policy *policy,
175 unsigned int target_freq,
176 unsigned int relation)
177{
178 unsigned int set, idx = 0;
179
180 if (cpufreq_frequency_table_target(policy, &sh_freqs[0], target_freq, relation, &idx))
181 return -EINVAL;
182
183 set = (idx == SH_FREQ_MIN) ? MIN_CLOCK_SET : MAX_CLOCK_SET;
184
185 sh_cpufreq_setstate(policy->cpu, set);
186
187 return 0;
188}
189
190static struct cpufreq_driver sh_cpufreq_driver = {
191 .owner = THIS_MODULE,
192 .name = "SH cpufreq",
193 .init = sh_cpufreq_cpu_init,
194 .verify = sh_cpufreq_verify,
195 .target = sh_cpufreq_target,
196};
197
198static int __init sh_cpufreq_init(void)
199{
200 if (!current_cpu_data.cpu_clock)
201 return -EINVAL;
202 if (cpufreq_register_driver(&sh_cpufreq_driver))
203 return -EINVAL;
204
205 return 0;
206}
207
208static void __exit sh_cpufreq_exit(void)
209{
210 cpufreq_unregister_driver(&sh_cpufreq_driver);
211}
212
213module_init(sh_cpufreq_init);
214module_exit(sh_cpufreq_exit);
215
216MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
217MODULE_DESCRIPTION("cpufreq driver for SuperH");
218MODULE_LICENSE("GPL");
219