Commit | Line | Data |
---|---|---|
a0a22cf1 KC |
1 | /* |
2 | * CPU Frequency Scaling for Loongson 1 SoC | |
3 | * | |
6a1d55cc | 4 | * Copyright (C) 2014-2016 Zhang, Keguang <keguang.zhang@gmail.com> |
a0a22cf1 KC |
5 | * |
6 | * This file is licensed under the terms of the GNU General Public | |
7 | * License version 2. This program is licensed "as is" without any | |
8 | * warranty of any kind, whether express or implied. | |
9 | */ | |
10 | ||
11 | #include <linux/clk.h> | |
12 | #include <linux/clk-provider.h> | |
13 | #include <linux/cpu.h> | |
14 | #include <linux/cpufreq.h> | |
15 | #include <linux/delay.h> | |
62e59c4e | 16 | #include <linux/io.h> |
a0a22cf1 KC |
17 | #include <linux/module.h> |
18 | #include <linux/platform_device.h> | |
19 | #include <linux/slab.h> | |
20 | ||
30ad29bb HC |
21 | #include <cpufreq.h> |
22 | #include <loongson1.h> | |
a0a22cf1 | 23 | |
99bf2e68 | 24 | struct ls1x_cpufreq { |
a0a22cf1 KC |
25 | struct device *dev; |
26 | struct clk *clk; /* CPU clk */ | |
27 | struct clk *mux_clk; /* MUX of CPU clk */ | |
28 | struct clk *pll_clk; /* PLL clk */ | |
29 | struct clk *osc_clk; /* OSC clk */ | |
30 | unsigned int max_freq; | |
31 | unsigned int min_freq; | |
99bf2e68 KC |
32 | }; |
33 | ||
34 | static struct ls1x_cpufreq *cpufreq; | |
a0a22cf1 KC |
35 | |
36 | static int ls1x_cpufreq_notifier(struct notifier_block *nb, | |
37 | unsigned long val, void *data) | |
38 | { | |
39 | if (val == CPUFREQ_POSTCHANGE) | |
40 | current_cpu_data.udelay_val = loops_per_jiffy; | |
41 | ||
42 | return NOTIFY_OK; | |
43 | } | |
44 | ||
45 | static struct notifier_block ls1x_cpufreq_notifier_block = { | |
46 | .notifier_call = ls1x_cpufreq_notifier | |
47 | }; | |
48 | ||
49 | static int ls1x_cpufreq_target(struct cpufreq_policy *policy, | |
50 | unsigned int index) | |
51 | { | |
99bf2e68 | 52 | struct device *cpu_dev = get_cpu_device(policy->cpu); |
a0a22cf1 KC |
53 | unsigned int old_freq, new_freq; |
54 | ||
55 | old_freq = policy->cur; | |
56 | new_freq = policy->freq_table[index].frequency; | |
57 | ||
58 | /* | |
59 | * The procedure of reconfiguring CPU clk is as below. | |
60 | * | |
61 | * - Reparent CPU clk to OSC clk | |
62 | * - Reset CPU clock (very important) | |
63 | * - Reconfigure CPU DIV | |
64 | * - Reparent CPU clk back to CPU DIV clk | |
65 | */ | |
66 | ||
99bf2e68 | 67 | clk_set_parent(policy->clk, cpufreq->osc_clk); |
a0a22cf1 KC |
68 | __raw_writel(__raw_readl(LS1X_CLK_PLL_DIV) | RST_CPU_EN | RST_CPU, |
69 | LS1X_CLK_PLL_DIV); | |
70 | __raw_writel(__raw_readl(LS1X_CLK_PLL_DIV) & ~(RST_CPU_EN | RST_CPU), | |
71 | LS1X_CLK_PLL_DIV); | |
99bf2e68 KC |
72 | clk_set_rate(cpufreq->mux_clk, new_freq * 1000); |
73 | clk_set_parent(policy->clk, cpufreq->mux_clk); | |
74 | dev_dbg(cpu_dev, "%u KHz --> %u KHz\n", old_freq, new_freq); | |
a0a22cf1 KC |
75 | |
76 | return 0; | |
77 | } | |
78 | ||
79 | static int ls1x_cpufreq_init(struct cpufreq_policy *policy) | |
80 | { | |
99bf2e68 | 81 | struct device *cpu_dev = get_cpu_device(policy->cpu); |
a0a22cf1 KC |
82 | struct cpufreq_frequency_table *freq_tbl; |
83 | unsigned int pll_freq, freq; | |
84 | int steps, i, ret; | |
85 | ||
99bf2e68 | 86 | pll_freq = clk_get_rate(cpufreq->pll_clk) / 1000; |
a0a22cf1 KC |
87 | |
88 | steps = 1 << DIV_CPU_WIDTH; | |
379e38a7 KC |
89 | freq_tbl = kcalloc(steps, sizeof(*freq_tbl), GFP_KERNEL); |
90 | if (!freq_tbl) | |
91 | return -ENOMEM; | |
a0a22cf1 KC |
92 | |
93 | for (i = 0; i < (steps - 1); i++) { | |
94 | freq = pll_freq / (i + 1); | |
99bf2e68 | 95 | if ((freq < cpufreq->min_freq) || (freq > cpufreq->max_freq)) |
a0a22cf1 KC |
96 | freq_tbl[i].frequency = CPUFREQ_ENTRY_INVALID; |
97 | else | |
98 | freq_tbl[i].frequency = freq; | |
99bf2e68 | 99 | dev_dbg(cpu_dev, |
a0a22cf1 KC |
100 | "cpufreq table: index %d: frequency %d\n", i, |
101 | freq_tbl[i].frequency); | |
102 | } | |
103 | freq_tbl[i].frequency = CPUFREQ_TABLE_END; | |
104 | ||
99bf2e68 | 105 | policy->clk = cpufreq->clk; |
a0a22cf1 KC |
106 | ret = cpufreq_generic_init(policy, freq_tbl, 0); |
107 | if (ret) | |
108 | kfree(freq_tbl); | |
379e38a7 | 109 | |
a0a22cf1 KC |
110 | return ret; |
111 | } | |
112 | ||
113 | static int ls1x_cpufreq_exit(struct cpufreq_policy *policy) | |
114 | { | |
115 | kfree(policy->freq_table); | |
116 | return 0; | |
117 | } | |
118 | ||
119 | static struct cpufreq_driver ls1x_cpufreq_driver = { | |
120 | .name = "cpufreq-ls1x", | |
121 | .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK, | |
122 | .verify = cpufreq_generic_frequency_table_verify, | |
123 | .target_index = ls1x_cpufreq_target, | |
124 | .get = cpufreq_generic_get, | |
125 | .init = ls1x_cpufreq_init, | |
126 | .exit = ls1x_cpufreq_exit, | |
127 | .attr = cpufreq_generic_attr, | |
128 | }; | |
129 | ||
130 | static int ls1x_cpufreq_remove(struct platform_device *pdev) | |
131 | { | |
132 | cpufreq_unregister_notifier(&ls1x_cpufreq_notifier_block, | |
133 | CPUFREQ_TRANSITION_NOTIFIER); | |
134 | cpufreq_unregister_driver(&ls1x_cpufreq_driver); | |
135 | ||
136 | return 0; | |
137 | } | |
138 | ||
139 | static int ls1x_cpufreq_probe(struct platform_device *pdev) | |
140 | { | |
25581d2b | 141 | struct plat_ls1x_cpufreq *pdata = dev_get_platdata(&pdev->dev); |
a0a22cf1 KC |
142 | struct clk *clk; |
143 | int ret; | |
144 | ||
65b2849a KC |
145 | if (!pdata || !pdata->clk_name || !pdata->osc_clk_name) { |
146 | dev_err(&pdev->dev, "platform data missing\n"); | |
a0a22cf1 | 147 | return -EINVAL; |
65b2849a | 148 | } |
a0a22cf1 | 149 | |
99bf2e68 KC |
150 | cpufreq = |
151 | devm_kzalloc(&pdev->dev, sizeof(struct ls1x_cpufreq), GFP_KERNEL); | |
152 | if (!cpufreq) | |
153 | return -ENOMEM; | |
154 | ||
155 | cpufreq->dev = &pdev->dev; | |
a0a22cf1 KC |
156 | |
157 | clk = devm_clk_get(&pdev->dev, pdata->clk_name); | |
158 | if (IS_ERR(clk)) { | |
99bf2e68 | 159 | dev_err(&pdev->dev, "unable to get %s clock\n", |
a0a22cf1 | 160 | pdata->clk_name); |
65b2849a | 161 | return PTR_ERR(clk); |
a0a22cf1 | 162 | } |
99bf2e68 | 163 | cpufreq->clk = clk; |
a0a22cf1 KC |
164 | |
165 | clk = clk_get_parent(clk); | |
166 | if (IS_ERR(clk)) { | |
99bf2e68 KC |
167 | dev_err(&pdev->dev, "unable to get parent of %s clock\n", |
168 | __clk_get_name(cpufreq->clk)); | |
65b2849a | 169 | return PTR_ERR(clk); |
a0a22cf1 | 170 | } |
99bf2e68 | 171 | cpufreq->mux_clk = clk; |
a0a22cf1 KC |
172 | |
173 | clk = clk_get_parent(clk); | |
174 | if (IS_ERR(clk)) { | |
99bf2e68 KC |
175 | dev_err(&pdev->dev, "unable to get parent of %s clock\n", |
176 | __clk_get_name(cpufreq->mux_clk)); | |
65b2849a | 177 | return PTR_ERR(clk); |
a0a22cf1 | 178 | } |
99bf2e68 | 179 | cpufreq->pll_clk = clk; |
a0a22cf1 KC |
180 | |
181 | clk = devm_clk_get(&pdev->dev, pdata->osc_clk_name); | |
182 | if (IS_ERR(clk)) { | |
99bf2e68 | 183 | dev_err(&pdev->dev, "unable to get %s clock\n", |
a0a22cf1 | 184 | pdata->osc_clk_name); |
65b2849a | 185 | return PTR_ERR(clk); |
a0a22cf1 | 186 | } |
99bf2e68 | 187 | cpufreq->osc_clk = clk; |
a0a22cf1 | 188 | |
99bf2e68 KC |
189 | cpufreq->max_freq = pdata->max_freq; |
190 | cpufreq->min_freq = pdata->min_freq; | |
a0a22cf1 KC |
191 | |
192 | ret = cpufreq_register_driver(&ls1x_cpufreq_driver); | |
193 | if (ret) { | |
99bf2e68 KC |
194 | dev_err(&pdev->dev, |
195 | "failed to register CPUFreq driver: %d\n", ret); | |
65b2849a | 196 | return ret; |
a0a22cf1 KC |
197 | } |
198 | ||
199 | ret = cpufreq_register_notifier(&ls1x_cpufreq_notifier_block, | |
200 | CPUFREQ_TRANSITION_NOTIFIER); | |
201 | ||
65b2849a KC |
202 | if (ret) { |
203 | dev_err(&pdev->dev, | |
204 | "failed to register CPUFreq notifier: %d\n",ret); | |
205 | cpufreq_unregister_driver(&ls1x_cpufreq_driver); | |
206 | } | |
a0a22cf1 | 207 | |
a0a22cf1 KC |
208 | return ret; |
209 | } | |
210 | ||
211 | static struct platform_driver ls1x_cpufreq_platdrv = { | |
6a1d55cc KC |
212 | .probe = ls1x_cpufreq_probe, |
213 | .remove = ls1x_cpufreq_remove, | |
214 | .driver = { | |
a0a22cf1 | 215 | .name = "ls1x-cpufreq", |
a0a22cf1 | 216 | }, |
a0a22cf1 KC |
217 | }; |
218 | ||
219 | module_platform_driver(ls1x_cpufreq_platdrv); | |
220 | ||
221 | MODULE_AUTHOR("Kelvin Cheung <keguang.zhang@gmail.com>"); | |
6a1d55cc | 222 | MODULE_DESCRIPTION("Loongson1 CPUFreq driver"); |
a0a22cf1 | 223 | MODULE_LICENSE("GPL"); |