blob: 72328f77dc53bdef993173a1832d3159ac3fd03c [file] [log] [blame]
Jesper Nilsson035e1112007-11-29 17:11:23 +01001#include <linux/init.h>
2#include <linux/module.h>
3#include <linux/cpufreq.h>
4#include <hwregs/reg_map.h>
Jesper Nilsson556dcee2008-10-21 17:45:58 +02005#include <arch/hwregs/reg_rdwr.h>
6#include <arch/hwregs/config_defs.h>
7#include <arch/hwregs/bif_core_defs.h>
Jesper Nilsson035e1112007-11-29 17:11:23 +01008
9static int
10cris_sdram_freq_notifier(struct notifier_block *nb, unsigned long val,
11 void *data);
12
13static struct notifier_block cris_sdram_freq_notifier_block = {
14 .notifier_call = cris_sdram_freq_notifier
15};
16
17static struct cpufreq_frequency_table cris_freq_table[] = {
18 {0x01, 6000},
19 {0x02, 200000},
20 {0, CPUFREQ_TABLE_END},
21};
22
23static unsigned int cris_freq_get_cpu_frequency(unsigned int cpu)
24{
25 reg_config_rw_clk_ctrl clk_ctrl;
26 clk_ctrl = REG_RD(config, regi_config, rw_clk_ctrl);
27 return clk_ctrl.pll ? 200000 : 6000;
28}
29
Viresh Kumarb43a7ff2013-03-24 11:56:43 +053030static void cris_freq_set_cpu_state(struct cpufreq_policy *policy,
31 unsigned int state)
Jesper Nilsson035e1112007-11-29 17:11:23 +010032{
Jesper Nilsson035e1112007-11-29 17:11:23 +010033 struct cpufreq_freqs freqs;
34 reg_config_rw_clk_ctrl clk_ctrl;
35 clk_ctrl = REG_RD(config, regi_config, rw_clk_ctrl);
36
Viresh Kumarb43a7ff2013-03-24 11:56:43 +053037 freqs.old = cris_freq_get_cpu_frequency(policy->cpu);
38 freqs.new = cris_freq_table[state].frequency;
Jesper Nilsson035e1112007-11-29 17:11:23 +010039
Viresh Kumarb43a7ff2013-03-24 11:56:43 +053040 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
Jesper Nilsson035e1112007-11-29 17:11:23 +010041
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(config, regi_config, rw_clk_ctrl, clk_ctrl);
51
52 local_irq_enable();
53
Viresh Kumarb43a7ff2013-03-24 11:56:43 +053054 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Jesper Nilsson035e1112007-11-29 17:11:23 +010055};
56
57static int cris_freq_verify(struct cpufreq_policy *policy)
58{
59 return cpufreq_frequency_table_verify(policy, &cris_freq_table[0]);
60}
61
62static int cris_freq_target(struct cpufreq_policy *policy,
63 unsigned int target_freq, unsigned int relation)
64{
65 unsigned int newstate = 0;
66
67 if (cpufreq_frequency_table_target
68 (policy, cris_freq_table, target_freq, relation, &newstate))
69 return -EINVAL;
70
Viresh Kumarb43a7ff2013-03-24 11:56:43 +053071 cris_freq_set_cpu_state(policy, newstate);
Jesper Nilsson035e1112007-11-29 17:11:23 +010072
73 return 0;
74}
75
76static int cris_freq_cpu_init(struct cpufreq_policy *policy)
77{
78 int result;
79
80 /* cpuinfo and default policy values */
Jesper Nilsson035e1112007-11-29 17:11:23 +010081 policy->cpuinfo.transition_latency = 1000000; /* 1ms */
82 policy->cur = cris_freq_get_cpu_frequency(0);
83
84 result = cpufreq_frequency_table_cpuinfo(policy, cris_freq_table);
85 if (result)
86 return (result);
87
88 cpufreq_frequency_table_get_attr(cris_freq_table, policy->cpu);
89
90 return 0;
91}
92
93static int cris_freq_cpu_exit(struct cpufreq_policy *policy)
94{
95 cpufreq_frequency_table_put_attr(policy->cpu);
96 return 0;
97}
98
99static struct freq_attr *cris_freq_attr[] = {
100 &cpufreq_freq_attr_scaling_available_freqs,
101 NULL,
102};
103
104static struct cpufreq_driver cris_freq_driver = {
105 .get = cris_freq_get_cpu_frequency,
106 .verify = cris_freq_verify,
107 .target = cris_freq_target,
108 .init = cris_freq_cpu_init,
109 .exit = cris_freq_cpu_exit,
110 .name = "cris_freq",
Jesper Nilsson035e1112007-11-29 17:11:23 +0100111 .attr = cris_freq_attr,
112};
113
114static int __init cris_freq_init(void)
115{
116 int ret;
117 ret = cpufreq_register_driver(&cris_freq_driver);
118 cpufreq_register_notifier(&cris_sdram_freq_notifier_block,
119 CPUFREQ_TRANSITION_NOTIFIER);
120 return ret;
121}
122
123static int
124cris_sdram_freq_notifier(struct notifier_block *nb, unsigned long val,
125 void *data)
126{
127 int i;
128 struct cpufreq_freqs *freqs = data;
129 if (val == CPUFREQ_PRECHANGE) {
130 reg_bif_core_rw_sdram_timing timing =
131 REG_RD(bif_core, regi_bif_core, rw_sdram_timing);
132 timing.cpd = (freqs->new == 200000 ? 0 : 1);
133
134 if (freqs->new == 200000)
135 for (i = 0; i < 50000; i++) ;
136 REG_WR(bif_core, regi_bif_core, rw_sdram_timing, timing);
137 }
138 return 0;
139}
140
141module_init(cris_freq_init);