blob: 3d79bca474336b16e0cc7810eafae9a28b65b0aa [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 Walleijbdac7ea2013-06-17 22:52:32 +020018#include <linux/platform_device.h>
19#include <linux/of.h>
20#include <linux/of_address.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/mach-types.h>
Russell Kingc5a0adb2010-01-16 20:16:10 +000023#include <asm/hardware/icst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Walleijbdac7ea2013-06-17 22:52:32 +020025static void __iomem *cm_base;
26/* The cpufreq driver only use the OSC register */
27#define INTEGRATOR_HDR_OSC_OFFSET 0x08
28#define INTEGRATOR_HDR_LOCK_OFFSET 0x14
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Walleijbdac7ea2013-06-17 22:52:32 +020030static struct cpufreq_driver integrator_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Russell King39c0cb02010-01-16 16:27:28 +000032static const struct icst_params lclk_params = {
Russell King64fceb12010-01-16 17:28:44 +000033 .ref = 24000000,
Russell King4de2edb2010-01-16 18:08:47 +000034 .vco_max = ICST525_VCO_MAX_5V,
Russell Kinge73a46a2010-01-16 19:49:39 +000035 .vco_min = ICST525_VCO_MIN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 .vd_min = 8,
37 .vd_max = 132,
38 .rd_min = 24,
39 .rd_max = 24,
Russell King232eaf72010-01-16 19:46:19 +000040 .s2div = icst525_s2div,
41 .idx2s = icst525_idx2s,
Linus Torvalds1da177e2005-04-16 15:20:36 -070042};
43
Russell King39c0cb02010-01-16 16:27:28 +000044static const struct icst_params cclk_params = {
Russell King64fceb12010-01-16 17:28:44 +000045 .ref = 24000000,
Russell King4de2edb2010-01-16 18:08:47 +000046 .vco_max = ICST525_VCO_MAX_5V,
Russell Kinge73a46a2010-01-16 19:49:39 +000047 .vco_min = ICST525_VCO_MIN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 .vd_min = 12,
49 .vd_max = 160,
50 .rd_min = 24,
51 .rd_max = 24,
Russell King232eaf72010-01-16 19:46:19 +000052 .s2div = icst525_s2div,
53 .idx2s = icst525_idx2s,
Linus Torvalds1da177e2005-04-16 15:20:36 -070054};
55
56/*
57 * Validate the speed policy.
58 */
59static int integrator_verify_policy(struct cpufreq_policy *policy)
60{
Russell King39c0cb02010-01-16 16:27:28 +000061 struct icst_vco vco;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 cpufreq_verify_within_limits(policy,
64 policy->cpuinfo.min_freq,
65 policy->cpuinfo.max_freq);
66
Russell Kingc5a0adb2010-01-16 20:16:10 +000067 vco = icst_hz_to_vco(&cclk_params, policy->max * 1000);
68 policy->max = icst_hz(&cclk_params, vco) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Russell Kingc5a0adb2010-01-16 20:16:10 +000070 vco = icst_hz_to_vco(&cclk_params, policy->min * 1000);
71 policy->min = icst_hz(&cclk_params, vco) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 cpufreq_verify_within_limits(policy,
74 policy->cpuinfo.min_freq,
75 policy->cpuinfo.max_freq);
76
77 return 0;
78}
79
80
81static int integrator_set_target(struct cpufreq_policy *policy,
82 unsigned int target_freq,
83 unsigned int relation)
84{
85 cpumask_t cpus_allowed;
86 int cpu = policy->cpu;
Russell King39c0cb02010-01-16 16:27:28 +000087 struct icst_vco vco;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 struct cpufreq_freqs freqs;
89 u_int cm_osc;
90
91 /*
92 * Save this threads cpus_allowed mask.
93 */
94 cpus_allowed = current->cpus_allowed;
95
96 /*
97 * Bind to the specified CPU. When this call returns,
98 * we should be running on the right CPU.
99 */
100 set_cpus_allowed(current, cpumask_of_cpu(cpu));
101 BUG_ON(cpu != smp_processor_id());
102
103 /* get current setting */
Linus Walleijbdac7ea2013-06-17 22:52:32 +0200104 cm_osc = __raw_readl(cm_base + INTEGRATOR_HDR_OSC_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 if (machine_is_integrator()) {
107 vco.s = (cm_osc >> 8) & 7;
108 } else if (machine_is_cintegrator()) {
109 vco.s = 1;
110 }
111 vco.v = cm_osc & 255;
112 vco.r = 22;
Russell Kingc5a0adb2010-01-16 20:16:10 +0000113 freqs.old = icst_hz(&cclk_params, vco) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Russell Kingc5a0adb2010-01-16 20:16:10 +0000115 /* icst_hz_to_vco rounds down -- so we need the next
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 * larger freq in case of CPUFREQ_RELATION_L.
117 */
118 if (relation == CPUFREQ_RELATION_L)
119 target_freq += 999;
120 if (target_freq > policy->max)
121 target_freq = policy->max;
Russell Kingc5a0adb2010-01-16 20:16:10 +0000122 vco = icst_hz_to_vco(&cclk_params, target_freq * 1000);
123 freqs.new = icst_hz(&cclk_params, vco) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (freqs.old == freqs.new) {
126 set_cpus_allowed(current, cpus_allowed);
127 return 0;
128 }
129
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530130 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Linus Walleijbdac7ea2013-06-17 22:52:32 +0200132 cm_osc = __raw_readl(cm_base + INTEGRATOR_HDR_OSC_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 if (machine_is_integrator()) {
135 cm_osc &= 0xfffff800;
136 cm_osc |= vco.s << 8;
137 } else if (machine_is_cintegrator()) {
138 cm_osc &= 0xffffff00;
139 }
140 cm_osc |= vco.v;
141
Linus Walleijbdac7ea2013-06-17 22:52:32 +0200142 __raw_writel(0xa05f, cm_base + INTEGRATOR_HDR_LOCK_OFFSET);
143 __raw_writel(cm_osc, cm_base + INTEGRATOR_HDR_OSC_OFFSET);
144 __raw_writel(0, cm_base + INTEGRATOR_HDR_LOCK_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 /*
147 * Restore the CPUs allowed mask.
148 */
149 set_cpus_allowed(current, cpus_allowed);
150
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530151 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 return 0;
154}
155
156static unsigned int integrator_get(unsigned int cpu)
157{
158 cpumask_t cpus_allowed;
159 unsigned int current_freq;
160 u_int cm_osc;
Russell King39c0cb02010-01-16 16:27:28 +0000161 struct icst_vco vco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 cpus_allowed = current->cpus_allowed;
164
165 set_cpus_allowed(current, cpumask_of_cpu(cpu));
166 BUG_ON(cpu != smp_processor_id());
167
168 /* detect memory etc. */
Linus Walleijbdac7ea2013-06-17 22:52:32 +0200169 cm_osc = __raw_readl(cm_base + INTEGRATOR_HDR_OSC_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 if (machine_is_integrator()) {
172 vco.s = (cm_osc >> 8) & 7;
Russell King1aa023b2011-01-11 13:48:17 +0000173 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 vco.s = 1;
175 }
176 vco.v = cm_osc & 255;
177 vco.r = 22;
178
Russell Kingc5a0adb2010-01-16 20:16:10 +0000179 current_freq = icst_hz(&cclk_params, vco) / 1000; /* current freq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 set_cpus_allowed(current, cpus_allowed);
182
183 return current_freq;
184}
185
186static int integrator_cpufreq_init(struct cpufreq_policy *policy)
187{
188
189 /* set default policy and cpuinfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 policy->cpuinfo.max_freq = 160000;
191 policy->cpuinfo.min_freq = 12000;
192 policy->cpuinfo.transition_latency = 1000000; /* 1 ms, assumed */
193 policy->cur = policy->min = policy->max = integrator_get(policy->cpu);
194
195 return 0;
196}
197
198static struct cpufreq_driver integrator_driver = {
199 .verify = integrator_verify_policy,
200 .target = integrator_set_target,
201 .get = integrator_get,
202 .init = integrator_cpufreq_init,
203 .name = "integrator",
204};
205
Linus Walleijbdac7ea2013-06-17 22:52:32 +0200206static int __init integrator_cpufreq_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Linus Walleijbdac7ea2013-06-17 22:52:32 +0200208 struct resource *res;
209
210 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
211 if (!res)
212 return -ENODEV;
213
214 cm_base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
215 if (!cm_base)
216 return -ENODEV;
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return cpufreq_register_driver(&integrator_driver);
219}
220
Linus Walleijbdac7ea2013-06-17 22:52:32 +0200221static void __exit integrator_cpufreq_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 cpufreq_unregister_driver(&integrator_driver);
224}
225
Linus Walleijbdac7ea2013-06-17 22:52:32 +0200226static const struct of_device_id integrator_cpufreq_match[] = {
227 { .compatible = "arm,core-module-integrator"},
228 { },
229};
230
231static struct platform_driver integrator_cpufreq_driver = {
232 .driver = {
233 .name = "integrator-cpufreq",
234 .owner = THIS_MODULE,
235 .of_match_table = integrator_cpufreq_match,
236 },
237 .remove = __exit_p(integrator_cpufreq_remove),
238};
239
240module_platform_driver_probe(integrator_cpufreq_driver,
241 integrator_cpufreq_probe);
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243MODULE_AUTHOR ("Russell M. King");
244MODULE_DESCRIPTION ("cpufreq driver for ARM Integrator CPUs");
245MODULE_LICENSE ("GPL");