Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 1 | /* |
| 2 | * CPU Frequency Scaling for Loongson 1 SoC |
| 3 | * |
Kelvin Cheung | 6a1d55c | 2016-04-12 18:40:15 +0800 | [diff] [blame] | 4 | * Copyright (C) 2014-2016 Zhang, Keguang <keguang.zhang@gmail.com> |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 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> |
Stephen Boyd | 62e59c4 | 2019-04-18 15:20:22 -0700 | [diff] [blame] | 16 | #include <linux/io.h> |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 17 | #include <linux/module.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/slab.h> |
| 20 | |
Huacai Chen | 30ad29b | 2015-04-21 10:00:35 +0800 | [diff] [blame] | 21 | #include <cpufreq.h> |
| 22 | #include <loongson1.h> |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 23 | |
Kelvin Cheung | 99bf2e6 | 2016-04-12 18:40:18 +0800 | [diff] [blame] | 24 | struct ls1x_cpufreq { |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 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; |
Kelvin Cheung | 99bf2e6 | 2016-04-12 18:40:18 +0800 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | static struct ls1x_cpufreq *cpufreq; |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 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 | { |
Kelvin Cheung | 99bf2e6 | 2016-04-12 18:40:18 +0800 | [diff] [blame] | 52 | struct device *cpu_dev = get_cpu_device(policy->cpu); |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 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 | |
Kelvin Cheung | 99bf2e6 | 2016-04-12 18:40:18 +0800 | [diff] [blame] | 67 | clk_set_parent(policy->clk, cpufreq->osc_clk); |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 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); |
Kelvin Cheung | 99bf2e6 | 2016-04-12 18:40:18 +0800 | [diff] [blame] | 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); |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static int ls1x_cpufreq_init(struct cpufreq_policy *policy) |
| 80 | { |
Kelvin Cheung | 99bf2e6 | 2016-04-12 18:40:18 +0800 | [diff] [blame] | 81 | struct device *cpu_dev = get_cpu_device(policy->cpu); |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 82 | struct cpufreq_frequency_table *freq_tbl; |
| 83 | unsigned int pll_freq, freq; |
Viresh Kumar | c4dcc8a | 2019-07-16 09:36:08 +0530 | [diff] [blame] | 84 | int steps, i; |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 85 | |
Kelvin Cheung | 99bf2e6 | 2016-04-12 18:40:18 +0800 | [diff] [blame] | 86 | pll_freq = clk_get_rate(cpufreq->pll_clk) / 1000; |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 87 | |
| 88 | steps = 1 << DIV_CPU_WIDTH; |
Kelvin Cheung | 379e38a | 2016-04-12 18:40:16 +0800 | [diff] [blame] | 89 | freq_tbl = kcalloc(steps, sizeof(*freq_tbl), GFP_KERNEL); |
| 90 | if (!freq_tbl) |
| 91 | return -ENOMEM; |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 92 | |
| 93 | for (i = 0; i < (steps - 1); i++) { |
| 94 | freq = pll_freq / (i + 1); |
Kelvin Cheung | 99bf2e6 | 2016-04-12 18:40:18 +0800 | [diff] [blame] | 95 | if ((freq < cpufreq->min_freq) || (freq > cpufreq->max_freq)) |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 96 | freq_tbl[i].frequency = CPUFREQ_ENTRY_INVALID; |
| 97 | else |
| 98 | freq_tbl[i].frequency = freq; |
Kelvin Cheung | 99bf2e6 | 2016-04-12 18:40:18 +0800 | [diff] [blame] | 99 | dev_dbg(cpu_dev, |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 100 | "cpufreq table: index %d: frequency %d\n", i, |
| 101 | freq_tbl[i].frequency); |
| 102 | } |
| 103 | freq_tbl[i].frequency = CPUFREQ_TABLE_END; |
| 104 | |
Kelvin Cheung | 99bf2e6 | 2016-04-12 18:40:18 +0800 | [diff] [blame] | 105 | policy->clk = cpufreq->clk; |
Viresh Kumar | c4dcc8a | 2019-07-16 09:36:08 +0530 | [diff] [blame] | 106 | cpufreq_generic_init(policy, freq_tbl, 0); |
Kelvin Cheung | 379e38a | 2016-04-12 18:40:16 +0800 | [diff] [blame] | 107 | |
Viresh Kumar | c4dcc8a | 2019-07-16 09:36:08 +0530 | [diff] [blame] | 108 | return 0; |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | static int ls1x_cpufreq_exit(struct cpufreq_policy *policy) |
| 112 | { |
| 113 | kfree(policy->freq_table); |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static struct cpufreq_driver ls1x_cpufreq_driver = { |
| 118 | .name = "cpufreq-ls1x", |
Viresh Kumar | 5ae4a4b | 2021-02-02 10:25:11 +0530 | [diff] [blame] | 119 | .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK, |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 120 | .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 | |
| 128 | static 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 | |
| 137 | static int ls1x_cpufreq_probe(struct platform_device *pdev) |
| 138 | { |
Kelvin Cheung | 25581d2 | 2016-04-12 18:40:17 +0800 | [diff] [blame] | 139 | struct plat_ls1x_cpufreq *pdata = dev_get_platdata(&pdev->dev); |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 140 | struct clk *clk; |
| 141 | int ret; |
| 142 | |
Kelvin Cheung | 65b2849 | 2016-04-12 18:40:19 +0800 | [diff] [blame] | 143 | if (!pdata || !pdata->clk_name || !pdata->osc_clk_name) { |
| 144 | dev_err(&pdev->dev, "platform data missing\n"); |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 145 | return -EINVAL; |
Kelvin Cheung | 65b2849 | 2016-04-12 18:40:19 +0800 | [diff] [blame] | 146 | } |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 147 | |
Kelvin Cheung | 99bf2e6 | 2016-04-12 18:40:18 +0800 | [diff] [blame] | 148 | 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 Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 154 | |
| 155 | clk = devm_clk_get(&pdev->dev, pdata->clk_name); |
| 156 | if (IS_ERR(clk)) { |
Kelvin Cheung | 99bf2e6 | 2016-04-12 18:40:18 +0800 | [diff] [blame] | 157 | dev_err(&pdev->dev, "unable to get %s clock\n", |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 158 | pdata->clk_name); |
Kelvin Cheung | 65b2849 | 2016-04-12 18:40:19 +0800 | [diff] [blame] | 159 | return PTR_ERR(clk); |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 160 | } |
Kelvin Cheung | 99bf2e6 | 2016-04-12 18:40:18 +0800 | [diff] [blame] | 161 | cpufreq->clk = clk; |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 162 | |
| 163 | clk = clk_get_parent(clk); |
| 164 | if (IS_ERR(clk)) { |
Kelvin Cheung | 99bf2e6 | 2016-04-12 18:40:18 +0800 | [diff] [blame] | 165 | dev_err(&pdev->dev, "unable to get parent of %s clock\n", |
| 166 | __clk_get_name(cpufreq->clk)); |
Kelvin Cheung | 65b2849 | 2016-04-12 18:40:19 +0800 | [diff] [blame] | 167 | return PTR_ERR(clk); |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 168 | } |
Kelvin Cheung | 99bf2e6 | 2016-04-12 18:40:18 +0800 | [diff] [blame] | 169 | cpufreq->mux_clk = clk; |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 170 | |
| 171 | clk = clk_get_parent(clk); |
| 172 | if (IS_ERR(clk)) { |
Kelvin Cheung | 99bf2e6 | 2016-04-12 18:40:18 +0800 | [diff] [blame] | 173 | dev_err(&pdev->dev, "unable to get parent of %s clock\n", |
| 174 | __clk_get_name(cpufreq->mux_clk)); |
Kelvin Cheung | 65b2849 | 2016-04-12 18:40:19 +0800 | [diff] [blame] | 175 | return PTR_ERR(clk); |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 176 | } |
Kelvin Cheung | 99bf2e6 | 2016-04-12 18:40:18 +0800 | [diff] [blame] | 177 | cpufreq->pll_clk = clk; |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 178 | |
| 179 | clk = devm_clk_get(&pdev->dev, pdata->osc_clk_name); |
| 180 | if (IS_ERR(clk)) { |
Kelvin Cheung | 99bf2e6 | 2016-04-12 18:40:18 +0800 | [diff] [blame] | 181 | dev_err(&pdev->dev, "unable to get %s clock\n", |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 182 | pdata->osc_clk_name); |
Kelvin Cheung | 65b2849 | 2016-04-12 18:40:19 +0800 | [diff] [blame] | 183 | return PTR_ERR(clk); |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 184 | } |
Kelvin Cheung | 99bf2e6 | 2016-04-12 18:40:18 +0800 | [diff] [blame] | 185 | cpufreq->osc_clk = clk; |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 186 | |
Kelvin Cheung | 99bf2e6 | 2016-04-12 18:40:18 +0800 | [diff] [blame] | 187 | cpufreq->max_freq = pdata->max_freq; |
| 188 | cpufreq->min_freq = pdata->min_freq; |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 189 | |
| 190 | ret = cpufreq_register_driver(&ls1x_cpufreq_driver); |
| 191 | if (ret) { |
Kelvin Cheung | 99bf2e6 | 2016-04-12 18:40:18 +0800 | [diff] [blame] | 192 | dev_err(&pdev->dev, |
| 193 | "failed to register CPUFreq driver: %d\n", ret); |
Kelvin Cheung | 65b2849 | 2016-04-12 18:40:19 +0800 | [diff] [blame] | 194 | return ret; |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | ret = cpufreq_register_notifier(&ls1x_cpufreq_notifier_block, |
| 198 | CPUFREQ_TRANSITION_NOTIFIER); |
| 199 | |
Kelvin Cheung | 65b2849 | 2016-04-12 18:40:19 +0800 | [diff] [blame] | 200 | 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 Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 205 | |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 206 | return ret; |
| 207 | } |
| 208 | |
| 209 | static struct platform_driver ls1x_cpufreq_platdrv = { |
Kelvin Cheung | 6a1d55c | 2016-04-12 18:40:15 +0800 | [diff] [blame] | 210 | .probe = ls1x_cpufreq_probe, |
| 211 | .remove = ls1x_cpufreq_remove, |
| 212 | .driver = { |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 213 | .name = "ls1x-cpufreq", |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 214 | }, |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | module_platform_driver(ls1x_cpufreq_platdrv); |
| 218 | |
Pali Rohár | b9acab0 | 2020-11-03 16:11:37 +0100 | [diff] [blame] | 219 | MODULE_ALIAS("platform:ls1x-cpufreq"); |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 220 | MODULE_AUTHOR("Kelvin Cheung <keguang.zhang@gmail.com>"); |
Kelvin Cheung | 6a1d55c | 2016-04-12 18:40:15 +0800 | [diff] [blame] | 221 | MODULE_DESCRIPTION("Loongson1 CPUFreq driver"); |
Kelvin Cheung | a0a22cf | 2014-10-17 18:23:31 +0800 | [diff] [blame] | 222 | MODULE_LICENSE("GPL"); |