Michael Hennerich | e6c91b6 | 2008-04-25 04:58:29 +0800 | [diff] [blame] | 1 | /* |
Robin Getz | 96f1050 | 2009-09-24 14:11:24 +0000 | [diff] [blame] | 2 | * Blackfin core clock scaling |
Michael Hennerich | e6c91b6 | 2008-04-25 04:58:29 +0800 | [diff] [blame] | 3 | * |
Robin Getz | 96f1050 | 2009-09-24 14:11:24 +0000 | [diff] [blame] | 4 | * Copyright 2008-2009 Analog Devices Inc. |
Michael Hennerich | e6c91b6 | 2008-04-25 04:58:29 +0800 | [diff] [blame] | 5 | * |
Robin Getz | 96f1050 | 2009-09-24 14:11:24 +0000 | [diff] [blame] | 6 | * Licensed under the GPL-2 or later. |
Michael Hennerich | e6c91b6 | 2008-04-25 04:58:29 +0800 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/types.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/cpufreq.h> |
| 13 | #include <linux/fs.h> |
| 14 | #include <asm/blackfin.h> |
| 15 | #include <asm/time.h> |
Mike Frysinger | 761ec44 | 2009-10-15 17:12:05 +0000 | [diff] [blame^] | 16 | #include <asm/dpmc.h> |
Michael Hennerich | e6c91b6 | 2008-04-25 04:58:29 +0800 | [diff] [blame] | 17 | |
| 18 | /* this is the table of CCLK frequencies, in Hz */ |
| 19 | /* .index is the entry in the auxillary dpm_state_table[] */ |
| 20 | static struct cpufreq_frequency_table bfin_freq_table[] = { |
| 21 | { |
| 22 | .frequency = CPUFREQ_TABLE_END, |
| 23 | .index = 0, |
| 24 | }, |
| 25 | { |
| 26 | .frequency = CPUFREQ_TABLE_END, |
| 27 | .index = 1, |
| 28 | }, |
| 29 | { |
| 30 | .frequency = CPUFREQ_TABLE_END, |
| 31 | .index = 2, |
| 32 | }, |
| 33 | { |
| 34 | .frequency = CPUFREQ_TABLE_END, |
| 35 | .index = 0, |
| 36 | }, |
| 37 | }; |
| 38 | |
| 39 | static struct bfin_dpm_state { |
| 40 | unsigned int csel; /* system clock divider */ |
| 41 | unsigned int tscale; /* change the divider on the core timer interrupt */ |
| 42 | } dpm_state_table[3]; |
| 43 | |
Vitja Makarov | 1bfb4b2 | 2008-05-07 11:41:26 +0800 | [diff] [blame] | 44 | /* |
| 45 | normalized to maximum frequncy offset for CYCLES, |
| 46 | used in time-ts cycles clock source, but could be used |
| 47 | somewhere also. |
| 48 | */ |
| 49 | unsigned long long __bfin_cycles_off; |
| 50 | unsigned int __bfin_cycles_mod; |
| 51 | |
Michael Hennerich | e6c91b6 | 2008-04-25 04:58:29 +0800 | [diff] [blame] | 52 | /**************************************************************************/ |
| 53 | |
Michael Hennerich | a10101d | 2008-10-28 14:18:29 +0800 | [diff] [blame] | 54 | static unsigned int bfin_getfreq_khz(unsigned int cpu) |
Michael Hennerich | e6c91b6 | 2008-04-25 04:58:29 +0800 | [diff] [blame] | 55 | { |
| 56 | /* The driver only support single cpu */ |
| 57 | if (cpu != 0) |
| 58 | return -1; |
| 59 | |
Michael Hennerich | a10101d | 2008-10-28 14:18:29 +0800 | [diff] [blame] | 60 | return get_cclk() / 1000; |
Michael Hennerich | e6c91b6 | 2008-04-25 04:58:29 +0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | |
| 64 | static int bfin_target(struct cpufreq_policy *policy, |
| 65 | unsigned int target_freq, unsigned int relation) |
| 66 | { |
| 67 | unsigned int index, plldiv, tscale; |
| 68 | unsigned long flags, cclk_hz; |
| 69 | struct cpufreq_freqs freqs; |
Vitja Makarov | 1bfb4b2 | 2008-05-07 11:41:26 +0800 | [diff] [blame] | 70 | cycles_t cycles; |
Michael Hennerich | e6c91b6 | 2008-04-25 04:58:29 +0800 | [diff] [blame] | 71 | |
| 72 | if (cpufreq_frequency_table_target(policy, bfin_freq_table, |
| 73 | target_freq, relation, &index)) |
| 74 | return -EINVAL; |
| 75 | |
| 76 | cclk_hz = bfin_freq_table[index].frequency; |
| 77 | |
Michael Hennerich | a10101d | 2008-10-28 14:18:29 +0800 | [diff] [blame] | 78 | freqs.old = bfin_getfreq_khz(0); |
Michael Hennerich | e6c91b6 | 2008-04-25 04:58:29 +0800 | [diff] [blame] | 79 | freqs.new = cclk_hz; |
| 80 | freqs.cpu = 0; |
| 81 | |
| 82 | pr_debug("cpufreq: changing cclk to %lu; target = %u, oldfreq = %u\n", |
| 83 | cclk_hz, target_freq, freqs.old); |
| 84 | |
| 85 | cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE); |
Yi Li | 6a01f23 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 86 | local_irq_save_hw(flags); |
Michael Hennerich | e6c91b6 | 2008-04-25 04:58:29 +0800 | [diff] [blame] | 87 | plldiv = (bfin_read_PLL_DIV() & SSEL) | dpm_state_table[index].csel; |
| 88 | tscale = dpm_state_table[index].tscale; |
| 89 | bfin_write_PLL_DIV(plldiv); |
| 90 | /* we have to adjust the core timer, because it is using cclk */ |
| 91 | bfin_write_TSCALE(tscale); |
Vitja Makarov | 1bfb4b2 | 2008-05-07 11:41:26 +0800 | [diff] [blame] | 92 | cycles = get_cycles(); |
Michael Hennerich | e6c91b6 | 2008-04-25 04:58:29 +0800 | [diff] [blame] | 93 | SSYNC(); |
Nick Andrew | bc39ac6 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 94 | cycles += 10; /* ~10 cycles we lose after get_cycles() */ |
Vitja Makarov | 1bfb4b2 | 2008-05-07 11:41:26 +0800 | [diff] [blame] | 95 | __bfin_cycles_off += (cycles << __bfin_cycles_mod) - (cycles << index); |
| 96 | __bfin_cycles_mod = index; |
Yi Li | 6a01f23 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 97 | local_irq_restore_hw(flags); |
Vitja Makarov | 1bfb4b2 | 2008-05-07 11:41:26 +0800 | [diff] [blame] | 98 | /* TODO: just test case for cycles clock source, remove later */ |
| 99 | pr_debug("cpufreq: done\n"); |
Michael Hennerich | e6c91b6 | 2008-04-25 04:58:29 +0800 | [diff] [blame] | 100 | cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE); |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static int bfin_verify_speed(struct cpufreq_policy *policy) |
| 106 | { |
| 107 | return cpufreq_frequency_table_verify(policy, bfin_freq_table); |
| 108 | } |
| 109 | |
| 110 | static int __init __bfin_cpu_init(struct cpufreq_policy *policy) |
| 111 | { |
| 112 | |
| 113 | unsigned long cclk, sclk, csel, min_cclk; |
| 114 | int index; |
| 115 | |
Michael Hennerich | e6c91b6 | 2008-04-25 04:58:29 +0800 | [diff] [blame] | 116 | if (policy->cpu != 0) |
| 117 | return -EINVAL; |
| 118 | |
Michael Hennerich | a10101d | 2008-10-28 14:18:29 +0800 | [diff] [blame] | 119 | cclk = get_cclk() / 1000; |
| 120 | sclk = get_sclk() / 1000; |
Michael Hennerich | e6c91b6 | 2008-04-25 04:58:29 +0800 | [diff] [blame] | 121 | |
Sonic Zhang | 7f3aee3 | 2009-05-07 10:04:19 +0000 | [diff] [blame] | 122 | #if ANOMALY_05000273 || ANOMALY_05000274 || \ |
Jie Zhang | 41ba653 | 2009-06-16 09:48:33 +0000 | [diff] [blame] | 123 | (!defined(CONFIG_BF54x) && defined(CONFIG_BFIN_EXTMEM_DCACHEABLE)) |
Michael Hennerich | e6c91b6 | 2008-04-25 04:58:29 +0800 | [diff] [blame] | 124 | min_cclk = sclk * 2; |
| 125 | #else |
| 126 | min_cclk = sclk; |
| 127 | #endif |
| 128 | csel = ((bfin_read_PLL_DIV() & CSEL) >> 4); |
| 129 | |
| 130 | for (index = 0; (cclk >> index) >= min_cclk && csel <= 3; index++, csel++) { |
| 131 | bfin_freq_table[index].frequency = cclk >> index; |
| 132 | dpm_state_table[index].csel = csel << 4; /* Shift now into PLL_DIV bitpos */ |
| 133 | dpm_state_table[index].tscale = (TIME_SCALE / (1 << csel)) - 1; |
| 134 | |
Michael Hennerich | a10101d | 2008-10-28 14:18:29 +0800 | [diff] [blame] | 135 | pr_debug("cpufreq: freq:%d csel:0x%x tscale:%d\n", |
Michael Hennerich | e6c91b6 | 2008-04-25 04:58:29 +0800 | [diff] [blame] | 136 | bfin_freq_table[index].frequency, |
| 137 | dpm_state_table[index].csel, |
| 138 | dpm_state_table[index].tscale); |
| 139 | } |
| 140 | |
Michael Hennerich | d887a1c | 2009-09-25 09:03:21 +0000 | [diff] [blame] | 141 | policy->cpuinfo.transition_latency = 50000; /* 50us assumed */ |
| 142 | |
Michael Hennerich | e6c91b6 | 2008-04-25 04:58:29 +0800 | [diff] [blame] | 143 | /*Now ,only support one cpu */ |
| 144 | policy->cur = cclk; |
| 145 | cpufreq_frequency_table_get_attr(bfin_freq_table, policy->cpu); |
| 146 | return cpufreq_frequency_table_cpuinfo(policy, bfin_freq_table); |
| 147 | } |
| 148 | |
| 149 | static struct freq_attr *bfin_freq_attr[] = { |
| 150 | &cpufreq_freq_attr_scaling_available_freqs, |
| 151 | NULL, |
| 152 | }; |
| 153 | |
| 154 | static struct cpufreq_driver bfin_driver = { |
| 155 | .verify = bfin_verify_speed, |
| 156 | .target = bfin_target, |
Michael Hennerich | a10101d | 2008-10-28 14:18:29 +0800 | [diff] [blame] | 157 | .get = bfin_getfreq_khz, |
Michael Hennerich | e6c91b6 | 2008-04-25 04:58:29 +0800 | [diff] [blame] | 158 | .init = __bfin_cpu_init, |
| 159 | .name = "bfin cpufreq", |
| 160 | .owner = THIS_MODULE, |
| 161 | .attr = bfin_freq_attr, |
| 162 | }; |
| 163 | |
| 164 | static int __init bfin_cpu_init(void) |
| 165 | { |
| 166 | return cpufreq_register_driver(&bfin_driver); |
| 167 | } |
| 168 | |
| 169 | static void __exit bfin_cpu_exit(void) |
| 170 | { |
| 171 | cpufreq_unregister_driver(&bfin_driver); |
| 172 | } |
| 173 | |
| 174 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); |
| 175 | MODULE_DESCRIPTION("cpufreq driver for Blackfin"); |
| 176 | MODULE_LICENSE("GPL"); |
| 177 | |
| 178 | module_init(bfin_cpu_init); |
| 179 | module_exit(bfin_cpu_exit); |