Merge branch 'cris' of git://www.jni.nu/cris
[linux-2.6] / arch / cris / arch-v32 / mach-fs / cpufreq.c
1 #include <linux/init.h>
2 #include <linux/module.h>
3 #include <linux/cpufreq.h>
4 #include <hwregs/reg_map.h>
5 #include <asm/arch/hwregs/reg_rdwr.h>
6 #include <asm/arch/hwregs/config_defs.h>
7 #include <asm/arch/hwregs/bif_core_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_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
30 static void cris_freq_set_cpu_state(unsigned int state)
31 {
32         int i;
33         struct cpufreq_freqs freqs;
34         reg_config_rw_clk_ctrl clk_ctrl;
35         clk_ctrl = REG_RD(config, regi_config, rw_clk_ctrl);
36
37         for_each_possible_cpu(i) {
38                 freqs.old = cris_freq_get_cpu_frequency(i);
39                 freqs.new = cris_freq_table[state].frequency;
40                 freqs.cpu = i;
41         }
42
43         cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
44
45         local_irq_disable();
46
47         /* Even though we may be SMP they will share the same clock
48          * so all settings are made on CPU0. */
49         if (cris_freq_table[state].frequency == 200000)
50                 clk_ctrl.pll = 1;
51         else
52                 clk_ctrl.pll = 0;
53         REG_WR(config, regi_config, rw_clk_ctrl, clk_ctrl);
54
55         local_irq_enable();
56
57         cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
58 };
59
60 static int cris_freq_verify(struct cpufreq_policy *policy)
61 {
62         return cpufreq_frequency_table_verify(policy, &cris_freq_table[0]);
63 }
64
65 static int cris_freq_target(struct cpufreq_policy *policy,
66                             unsigned int target_freq, unsigned int relation)
67 {
68         unsigned int newstate = 0;
69
70         if (cpufreq_frequency_table_target
71             (policy, cris_freq_table, target_freq, relation, &newstate))
72                 return -EINVAL;
73
74         cris_freq_set_cpu_state(newstate);
75
76         return 0;
77 }
78
79 static int cris_freq_cpu_init(struct cpufreq_policy *policy)
80 {
81         int result;
82
83         /* cpuinfo and default policy values */
84         policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
85         policy->cpuinfo.transition_latency = 1000000;   /* 1ms */
86         policy->cur = cris_freq_get_cpu_frequency(0);
87
88         result = cpufreq_frequency_table_cpuinfo(policy, cris_freq_table);
89         if (result)
90                 return (result);
91
92         cpufreq_frequency_table_get_attr(cris_freq_table, policy->cpu);
93
94         return 0;
95 }
96
97 static int cris_freq_cpu_exit(struct cpufreq_policy *policy)
98 {
99         cpufreq_frequency_table_put_attr(policy->cpu);
100         return 0;
101 }
102
103 static struct freq_attr *cris_freq_attr[] = {
104         &cpufreq_freq_attr_scaling_available_freqs,
105         NULL,
106 };
107
108 static struct cpufreq_driver cris_freq_driver = {
109         .get = cris_freq_get_cpu_frequency,
110         .verify = cris_freq_verify,
111         .target = cris_freq_target,
112         .init = cris_freq_cpu_init,
113         .exit = cris_freq_cpu_exit,
114         .name = "cris_freq",
115         .owner = THIS_MODULE,
116         .attr = cris_freq_attr,
117 };
118
119 static int __init cris_freq_init(void)
120 {
121         int ret;
122         ret = cpufreq_register_driver(&cris_freq_driver);
123         cpufreq_register_notifier(&cris_sdram_freq_notifier_block,
124                                   CPUFREQ_TRANSITION_NOTIFIER);
125         return ret;
126 }
127
128 static int
129 cris_sdram_freq_notifier(struct notifier_block *nb, unsigned long val,
130                          void *data)
131 {
132         int i;
133         struct cpufreq_freqs *freqs = data;
134         if (val == CPUFREQ_PRECHANGE) {
135                 reg_bif_core_rw_sdram_timing timing =
136                     REG_RD(bif_core, regi_bif_core, rw_sdram_timing);
137                 timing.cpd = (freqs->new == 200000 ? 0 : 1);
138
139                 if (freqs->new == 200000)
140                         for (i = 0; i < 50000; i++) ;
141                 REG_WR(bif_core, regi_bif_core, rw_sdram_timing, timing);
142         }
143         return 0;
144 }
145
146 module_init(cris_freq_init);