blob: fb72d709db565f4266158b65788bd0ddf86668bd [file] [log] [blame]
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +08001/*
2 * CPU Frequency Scaling for Loongson 1 SoC
3 *
Kelvin Cheung6a1d55c2016-04-12 18:40:15 +08004 * Copyright (C) 2014-2016 Zhang, Keguang <keguang.zhang@gmail.com>
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +08005 *
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>
Stephen Boyd62e59c42019-04-18 15:20:22 -070016#include <linux/io.h>
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +080017#include <linux/module.h>
18#include <linux/platform_device.h>
19#include <linux/slab.h>
20
Huacai Chen30ad29b2015-04-21 10:00:35 +080021#include <cpufreq.h>
22#include <loongson1.h>
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +080023
Kelvin Cheung99bf2e62016-04-12 18:40:18 +080024struct ls1x_cpufreq {
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +080025 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;
Kelvin Cheung99bf2e62016-04-12 18:40:18 +080032};
33
34static struct ls1x_cpufreq *cpufreq;
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +080035
36static 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
45static struct notifier_block ls1x_cpufreq_notifier_block = {
46 .notifier_call = ls1x_cpufreq_notifier
47};
48
49static int ls1x_cpufreq_target(struct cpufreq_policy *policy,
50 unsigned int index)
51{
Kelvin Cheung99bf2e62016-04-12 18:40:18 +080052 struct device *cpu_dev = get_cpu_device(policy->cpu);
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +080053 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
Kelvin Cheung99bf2e62016-04-12 18:40:18 +080067 clk_set_parent(policy->clk, cpufreq->osc_clk);
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +080068 __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);
Kelvin Cheung99bf2e62016-04-12 18:40:18 +080072 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);
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +080075
76 return 0;
77}
78
79static int ls1x_cpufreq_init(struct cpufreq_policy *policy)
80{
Kelvin Cheung99bf2e62016-04-12 18:40:18 +080081 struct device *cpu_dev = get_cpu_device(policy->cpu);
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +080082 struct cpufreq_frequency_table *freq_tbl;
83 unsigned int pll_freq, freq;
Viresh Kumarc4dcc8a2019-07-16 09:36:08 +053084 int steps, i;
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +080085
Kelvin Cheung99bf2e62016-04-12 18:40:18 +080086 pll_freq = clk_get_rate(cpufreq->pll_clk) / 1000;
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +080087
88 steps = 1 << DIV_CPU_WIDTH;
Kelvin Cheung379e38a2016-04-12 18:40:16 +080089 freq_tbl = kcalloc(steps, sizeof(*freq_tbl), GFP_KERNEL);
90 if (!freq_tbl)
91 return -ENOMEM;
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +080092
93 for (i = 0; i < (steps - 1); i++) {
94 freq = pll_freq / (i + 1);
Kelvin Cheung99bf2e62016-04-12 18:40:18 +080095 if ((freq < cpufreq->min_freq) || (freq > cpufreq->max_freq))
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +080096 freq_tbl[i].frequency = CPUFREQ_ENTRY_INVALID;
97 else
98 freq_tbl[i].frequency = freq;
Kelvin Cheung99bf2e62016-04-12 18:40:18 +080099 dev_dbg(cpu_dev,
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800100 "cpufreq table: index %d: frequency %d\n", i,
101 freq_tbl[i].frequency);
102 }
103 freq_tbl[i].frequency = CPUFREQ_TABLE_END;
104
Kelvin Cheung99bf2e62016-04-12 18:40:18 +0800105 policy->clk = cpufreq->clk;
Viresh Kumarc4dcc8a2019-07-16 09:36:08 +0530106 cpufreq_generic_init(policy, freq_tbl, 0);
Kelvin Cheung379e38a2016-04-12 18:40:16 +0800107
Viresh Kumarc4dcc8a2019-07-16 09:36:08 +0530108 return 0;
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800109}
110
111static int ls1x_cpufreq_exit(struct cpufreq_policy *policy)
112{
113 kfree(policy->freq_table);
114 return 0;
115}
116
117static struct cpufreq_driver ls1x_cpufreq_driver = {
118 .name = "cpufreq-ls1x",
Viresh Kumar5ae4a4b2021-02-02 10:25:11 +0530119 .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800120 .verify = cpufreq_generic_frequency_table_verify,
121 .target_index = ls1x_cpufreq_target,
122 .get = cpufreq_generic_get,
123 .init = ls1x_cpufreq_init,
124 .exit = ls1x_cpufreq_exit,
125 .attr = cpufreq_generic_attr,
126};
127
128static int ls1x_cpufreq_remove(struct platform_device *pdev)
129{
130 cpufreq_unregister_notifier(&ls1x_cpufreq_notifier_block,
131 CPUFREQ_TRANSITION_NOTIFIER);
132 cpufreq_unregister_driver(&ls1x_cpufreq_driver);
133
134 return 0;
135}
136
137static int ls1x_cpufreq_probe(struct platform_device *pdev)
138{
Kelvin Cheung25581d22016-04-12 18:40:17 +0800139 struct plat_ls1x_cpufreq *pdata = dev_get_platdata(&pdev->dev);
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800140 struct clk *clk;
141 int ret;
142
Kelvin Cheung65b28492016-04-12 18:40:19 +0800143 if (!pdata || !pdata->clk_name || !pdata->osc_clk_name) {
144 dev_err(&pdev->dev, "platform data missing\n");
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800145 return -EINVAL;
Kelvin Cheung65b28492016-04-12 18:40:19 +0800146 }
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800147
Kelvin Cheung99bf2e62016-04-12 18:40:18 +0800148 cpufreq =
149 devm_kzalloc(&pdev->dev, sizeof(struct ls1x_cpufreq), GFP_KERNEL);
150 if (!cpufreq)
151 return -ENOMEM;
152
153 cpufreq->dev = &pdev->dev;
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800154
155 clk = devm_clk_get(&pdev->dev, pdata->clk_name);
156 if (IS_ERR(clk)) {
Kelvin Cheung99bf2e62016-04-12 18:40:18 +0800157 dev_err(&pdev->dev, "unable to get %s clock\n",
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800158 pdata->clk_name);
Kelvin Cheung65b28492016-04-12 18:40:19 +0800159 return PTR_ERR(clk);
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800160 }
Kelvin Cheung99bf2e62016-04-12 18:40:18 +0800161 cpufreq->clk = clk;
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800162
163 clk = clk_get_parent(clk);
164 if (IS_ERR(clk)) {
Kelvin Cheung99bf2e62016-04-12 18:40:18 +0800165 dev_err(&pdev->dev, "unable to get parent of %s clock\n",
166 __clk_get_name(cpufreq->clk));
Kelvin Cheung65b28492016-04-12 18:40:19 +0800167 return PTR_ERR(clk);
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800168 }
Kelvin Cheung99bf2e62016-04-12 18:40:18 +0800169 cpufreq->mux_clk = clk;
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800170
171 clk = clk_get_parent(clk);
172 if (IS_ERR(clk)) {
Kelvin Cheung99bf2e62016-04-12 18:40:18 +0800173 dev_err(&pdev->dev, "unable to get parent of %s clock\n",
174 __clk_get_name(cpufreq->mux_clk));
Kelvin Cheung65b28492016-04-12 18:40:19 +0800175 return PTR_ERR(clk);
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800176 }
Kelvin Cheung99bf2e62016-04-12 18:40:18 +0800177 cpufreq->pll_clk = clk;
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800178
179 clk = devm_clk_get(&pdev->dev, pdata->osc_clk_name);
180 if (IS_ERR(clk)) {
Kelvin Cheung99bf2e62016-04-12 18:40:18 +0800181 dev_err(&pdev->dev, "unable to get %s clock\n",
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800182 pdata->osc_clk_name);
Kelvin Cheung65b28492016-04-12 18:40:19 +0800183 return PTR_ERR(clk);
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800184 }
Kelvin Cheung99bf2e62016-04-12 18:40:18 +0800185 cpufreq->osc_clk = clk;
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800186
Kelvin Cheung99bf2e62016-04-12 18:40:18 +0800187 cpufreq->max_freq = pdata->max_freq;
188 cpufreq->min_freq = pdata->min_freq;
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800189
190 ret = cpufreq_register_driver(&ls1x_cpufreq_driver);
191 if (ret) {
Kelvin Cheung99bf2e62016-04-12 18:40:18 +0800192 dev_err(&pdev->dev,
193 "failed to register CPUFreq driver: %d\n", ret);
Kelvin Cheung65b28492016-04-12 18:40:19 +0800194 return ret;
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800195 }
196
197 ret = cpufreq_register_notifier(&ls1x_cpufreq_notifier_block,
198 CPUFREQ_TRANSITION_NOTIFIER);
199
Kelvin Cheung65b28492016-04-12 18:40:19 +0800200 if (ret) {
201 dev_err(&pdev->dev,
202 "failed to register CPUFreq notifier: %d\n",ret);
203 cpufreq_unregister_driver(&ls1x_cpufreq_driver);
204 }
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800205
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800206 return ret;
207}
208
209static struct platform_driver ls1x_cpufreq_platdrv = {
Kelvin Cheung6a1d55c2016-04-12 18:40:15 +0800210 .probe = ls1x_cpufreq_probe,
211 .remove = ls1x_cpufreq_remove,
212 .driver = {
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800213 .name = "ls1x-cpufreq",
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800214 },
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800215};
216
217module_platform_driver(ls1x_cpufreq_platdrv);
218
Pali Rohárb9acab02020-11-03 16:11:37 +0100219MODULE_ALIAS("platform:ls1x-cpufreq");
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800220MODULE_AUTHOR("Kelvin Cheung <keguang.zhang@gmail.com>");
Kelvin Cheung6a1d55c2016-04-12 18:40:15 +0800221MODULE_DESCRIPTION("Loongson1 CPUFreq driver");
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800222MODULE_LICENSE("GPL");