xref: /openbmc/linux/drivers/clk/mvebu/clk-cpu.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1c3828949SGregory CLEMENT // SPDX-License-Identifier: GPL-2.0
2ab8ba01bSGregory CLEMENT /*
3ab8ba01bSGregory CLEMENT  * Marvell MVEBU CPU clock handling.
4ab8ba01bSGregory CLEMENT  *
5ab8ba01bSGregory CLEMENT  * Copyright (C) 2012 Marvell
6ab8ba01bSGregory CLEMENT  *
7ab8ba01bSGregory CLEMENT  * Gregory CLEMENT <gregory.clement@free-electrons.com>
8ab8ba01bSGregory CLEMENT  *
9ab8ba01bSGregory CLEMENT  */
10ab8ba01bSGregory CLEMENT #include <linux/kernel.h>
11db00c3e5SStephen Boyd #include <linux/slab.h>
12db00c3e5SStephen Boyd #include <linux/clk.h>
13ab8ba01bSGregory CLEMENT #include <linux/clk-provider.h>
14ab8ba01bSGregory CLEMENT #include <linux/of_address.h>
15ab8ba01bSGregory CLEMENT #include <linux/io.h>
16ab8ba01bSGregory CLEMENT #include <linux/of.h>
17ab8ba01bSGregory CLEMENT #include <linux/delay.h>
18ee2d8ea1SThomas Petazzoni #include <linux/mvebu-pmsu.h>
19ee2d8ea1SThomas Petazzoni #include <asm/smp_plat.h>
20ab8ba01bSGregory CLEMENT 
21ab8ba01bSGregory CLEMENT #define SYS_CTRL_CLK_DIVIDER_CTRL_OFFSET               0x0
22ee2d8ea1SThomas Petazzoni #define   SYS_CTRL_CLK_DIVIDER_CTRL_RESET_ALL          0xff
23ee2d8ea1SThomas Petazzoni #define   SYS_CTRL_CLK_DIVIDER_CTRL_RESET_SHIFT        8
24ee2d8ea1SThomas Petazzoni #define SYS_CTRL_CLK_DIVIDER_CTRL2_OFFSET              0x8
25ee2d8ea1SThomas Petazzoni #define   SYS_CTRL_CLK_DIVIDER_CTRL2_NBCLK_RATIO_SHIFT 16
26ab8ba01bSGregory CLEMENT #define SYS_CTRL_CLK_DIVIDER_VALUE_OFFSET              0xC
27ab8ba01bSGregory CLEMENT #define SYS_CTRL_CLK_DIVIDER_MASK                      0x3F
28ab8ba01bSGregory CLEMENT 
29ee2d8ea1SThomas Petazzoni #define PMU_DFS_RATIO_SHIFT 16
30ee2d8ea1SThomas Petazzoni #define PMU_DFS_RATIO_MASK  0x3F
31ee2d8ea1SThomas Petazzoni 
32ab8ba01bSGregory CLEMENT #define MAX_CPU	    4
33ab8ba01bSGregory CLEMENT struct cpu_clk {
34ab8ba01bSGregory CLEMENT 	struct clk_hw hw;
35ab8ba01bSGregory CLEMENT 	int cpu;
36ab8ba01bSGregory CLEMENT 	const char *clk_name;
37ab8ba01bSGregory CLEMENT 	const char *parent_name;
38ab8ba01bSGregory CLEMENT 	void __iomem *reg_base;
39ee2d8ea1SThomas Petazzoni 	void __iomem *pmu_dfs;
40ab8ba01bSGregory CLEMENT };
41ab8ba01bSGregory CLEMENT 
42ab8ba01bSGregory CLEMENT static struct clk **clks;
43ab8ba01bSGregory CLEMENT 
44ab8ba01bSGregory CLEMENT static struct clk_onecell_data clk_data;
45ab8ba01bSGregory CLEMENT 
46ab8ba01bSGregory CLEMENT #define to_cpu_clk(p) container_of(p, struct cpu_clk, hw)
47ab8ba01bSGregory CLEMENT 
clk_cpu_recalc_rate(struct clk_hw * hwclk,unsigned long parent_rate)48ab8ba01bSGregory CLEMENT static unsigned long clk_cpu_recalc_rate(struct clk_hw *hwclk,
49ab8ba01bSGregory CLEMENT 					 unsigned long parent_rate)
50ab8ba01bSGregory CLEMENT {
51ab8ba01bSGregory CLEMENT 	struct cpu_clk *cpuclk = to_cpu_clk(hwclk);
52ab8ba01bSGregory CLEMENT 	u32 reg, div;
53ab8ba01bSGregory CLEMENT 
54ab8ba01bSGregory CLEMENT 	reg = readl(cpuclk->reg_base + SYS_CTRL_CLK_DIVIDER_VALUE_OFFSET);
55ab8ba01bSGregory CLEMENT 	div = (reg >> (cpuclk->cpu * 8)) & SYS_CTRL_CLK_DIVIDER_MASK;
56ab8ba01bSGregory CLEMENT 	return parent_rate / div;
57ab8ba01bSGregory CLEMENT }
58ab8ba01bSGregory CLEMENT 
clk_cpu_round_rate(struct clk_hw * hwclk,unsigned long rate,unsigned long * parent_rate)59ab8ba01bSGregory CLEMENT static long clk_cpu_round_rate(struct clk_hw *hwclk, unsigned long rate,
60ab8ba01bSGregory CLEMENT 			       unsigned long *parent_rate)
61ab8ba01bSGregory CLEMENT {
62ab8ba01bSGregory CLEMENT 	/* Valid ratio are 1:1, 1:2 and 1:3 */
63ab8ba01bSGregory CLEMENT 	u32 div;
64ab8ba01bSGregory CLEMENT 
65ab8ba01bSGregory CLEMENT 	div = *parent_rate / rate;
66ab8ba01bSGregory CLEMENT 	if (div == 0)
67ab8ba01bSGregory CLEMENT 		div = 1;
68ab8ba01bSGregory CLEMENT 	else if (div > 3)
69ab8ba01bSGregory CLEMENT 		div = 3;
70ab8ba01bSGregory CLEMENT 
71ab8ba01bSGregory CLEMENT 	return *parent_rate / div;
72ab8ba01bSGregory CLEMENT }
73ab8ba01bSGregory CLEMENT 
clk_cpu_off_set_rate(struct clk_hw * hwclk,unsigned long rate,unsigned long parent_rate)74ee2d8ea1SThomas Petazzoni static int clk_cpu_off_set_rate(struct clk_hw *hwclk, unsigned long rate,
75ab8ba01bSGregory CLEMENT 				unsigned long parent_rate)
76ee2d8ea1SThomas Petazzoni 
77ab8ba01bSGregory CLEMENT {
78ab8ba01bSGregory CLEMENT 	struct cpu_clk *cpuclk = to_cpu_clk(hwclk);
79ab8ba01bSGregory CLEMENT 	u32 reg, div;
80ab8ba01bSGregory CLEMENT 	u32 reload_mask;
81ab8ba01bSGregory CLEMENT 
82ab8ba01bSGregory CLEMENT 	div = parent_rate / rate;
83ab8ba01bSGregory CLEMENT 	reg = (readl(cpuclk->reg_base + SYS_CTRL_CLK_DIVIDER_VALUE_OFFSET)
84ab8ba01bSGregory CLEMENT 		& (~(SYS_CTRL_CLK_DIVIDER_MASK << (cpuclk->cpu * 8))))
85ab8ba01bSGregory CLEMENT 		| (div << (cpuclk->cpu * 8));
86ab8ba01bSGregory CLEMENT 	writel(reg, cpuclk->reg_base + SYS_CTRL_CLK_DIVIDER_VALUE_OFFSET);
87ab8ba01bSGregory CLEMENT 	/* Set clock divider reload smooth bit mask */
88ab8ba01bSGregory CLEMENT 	reload_mask = 1 << (20 + cpuclk->cpu);
89ab8ba01bSGregory CLEMENT 
90ab8ba01bSGregory CLEMENT 	reg = readl(cpuclk->reg_base + SYS_CTRL_CLK_DIVIDER_CTRL_OFFSET)
91ab8ba01bSGregory CLEMENT 	    | reload_mask;
92ab8ba01bSGregory CLEMENT 	writel(reg, cpuclk->reg_base + SYS_CTRL_CLK_DIVIDER_CTRL_OFFSET);
93ab8ba01bSGregory CLEMENT 
94ab8ba01bSGregory CLEMENT 	/* Now trigger the clock update */
95ab8ba01bSGregory CLEMENT 	reg = readl(cpuclk->reg_base + SYS_CTRL_CLK_DIVIDER_CTRL_OFFSET)
96ab8ba01bSGregory CLEMENT 	    | 1 << 24;
97ab8ba01bSGregory CLEMENT 	writel(reg, cpuclk->reg_base + SYS_CTRL_CLK_DIVIDER_CTRL_OFFSET);
98ab8ba01bSGregory CLEMENT 
99ab8ba01bSGregory CLEMENT 	/* Wait for clocks to settle down then clear reload request */
100ab8ba01bSGregory CLEMENT 	udelay(1000);
101ab8ba01bSGregory CLEMENT 	reg &= ~(reload_mask | 1 << 24);
102ab8ba01bSGregory CLEMENT 	writel(reg, cpuclk->reg_base + SYS_CTRL_CLK_DIVIDER_CTRL_OFFSET);
103ab8ba01bSGregory CLEMENT 	udelay(1000);
104ab8ba01bSGregory CLEMENT 
105ab8ba01bSGregory CLEMENT 	return 0;
106ab8ba01bSGregory CLEMENT }
107ab8ba01bSGregory CLEMENT 
clk_cpu_on_set_rate(struct clk_hw * hwclk,unsigned long rate,unsigned long parent_rate)108ee2d8ea1SThomas Petazzoni static int clk_cpu_on_set_rate(struct clk_hw *hwclk, unsigned long rate,
109ee2d8ea1SThomas Petazzoni 			       unsigned long parent_rate)
110ee2d8ea1SThomas Petazzoni {
111ee2d8ea1SThomas Petazzoni 	u32 reg;
112ee2d8ea1SThomas Petazzoni 	unsigned long fabric_div, target_div, cur_rate;
113ee2d8ea1SThomas Petazzoni 	struct cpu_clk *cpuclk = to_cpu_clk(hwclk);
114ee2d8ea1SThomas Petazzoni 
115ee2d8ea1SThomas Petazzoni 	/*
116ee2d8ea1SThomas Petazzoni 	 * PMU DFS registers are not mapped, Device Tree does not
117ee2d8ea1SThomas Petazzoni 	 * describes them. We cannot change the frequency dynamically.
118ee2d8ea1SThomas Petazzoni 	 */
119ee2d8ea1SThomas Petazzoni 	if (!cpuclk->pmu_dfs)
120ee2d8ea1SThomas Petazzoni 		return -ENODEV;
121ee2d8ea1SThomas Petazzoni 
122eca61c9fSStephen Boyd 	cur_rate = clk_hw_get_rate(hwclk);
123ee2d8ea1SThomas Petazzoni 
124ee2d8ea1SThomas Petazzoni 	reg = readl(cpuclk->reg_base + SYS_CTRL_CLK_DIVIDER_CTRL2_OFFSET);
125ee2d8ea1SThomas Petazzoni 	fabric_div = (reg >> SYS_CTRL_CLK_DIVIDER_CTRL2_NBCLK_RATIO_SHIFT) &
126ee2d8ea1SThomas Petazzoni 		SYS_CTRL_CLK_DIVIDER_MASK;
127ee2d8ea1SThomas Petazzoni 
128ee2d8ea1SThomas Petazzoni 	/* Frequency is going up */
129ee2d8ea1SThomas Petazzoni 	if (rate == 2 * cur_rate)
130ee2d8ea1SThomas Petazzoni 		target_div = fabric_div / 2;
131ee2d8ea1SThomas Petazzoni 	/* Frequency is going down */
132ee2d8ea1SThomas Petazzoni 	else
133ee2d8ea1SThomas Petazzoni 		target_div = fabric_div;
134ee2d8ea1SThomas Petazzoni 
135ee2d8ea1SThomas Petazzoni 	if (target_div == 0)
136ee2d8ea1SThomas Petazzoni 		target_div = 1;
137ee2d8ea1SThomas Petazzoni 
138ee2d8ea1SThomas Petazzoni 	reg = readl(cpuclk->pmu_dfs);
139ee2d8ea1SThomas Petazzoni 	reg &= ~(PMU_DFS_RATIO_MASK << PMU_DFS_RATIO_SHIFT);
140ee2d8ea1SThomas Petazzoni 	reg |= (target_div << PMU_DFS_RATIO_SHIFT);
141ee2d8ea1SThomas Petazzoni 	writel(reg, cpuclk->pmu_dfs);
142ee2d8ea1SThomas Petazzoni 
143ee2d8ea1SThomas Petazzoni 	reg = readl(cpuclk->reg_base + SYS_CTRL_CLK_DIVIDER_CTRL_OFFSET);
144ee2d8ea1SThomas Petazzoni 	reg |= (SYS_CTRL_CLK_DIVIDER_CTRL_RESET_ALL <<
145ee2d8ea1SThomas Petazzoni 		SYS_CTRL_CLK_DIVIDER_CTRL_RESET_SHIFT);
146ee2d8ea1SThomas Petazzoni 	writel(reg, cpuclk->reg_base + SYS_CTRL_CLK_DIVIDER_CTRL_OFFSET);
147ee2d8ea1SThomas Petazzoni 
148ee2d8ea1SThomas Petazzoni 	return mvebu_pmsu_dfs_request(cpuclk->cpu);
149ee2d8ea1SThomas Petazzoni }
150ee2d8ea1SThomas Petazzoni 
clk_cpu_set_rate(struct clk_hw * hwclk,unsigned long rate,unsigned long parent_rate)151ee2d8ea1SThomas Petazzoni static int clk_cpu_set_rate(struct clk_hw *hwclk, unsigned long rate,
152ee2d8ea1SThomas Petazzoni 			    unsigned long parent_rate)
153ee2d8ea1SThomas Petazzoni {
154ee2d8ea1SThomas Petazzoni 	if (__clk_is_enabled(hwclk->clk))
155ee2d8ea1SThomas Petazzoni 		return clk_cpu_on_set_rate(hwclk, rate, parent_rate);
156ee2d8ea1SThomas Petazzoni 	else
157ee2d8ea1SThomas Petazzoni 		return clk_cpu_off_set_rate(hwclk, rate, parent_rate);
158ee2d8ea1SThomas Petazzoni }
159ee2d8ea1SThomas Petazzoni 
160ab8ba01bSGregory CLEMENT static const struct clk_ops cpu_ops = {
161ab8ba01bSGregory CLEMENT 	.recalc_rate = clk_cpu_recalc_rate,
162ab8ba01bSGregory CLEMENT 	.round_rate = clk_cpu_round_rate,
163ab8ba01bSGregory CLEMENT 	.set_rate = clk_cpu_set_rate,
164ab8ba01bSGregory CLEMENT };
165ab8ba01bSGregory CLEMENT 
of_cpu_clk_setup(struct device_node * node)1669ac81751SSachin Kamat static void __init of_cpu_clk_setup(struct device_node *node)
167ab8ba01bSGregory CLEMENT {
168ab8ba01bSGregory CLEMENT 	struct cpu_clk *cpuclk;
169ab8ba01bSGregory CLEMENT 	void __iomem *clock_complex_base = of_iomap(node, 0);
170ee2d8ea1SThomas Petazzoni 	void __iomem *pmu_dfs_base = of_iomap(node, 1);
171*bd73d1fdSRob Herring 	int ncpus = num_possible_cpus();
172*bd73d1fdSRob Herring 	int cpu;
173ab8ba01bSGregory CLEMENT 
174ab8ba01bSGregory CLEMENT 	if (clock_complex_base == NULL) {
175ab8ba01bSGregory CLEMENT 		pr_err("%s: clock-complex base register not set\n",
176ab8ba01bSGregory CLEMENT 			__func__);
177ab8ba01bSGregory CLEMENT 		return;
178ab8ba01bSGregory CLEMENT 	}
179ab8ba01bSGregory CLEMENT 
180ee2d8ea1SThomas Petazzoni 	if (pmu_dfs_base == NULL)
181ee2d8ea1SThomas Petazzoni 		pr_warn("%s: pmu-dfs base register not set, dynamic frequency scaling not available\n",
182ee2d8ea1SThomas Petazzoni 			__func__);
183ee2d8ea1SThomas Petazzoni 
18423826e24SMarkus Elfring 	cpuclk = kcalloc(ncpus, sizeof(*cpuclk), GFP_KERNEL);
185ab8ba01bSGregory CLEMENT 	if (WARN_ON(!cpuclk))
186f98d007dSJisheng Zhang 		goto cpuclk_out;
187ab8ba01bSGregory CLEMENT 
18823826e24SMarkus Elfring 	clks = kcalloc(ncpus, sizeof(*clks), GFP_KERNEL);
189ab8ba01bSGregory CLEMENT 	if (WARN_ON(!clks))
190d6f620a4SCong Ding 		goto clks_out;
191ab8ba01bSGregory CLEMENT 
192*bd73d1fdSRob Herring 	for_each_possible_cpu(cpu) {
193ab8ba01bSGregory CLEMENT 		struct clk_init_data init;
194ab8ba01bSGregory CLEMENT 		struct clk *clk;
195ab8ba01bSGregory CLEMENT 		char *clk_name = kzalloc(5, GFP_KERNEL);
196ab8ba01bSGregory CLEMENT 
197ab8ba01bSGregory CLEMENT 		if (WARN_ON(!clk_name))
198d6f620a4SCong Ding 			goto bail_out;
199ab8ba01bSGregory CLEMENT 
200ab8ba01bSGregory CLEMENT 		sprintf(clk_name, "cpu%d", cpu);
201ab8ba01bSGregory CLEMENT 
20261e22fffSStephen Boyd 		cpuclk[cpu].parent_name = of_clk_get_parent_name(node, 0);
203ab8ba01bSGregory CLEMENT 		cpuclk[cpu].clk_name = clk_name;
204ab8ba01bSGregory CLEMENT 		cpuclk[cpu].cpu = cpu;
205ab8ba01bSGregory CLEMENT 		cpuclk[cpu].reg_base = clock_complex_base;
206ee2d8ea1SThomas Petazzoni 		if (pmu_dfs_base)
207ee2d8ea1SThomas Petazzoni 			cpuclk[cpu].pmu_dfs = pmu_dfs_base + 4 * cpu;
208ab8ba01bSGregory CLEMENT 		cpuclk[cpu].hw.init = &init;
209ab8ba01bSGregory CLEMENT 
210ab8ba01bSGregory CLEMENT 		init.name = cpuclk[cpu].clk_name;
211ab8ba01bSGregory CLEMENT 		init.ops = &cpu_ops;
212ab8ba01bSGregory CLEMENT 		init.flags = 0;
213ab8ba01bSGregory CLEMENT 		init.parent_names = &cpuclk[cpu].parent_name;
214ab8ba01bSGregory CLEMENT 		init.num_parents = 1;
215ab8ba01bSGregory CLEMENT 
216ab8ba01bSGregory CLEMENT 		clk = clk_register(NULL, &cpuclk[cpu].hw);
217ab8ba01bSGregory CLEMENT 		if (WARN_ON(IS_ERR(clk)))
218ab8ba01bSGregory CLEMENT 			goto bail_out;
219ab8ba01bSGregory CLEMENT 		clks[cpu] = clk;
220ab8ba01bSGregory CLEMENT 	}
221ab8ba01bSGregory CLEMENT 	clk_data.clk_num = MAX_CPU;
222ab8ba01bSGregory CLEMENT 	clk_data.clks = clks;
223ab8ba01bSGregory CLEMENT 	of_clk_add_provider(node, of_clk_src_onecell_get, &clk_data);
224ab8ba01bSGregory CLEMENT 
225ab8ba01bSGregory CLEMENT 	return;
226ab8ba01bSGregory CLEMENT bail_out:
227ab8ba01bSGregory CLEMENT 	kfree(clks);
228d6f620a4SCong Ding 	while(ncpus--)
229d6f620a4SCong Ding 		kfree(cpuclk[ncpus].clk_name);
230d6f620a4SCong Ding clks_out:
231ab8ba01bSGregory CLEMENT 	kfree(cpuclk);
232f98d007dSJisheng Zhang cpuclk_out:
233f98d007dSJisheng Zhang 	iounmap(clock_complex_base);
234ab8ba01bSGregory CLEMENT }
235ab8ba01bSGregory CLEMENT 
236f640c0faSJean-Francois Moine CLK_OF_DECLARE(armada_xp_cpu_clock, "marvell,armada-xp-cpu-clock",
237f640c0faSJean-Francois Moine 					 of_cpu_clk_setup);
238e120c17aSChris Packham 
of_mv98dx3236_cpu_clk_setup(struct device_node * node)239e120c17aSChris Packham static void __init of_mv98dx3236_cpu_clk_setup(struct device_node *node)
240e120c17aSChris Packham {
241e120c17aSChris Packham 	of_clk_add_provider(node, of_clk_src_simple_get, NULL);
242e120c17aSChris Packham }
243e120c17aSChris Packham 
244e120c17aSChris Packham CLK_OF_DECLARE(mv98dx3236_cpu_clock, "marvell,mv98dx3236-cpu-clock",
245e120c17aSChris Packham 					 of_mv98dx3236_cpu_clk_setup);
246