1 From 6912e27d97ba5671e8c2434bed0ebd23fde5e13d Mon Sep 17 00:00:00 2001
2 From: Stephen Boyd <sboyd@codeaurora.org>
3 Date: Wed, 18 Jun 2014 14:29:29 -0700
4 Subject: [PATCH 171/182] clk: qcom: Add Krait clock controller driver
6 The Krait CPU clocks are made up of a primary mux and secondary
7 mux for each CPU and the L2, controlled via cp15 accessors. For
8 Kraits within KPSSv1 each secondary mux accepts a different aux
9 source, but on KPSSv2 each secondary mux accepts the same aux
12 Signed-off-by: Stephen Boyd <sboyd@codeaurora.org>
14 drivers/clk/qcom/Kconfig | 8 +
15 drivers/clk/qcom/Makefile | 1 +
16 drivers/clk/qcom/krait-cc.c | 364 +++++++++++++++++++++++++++++++++++++++++++
17 3 files changed, 373 insertions(+)
18 create mode 100644 drivers/clk/qcom/krait-cc.c
20 diff --git a/drivers/clk/qcom/Kconfig b/drivers/clk/qcom/Kconfig
21 index e9e5360..7418108 100644
22 --- a/drivers/clk/qcom/Kconfig
23 +++ b/drivers/clk/qcom/Kconfig
24 @@ -70,6 +70,14 @@ config KPSS_XCC
25 if you want to support CPU frequency scaling on devices such
26 as MSM8960, APQ8064, etc.
29 + tristate "Krait Clock Controller"
30 + depends on COMMON_CLK_QCOM && ARM
33 + Support for the Krait CPU clocks on Qualcomm devices.
34 + Say Y if you want to support CPU frequency scaling.
38 select KRAIT_L2_ACCESSORS
39 diff --git a/drivers/clk/qcom/Makefile b/drivers/clk/qcom/Makefile
40 index 29b2a45..1b88abe 100644
41 --- a/drivers/clk/qcom/Makefile
42 +++ b/drivers/clk/qcom/Makefile
43 @@ -19,3 +19,4 @@ obj-$(CONFIG_MSM_MMCC_8960) += mmcc-msm8960.o
44 obj-$(CONFIG_MSM_MMCC_8974) += mmcc-msm8974.o
45 obj-$(CONFIG_KPSS_XCC) += kpss-xcc.o
46 obj-$(CONFIG_QCOM_HFPLL) += hfpll.o
47 +obj-$(CONFIG_KRAITCC) += krait-cc.o
48 diff --git a/drivers/clk/qcom/krait-cc.c b/drivers/clk/qcom/krait-cc.c
50 index 0000000..90985ea
52 +++ b/drivers/clk/qcom/krait-cc.c
54 +/* Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
56 + * This program is free software; you can redistribute it and/or modify
57 + * it under the terms of the GNU General Public License version 2 and
58 + * only version 2 as published by the Free Software Foundation.
60 + * This program is distributed in the hope that it will be useful,
61 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
62 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
63 + * GNU General Public License for more details.
66 +#include <linux/kernel.h>
67 +#include <linux/init.h>
68 +#include <linux/module.h>
69 +#include <linux/platform_device.h>
70 +#include <linux/err.h>
71 +#include <linux/io.h>
72 +#include <linux/of.h>
73 +#include <linux/of_device.h>
74 +#include <linux/clk.h>
75 +#include <linux/clk-provider.h>
76 +#include <linux/slab.h>
78 +#include <asm/smp_plat.h>
80 +#include "clk-krait.h"
82 +DEFINE_FIXED_DIV_CLK(acpu_aux, 2, "gpll0_vote");
84 +static u8 sec_mux_map[] = {
89 +static u8 pri_mux_map[] = {
96 +krait_add_div(struct device *dev, int id, const char *s, unsigned offset)
98 + struct div_clk *div;
99 + struct clk_init_data init = {
101 + .ops = &clk_ops_div,
102 + .flags = CLK_SET_RATE_PARENT,
104 + const char *p_names[1];
107 + div = devm_kzalloc(dev, sizeof(*dev), GFP_KERNEL);
112 + div->data.min_div = 2;
113 + div->data.max_div = 2;
114 + div->ops = &clk_div_ops_kpss_div2;
117 + div->priv = (void *)(id >= 0);
118 + div->offset = offset;
119 + div->hw.init = &init;
121 + init.name = kasprintf(GFP_KERNEL, "hfpll%s_div", s);
125 + init.parent_names = p_names;
126 + p_names[0] = kasprintf(GFP_KERNEL, "hfpll%s", s);
132 + clk = devm_clk_register(dev, &div->hw);
136 + return PTR_ERR_OR_ZERO(clk);
140 +krait_add_sec_mux(struct device *dev, int id, const char *s, unsigned offset,
143 + struct mux_clk *mux;
144 + static const char *sec_mux_list[] = {
148 + struct clk_init_data init = {
149 + .parent_names = sec_mux_list,
150 + .num_parents = ARRAY_SIZE(sec_mux_list),
151 + .ops = &clk_ops_gen_mux,
152 + .flags = CLK_SET_RATE_PARENT,
156 + mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
160 + mux->offset = offset;
161 + mux->priv = (void *)(id >= 0);
162 + mux->has_safe_parent = true;
164 + mux->ops = &clk_mux_ops_kpss;
167 + mux->parent_map = sec_mux_map;
168 + mux->hw.init = &init;
170 + init.name = kasprintf(GFP_KERNEL, "krait%s_sec_mux", s);
175 + sec_mux_list[0] = kasprintf(GFP_KERNEL, "acpu%s_aux", s);
176 + if (!sec_mux_list[0]) {
177 + clk = ERR_PTR(-ENOMEM);
182 + clk = devm_clk_register(dev, &mux->hw);
185 + kfree(sec_mux_list[0]);
188 + return PTR_ERR_OR_ZERO(clk);
192 +krait_add_pri_mux(struct device *dev, int id, const char * s, unsigned offset)
194 + struct mux_clk *mux;
195 + const char *p_names[3];
196 + struct clk_init_data init = {
197 + .parent_names = p_names,
198 + .num_parents = ARRAY_SIZE(p_names),
199 + .ops = &clk_ops_gen_mux,
200 + .flags = CLK_SET_RATE_PARENT,
204 + mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
206 + return ERR_PTR(-ENOMEM);
208 + mux->has_safe_parent = true;
210 + mux->ops = &clk_mux_ops_kpss;
213 + mux->offset = offset;
214 + mux->priv = (void *)(id >= 0);
215 + mux->parent_map = pri_mux_map;
216 + mux->hw.init = &init;
218 + init.name = kasprintf(GFP_KERNEL, "krait%s_pri_mux", s);
220 + return ERR_PTR(-ENOMEM);
222 + p_names[0] = kasprintf(GFP_KERNEL, "hfpll%s", s);
224 + clk = ERR_PTR(-ENOMEM);
228 + p_names[1] = kasprintf(GFP_KERNEL, "hfpll%s_div", s);
230 + clk = ERR_PTR(-ENOMEM);
234 + p_names[2] = kasprintf(GFP_KERNEL, "krait%s_sec_mux", s);
236 + clk = ERR_PTR(-ENOMEM);
240 + clk = devm_clk_register(dev, &mux->hw);
252 +/* id < 0 for L2, otherwise id == physical CPU number */
253 +static struct clk *krait_add_clks(struct device *dev, int id, bool unique_aux)
262 + offset = 0x4501 + (0x1000 * id);
263 + s = p = kasprintf(GFP_KERNEL, "%d", id);
265 + return ERR_PTR(-ENOMEM);
271 + ret = krait_add_div(dev, id, s, offset);
273 + clk = ERR_PTR(ret);
277 + ret = krait_add_sec_mux(dev, id, s, offset, unique_aux);
279 + clk = ERR_PTR(ret);
283 + clk = krait_add_pri_mux(dev, id, s, offset);
289 +static struct clk *krait_of_get(struct of_phandle_args *clkspec, void *data)
291 + unsigned int idx = clkspec->args[0];
292 + struct clk **clks = data;
295 + pr_err("%s: invalid clock index %d\n", __func__, idx);
296 + return ERR_PTR(-EINVAL);
299 + return clks[idx] ? : ERR_PTR(-ENODEV);
302 +static const struct of_device_id krait_cc_match_table[] = {
303 + { .compatible = "qcom,krait-cc-v1", (void *)1UL },
304 + { .compatible = "qcom,krait-cc-v2" },
307 +MODULE_DEVICE_TABLE(of, krait_cc_match_table);
309 +static int krait_cc_probe(struct platform_device *pdev)
311 + struct device *dev = &pdev->dev;
312 + const struct of_device_id *id;
313 + unsigned long cur_rate, aux_rate;
317 + struct clk *l2_pri_mux_clk;
319 + id = of_match_device(krait_cc_match_table, &pdev->dev);
323 + /* Rate is 1 because 0 causes problems for __clk_mux_determine_rate */
324 + clk = clk_register_fixed_rate(dev, "qsb", NULL, CLK_IS_ROOT, 1);
326 + return PTR_ERR(clk);
329 + clk = devm_clk_register(dev, &acpu_aux.hw);
331 + return PTR_ERR(clk);
334 + /* Krait configurations have at most 4 CPUs and one L2 */
335 + clks = devm_kcalloc(dev, 5, sizeof(*clks), GFP_KERNEL);
339 + for_each_possible_cpu(i) {
340 + cpu = cpu_logical_map(i);
341 + clk = krait_add_clks(dev, cpu, id->data);
343 + return PTR_ERR(clk);
347 + l2_pri_mux_clk = krait_add_clks(dev, -1, id->data);
348 + if (IS_ERR(l2_pri_mux_clk))
349 + return PTR_ERR(l2_pri_mux_clk);
350 + clks[4] = l2_pri_mux_clk;
353 + * We don't want the CPU or L2 clocks to be turned off at late init
354 + * if CPUFREQ or HOTPLUG configs are disabled. So, bump up the
355 + * refcount of these clocks. Any cpufreq/hotplug manager can assume
356 + * that the clocks have already been prepared and enabled by the time
359 + for_each_online_cpu(i) {
360 + cpu = cpu_logical_map(i);
361 + clk_prepare_enable(l2_pri_mux_clk);
362 + WARN(clk_prepare_enable(clks[cpu]),
363 + "Unable to turn on CPU%d clock", cpu);
367 + * Force reinit of HFPLLs and muxes to overwrite any potential
368 + * incorrect configuration of HFPLLs and muxes by the bootloader.
369 + * While at it, also make sure the cores are running at known rates
370 + * and print the current rate.
372 + * The clocks are set to aux clock rate first to make sure the
373 + * secondary mux is not sourcing off of QSB. The rate is then set to
374 + * two different rates to force a HFPLL reinit under all
377 + cur_rate = clk_get_rate(l2_pri_mux_clk);
378 + aux_rate = 384000000;
379 + if (cur_rate == 1) {
380 + pr_info("L2 @ QSB rate. Forcing new rate.\n");
381 + cur_rate = aux_rate;
383 + clk_set_rate(l2_pri_mux_clk, aux_rate);
384 + clk_set_rate(l2_pri_mux_clk, 2);
385 + clk_set_rate(l2_pri_mux_clk, cur_rate);
386 + pr_info("L2 @ %lu KHz\n", clk_get_rate(l2_pri_mux_clk) / 1000);
387 + for_each_possible_cpu(i) {
388 + cpu = cpu_logical_map(i);
390 + cur_rate = clk_get_rate(clk);
391 + if (cur_rate == 1) {
392 + pr_info("CPU%d @ QSB rate. Forcing new rate.\n", i);
393 + cur_rate = aux_rate;
395 + clk_set_rate(clk, aux_rate);
396 + clk_set_rate(clk, 2);
397 + clk_set_rate(clk, cur_rate);
398 + pr_info("CPU%d @ %lu KHz\n", i, clk_get_rate(clk) / 1000);
401 + of_clk_add_provider(dev->of_node, krait_of_get, clks);
406 +static struct platform_driver krait_cc_driver = {
407 + .probe = krait_cc_probe,
409 + .name = "clock-krait",
410 + .of_match_table = krait_cc_match_table,
411 + .owner = THIS_MODULE,
414 +module_platform_driver(krait_cc_driver);
416 +MODULE_DESCRIPTION("Krait CPU Clock Driver");
417 +MODULE_LICENSE("GPL v2");