blob: 12528b28d45d42931285c1a3c4fb603e35aa8e5a [file] [log] [blame]
Michael Henneriche6c91b62008-04-25 04:58:29 +08001/*
Robin Getz96f10502009-09-24 14:11:24 +00002 * Blackfin core clock scaling
Michael Henneriche6c91b62008-04-25 04:58:29 +08003 *
Michael Hennerich8944b5a2011-02-28 21:23:36 +00004 * Copyright 2008-2011 Analog Devices Inc.
Michael Henneriche6c91b62008-04-25 04:58:29 +08005 *
Robin Getz96f10502009-09-24 14:11:24 +00006 * Licensed under the GPL-2 or later.
Michael Henneriche6c91b62008-04-25 04:58:29 +08007 */
8
9#include <linux/kernel.h>
Paul Gortmaker6a550b92011-08-09 16:54:30 -040010#include <linux/module.h>
Michael Henneriche6c91b62008-04-25 04:58:29 +080011#include <linux/types.h>
12#include <linux/init.h>
Steven Miao96900312012-05-16 17:49:52 +080013#include <linux/clk.h>
Michael Henneriche6c91b62008-04-25 04:58:29 +080014#include <linux/cpufreq.h>
15#include <linux/fs.h>
Graf Yang7998a872010-03-08 03:01:35 +000016#include <linux/delay.h>
Michael Henneriche6c91b62008-04-25 04:58:29 +080017#include <asm/blackfin.h>
18#include <asm/time.h>
Mike Frysinger761ec442009-10-15 17:12:05 +000019#include <asm/dpmc.h>
Michael Henneriche6c91b62008-04-25 04:58:29 +080020
Steven Miao96900312012-05-16 17:49:52 +080021
Michael Henneriche6c91b62008-04-25 04:58:29 +080022/* this is the table of CCLK frequencies, in Hz */
Viresh Kumar50701582013-03-30 16:25:15 +053023/* .driver_data is the entry in the auxiliary dpm_state_table[] */
Michael Henneriche6c91b62008-04-25 04:58:29 +080024static struct cpufreq_frequency_table bfin_freq_table[] = {
25 {
26 .frequency = CPUFREQ_TABLE_END,
Viresh Kumar50701582013-03-30 16:25:15 +053027 .driver_data = 0,
Michael Henneriche6c91b62008-04-25 04:58:29 +080028 },
29 {
30 .frequency = CPUFREQ_TABLE_END,
Viresh Kumar50701582013-03-30 16:25:15 +053031 .driver_data = 1,
Michael Henneriche6c91b62008-04-25 04:58:29 +080032 },
33 {
34 .frequency = CPUFREQ_TABLE_END,
Viresh Kumar50701582013-03-30 16:25:15 +053035 .driver_data = 2,
Michael Henneriche6c91b62008-04-25 04:58:29 +080036 },
37 {
38 .frequency = CPUFREQ_TABLE_END,
Viresh Kumar50701582013-03-30 16:25:15 +053039 .driver_data = 0,
Michael Henneriche6c91b62008-04-25 04:58:29 +080040 },
41};
42
43static struct bfin_dpm_state {
44 unsigned int csel; /* system clock divider */
45 unsigned int tscale; /* change the divider on the core timer interrupt */
46} dpm_state_table[3];
47
Graf Yang6c2b7072010-01-27 11:16:32 +000048#if defined(CONFIG_CYCLES_CLOCKSOURCE)
Vitja Makarov1bfb4b22008-05-07 11:41:26 +080049/*
Michael Hennerich8944b5a2011-02-28 21:23:36 +000050 * normalized to maximum frequency offset for CYCLES,
Graf Yang6c2b7072010-01-27 11:16:32 +000051 * used in time-ts cycles clock source, but could be used
52 * somewhere also.
Vitja Makarov1bfb4b22008-05-07 11:41:26 +080053 */
54unsigned long long __bfin_cycles_off;
55unsigned int __bfin_cycles_mod;
Graf Yang6c2b7072010-01-27 11:16:32 +000056#endif
Vitja Makarov1bfb4b22008-05-07 11:41:26 +080057
Michael Henneriche6c91b62008-04-25 04:58:29 +080058/**************************************************************************/
Graf Yang6c2b7072010-01-27 11:16:32 +000059static void __init bfin_init_tables(unsigned long cclk, unsigned long sclk)
Michael Henneriche6c91b62008-04-25 04:58:29 +080060{
61
Graf Yang6c2b7072010-01-27 11:16:32 +000062 unsigned long csel, min_cclk;
Michael Henneriche6c91b62008-04-25 04:58:29 +080063 int index;
64
Graf Yang6c2b7072010-01-27 11:16:32 +000065 /* Anomaly 273 seems to still exist on non-BF54x w/dcache turned on */
Sonic Zhang7f3aee32009-05-07 10:04:19 +000066#if ANOMALY_05000273 || ANOMALY_05000274 || \
Sonic Zhang7c7d0272012-07-17 13:40:15 +080067 (!(defined(CONFIG_BF54x) || defined(CONFIG_BF60x)) \
68 && defined(CONFIG_BFIN_EXTMEM_DCACHEABLE))
Michael Henneriche6c91b62008-04-25 04:58:29 +080069 min_cclk = sclk * 2;
70#else
71 min_cclk = sclk;
72#endif
Steven Miao96900312012-05-16 17:49:52 +080073
74#ifndef CONFIG_BF60x
Michael Henneriche6c91b62008-04-25 04:58:29 +080075 csel = ((bfin_read_PLL_DIV() & CSEL) >> 4);
Steven Miao96900312012-05-16 17:49:52 +080076#else
77 csel = bfin_read32(CGU0_DIV) & 0x1F;
78#endif
Michael Henneriche6c91b62008-04-25 04:58:29 +080079
James Cosin810f1512012-08-20 11:55:36 +080080 for (index = 0; (cclk >> index) >= min_cclk && csel <= 3 && index < 3; index++, csel++) {
Michael Henneriche6c91b62008-04-25 04:58:29 +080081 bfin_freq_table[index].frequency = cclk >> index;
Steven Miao96900312012-05-16 17:49:52 +080082#ifndef CONFIG_BF60x
Michael Henneriche6c91b62008-04-25 04:58:29 +080083 dpm_state_table[index].csel = csel << 4; /* Shift now into PLL_DIV bitpos */
Steven Miao96900312012-05-16 17:49:52 +080084#else
85 dpm_state_table[index].csel = csel;
Steven Miao96900312012-05-16 17:49:52 +080086#endif
James Cosin810f1512012-08-20 11:55:36 +080087 dpm_state_table[index].tscale = (TIME_SCALE >> index) - 1;
Michael Henneriche6c91b62008-04-25 04:58:29 +080088
Michael Hennericha10101d2008-10-28 14:18:29 +080089 pr_debug("cpufreq: freq:%d csel:0x%x tscale:%d\n",
Michael Henneriche6c91b62008-04-25 04:58:29 +080090 bfin_freq_table[index].frequency,
91 dpm_state_table[index].csel,
92 dpm_state_table[index].tscale);
93 }
Graf Yang6c2b7072010-01-27 11:16:32 +000094 return;
95}
96
97static void bfin_adjust_core_timer(void *info)
98{
99 unsigned int tscale;
100 unsigned int index = *(unsigned int *)info;
101
102 /* we have to adjust the core timer, because it is using cclk */
103 tscale = dpm_state_table[index].tscale;
104 bfin_write_TSCALE(tscale);
105 return;
106}
107
108static unsigned int bfin_getfreq_khz(unsigned int cpu)
109{
110 /* Both CoreA/B have the same core clock */
111 return get_cclk() / 1000;
112}
113
Bob Liu3a3cf0d2012-05-17 14:21:22 +0800114#ifdef CONFIG_BF60x
Steven Miao96900312012-05-16 17:49:52 +0800115unsigned long cpu_set_cclk(int cpu, unsigned long new)
116{
117 struct clk *clk;
118 int ret;
119
120 clk = clk_get(NULL, "CCLK");
121 if (IS_ERR(clk))
122 return -ENODEV;
123
124 ret = clk_set_rate(clk, new);
125 clk_put(clk);
126 return ret;
127}
Bob Liu3a3cf0d2012-05-17 14:21:22 +0800128#endif
Steven Miao96900312012-05-16 17:49:52 +0800129
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530130static int bfin_target(struct cpufreq_policy *policy, unsigned int index)
Graf Yang6c2b7072010-01-27 11:16:32 +0000131{
Bob Liu3a3cf0d2012-05-17 14:21:22 +0800132#ifndef CONFIG_BF60x
133 unsigned int plldiv;
134#endif
Graf Yang6c2b7072010-01-27 11:16:32 +0000135 struct cpufreq_freqs freqs;
Graf Yang7998a872010-03-08 03:01:35 +0000136 static unsigned long lpj_ref;
137 static unsigned int lpj_ref_freq;
Steven Miao96900312012-05-16 17:49:52 +0800138 int ret = 0;
Graf Yang7998a872010-03-08 03:01:35 +0000139
Graf Yang6c2b7072010-01-27 11:16:32 +0000140#if defined(CONFIG_CYCLES_CLOCKSOURCE)
141 cycles_t cycles;
142#endif
143
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530144 freqs.old = bfin_getfreq_khz(0);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530145 freqs.new = bfin_freq_table[index].frequency;
Graf Yang6c2b7072010-01-27 11:16:32 +0000146
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530147 pr_debug("cpufreq: changing cclk to %lu; target = %u, oldfreq = %u\n",
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530148 freqs.new, freqs.new, freqs.old);
Graf Yang6c2b7072010-01-27 11:16:32 +0000149
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530150 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
Steven Miao96900312012-05-16 17:49:52 +0800151#ifndef CONFIG_BF60x
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530152 plldiv = (bfin_read_PLL_DIV() & SSEL) | dpm_state_table[index].csel;
153 bfin_write_PLL_DIV(plldiv);
Steven Miao96900312012-05-16 17:49:52 +0800154#else
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530155 ret = cpu_set_cclk(policy->cpu, freqs.new * 1000);
156 if (ret != 0) {
157 WARN_ONCE(ret, "cpufreq set freq failed %d\n", ret);
158 return ret;
Graf Yang6c2b7072010-01-27 11:16:32 +0000159 }
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530160#endif
161 on_each_cpu(bfin_adjust_core_timer, &index, 1);
162#if defined(CONFIG_CYCLES_CLOCKSOURCE)
163 cycles = get_cycles();
164 SSYNC();
165 cycles += 10; /* ~10 cycles we lose after get_cycles() */
166 __bfin_cycles_off += (cycles << __bfin_cycles_mod) - (cycles << index);
167 __bfin_cycles_mod = index;
168#endif
169 if (!lpj_ref_freq) {
170 lpj_ref = loops_per_jiffy;
171 lpj_ref_freq = freqs.old;
172 }
173 if (freqs.new != freqs.old) {
174 loops_per_jiffy = cpufreq_scale(lpj_ref,
175 lpj_ref_freq, freqs.new);
176 }
177
178 /* TODO: just test case for cycles clock source, remove later */
179 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Graf Yang6c2b7072010-01-27 11:16:32 +0000180
181 pr_debug("cpufreq: done\n");
Steven Miao96900312012-05-16 17:49:52 +0800182 return ret;
Graf Yang6c2b7072010-01-27 11:16:32 +0000183}
184
Steven Miao96900312012-05-16 17:49:52 +0800185static int __bfin_cpu_init(struct cpufreq_policy *policy)
Graf Yang6c2b7072010-01-27 11:16:32 +0000186{
187
188 unsigned long cclk, sclk;
189
190 cclk = get_cclk() / 1000;
191 sclk = get_sclk() / 1000;
192
193 if (policy->cpu == CPUFREQ_CPU)
194 bfin_init_tables(cclk, sclk);
Michael Henneriche6c91b62008-04-25 04:58:29 +0800195
Michael Hennerichd887a1c2009-09-25 09:03:21 +0000196 policy->cpuinfo.transition_latency = 50000; /* 50us assumed */
197
Viresh Kumare2889e22013-09-16 18:56:09 +0530198 return cpufreq_table_validate_and_show(policy, bfin_freq_table);
Michael Henneriche6c91b62008-04-25 04:58:29 +0800199}
200
Michael Henneriche6c91b62008-04-25 04:58:29 +0800201static struct cpufreq_driver bfin_driver = {
Viresh Kumar00ff4242013-10-03 20:27:59 +0530202 .verify = cpufreq_generic_frequency_table_verify,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530203 .target_index = bfin_target,
Michael Hennericha10101d2008-10-28 14:18:29 +0800204 .get = bfin_getfreq_khz,
Michael Henneriche6c91b62008-04-25 04:58:29 +0800205 .init = __bfin_cpu_init,
Viresh Kumar00ff4242013-10-03 20:27:59 +0530206 .exit = cpufreq_generic_exit,
Michael Henneriche6c91b62008-04-25 04:58:29 +0800207 .name = "bfin cpufreq",
Viresh Kumar00ff4242013-10-03 20:27:59 +0530208 .attr = cpufreq_generic_attr,
Michael Henneriche6c91b62008-04-25 04:58:29 +0800209};
210
211static int __init bfin_cpu_init(void)
212{
213 return cpufreq_register_driver(&bfin_driver);
214}
215
216static void __exit bfin_cpu_exit(void)
217{
218 cpufreq_unregister_driver(&bfin_driver);
219}
220
221MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
222MODULE_DESCRIPTION("cpufreq driver for Blackfin");
223MODULE_LICENSE("GPL");
224
225module_init(bfin_cpu_init);
226module_exit(bfin_cpu_exit);