Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (C) 2001-2002 Deep Blue Solutions Ltd. |
| 3 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/sched.h> |
| 15 | #include <linux/smp.h> |
| 16 | #include <linux/init.h> |
Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 17 | #include <linux/io.h> |
Linus Walleij | bdac7ea | 2013-06-17 22:52:32 +0200 | [diff] [blame] | 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/of.h> |
| 20 | #include <linux/of_address.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <asm/mach-types.h> |
Russell King | c5a0adb | 2010-01-16 20:16:10 +0000 | [diff] [blame] | 23 | #include <asm/hardware/icst.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Linus Walleij | bdac7ea | 2013-06-17 22:52:32 +0200 | [diff] [blame] | 25 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Linus Walleij | bdac7ea | 2013-06-17 22:52:32 +0200 | [diff] [blame] | 30 | static struct cpufreq_driver integrator_driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Russell King | 39c0cb0 | 2010-01-16 16:27:28 +0000 | [diff] [blame] | 32 | static const struct icst_params lclk_params = { |
Russell King | 64fceb1 | 2010-01-16 17:28:44 +0000 | [diff] [blame] | 33 | .ref = 24000000, |
Russell King | 4de2edb | 2010-01-16 18:08:47 +0000 | [diff] [blame] | 34 | .vco_max = ICST525_VCO_MAX_5V, |
Russell King | e73a46a | 2010-01-16 19:49:39 +0000 | [diff] [blame] | 35 | .vco_min = ICST525_VCO_MIN, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | .vd_min = 8, |
| 37 | .vd_max = 132, |
| 38 | .rd_min = 24, |
| 39 | .rd_max = 24, |
Russell King | 232eaf7 | 2010-01-16 19:46:19 +0000 | [diff] [blame] | 40 | .s2div = icst525_s2div, |
| 41 | .idx2s = icst525_idx2s, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Russell King | 39c0cb0 | 2010-01-16 16:27:28 +0000 | [diff] [blame] | 44 | static const struct icst_params cclk_params = { |
Russell King | 64fceb1 | 2010-01-16 17:28:44 +0000 | [diff] [blame] | 45 | .ref = 24000000, |
Russell King | 4de2edb | 2010-01-16 18:08:47 +0000 | [diff] [blame] | 46 | .vco_max = ICST525_VCO_MAX_5V, |
Russell King | e73a46a | 2010-01-16 19:49:39 +0000 | [diff] [blame] | 47 | .vco_min = ICST525_VCO_MIN, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | .vd_min = 12, |
| 49 | .vd_max = 160, |
| 50 | .rd_min = 24, |
| 51 | .rd_max = 24, |
Russell King | 232eaf7 | 2010-01-16 19:46:19 +0000 | [diff] [blame] | 52 | .s2div = icst525_s2div, |
| 53 | .idx2s = icst525_idx2s, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | /* |
| 57 | * Validate the speed policy. |
| 58 | */ |
| 59 | static int integrator_verify_policy(struct cpufreq_policy *policy) |
| 60 | { |
Russell King | 39c0cb0 | 2010-01-16 16:27:28 +0000 | [diff] [blame] | 61 | struct icst_vco vco; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Viresh Kumar | be49e34 | 2013-10-02 14:13:19 +0530 | [diff] [blame] | 63 | cpufreq_verify_within_cpu_limits(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Russell King | c5a0adb | 2010-01-16 20:16:10 +0000 | [diff] [blame] | 65 | vco = icst_hz_to_vco(&cclk_params, policy->max * 1000); |
| 66 | policy->max = icst_hz(&cclk_params, vco) / 1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Russell King | c5a0adb | 2010-01-16 20:16:10 +0000 | [diff] [blame] | 68 | vco = icst_hz_to_vco(&cclk_params, policy->min * 1000); |
| 69 | policy->min = icst_hz(&cclk_params, vco) / 1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Viresh Kumar | be49e34 | 2013-10-02 14:13:19 +0530 | [diff] [blame] | 71 | cpufreq_verify_within_cpu_limits(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | static 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 King | 39c0cb0 | 2010-01-16 16:27:28 +0000 | [diff] [blame] | 82 | struct icst_vco vco; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | 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 Saraogi | 18360d6 | 2014-08-03 15:45:40 +0530 | [diff] [blame] | 95 | set_cpus_allowed_ptr(current, cpumask_of(cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | BUG_ON(cpu != smp_processor_id()); |
| 97 | |
| 98 | /* get current setting */ |
Linus Walleij | bdac7ea | 2013-06-17 22:52:32 +0200 | [diff] [blame] | 99 | cm_osc = __raw_readl(cm_base + INTEGRATOR_HDR_OSC_OFFSET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Cristian Ardelean | 3a5f5b2 | 2015-07-10 14:42:15 -0400 | [diff] [blame] | 101 | if (machine_is_integrator()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | vco.s = (cm_osc >> 8) & 7; |
Cristian Ardelean | 3a5f5b2 | 2015-07-10 14:42:15 -0400 | [diff] [blame] | 103 | else if (machine_is_cintegrator()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | vco.s = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | vco.v = cm_osc & 255; |
| 106 | vco.r = 22; |
Russell King | c5a0adb | 2010-01-16 20:16:10 +0000 | [diff] [blame] | 107 | freqs.old = icst_hz(&cclk_params, vco) / 1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
Russell King | c5a0adb | 2010-01-16 20:16:10 +0000 | [diff] [blame] | 109 | /* icst_hz_to_vco rounds down -- so we need the next |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | * 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 King | c5a0adb | 2010-01-16 20:16:10 +0000 | [diff] [blame] | 116 | vco = icst_hz_to_vco(&cclk_params, target_freq * 1000); |
| 117 | freqs.new = icst_hz(&cclk_params, vco) / 1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | if (freqs.old == freqs.new) { |
Himangi Saraogi | 18360d6 | 2014-08-03 15:45:40 +0530 | [diff] [blame] | 120 | set_cpus_allowed_ptr(current, &cpus_allowed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | |
Viresh Kumar | 8fec051 | 2014-03-24 13:35:45 +0530 | [diff] [blame] | 124 | cpufreq_freq_transition_begin(policy, &freqs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
Linus Walleij | bdac7ea | 2013-06-17 22:52:32 +0200 | [diff] [blame] | 126 | cm_osc = __raw_readl(cm_base + INTEGRATOR_HDR_OSC_OFFSET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Walleij | bdac7ea | 2013-06-17 22:52:32 +0200 | [diff] [blame] | 136 | __raw_writel(0xa05f, cm_base + INTEGRATOR_HDR_LOCK_OFFSET); |
| 137 | __raw_writel(cm_osc, cm_base + INTEGRATOR_HDR_OSC_OFFSET); |
| 138 | __raw_writel(0, cm_base + INTEGRATOR_HDR_LOCK_OFFSET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * Restore the CPUs allowed mask. |
| 142 | */ |
Himangi Saraogi | 18360d6 | 2014-08-03 15:45:40 +0530 | [diff] [blame] | 143 | set_cpus_allowed_ptr(current, &cpus_allowed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Viresh Kumar | 8fec051 | 2014-03-24 13:35:45 +0530 | [diff] [blame] | 145 | cpufreq_freq_transition_end(policy, &freqs, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static unsigned int integrator_get(unsigned int cpu) |
| 151 | { |
| 152 | cpumask_t cpus_allowed; |
| 153 | unsigned int current_freq; |
| 154 | u_int cm_osc; |
Russell King | 39c0cb0 | 2010-01-16 16:27:28 +0000 | [diff] [blame] | 155 | struct icst_vco vco; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
| 157 | cpus_allowed = current->cpus_allowed; |
| 158 | |
Himangi Saraogi | 18360d6 | 2014-08-03 15:45:40 +0530 | [diff] [blame] | 159 | set_cpus_allowed_ptr(current, cpumask_of(cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | BUG_ON(cpu != smp_processor_id()); |
| 161 | |
| 162 | /* detect memory etc. */ |
Linus Walleij | bdac7ea | 2013-06-17 22:52:32 +0200 | [diff] [blame] | 163 | cm_osc = __raw_readl(cm_base + INTEGRATOR_HDR_OSC_OFFSET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
Cristian Ardelean | 3a5f5b2 | 2015-07-10 14:42:15 -0400 | [diff] [blame] | 165 | if (machine_is_integrator()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | vco.s = (cm_osc >> 8) & 7; |
Cristian Ardelean | 3a5f5b2 | 2015-07-10 14:42:15 -0400 | [diff] [blame] | 167 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | vco.s = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | vco.v = cm_osc & 255; |
| 170 | vco.r = 22; |
| 171 | |
Russell King | c5a0adb | 2010-01-16 20:16:10 +0000 | [diff] [blame] | 172 | current_freq = icst_hz(&cclk_params, vco) / 1000; /* current freq */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
Himangi Saraogi | 18360d6 | 2014-08-03 15:45:40 +0530 | [diff] [blame] | 174 | set_cpus_allowed_ptr(current, &cpus_allowed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
| 176 | return current_freq; |
| 177 | } |
| 178 | |
| 179 | static int integrator_cpufreq_init(struct cpufreq_policy *policy) |
| 180 | { |
| 181 | |
| 182 | /* set default policy and cpuinfo */ |
Viresh Kumar | ab53701 | 2013-10-03 20:28:46 +0530 | [diff] [blame] | 183 | policy->max = policy->cpuinfo.max_freq = 160000; |
| 184 | policy->min = policy->cpuinfo.min_freq = 12000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | policy->cpuinfo.transition_latency = 1000000; /* 1 ms, assumed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | static struct cpufreq_driver integrator_driver = { |
Viresh Kumar | ae6b427 | 2013-12-03 11:20:45 +0530 | [diff] [blame] | 191 | .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | .verify = integrator_verify_policy, |
| 193 | .target = integrator_set_target, |
| 194 | .get = integrator_get, |
| 195 | .init = integrator_cpufreq_init, |
| 196 | .name = "integrator", |
| 197 | }; |
| 198 | |
Linus Walleij | bdac7ea | 2013-06-17 22:52:32 +0200 | [diff] [blame] | 199 | static int __init integrator_cpufreq_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
Linus Walleij | bdac7ea | 2013-06-17 22:52:32 +0200 | [diff] [blame] | 201 | struct resource *res; |
| 202 | |
| 203 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Cristian Ardelean | 3a5f5b2 | 2015-07-10 14:42:15 -0400 | [diff] [blame] | 204 | if (!res) |
Linus Walleij | bdac7ea | 2013-06-17 22:52:32 +0200 | [diff] [blame] | 205 | return -ENODEV; |
| 206 | |
| 207 | cm_base = devm_ioremap(&pdev->dev, res->start, resource_size(res)); |
| 208 | if (!cm_base) |
| 209 | return -ENODEV; |
| 210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | return cpufreq_register_driver(&integrator_driver); |
| 212 | } |
| 213 | |
Arnd Bergmann | d62dbf7 | 2014-09-26 22:19:12 +0200 | [diff] [blame] | 214 | static int __exit integrator_cpufreq_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | { |
Arnd Bergmann | d62dbf7 | 2014-09-26 22:19:12 +0200 | [diff] [blame] | 216 | return cpufreq_unregister_driver(&integrator_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Linus Walleij | bdac7ea | 2013-06-17 22:52:32 +0200 | [diff] [blame] | 219 | static const struct of_device_id integrator_cpufreq_match[] = { |
| 220 | { .compatible = "arm,core-module-integrator"}, |
| 221 | { }, |
| 222 | }; |
| 223 | |
Luis de Bethencourt | 78ce6db | 2015-09-16 22:24:46 +0200 | [diff] [blame] | 224 | MODULE_DEVICE_TABLE(of, integrator_cpufreq_match); |
| 225 | |
Linus Walleij | bdac7ea | 2013-06-17 22:52:32 +0200 | [diff] [blame] | 226 | static struct platform_driver integrator_cpufreq_driver = { |
| 227 | .driver = { |
| 228 | .name = "integrator-cpufreq", |
Linus Walleij | bdac7ea | 2013-06-17 22:52:32 +0200 | [diff] [blame] | 229 | .of_match_table = integrator_cpufreq_match, |
| 230 | }, |
| 231 | .remove = __exit_p(integrator_cpufreq_remove), |
| 232 | }; |
| 233 | |
| 234 | module_platform_driver_probe(integrator_cpufreq_driver, |
| 235 | integrator_cpufreq_probe); |
| 236 | |
Cristian Ardelean | 3a5f5b2 | 2015-07-10 14:42:15 -0400 | [diff] [blame] | 237 | MODULE_AUTHOR("Russell M. King"); |
| 238 | MODULE_DESCRIPTION("cpufreq driver for ARM Integrator CPUs"); |
| 239 | MODULE_LICENSE("GPL"); |