blob: 1cb222daa06d505579c25cdda97573ada746cb81 [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>
16#include <linux/slab.h>
17#include <linux/sched.h>
18#include <linux/smp.h>
19#include <linux/init.h>
Russell Kingfced80c2008-09-06 12:10:45 +010020#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Russell Kinga09e64f2008-08-05 16:14:15 +010022#include <mach/hardware.h>
Russell Kinga285edc2010-01-14 19:59:37 +000023#include <mach/platform.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/mach-types.h>
Russell Kingc5a0adb2010-01-16 20:16:10 +000025#include <asm/hardware/icst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27static struct cpufreq_driver integrator_driver;
28
Russell Kingb830b9b2010-01-17 20:45:12 +000029#define CM_ID IO_ADDRESS(INTEGRATOR_HDR_ID)
30#define CM_OSC IO_ADDRESS(INTEGRATOR_HDR_OSC)
31#define CM_STAT IO_ADDRESS(INTEGRATOR_HDR_STAT)
32#define CM_LOCK IO_ADDRESS(INTEGRATOR_HDR_LOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Russell King39c0cb02010-01-16 16:27:28 +000034static const struct icst_params lclk_params = {
Russell King64fceb12010-01-16 17:28:44 +000035 .ref = 24000000,
Russell King4de2edb2010-01-16 18:08:47 +000036 .vco_max = ICST525_VCO_MAX_5V,
Russell Kinge73a46a2010-01-16 19:49:39 +000037 .vco_min = ICST525_VCO_MIN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 .vd_min = 8,
39 .vd_max = 132,
40 .rd_min = 24,
41 .rd_max = 24,
Russell King232eaf72010-01-16 19:46:19 +000042 .s2div = icst525_s2div,
43 .idx2s = icst525_idx2s,
Linus Torvalds1da177e2005-04-16 15:20:36 -070044};
45
Russell King39c0cb02010-01-16 16:27:28 +000046static const struct icst_params cclk_params = {
Russell King64fceb12010-01-16 17:28:44 +000047 .ref = 24000000,
Russell King4de2edb2010-01-16 18:08:47 +000048 .vco_max = ICST525_VCO_MAX_5V,
Russell Kinge73a46a2010-01-16 19:49:39 +000049 .vco_min = ICST525_VCO_MIN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 .vd_min = 12,
51 .vd_max = 160,
52 .rd_min = 24,
53 .rd_max = 24,
Russell King232eaf72010-01-16 19:46:19 +000054 .s2div = icst525_s2div,
55 .idx2s = icst525_idx2s,
Linus Torvalds1da177e2005-04-16 15:20:36 -070056};
57
58/*
59 * Validate the speed policy.
60 */
61static int integrator_verify_policy(struct cpufreq_policy *policy)
62{
Russell King39c0cb02010-01-16 16:27:28 +000063 struct icst_vco vco;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 cpufreq_verify_within_limits(policy,
66 policy->cpuinfo.min_freq,
67 policy->cpuinfo.max_freq);
68
Russell Kingc5a0adb2010-01-16 20:16:10 +000069 vco = icst_hz_to_vco(&cclk_params, policy->max * 1000);
70 policy->max = icst_hz(&cclk_params, vco) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Russell Kingc5a0adb2010-01-16 20:16:10 +000072 vco = icst_hz_to_vco(&cclk_params, policy->min * 1000);
73 policy->min = icst_hz(&cclk_params, vco) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 cpufreq_verify_within_limits(policy,
76 policy->cpuinfo.min_freq,
77 policy->cpuinfo.max_freq);
78
79 return 0;
80}
81
82
83static int integrator_set_target(struct cpufreq_policy *policy,
84 unsigned int target_freq,
85 unsigned int relation)
86{
87 cpumask_t cpus_allowed;
88 int cpu = policy->cpu;
Russell King39c0cb02010-01-16 16:27:28 +000089 struct icst_vco vco;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 struct cpufreq_freqs freqs;
91 u_int cm_osc;
92
93 /*
94 * Save this threads cpus_allowed mask.
95 */
96 cpus_allowed = current->cpus_allowed;
97
98 /*
99 * Bind to the specified CPU. When this call returns,
100 * we should be running on the right CPU.
101 */
102 set_cpus_allowed(current, cpumask_of_cpu(cpu));
103 BUG_ON(cpu != smp_processor_id());
104
105 /* get current setting */
106 cm_osc = __raw_readl(CM_OSC);
107
108 if (machine_is_integrator()) {
109 vco.s = (cm_osc >> 8) & 7;
110 } else if (machine_is_cintegrator()) {
111 vco.s = 1;
112 }
113 vco.v = cm_osc & 255;
114 vco.r = 22;
Russell Kingc5a0adb2010-01-16 20:16:10 +0000115 freqs.old = icst_hz(&cclk_params, vco) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Russell Kingc5a0adb2010-01-16 20:16:10 +0000117 /* icst_hz_to_vco rounds down -- so we need the next
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 * larger freq in case of CPUFREQ_RELATION_L.
119 */
120 if (relation == CPUFREQ_RELATION_L)
121 target_freq += 999;
122 if (target_freq > policy->max)
123 target_freq = policy->max;
Russell Kingc5a0adb2010-01-16 20:16:10 +0000124 vco = icst_hz_to_vco(&cclk_params, target_freq * 1000);
125 freqs.new = icst_hz(&cclk_params, vco) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 freqs.cpu = policy->cpu;
128
129 if (freqs.old == freqs.new) {
130 set_cpus_allowed(current, cpus_allowed);
131 return 0;
132 }
133
134 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
135
136 cm_osc = __raw_readl(CM_OSC);
137
138 if (machine_is_integrator()) {
139 cm_osc &= 0xfffff800;
140 cm_osc |= vco.s << 8;
141 } else if (machine_is_cintegrator()) {
142 cm_osc &= 0xffffff00;
143 }
144 cm_osc |= vco.v;
145
146 __raw_writel(0xa05f, CM_LOCK);
147 __raw_writel(cm_osc, CM_OSC);
148 __raw_writel(0, CM_LOCK);
149
150 /*
151 * Restore the CPUs allowed mask.
152 */
153 set_cpus_allowed(current, cpus_allowed);
154
155 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
156
157 return 0;
158}
159
160static unsigned int integrator_get(unsigned int cpu)
161{
162 cpumask_t cpus_allowed;
163 unsigned int current_freq;
164 u_int cm_osc;
Russell King39c0cb02010-01-16 16:27:28 +0000165 struct icst_vco vco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 cpus_allowed = current->cpus_allowed;
168
169 set_cpus_allowed(current, cpumask_of_cpu(cpu));
170 BUG_ON(cpu != smp_processor_id());
171
172 /* detect memory etc. */
173 cm_osc = __raw_readl(CM_OSC);
174
175 if (machine_is_integrator()) {
176 vco.s = (cm_osc >> 8) & 7;
177 } else if (machine_is_cintegrator()) {
178 vco.s = 1;
179 }
180 vco.v = cm_osc & 255;
181 vco.r = 22;
182
Russell Kingc5a0adb2010-01-16 20:16:10 +0000183 current_freq = icst_hz(&cclk_params, vco) / 1000; /* current freq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 set_cpus_allowed(current, cpus_allowed);
186
187 return current_freq;
188}
189
190static int integrator_cpufreq_init(struct cpufreq_policy *policy)
191{
192
193 /* set default policy and cpuinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 policy->cpuinfo.max_freq = 160000;
195 policy->cpuinfo.min_freq = 12000;
196 policy->cpuinfo.transition_latency = 1000000; /* 1 ms, assumed */
197 policy->cur = policy->min = policy->max = integrator_get(policy->cpu);
198
199 return 0;
200}
201
202static struct cpufreq_driver integrator_driver = {
203 .verify = integrator_verify_policy,
204 .target = integrator_set_target,
205 .get = integrator_get,
206 .init = integrator_cpufreq_init,
207 .name = "integrator",
208};
209
210static int __init integrator_cpu_init(void)
211{
212 return cpufreq_register_driver(&integrator_driver);
213}
214
215static void __exit integrator_cpu_exit(void)
216{
217 cpufreq_unregister_driver(&integrator_driver);
218}
219
220MODULE_AUTHOR ("Russell M. King");
221MODULE_DESCRIPTION ("cpufreq driver for ARM Integrator CPUs");
222MODULE_LICENSE ("GPL");
223
224module_init(integrator_cpu_init);
225module_exit(integrator_cpu_exit);