Jesper Nilsson | 035e111 | 2007-11-29 17:11:23 +0100 | [diff] [blame] | 1 | #include <linux/init.h> |
| 2 | #include <linux/module.h> |
| 3 | #include <linux/cpufreq.h> |
| 4 | #include <hwregs/reg_map.h> |
| 5 | #include <hwregs/reg_rdwr.h> |
| 6 | #include <hwregs/clkgen_defs.h> |
| 7 | #include <hwregs/ddr2_defs.h> |
| 8 | |
| 9 | static int |
| 10 | cris_sdram_freq_notifier(struct notifier_block *nb, unsigned long val, |
| 11 | void *data); |
| 12 | |
| 13 | static struct notifier_block cris_sdram_freq_notifier_block = { |
| 14 | .notifier_call = cris_sdram_freq_notifier |
| 15 | }; |
| 16 | |
| 17 | static struct cpufreq_frequency_table cris_freq_table[] = { |
| 18 | {0x01, 6000}, |
| 19 | {0x02, 200000}, |
| 20 | {0, CPUFREQ_TABLE_END}, |
| 21 | }; |
| 22 | |
| 23 | static unsigned int cris_freq_get_cpu_frequency(unsigned int cpu) |
| 24 | { |
| 25 | reg_clkgen_rw_clk_ctrl clk_ctrl; |
| 26 | clk_ctrl = REG_RD(clkgen, regi_clkgen, rw_clk_ctrl); |
| 27 | return clk_ctrl.pll ? 200000 : 6000; |
| 28 | } |
| 29 | |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 30 | static void cris_freq_set_cpu_state(struct cpufreq_policy *policy, |
| 31 | unsigned int state) |
Jesper Nilsson | 035e111 | 2007-11-29 17:11:23 +0100 | [diff] [blame] | 32 | { |
Jesper Nilsson | 035e111 | 2007-11-29 17:11:23 +0100 | [diff] [blame] | 33 | struct cpufreq_freqs freqs; |
| 34 | reg_clkgen_rw_clk_ctrl clk_ctrl; |
| 35 | clk_ctrl = REG_RD(clkgen, regi_clkgen, rw_clk_ctrl); |
| 36 | |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 37 | freqs.old = cris_freq_get_cpu_frequency(policy->cpu); |
| 38 | freqs.new = cris_freq_table[state].frequency; |
Jesper Nilsson | 035e111 | 2007-11-29 17:11:23 +0100 | [diff] [blame] | 39 | |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 40 | cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE); |
Jesper Nilsson | 035e111 | 2007-11-29 17:11:23 +0100 | [diff] [blame] | 41 | |
| 42 | local_irq_disable(); |
| 43 | |
| 44 | /* Even though we may be SMP they will share the same clock |
| 45 | * so all settings are made on CPU0. */ |
| 46 | if (cris_freq_table[state].frequency == 200000) |
| 47 | clk_ctrl.pll = 1; |
| 48 | else |
| 49 | clk_ctrl.pll = 0; |
| 50 | REG_WR(clkgen, regi_clkgen, rw_clk_ctrl, clk_ctrl); |
| 51 | |
| 52 | local_irq_enable(); |
| 53 | |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 54 | cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE); |
Jesper Nilsson | 035e111 | 2007-11-29 17:11:23 +0100 | [diff] [blame] | 55 | }; |
| 56 | |
Jesper Nilsson | 035e111 | 2007-11-29 17:11:23 +0100 | [diff] [blame] | 57 | static int cris_freq_target(struct cpufreq_policy *policy, |
| 58 | unsigned int target_freq, |
| 59 | unsigned int relation) |
| 60 | { |
| 61 | unsigned int newstate = 0; |
| 62 | |
| 63 | if (cpufreq_frequency_table_target(policy, cris_freq_table, |
| 64 | target_freq, relation, &newstate)) |
| 65 | return -EINVAL; |
| 66 | |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 67 | cris_freq_set_cpu_state(policy, newstate); |
Jesper Nilsson | 035e111 | 2007-11-29 17:11:23 +0100 | [diff] [blame] | 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | static int cris_freq_cpu_init(struct cpufreq_policy *policy) |
| 73 | { |
Jesper Nilsson | 035e111 | 2007-11-29 17:11:23 +0100 | [diff] [blame] | 74 | /* cpuinfo and default policy values */ |
Jesper Nilsson | 035e111 | 2007-11-29 17:11:23 +0100 | [diff] [blame] | 75 | policy->cpuinfo.transition_latency = 1000000; /* 1ms */ |
| 76 | policy->cur = cris_freq_get_cpu_frequency(0); |
| 77 | |
Viresh Kumar | ae24b5c | 2013-09-16 18:56:11 +0530 | [diff] [blame] | 78 | return cpufreq_table_validate_and_show(policy, cris_freq_table); |
Jesper Nilsson | 035e111 | 2007-11-29 17:11:23 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | |
Jesper Nilsson | 035e111 | 2007-11-29 17:11:23 +0100 | [diff] [blame] | 82 | static struct cpufreq_driver cris_freq_driver = { |
| 83 | .get = cris_freq_get_cpu_frequency, |
Viresh Kumar | 361db10 | 2013-10-03 20:28:01 +0530 | [diff] [blame^] | 84 | .verify = cpufreq_generic_frequency_table_verify, |
Jesper Nilsson | 035e111 | 2007-11-29 17:11:23 +0100 | [diff] [blame] | 85 | .target = cris_freq_target, |
| 86 | .init = cris_freq_cpu_init, |
Viresh Kumar | 361db10 | 2013-10-03 20:28:01 +0530 | [diff] [blame^] | 87 | .exit = cpufreq_generic_exit, |
Jesper Nilsson | 035e111 | 2007-11-29 17:11:23 +0100 | [diff] [blame] | 88 | .name = "cris_freq", |
Viresh Kumar | 361db10 | 2013-10-03 20:28:01 +0530 | [diff] [blame^] | 89 | .attr = cpufreq_generic_attr, |
Jesper Nilsson | 035e111 | 2007-11-29 17:11:23 +0100 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | static int __init cris_freq_init(void) |
| 93 | { |
| 94 | int ret; |
| 95 | ret = cpufreq_register_driver(&cris_freq_driver); |
| 96 | cpufreq_register_notifier(&cris_sdram_freq_notifier_block, |
| 97 | CPUFREQ_TRANSITION_NOTIFIER); |
| 98 | return ret; |
| 99 | } |
| 100 | |
| 101 | static int |
| 102 | cris_sdram_freq_notifier(struct notifier_block *nb, unsigned long val, |
| 103 | void *data) |
| 104 | { |
| 105 | int i; |
| 106 | struct cpufreq_freqs *freqs = data; |
| 107 | if (val == CPUFREQ_PRECHANGE) { |
| 108 | reg_ddr2_rw_cfg cfg = |
| 109 | REG_RD(ddr2, regi_ddr2_ctrl, rw_cfg); |
| 110 | cfg.ref_interval = (freqs->new == 200000 ? 1560 : 46); |
| 111 | |
| 112 | if (freqs->new == 200000) |
| 113 | for (i = 0; i < 50000; i++); |
| 114 | REG_WR(bif_core, regi_bif_core, rw_sdram_timing, timing); |
| 115 | } |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | module_init(cris_freq_init); |