blob: c1320528b9d0c0060247b4d025bc35116701996a [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
Viresh Kumarbe49e342013-10-02 14:13:19 +053063 cpufreq_verify_within_cpu_limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Russell Kingc5a0adb2010-01-16 20:16:10 +000065 vco = icst_hz_to_vco(&cclk_params, policy->max * 1000);
66 policy->max = icst_hz(&cclk_params, vco) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Russell Kingc5a0adb2010-01-16 20:16:10 +000068 vco = icst_hz_to_vco(&cclk_params, policy->min * 1000);
69 policy->min = icst_hz(&cclk_params, vco) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Viresh Kumarbe49e342013-10-02 14:13:19 +053071 cpufreq_verify_within_cpu_limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return 0;
73}
74
75
76static int integrator_set_target(struct cpufreq_policy *policy,
77 unsigned int target_freq,
78 unsigned int relation)
79{
80 cpumask_t cpus_allowed;
81 int cpu = policy->cpu;
Russell King39c0cb02010-01-16 16:27:28 +000082 struct icst_vco vco;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 struct cpufreq_freqs freqs;
84 u_int cm_osc;
85
86 /*
87 * Save this threads cpus_allowed mask.
88 */
89 cpus_allowed = current->cpus_allowed;
90
91 /*
92 * Bind to the specified CPU. When this call returns,
93 * we should be running on the right CPU.
94 */
Himangi Saraogi18360d62014-08-03 15:45:40 +053095 set_cpus_allowed_ptr(current, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 BUG_ON(cpu != smp_processor_id());
97
98 /* get current setting */
Linus Walleijbdac7ea2013-06-17 22:52:32 +020099 cm_osc = __raw_readl(cm_base + INTEGRATOR_HDR_OSC_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 if (machine_is_integrator()) {
102 vco.s = (cm_osc >> 8) & 7;
103 } else if (machine_is_cintegrator()) {
104 vco.s = 1;
105 }
106 vco.v = cm_osc & 255;
107 vco.r = 22;
Russell Kingc5a0adb2010-01-16 20:16:10 +0000108 freqs.old = icst_hz(&cclk_params, vco) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Russell Kingc5a0adb2010-01-16 20:16:10 +0000110 /* icst_hz_to_vco rounds down -- so we need the next
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * larger freq in case of CPUFREQ_RELATION_L.
112 */
113 if (relation == CPUFREQ_RELATION_L)
114 target_freq += 999;
115 if (target_freq > policy->max)
116 target_freq = policy->max;
Russell Kingc5a0adb2010-01-16 20:16:10 +0000117 vco = icst_hz_to_vco(&cclk_params, target_freq * 1000);
118 freqs.new = icst_hz(&cclk_params, vco) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (freqs.old == freqs.new) {
Himangi Saraogi18360d62014-08-03 15:45:40 +0530121 set_cpus_allowed_ptr(current, &cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return 0;
123 }
124
Viresh Kumar8fec0512014-03-24 13:35:45 +0530125 cpufreq_freq_transition_begin(policy, &freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Linus Walleijbdac7ea2013-06-17 22:52:32 +0200127 cm_osc = __raw_readl(cm_base + INTEGRATOR_HDR_OSC_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 if (machine_is_integrator()) {
130 cm_osc &= 0xfffff800;
131 cm_osc |= vco.s << 8;
132 } else if (machine_is_cintegrator()) {
133 cm_osc &= 0xffffff00;
134 }
135 cm_osc |= vco.v;
136
Linus Walleijbdac7ea2013-06-17 22:52:32 +0200137 __raw_writel(0xa05f, cm_base + INTEGRATOR_HDR_LOCK_OFFSET);
138 __raw_writel(cm_osc, cm_base + INTEGRATOR_HDR_OSC_OFFSET);
139 __raw_writel(0, cm_base + INTEGRATOR_HDR_LOCK_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 /*
142 * Restore the CPUs allowed mask.
143 */
Himangi Saraogi18360d62014-08-03 15:45:40 +0530144 set_cpus_allowed_ptr(current, &cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Viresh Kumar8fec0512014-03-24 13:35:45 +0530146 cpufreq_freq_transition_end(policy, &freqs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 return 0;
149}
150
151static unsigned int integrator_get(unsigned int cpu)
152{
153 cpumask_t cpus_allowed;
154 unsigned int current_freq;
155 u_int cm_osc;
Russell King39c0cb02010-01-16 16:27:28 +0000156 struct icst_vco vco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 cpus_allowed = current->cpus_allowed;
159
Himangi Saraogi18360d62014-08-03 15:45:40 +0530160 set_cpus_allowed_ptr(current, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 BUG_ON(cpu != smp_processor_id());
162
163 /* detect memory etc. */
Linus Walleijbdac7ea2013-06-17 22:52:32 +0200164 cm_osc = __raw_readl(cm_base + INTEGRATOR_HDR_OSC_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 if (machine_is_integrator()) {
167 vco.s = (cm_osc >> 8) & 7;
Russell King1aa023b2011-01-11 13:48:17 +0000168 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 vco.s = 1;
170 }
171 vco.v = cm_osc & 255;
172 vco.r = 22;
173
Russell Kingc5a0adb2010-01-16 20:16:10 +0000174 current_freq = icst_hz(&cclk_params, vco) / 1000; /* current freq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Himangi Saraogi18360d62014-08-03 15:45:40 +0530176 set_cpus_allowed_ptr(current, &cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 return current_freq;
179}
180
181static int integrator_cpufreq_init(struct cpufreq_policy *policy)
182{
183
184 /* set default policy and cpuinfo */
Viresh Kumarab537012013-10-03 20:28:46 +0530185 policy->max = policy->cpuinfo.max_freq = 160000;
186 policy->min = policy->cpuinfo.min_freq = 12000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 policy->cpuinfo.transition_latency = 1000000; /* 1 ms, assumed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 return 0;
190}
191
192static struct cpufreq_driver integrator_driver = {
Viresh Kumarae6b4272013-12-03 11:20:45 +0530193 .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 .verify = integrator_verify_policy,
195 .target = integrator_set_target,
196 .get = integrator_get,
197 .init = integrator_cpufreq_init,
198 .name = "integrator",
199};
200
Linus Walleijbdac7ea2013-06-17 22:52:32 +0200201static int __init integrator_cpufreq_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Linus Walleijbdac7ea2013-06-17 22:52:32 +0200203 struct resource *res;
204
205 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
206 if (!res)
207 return -ENODEV;
208
209 cm_base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
210 if (!cm_base)
211 return -ENODEV;
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return cpufreq_register_driver(&integrator_driver);
214}
215
Linus Walleijbdac7ea2013-06-17 22:52:32 +0200216static void __exit integrator_cpufreq_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 cpufreq_unregister_driver(&integrator_driver);
219}
220
Linus Walleijbdac7ea2013-06-17 22:52:32 +0200221static const struct of_device_id integrator_cpufreq_match[] = {
222 { .compatible = "arm,core-module-integrator"},
223 { },
224};
225
226static struct platform_driver integrator_cpufreq_driver = {
227 .driver = {
228 .name = "integrator-cpufreq",
229 .owner = THIS_MODULE,
230 .of_match_table = integrator_cpufreq_match,
231 },
232 .remove = __exit_p(integrator_cpufreq_remove),
233};
234
235module_platform_driver_probe(integrator_cpufreq_driver,
236 integrator_cpufreq_probe);
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238MODULE_AUTHOR ("Russell M. King");
239MODULE_DESCRIPTION ("cpufreq driver for ARM Integrator CPUs");
240MODULE_LICENSE ("GPL");