blob: babf3e40e9fa5030be4420f7038681954ebc5bb8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2001-2002 Deep Blue Solutions Ltd.
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * CPU support functions
9 */
10#include <linux/module.h>
11#include <linux/types.h>
12#include <linux/kernel.h>
13#include <linux/cpufreq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/sched.h>
15#include <linux/smp.h>
16#include <linux/init.h>
Russell Kingfced80c2008-09-06 12:10:45 +010017#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Russell Kinga09e64f2008-08-05 16:14:15 +010019#include <mach/hardware.h>
Russell Kinga285edc2010-01-14 19:59:37 +000020#include <mach/platform.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/mach-types.h>
Russell Kingc5a0adb2010-01-16 20:16:10 +000022#include <asm/hardware/icst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24static struct cpufreq_driver integrator_driver;
25
Arnd Bergmannb7a3f8d2012-09-14 20:16:39 +000026#define CM_ID __io_address(INTEGRATOR_HDR_ID)
27#define CM_OSC __io_address(INTEGRATOR_HDR_OSC)
28#define CM_STAT __io_address(INTEGRATOR_HDR_STAT)
29#define CM_LOCK __io_address(INTEGRATOR_HDR_LOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Russell King39c0cb02010-01-16 16:27:28 +000031static const struct icst_params lclk_params = {
Russell King64fceb12010-01-16 17:28:44 +000032 .ref = 24000000,
Russell King4de2edb2010-01-16 18:08:47 +000033 .vco_max = ICST525_VCO_MAX_5V,
Russell Kinge73a46a2010-01-16 19:49:39 +000034 .vco_min = ICST525_VCO_MIN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 .vd_min = 8,
36 .vd_max = 132,
37 .rd_min = 24,
38 .rd_max = 24,
Russell King232eaf72010-01-16 19:46:19 +000039 .s2div = icst525_s2div,
40 .idx2s = icst525_idx2s,
Linus Torvalds1da177e2005-04-16 15:20:36 -070041};
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,
Russell Kinge73a46a2010-01-16 19:49:39 +000046 .vco_min = ICST525_VCO_MIN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 .vd_min = 12,
48 .vd_max = 160,
49 .rd_min = 24,
50 .rd_max = 24,
Russell King232eaf72010-01-16 19:46:19 +000051 .s2div = icst525_s2div,
52 .idx2s = icst525_idx2s,
Linus Torvalds1da177e2005-04-16 15:20:36 -070053};
54
55/*
56 * Validate the speed policy.
57 */
58static int integrator_verify_policy(struct cpufreq_policy *policy)
59{
Russell King39c0cb02010-01-16 16:27:28 +000060 struct icst_vco vco;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Viresh Kumarbe49e342013-10-02 14:13:19 +053062 cpufreq_verify_within_cpu_limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Russell Kingc5a0adb2010-01-16 20:16:10 +000064 vco = icst_hz_to_vco(&cclk_params, policy->max * 1000);
65 policy->max = icst_hz(&cclk_params, vco) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Russell Kingc5a0adb2010-01-16 20:16:10 +000067 vco = icst_hz_to_vco(&cclk_params, policy->min * 1000);
68 policy->min = icst_hz(&cclk_params, vco) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Viresh Kumarbe49e342013-10-02 14:13:19 +053070 cpufreq_verify_within_cpu_limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 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;
Russell King39c0cb02010-01-16 16:27:28 +000081 struct icst_vco vco;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 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;
Russell Kingc5a0adb2010-01-16 20:16:10 +0000107 freqs.old = icst_hz(&cclk_params, vco) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Russell Kingc5a0adb2010-01-16 20:16:10 +0000109 /* icst_hz_to_vco rounds down -- so we need the next
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * 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;
Russell Kingc5a0adb2010-01-16 20:16:10 +0000116 vco = icst_hz_to_vco(&cclk_params, target_freq * 1000);
117 freqs.new = icst_hz(&cclk_params, vco) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (freqs.old == freqs.new) {
120 set_cpus_allowed(current, cpus_allowed);
121 return 0;
122 }
123
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530124 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 cm_osc = __raw_readl(CM_OSC);
127
128 if (machine_is_integrator()) {
129 cm_osc &= 0xfffff800;
130 cm_osc |= vco.s << 8;
131 } else if (machine_is_cintegrator()) {
132 cm_osc &= 0xffffff00;
133 }
134 cm_osc |= vco.v;
135
136 __raw_writel(0xa05f, CM_LOCK);
137 __raw_writel(cm_osc, CM_OSC);
138 __raw_writel(0, CM_LOCK);
139
140 /*
141 * Restore the CPUs allowed mask.
142 */
143 set_cpus_allowed(current, cpus_allowed);
144
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530145 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 return 0;
148}
149
150static unsigned int integrator_get(unsigned int cpu)
151{
152 cpumask_t cpus_allowed;
153 unsigned int current_freq;
154 u_int cm_osc;
Russell King39c0cb02010-01-16 16:27:28 +0000155 struct icst_vco vco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 cpus_allowed = current->cpus_allowed;
158
159 set_cpus_allowed(current, cpumask_of_cpu(cpu));
160 BUG_ON(cpu != smp_processor_id());
161
162 /* detect memory etc. */
163 cm_osc = __raw_readl(CM_OSC);
164
165 if (machine_is_integrator()) {
166 vco.s = (cm_osc >> 8) & 7;
Russell King1aa023b2011-01-11 13:48:17 +0000167 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 vco.s = 1;
169 }
170 vco.v = cm_osc & 255;
171 vco.r = 22;
172
Russell Kingc5a0adb2010-01-16 20:16:10 +0000173 current_freq = icst_hz(&cclk_params, vco) / 1000; /* current freq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 set_cpus_allowed(current, cpus_allowed);
176
177 return current_freq;
178}
179
180static int integrator_cpufreq_init(struct cpufreq_policy *policy)
181{
182
183 /* set default policy and cpuinfo */
Viresh Kumarab537012013-10-03 20:28:46 +0530184 policy->max = policy->cpuinfo.max_freq = 160000;
185 policy->min = policy->cpuinfo.min_freq = 12000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 policy->cpuinfo.transition_latency = 1000000; /* 1 ms, assumed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 return 0;
189}
190
191static struct cpufreq_driver integrator_driver = {
192 .verify = integrator_verify_policy,
193 .target = integrator_set_target,
194 .get = integrator_get,
195 .init = integrator_cpufreq_init,
196 .name = "integrator",
197};
198
199static int __init integrator_cpu_init(void)
200{
201 return cpufreq_register_driver(&integrator_driver);
202}
203
204static void __exit integrator_cpu_exit(void)
205{
206 cpufreq_unregister_driver(&integrator_driver);
207}
208
209MODULE_AUTHOR ("Russell M. King");
210MODULE_DESCRIPTION ("cpufreq driver for ARM Integrator CPUs");
211MODULE_LICENSE ("GPL");
212
213module_init(integrator_cpu_init);
214module_exit(integrator_cpu_exit);