blob: 590c192cdf4d43f58e6eafc7670048f2ea2247fe [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>
Russell Kinga285edc2010-01-14 19:59:37 +000022#include <mach/platform.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/mach-types.h>
Russell Kingc5a0adb2010-01-16 20:16:10 +000024#include <asm/hardware/icst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26static struct cpufreq_driver integrator_driver;
27
Arnd Bergmannb7a3f8d2012-09-14 20:16:39 +000028#define CM_ID __io_address(INTEGRATOR_HDR_ID)
29#define CM_OSC __io_address(INTEGRATOR_HDR_OSC)
30#define CM_STAT __io_address(INTEGRATOR_HDR_STAT)
31#define CM_LOCK __io_address(INTEGRATOR_HDR_LOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Russell King39c0cb02010-01-16 16:27:28 +000033static const struct icst_params lclk_params = {
Russell King64fceb12010-01-16 17:28:44 +000034 .ref = 24000000,
Russell King4de2edb2010-01-16 18:08:47 +000035 .vco_max = ICST525_VCO_MAX_5V,
Russell Kinge73a46a2010-01-16 19:49:39 +000036 .vco_min = ICST525_VCO_MIN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 .vd_min = 8,
38 .vd_max = 132,
39 .rd_min = 24,
40 .rd_max = 24,
Russell King232eaf72010-01-16 19:46:19 +000041 .s2div = icst525_s2div,
42 .idx2s = icst525_idx2s,
Linus Torvalds1da177e2005-04-16 15:20:36 -070043};
44
Russell King39c0cb02010-01-16 16:27:28 +000045static const struct icst_params cclk_params = {
Russell King64fceb12010-01-16 17:28:44 +000046 .ref = 24000000,
Russell King4de2edb2010-01-16 18:08:47 +000047 .vco_max = ICST525_VCO_MAX_5V,
Russell Kinge73a46a2010-01-16 19:49:39 +000048 .vco_min = ICST525_VCO_MIN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 .vd_min = 12,
50 .vd_max = 160,
51 .rd_min = 24,
52 .rd_max = 24,
Russell King232eaf72010-01-16 19:46:19 +000053 .s2div = icst525_s2div,
54 .idx2s = icst525_idx2s,
Linus Torvalds1da177e2005-04-16 15:20:36 -070055};
56
57/*
58 * Validate the speed policy.
59 */
60static int integrator_verify_policy(struct cpufreq_policy *policy)
61{
Russell King39c0cb02010-01-16 16:27:28 +000062 struct icst_vco vco;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 cpufreq_verify_within_limits(policy,
65 policy->cpuinfo.min_freq,
66 policy->cpuinfo.max_freq);
67
Russell Kingc5a0adb2010-01-16 20:16:10 +000068 vco = icst_hz_to_vco(&cclk_params, policy->max * 1000);
69 policy->max = icst_hz(&cclk_params, vco) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Russell Kingc5a0adb2010-01-16 20:16:10 +000071 vco = icst_hz_to_vco(&cclk_params, policy->min * 1000);
72 policy->min = icst_hz(&cclk_params, vco) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 cpufreq_verify_within_limits(policy,
75 policy->cpuinfo.min_freq,
76 policy->cpuinfo.max_freq);
77
78 return 0;
79}
80
81
82static int integrator_set_target(struct cpufreq_policy *policy,
83 unsigned int target_freq,
84 unsigned int relation)
85{
86 cpumask_t cpus_allowed;
87 int cpu = policy->cpu;
Russell King39c0cb02010-01-16 16:27:28 +000088 struct icst_vco vco;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 struct cpufreq_freqs freqs;
90 u_int cm_osc;
91
92 /*
93 * Save this threads cpus_allowed mask.
94 */
95 cpus_allowed = current->cpus_allowed;
96
97 /*
98 * Bind to the specified CPU. When this call returns,
99 * we should be running on the right CPU.
100 */
101 set_cpus_allowed(current, cpumask_of_cpu(cpu));
102 BUG_ON(cpu != smp_processor_id());
103
104 /* get current setting */
105 cm_osc = __raw_readl(CM_OSC);
106
107 if (machine_is_integrator()) {
108 vco.s = (cm_osc >> 8) & 7;
109 } else if (machine_is_cintegrator()) {
110 vco.s = 1;
111 }
112 vco.v = cm_osc & 255;
113 vco.r = 22;
Russell Kingc5a0adb2010-01-16 20:16:10 +0000114 freqs.old = icst_hz(&cclk_params, vco) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Russell Kingc5a0adb2010-01-16 20:16:10 +0000116 /* icst_hz_to_vco rounds down -- so we need the next
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * larger freq in case of CPUFREQ_RELATION_L.
118 */
119 if (relation == CPUFREQ_RELATION_L)
120 target_freq += 999;
121 if (target_freq > policy->max)
122 target_freq = policy->max;
Russell Kingc5a0adb2010-01-16 20:16:10 +0000123 vco = icst_hz_to_vco(&cclk_params, target_freq * 1000);
124 freqs.new = icst_hz(&cclk_params, vco) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 freqs.cpu = policy->cpu;
127
128 if (freqs.old == freqs.new) {
129 set_cpus_allowed(current, cpus_allowed);
130 return 0;
131 }
132
133 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
134
135 cm_osc = __raw_readl(CM_OSC);
136
137 if (machine_is_integrator()) {
138 cm_osc &= 0xfffff800;
139 cm_osc |= vco.s << 8;
140 } else if (machine_is_cintegrator()) {
141 cm_osc &= 0xffffff00;
142 }
143 cm_osc |= vco.v;
144
145 __raw_writel(0xa05f, CM_LOCK);
146 __raw_writel(cm_osc, CM_OSC);
147 __raw_writel(0, CM_LOCK);
148
149 /*
150 * Restore the CPUs allowed mask.
151 */
152 set_cpus_allowed(current, cpus_allowed);
153
154 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
155
156 return 0;
157}
158
159static unsigned int integrator_get(unsigned int cpu)
160{
161 cpumask_t cpus_allowed;
162 unsigned int current_freq;
163 u_int cm_osc;
Russell King39c0cb02010-01-16 16:27:28 +0000164 struct icst_vco vco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 cpus_allowed = current->cpus_allowed;
167
168 set_cpus_allowed(current, cpumask_of_cpu(cpu));
169 BUG_ON(cpu != smp_processor_id());
170
171 /* detect memory etc. */
172 cm_osc = __raw_readl(CM_OSC);
173
174 if (machine_is_integrator()) {
175 vco.s = (cm_osc >> 8) & 7;
Russell King1aa023b2011-01-11 13:48:17 +0000176 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 vco.s = 1;
178 }
179 vco.v = cm_osc & 255;
180 vco.r = 22;
181
Russell Kingc5a0adb2010-01-16 20:16:10 +0000182 current_freq = icst_hz(&cclk_params, vco) / 1000; /* current freq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 set_cpus_allowed(current, cpus_allowed);
185
186 return current_freq;
187}
188
189static int integrator_cpufreq_init(struct cpufreq_policy *policy)
190{
191
192 /* set default policy and cpuinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 policy->cpuinfo.max_freq = 160000;
194 policy->cpuinfo.min_freq = 12000;
195 policy->cpuinfo.transition_latency = 1000000; /* 1 ms, assumed */
196 policy->cur = policy->min = policy->max = integrator_get(policy->cpu);
197
198 return 0;
199}
200
201static struct cpufreq_driver integrator_driver = {
202 .verify = integrator_verify_policy,
203 .target = integrator_set_target,
204 .get = integrator_get,
205 .init = integrator_cpufreq_init,
206 .name = "integrator",
207};
208
209static int __init integrator_cpu_init(void)
210{
211 return cpufreq_register_driver(&integrator_driver);
212}
213
214static void __exit integrator_cpu_exit(void)
215{
216 cpufreq_unregister_driver(&integrator_driver);
217}
218
219MODULE_AUTHOR ("Russell M. King");
220MODULE_DESCRIPTION ("cpufreq driver for ARM Integrator CPUs");
221MODULE_LICENSE ("GPL");
222
223module_init(integrator_cpu_init);
224module_exit(integrator_cpu_exit);