blob: f77f20255045aed83f8648bf1be2d0afbed5b7a3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/mach-integrator/cpu.c
3 *
4 * Copyright (C) 2001-2002 Deep Blue Solutions Ltd.
5 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * CPU support functions
11 */
12#include <linux/module.h>
13#include <linux/types.h>
14#include <linux/kernel.h>
15#include <linux/cpufreq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/sched.h>
17#include <linux/smp.h>
18#include <linux/init.h>
Russell Kingfced80c2008-09-06 12:10:45 +010019#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Russell Kinga09e64f2008-08-05 16:14:15 +010021#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/mach-types.h>
23#include <asm/hardware/icst525.h>
24
25static struct cpufreq_driver integrator_driver;
26
27#define CM_ID (IO_ADDRESS(INTEGRATOR_HDR_BASE)+INTEGRATOR_HDR_ID_OFFSET)
28#define CM_OSC (IO_ADDRESS(INTEGRATOR_HDR_BASE)+INTEGRATOR_HDR_OSC_OFFSET)
29#define CM_STAT (IO_ADDRESS(INTEGRATOR_HDR_BASE)+INTEGRATOR_HDR_STAT_OFFSET)
30#define CM_LOCK (IO_ADDRESS(INTEGRATOR_HDR_BASE)+INTEGRATOR_HDR_LOCK_OFFSET)
31
32static const struct icst525_params lclk_params = {
33 .ref = 24000,
34 .vco_max = 320000,
35 .vd_min = 8,
36 .vd_max = 132,
37 .rd_min = 24,
38 .rd_max = 24,
39};
40
41static const struct icst525_params cclk_params = {
42 .ref = 24000,
43 .vco_max = 320000,
44 .vd_min = 12,
45 .vd_max = 160,
46 .rd_min = 24,
47 .rd_max = 24,
48};
49
50/*
51 * Validate the speed policy.
52 */
53static int integrator_verify_policy(struct cpufreq_policy *policy)
54{
55 struct icst525_vco vco;
56
57 cpufreq_verify_within_limits(policy,
58 policy->cpuinfo.min_freq,
59 policy->cpuinfo.max_freq);
60
61 vco = icst525_khz_to_vco(&cclk_params, policy->max);
62 policy->max = icst525_khz(&cclk_params, vco);
63
64 vco = icst525_khz_to_vco(&cclk_params, policy->min);
65 policy->min = icst525_khz(&cclk_params, vco);
66
67 cpufreq_verify_within_limits(policy,
68 policy->cpuinfo.min_freq,
69 policy->cpuinfo.max_freq);
70
71 return 0;
72}
73
74
75static int integrator_set_target(struct cpufreq_policy *policy,
76 unsigned int target_freq,
77 unsigned int relation)
78{
79 cpumask_t cpus_allowed;
80 int cpu = policy->cpu;
81 struct icst525_vco vco;
82 struct cpufreq_freqs freqs;
83 u_int cm_osc;
84
85 /*
86 * Save this threads cpus_allowed mask.
87 */
88 cpus_allowed = current->cpus_allowed;
89
90 /*
91 * Bind to the specified CPU. When this call returns,
92 * we should be running on the right CPU.
93 */
94 set_cpus_allowed(current, cpumask_of_cpu(cpu));
95 BUG_ON(cpu != smp_processor_id());
96
97 /* get current setting */
98 cm_osc = __raw_readl(CM_OSC);
99
100 if (machine_is_integrator()) {
101 vco.s = (cm_osc >> 8) & 7;
102 } else if (machine_is_cintegrator()) {
103 vco.s = 1;
104 }
105 vco.v = cm_osc & 255;
106 vco.r = 22;
107 freqs.old = icst525_khz(&cclk_params, vco);
108
109 /* icst525_khz_to_vco rounds down -- so we need the next
110 * larger freq in case of CPUFREQ_RELATION_L.
111 */
112 if (relation == CPUFREQ_RELATION_L)
113 target_freq += 999;
114 if (target_freq > policy->max)
115 target_freq = policy->max;
116 vco = icst525_khz_to_vco(&cclk_params, target_freq);
117 freqs.new = icst525_khz(&cclk_params, vco);
118
119 freqs.cpu = policy->cpu;
120
121 if (freqs.old == freqs.new) {
122 set_cpus_allowed(current, cpus_allowed);
123 return 0;
124 }
125
126 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
127
128 cm_osc = __raw_readl(CM_OSC);
129
130 if (machine_is_integrator()) {
131 cm_osc &= 0xfffff800;
132 cm_osc |= vco.s << 8;
133 } else if (machine_is_cintegrator()) {
134 cm_osc &= 0xffffff00;
135 }
136 cm_osc |= vco.v;
137
138 __raw_writel(0xa05f, CM_LOCK);
139 __raw_writel(cm_osc, CM_OSC);
140 __raw_writel(0, CM_LOCK);
141
142 /*
143 * Restore the CPUs allowed mask.
144 */
145 set_cpus_allowed(current, cpus_allowed);
146
147 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
148
149 return 0;
150}
151
152static unsigned int integrator_get(unsigned int cpu)
153{
154 cpumask_t cpus_allowed;
155 unsigned int current_freq;
156 u_int cm_osc;
157 struct icst525_vco vco;
158
159 cpus_allowed = current->cpus_allowed;
160
161 set_cpus_allowed(current, cpumask_of_cpu(cpu));
162 BUG_ON(cpu != smp_processor_id());
163
164 /* detect memory etc. */
165 cm_osc = __raw_readl(CM_OSC);
166
167 if (machine_is_integrator()) {
168 vco.s = (cm_osc >> 8) & 7;
169 } else if (machine_is_cintegrator()) {
170 vco.s = 1;
171 }
172 vco.v = cm_osc & 255;
173 vco.r = 22;
174
175 current_freq = icst525_khz(&cclk_params, vco); /* current freq */
176
177 set_cpus_allowed(current, cpus_allowed);
178
179 return current_freq;
180}
181
182static int integrator_cpufreq_init(struct cpufreq_policy *policy)
183{
184
185 /* set default policy and cpuinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 policy->cpuinfo.max_freq = 160000;
187 policy->cpuinfo.min_freq = 12000;
188 policy->cpuinfo.transition_latency = 1000000; /* 1 ms, assumed */
189 policy->cur = policy->min = policy->max = integrator_get(policy->cpu);
190
191 return 0;
192}
193
194static struct cpufreq_driver integrator_driver = {
195 .verify = integrator_verify_policy,
196 .target = integrator_set_target,
197 .get = integrator_get,
198 .init = integrator_cpufreq_init,
199 .name = "integrator",
200};
201
202static int __init integrator_cpu_init(void)
203{
204 return cpufreq_register_driver(&integrator_driver);
205}
206
207static void __exit integrator_cpu_exit(void)
208{
209 cpufreq_unregister_driver(&integrator_driver);
210}
211
212MODULE_AUTHOR ("Russell M. King");
213MODULE_DESCRIPTION ("cpufreq driver for ARM Integrator CPUs");
214MODULE_LICENSE ("GPL");
215
216module_init(integrator_cpu_init);
217module_exit(integrator_cpu_exit);