blob: 3ebb785f30c1b90f1eb6ab47743005f75be4e3a8 [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>
25#include <asm/hardware/icst525.h>
26
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,
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 .vd_min = 8,
38 .vd_max = 132,
39 .rd_min = 24,
40 .rd_max = 24,
41};
42
Russell King39c0cb02010-01-16 16:27:28 +000043static const struct icst_params cclk_params = {
Russell King64fceb12010-01-16 17:28:44 +000044 .ref = 24000000,
Russell King4de2edb2010-01-16 18:08:47 +000045 .vco_max = ICST525_VCO_MAX_5V,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 .vd_min = 12,
47 .vd_max = 160,
48 .rd_min = 24,
49 .rd_max = 24,
50};
51
52/*
53 * Validate the speed policy.
54 */
55static int integrator_verify_policy(struct cpufreq_policy *policy)
56{
Russell King39c0cb02010-01-16 16:27:28 +000057 struct icst_vco vco;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 cpufreq_verify_within_limits(policy,
60 policy->cpuinfo.min_freq,
61 policy->cpuinfo.max_freq);
62
Russell King64fceb12010-01-16 17:28:44 +000063 vco = icst525_hz_to_vco(&cclk_params, policy->max * 1000);
64 policy->max = icst525_hz(&cclk_params, vco) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Russell King64fceb12010-01-16 17:28:44 +000066 vco = icst525_hz_to_vco(&cclk_params, policy->min * 1000);
67 policy->min = icst525_hz(&cclk_params, vco) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 cpufreq_verify_within_limits(policy,
70 policy->cpuinfo.min_freq,
71 policy->cpuinfo.max_freq);
72
73 return 0;
74}
75
76
77static int integrator_set_target(struct cpufreq_policy *policy,
78 unsigned int target_freq,
79 unsigned int relation)
80{
81 cpumask_t cpus_allowed;
82 int cpu = policy->cpu;
Russell King39c0cb02010-01-16 16:27:28 +000083 struct icst_vco vco;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 struct cpufreq_freqs freqs;
85 u_int cm_osc;
86
87 /*
88 * Save this threads cpus_allowed mask.
89 */
90 cpus_allowed = current->cpus_allowed;
91
92 /*
93 * Bind to the specified CPU. When this call returns,
94 * we should be running on the right CPU.
95 */
96 set_cpus_allowed(current, cpumask_of_cpu(cpu));
97 BUG_ON(cpu != smp_processor_id());
98
99 /* get current setting */
100 cm_osc = __raw_readl(CM_OSC);
101
102 if (machine_is_integrator()) {
103 vco.s = (cm_osc >> 8) & 7;
104 } else if (machine_is_cintegrator()) {
105 vco.s = 1;
106 }
107 vco.v = cm_osc & 255;
108 vco.r = 22;
Russell King64fceb12010-01-16 17:28:44 +0000109 freqs.old = icst525_hz(&cclk_params, vco) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Russell King64fceb12010-01-16 17:28:44 +0000111 /* icst525_hz_to_vco rounds down -- so we need the next
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * larger freq in case of CPUFREQ_RELATION_L.
113 */
114 if (relation == CPUFREQ_RELATION_L)
115 target_freq += 999;
116 if (target_freq > policy->max)
117 target_freq = policy->max;
Russell King64fceb12010-01-16 17:28:44 +0000118 vco = icst525_hz_to_vco(&cclk_params, target_freq * 1000);
119 freqs.new = icst525_hz(&cclk_params, vco) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 freqs.cpu = policy->cpu;
122
123 if (freqs.old == freqs.new) {
124 set_cpus_allowed(current, cpus_allowed);
125 return 0;
126 }
127
128 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
129
130 cm_osc = __raw_readl(CM_OSC);
131
132 if (machine_is_integrator()) {
133 cm_osc &= 0xfffff800;
134 cm_osc |= vco.s << 8;
135 } else if (machine_is_cintegrator()) {
136 cm_osc &= 0xffffff00;
137 }
138 cm_osc |= vco.v;
139
140 __raw_writel(0xa05f, CM_LOCK);
141 __raw_writel(cm_osc, CM_OSC);
142 __raw_writel(0, CM_LOCK);
143
144 /*
145 * Restore the CPUs allowed mask.
146 */
147 set_cpus_allowed(current, cpus_allowed);
148
149 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
150
151 return 0;
152}
153
154static unsigned int integrator_get(unsigned int cpu)
155{
156 cpumask_t cpus_allowed;
157 unsigned int current_freq;
158 u_int cm_osc;
Russell King39c0cb02010-01-16 16:27:28 +0000159 struct icst_vco vco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 cpus_allowed = current->cpus_allowed;
162
163 set_cpus_allowed(current, cpumask_of_cpu(cpu));
164 BUG_ON(cpu != smp_processor_id());
165
166 /* detect memory etc. */
167 cm_osc = __raw_readl(CM_OSC);
168
169 if (machine_is_integrator()) {
170 vco.s = (cm_osc >> 8) & 7;
171 } else if (machine_is_cintegrator()) {
172 vco.s = 1;
173 }
174 vco.v = cm_osc & 255;
175 vco.r = 22;
176
Russell King64fceb12010-01-16 17:28:44 +0000177 current_freq = icst525_hz(&cclk_params, vco) / 1000; /* current freq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 set_cpus_allowed(current, cpus_allowed);
180
181 return current_freq;
182}
183
184static int integrator_cpufreq_init(struct cpufreq_policy *policy)
185{
186
187 /* set default policy and cpuinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 policy->cpuinfo.max_freq = 160000;
189 policy->cpuinfo.min_freq = 12000;
190 policy->cpuinfo.transition_latency = 1000000; /* 1 ms, assumed */
191 policy->cur = policy->min = policy->max = integrator_get(policy->cpu);
192
193 return 0;
194}
195
196static struct cpufreq_driver integrator_driver = {
197 .verify = integrator_verify_policy,
198 .target = integrator_set_target,
199 .get = integrator_get,
200 .init = integrator_cpufreq_init,
201 .name = "integrator",
202};
203
204static int __init integrator_cpu_init(void)
205{
206 return cpufreq_register_driver(&integrator_driver);
207}
208
209static void __exit integrator_cpu_exit(void)
210{
211 cpufreq_unregister_driver(&integrator_driver);
212}
213
214MODULE_AUTHOR ("Russell M. King");
215MODULE_DESCRIPTION ("cpufreq driver for ARM Integrator CPUs");
216MODULE_LICENSE ("GPL");
217
218module_init(integrator_cpu_init);
219module_exit(integrator_cpu_exit);