xref: /linux/drivers/clk/tegra/clk-tegra124-emc.c (revision 3dcbd36fa34ce9124ec51accd835130251f74213)
1*3dcbd36fSThierry Reding // SPDX-License-Identifier: GPL-2.0-only
2*3dcbd36fSThierry Reding /*
3*3dcbd36fSThierry Reding  * drivers/clk/tegra/clk-emc.c
4*3dcbd36fSThierry Reding  *
5*3dcbd36fSThierry Reding  * Copyright (c) 2014, NVIDIA CORPORATION.  All rights reserved.
6*3dcbd36fSThierry Reding  *
7*3dcbd36fSThierry Reding  * Author:
8*3dcbd36fSThierry Reding  *	Mikko Perttunen <mperttunen@nvidia.com>
9*3dcbd36fSThierry Reding  */
10*3dcbd36fSThierry Reding 
11*3dcbd36fSThierry Reding #include <linux/clk-provider.h>
12*3dcbd36fSThierry Reding #include <linux/clk.h>
13*3dcbd36fSThierry Reding #include <linux/clkdev.h>
14*3dcbd36fSThierry Reding #include <linux/delay.h>
15*3dcbd36fSThierry Reding #include <linux/io.h>
16*3dcbd36fSThierry Reding #include <linux/module.h>
17*3dcbd36fSThierry Reding #include <linux/of_address.h>
18*3dcbd36fSThierry Reding #include <linux/of_platform.h>
19*3dcbd36fSThierry Reding #include <linux/platform_device.h>
20*3dcbd36fSThierry Reding #include <linux/sort.h>
21*3dcbd36fSThierry Reding #include <linux/string.h>
22*3dcbd36fSThierry Reding 
23*3dcbd36fSThierry Reding #include <soc/tegra/fuse.h>
24*3dcbd36fSThierry Reding #include <soc/tegra/emc.h>
25*3dcbd36fSThierry Reding 
26*3dcbd36fSThierry Reding #include "clk.h"
27*3dcbd36fSThierry Reding 
28*3dcbd36fSThierry Reding #define CLK_SOURCE_EMC 0x19c
29*3dcbd36fSThierry Reding 
30*3dcbd36fSThierry Reding #define CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR_SHIFT 0
31*3dcbd36fSThierry Reding #define CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR_MASK 0xff
32*3dcbd36fSThierry Reding #define CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR(x) (((x) & CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR_MASK) << \
33*3dcbd36fSThierry Reding 					      CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR_SHIFT)
34*3dcbd36fSThierry Reding 
35*3dcbd36fSThierry Reding #define CLK_SOURCE_EMC_EMC_2X_CLK_SRC_SHIFT 29
36*3dcbd36fSThierry Reding #define CLK_SOURCE_EMC_EMC_2X_CLK_SRC_MASK 0x7
37*3dcbd36fSThierry Reding #define CLK_SOURCE_EMC_EMC_2X_CLK_SRC(x) (((x) & CLK_SOURCE_EMC_EMC_2X_CLK_SRC_MASK) << \
38*3dcbd36fSThierry Reding 					  CLK_SOURCE_EMC_EMC_2X_CLK_SRC_SHIFT)
39*3dcbd36fSThierry Reding 
40*3dcbd36fSThierry Reding static const char * const emc_parent_clk_names[] = {
41*3dcbd36fSThierry Reding 	"pll_m", "pll_c", "pll_p", "clk_m", "pll_m_ud",
42*3dcbd36fSThierry Reding 	"pll_c2", "pll_c3", "pll_c_ud"
43*3dcbd36fSThierry Reding };
44*3dcbd36fSThierry Reding 
45*3dcbd36fSThierry Reding /*
46*3dcbd36fSThierry Reding  * List of clock sources for various parents the EMC clock can have.
47*3dcbd36fSThierry Reding  * When we change the timing to a timing with a parent that has the same
48*3dcbd36fSThierry Reding  * clock source as the current parent, we must first change to a backup
49*3dcbd36fSThierry Reding  * timing that has a different clock source.
50*3dcbd36fSThierry Reding  */
51*3dcbd36fSThierry Reding 
52*3dcbd36fSThierry Reding #define EMC_SRC_PLL_M 0
53*3dcbd36fSThierry Reding #define EMC_SRC_PLL_C 1
54*3dcbd36fSThierry Reding #define EMC_SRC_PLL_P 2
55*3dcbd36fSThierry Reding #define EMC_SRC_CLK_M 3
56*3dcbd36fSThierry Reding #define EMC_SRC_PLL_C2 4
57*3dcbd36fSThierry Reding #define EMC_SRC_PLL_C3 5
58*3dcbd36fSThierry Reding 
59*3dcbd36fSThierry Reding static const char emc_parent_clk_sources[] = {
60*3dcbd36fSThierry Reding 	EMC_SRC_PLL_M, EMC_SRC_PLL_C, EMC_SRC_PLL_P, EMC_SRC_CLK_M,
61*3dcbd36fSThierry Reding 	EMC_SRC_PLL_M, EMC_SRC_PLL_C2, EMC_SRC_PLL_C3, EMC_SRC_PLL_C
62*3dcbd36fSThierry Reding };
63*3dcbd36fSThierry Reding 
64*3dcbd36fSThierry Reding struct emc_timing {
65*3dcbd36fSThierry Reding 	unsigned long rate, parent_rate;
66*3dcbd36fSThierry Reding 	u8 parent_index;
67*3dcbd36fSThierry Reding 	struct clk *parent;
68*3dcbd36fSThierry Reding 	u32 ram_code;
69*3dcbd36fSThierry Reding };
70*3dcbd36fSThierry Reding 
71*3dcbd36fSThierry Reding struct tegra_clk_emc {
72*3dcbd36fSThierry Reding 	struct clk_hw hw;
73*3dcbd36fSThierry Reding 	void __iomem *clk_regs;
74*3dcbd36fSThierry Reding 	struct clk *prev_parent;
75*3dcbd36fSThierry Reding 	bool changing_timing;
76*3dcbd36fSThierry Reding 
77*3dcbd36fSThierry Reding 	struct device_node *emc_node;
78*3dcbd36fSThierry Reding 	struct tegra_emc *emc;
79*3dcbd36fSThierry Reding 
80*3dcbd36fSThierry Reding 	int num_timings;
81*3dcbd36fSThierry Reding 	struct emc_timing *timings;
82*3dcbd36fSThierry Reding 	spinlock_t *lock;
83*3dcbd36fSThierry Reding };
84*3dcbd36fSThierry Reding 
85*3dcbd36fSThierry Reding /* Common clock framework callback implementations */
86*3dcbd36fSThierry Reding 
87*3dcbd36fSThierry Reding static unsigned long emc_recalc_rate(struct clk_hw *hw,
88*3dcbd36fSThierry Reding 				     unsigned long parent_rate)
89*3dcbd36fSThierry Reding {
90*3dcbd36fSThierry Reding 	struct tegra_clk_emc *tegra;
91*3dcbd36fSThierry Reding 	u32 val, div;
92*3dcbd36fSThierry Reding 
93*3dcbd36fSThierry Reding 	tegra = container_of(hw, struct tegra_clk_emc, hw);
94*3dcbd36fSThierry Reding 
95*3dcbd36fSThierry Reding 	/*
96*3dcbd36fSThierry Reding 	 * CCF wrongly assumes that the parent won't change during set_rate,
97*3dcbd36fSThierry Reding 	 * so get the parent rate explicitly.
98*3dcbd36fSThierry Reding 	 */
99*3dcbd36fSThierry Reding 	parent_rate = clk_hw_get_rate(clk_hw_get_parent(hw));
100*3dcbd36fSThierry Reding 
101*3dcbd36fSThierry Reding 	val = readl(tegra->clk_regs + CLK_SOURCE_EMC);
102*3dcbd36fSThierry Reding 	div = val & CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR_MASK;
103*3dcbd36fSThierry Reding 
104*3dcbd36fSThierry Reding 	return parent_rate / (div + 2) * 2;
105*3dcbd36fSThierry Reding }
106*3dcbd36fSThierry Reding 
107*3dcbd36fSThierry Reding /*
108*3dcbd36fSThierry Reding  * Rounds up unless no higher rate exists, in which case down. This way is
109*3dcbd36fSThierry Reding  * safer since things have EMC rate floors. Also don't touch parent_rate
110*3dcbd36fSThierry Reding  * since we don't want the CCF to play with our parent clocks.
111*3dcbd36fSThierry Reding  */
112*3dcbd36fSThierry Reding static int emc_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
113*3dcbd36fSThierry Reding {
114*3dcbd36fSThierry Reding 	struct tegra_clk_emc *tegra;
115*3dcbd36fSThierry Reding 	u8 ram_code = tegra_read_ram_code();
116*3dcbd36fSThierry Reding 	struct emc_timing *timing = NULL;
117*3dcbd36fSThierry Reding 	int i, k, t;
118*3dcbd36fSThierry Reding 
119*3dcbd36fSThierry Reding 	tegra = container_of(hw, struct tegra_clk_emc, hw);
120*3dcbd36fSThierry Reding 
121*3dcbd36fSThierry Reding 	for (k = 0; k < tegra->num_timings; k++) {
122*3dcbd36fSThierry Reding 		if (tegra->timings[k].ram_code == ram_code)
123*3dcbd36fSThierry Reding 			break;
124*3dcbd36fSThierry Reding 	}
125*3dcbd36fSThierry Reding 
126*3dcbd36fSThierry Reding 	for (t = k; t < tegra->num_timings; t++) {
127*3dcbd36fSThierry Reding 		if (tegra->timings[t].ram_code != ram_code)
128*3dcbd36fSThierry Reding 			break;
129*3dcbd36fSThierry Reding 	}
130*3dcbd36fSThierry Reding 
131*3dcbd36fSThierry Reding 	for (i = k; i < t; i++) {
132*3dcbd36fSThierry Reding 		timing = tegra->timings + i;
133*3dcbd36fSThierry Reding 
134*3dcbd36fSThierry Reding 		if (timing->rate < req->rate && i != t - 1)
135*3dcbd36fSThierry Reding 			continue;
136*3dcbd36fSThierry Reding 
137*3dcbd36fSThierry Reding 		if (timing->rate > req->max_rate) {
138*3dcbd36fSThierry Reding 			i = max(i, k + 1);
139*3dcbd36fSThierry Reding 			req->rate = tegra->timings[i - 1].rate;
140*3dcbd36fSThierry Reding 			return 0;
141*3dcbd36fSThierry Reding 		}
142*3dcbd36fSThierry Reding 
143*3dcbd36fSThierry Reding 		if (timing->rate < req->min_rate)
144*3dcbd36fSThierry Reding 			continue;
145*3dcbd36fSThierry Reding 
146*3dcbd36fSThierry Reding 		req->rate = timing->rate;
147*3dcbd36fSThierry Reding 		return 0;
148*3dcbd36fSThierry Reding 	}
149*3dcbd36fSThierry Reding 
150*3dcbd36fSThierry Reding 	if (timing) {
151*3dcbd36fSThierry Reding 		req->rate = timing->rate;
152*3dcbd36fSThierry Reding 		return 0;
153*3dcbd36fSThierry Reding 	}
154*3dcbd36fSThierry Reding 
155*3dcbd36fSThierry Reding 	req->rate = clk_hw_get_rate(hw);
156*3dcbd36fSThierry Reding 	return 0;
157*3dcbd36fSThierry Reding }
158*3dcbd36fSThierry Reding 
159*3dcbd36fSThierry Reding static u8 emc_get_parent(struct clk_hw *hw)
160*3dcbd36fSThierry Reding {
161*3dcbd36fSThierry Reding 	struct tegra_clk_emc *tegra;
162*3dcbd36fSThierry Reding 	u32 val;
163*3dcbd36fSThierry Reding 
164*3dcbd36fSThierry Reding 	tegra = container_of(hw, struct tegra_clk_emc, hw);
165*3dcbd36fSThierry Reding 
166*3dcbd36fSThierry Reding 	val = readl(tegra->clk_regs + CLK_SOURCE_EMC);
167*3dcbd36fSThierry Reding 
168*3dcbd36fSThierry Reding 	return (val >> CLK_SOURCE_EMC_EMC_2X_CLK_SRC_SHIFT)
169*3dcbd36fSThierry Reding 		& CLK_SOURCE_EMC_EMC_2X_CLK_SRC_MASK;
170*3dcbd36fSThierry Reding }
171*3dcbd36fSThierry Reding 
172*3dcbd36fSThierry Reding static struct tegra_emc *emc_ensure_emc_driver(struct tegra_clk_emc *tegra)
173*3dcbd36fSThierry Reding {
174*3dcbd36fSThierry Reding 	struct platform_device *pdev;
175*3dcbd36fSThierry Reding 
176*3dcbd36fSThierry Reding 	if (tegra->emc)
177*3dcbd36fSThierry Reding 		return tegra->emc;
178*3dcbd36fSThierry Reding 
179*3dcbd36fSThierry Reding 	if (!tegra->emc_node)
180*3dcbd36fSThierry Reding 		return NULL;
181*3dcbd36fSThierry Reding 
182*3dcbd36fSThierry Reding 	pdev = of_find_device_by_node(tegra->emc_node);
183*3dcbd36fSThierry Reding 	if (!pdev) {
184*3dcbd36fSThierry Reding 		pr_err("%s: could not get external memory controller\n",
185*3dcbd36fSThierry Reding 		       __func__);
186*3dcbd36fSThierry Reding 		return NULL;
187*3dcbd36fSThierry Reding 	}
188*3dcbd36fSThierry Reding 
189*3dcbd36fSThierry Reding 	of_node_put(tegra->emc_node);
190*3dcbd36fSThierry Reding 	tegra->emc_node = NULL;
191*3dcbd36fSThierry Reding 
192*3dcbd36fSThierry Reding 	tegra->emc = platform_get_drvdata(pdev);
193*3dcbd36fSThierry Reding 	if (!tegra->emc) {
194*3dcbd36fSThierry Reding 		pr_err("%s: cannot find EMC driver\n", __func__);
195*3dcbd36fSThierry Reding 		return NULL;
196*3dcbd36fSThierry Reding 	}
197*3dcbd36fSThierry Reding 
198*3dcbd36fSThierry Reding 	return tegra->emc;
199*3dcbd36fSThierry Reding }
200*3dcbd36fSThierry Reding 
201*3dcbd36fSThierry Reding static int emc_set_timing(struct tegra_clk_emc *tegra,
202*3dcbd36fSThierry Reding 			  struct emc_timing *timing)
203*3dcbd36fSThierry Reding {
204*3dcbd36fSThierry Reding 	int err;
205*3dcbd36fSThierry Reding 	u8 div;
206*3dcbd36fSThierry Reding 	u32 car_value;
207*3dcbd36fSThierry Reding 	unsigned long flags = 0;
208*3dcbd36fSThierry Reding 	struct tegra_emc *emc = emc_ensure_emc_driver(tegra);
209*3dcbd36fSThierry Reding 
210*3dcbd36fSThierry Reding 	if (!emc)
211*3dcbd36fSThierry Reding 		return -ENOENT;
212*3dcbd36fSThierry Reding 
213*3dcbd36fSThierry Reding 	pr_debug("going to rate %ld prate %ld p %s\n", timing->rate,
214*3dcbd36fSThierry Reding 		 timing->parent_rate, __clk_get_name(timing->parent));
215*3dcbd36fSThierry Reding 
216*3dcbd36fSThierry Reding 	if (emc_get_parent(&tegra->hw) == timing->parent_index &&
217*3dcbd36fSThierry Reding 	    clk_get_rate(timing->parent) != timing->parent_rate) {
218*3dcbd36fSThierry Reding 		WARN_ONCE(1, "parent %s rate mismatch %lu %lu\n",
219*3dcbd36fSThierry Reding 			  __clk_get_name(timing->parent),
220*3dcbd36fSThierry Reding 			  clk_get_rate(timing->parent),
221*3dcbd36fSThierry Reding 			  timing->parent_rate);
222*3dcbd36fSThierry Reding 		return -EINVAL;
223*3dcbd36fSThierry Reding 	}
224*3dcbd36fSThierry Reding 
225*3dcbd36fSThierry Reding 	tegra->changing_timing = true;
226*3dcbd36fSThierry Reding 
227*3dcbd36fSThierry Reding 	err = clk_set_rate(timing->parent, timing->parent_rate);
228*3dcbd36fSThierry Reding 	if (err) {
229*3dcbd36fSThierry Reding 		pr_err("cannot change parent %s rate to %ld: %d\n",
230*3dcbd36fSThierry Reding 		       __clk_get_name(timing->parent), timing->parent_rate,
231*3dcbd36fSThierry Reding 		       err);
232*3dcbd36fSThierry Reding 
233*3dcbd36fSThierry Reding 		return err;
234*3dcbd36fSThierry Reding 	}
235*3dcbd36fSThierry Reding 
236*3dcbd36fSThierry Reding 	err = clk_prepare_enable(timing->parent);
237*3dcbd36fSThierry Reding 	if (err) {
238*3dcbd36fSThierry Reding 		pr_err("cannot enable parent clock: %d\n", err);
239*3dcbd36fSThierry Reding 		return err;
240*3dcbd36fSThierry Reding 	}
241*3dcbd36fSThierry Reding 
242*3dcbd36fSThierry Reding 	div = timing->parent_rate / (timing->rate / 2) - 2;
243*3dcbd36fSThierry Reding 
244*3dcbd36fSThierry Reding 	err = tegra_emc_prepare_timing_change(emc, timing->rate);
245*3dcbd36fSThierry Reding 	if (err)
246*3dcbd36fSThierry Reding 		return err;
247*3dcbd36fSThierry Reding 
248*3dcbd36fSThierry Reding 	spin_lock_irqsave(tegra->lock, flags);
249*3dcbd36fSThierry Reding 
250*3dcbd36fSThierry Reding 	car_value = readl(tegra->clk_regs + CLK_SOURCE_EMC);
251*3dcbd36fSThierry Reding 
252*3dcbd36fSThierry Reding 	car_value &= ~CLK_SOURCE_EMC_EMC_2X_CLK_SRC(~0);
253*3dcbd36fSThierry Reding 	car_value |= CLK_SOURCE_EMC_EMC_2X_CLK_SRC(timing->parent_index);
254*3dcbd36fSThierry Reding 
255*3dcbd36fSThierry Reding 	car_value &= ~CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR(~0);
256*3dcbd36fSThierry Reding 	car_value |= CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR(div);
257*3dcbd36fSThierry Reding 
258*3dcbd36fSThierry Reding 	writel(car_value, tegra->clk_regs + CLK_SOURCE_EMC);
259*3dcbd36fSThierry Reding 
260*3dcbd36fSThierry Reding 	spin_unlock_irqrestore(tegra->lock, flags);
261*3dcbd36fSThierry Reding 
262*3dcbd36fSThierry Reding 	tegra_emc_complete_timing_change(emc, timing->rate);
263*3dcbd36fSThierry Reding 
264*3dcbd36fSThierry Reding 	clk_hw_reparent(&tegra->hw, __clk_get_hw(timing->parent));
265*3dcbd36fSThierry Reding 	clk_disable_unprepare(tegra->prev_parent);
266*3dcbd36fSThierry Reding 
267*3dcbd36fSThierry Reding 	tegra->prev_parent = timing->parent;
268*3dcbd36fSThierry Reding 	tegra->changing_timing = false;
269*3dcbd36fSThierry Reding 
270*3dcbd36fSThierry Reding 	return 0;
271*3dcbd36fSThierry Reding }
272*3dcbd36fSThierry Reding 
273*3dcbd36fSThierry Reding /*
274*3dcbd36fSThierry Reding  * Get backup timing to use as an intermediate step when a change between
275*3dcbd36fSThierry Reding  * two timings with the same clock source has been requested. First try to
276*3dcbd36fSThierry Reding  * find a timing with a higher clock rate to avoid a rate below any set rate
277*3dcbd36fSThierry Reding  * floors. If that is not possible, find a lower rate.
278*3dcbd36fSThierry Reding  */
279*3dcbd36fSThierry Reding static struct emc_timing *get_backup_timing(struct tegra_clk_emc *tegra,
280*3dcbd36fSThierry Reding 					    int timing_index)
281*3dcbd36fSThierry Reding {
282*3dcbd36fSThierry Reding 	int i;
283*3dcbd36fSThierry Reding 	u32 ram_code = tegra_read_ram_code();
284*3dcbd36fSThierry Reding 	struct emc_timing *timing;
285*3dcbd36fSThierry Reding 
286*3dcbd36fSThierry Reding 	for (i = timing_index+1; i < tegra->num_timings; i++) {
287*3dcbd36fSThierry Reding 		timing = tegra->timings + i;
288*3dcbd36fSThierry Reding 		if (timing->ram_code != ram_code)
289*3dcbd36fSThierry Reding 			break;
290*3dcbd36fSThierry Reding 
291*3dcbd36fSThierry Reding 		if (emc_parent_clk_sources[timing->parent_index] !=
292*3dcbd36fSThierry Reding 		    emc_parent_clk_sources[
293*3dcbd36fSThierry Reding 		      tegra->timings[timing_index].parent_index])
294*3dcbd36fSThierry Reding 			return timing;
295*3dcbd36fSThierry Reding 	}
296*3dcbd36fSThierry Reding 
297*3dcbd36fSThierry Reding 	for (i = timing_index-1; i >= 0; --i) {
298*3dcbd36fSThierry Reding 		timing = tegra->timings + i;
299*3dcbd36fSThierry Reding 		if (timing->ram_code != ram_code)
300*3dcbd36fSThierry Reding 			break;
301*3dcbd36fSThierry Reding 
302*3dcbd36fSThierry Reding 		if (emc_parent_clk_sources[timing->parent_index] !=
303*3dcbd36fSThierry Reding 		    emc_parent_clk_sources[
304*3dcbd36fSThierry Reding 		      tegra->timings[timing_index].parent_index])
305*3dcbd36fSThierry Reding 			return timing;
306*3dcbd36fSThierry Reding 	}
307*3dcbd36fSThierry Reding 
308*3dcbd36fSThierry Reding 	return NULL;
309*3dcbd36fSThierry Reding }
310*3dcbd36fSThierry Reding 
311*3dcbd36fSThierry Reding static int emc_set_rate(struct clk_hw *hw, unsigned long rate,
312*3dcbd36fSThierry Reding 			unsigned long parent_rate)
313*3dcbd36fSThierry Reding {
314*3dcbd36fSThierry Reding 	struct tegra_clk_emc *tegra;
315*3dcbd36fSThierry Reding 	struct emc_timing *timing = NULL;
316*3dcbd36fSThierry Reding 	int i, err;
317*3dcbd36fSThierry Reding 	u32 ram_code = tegra_read_ram_code();
318*3dcbd36fSThierry Reding 
319*3dcbd36fSThierry Reding 	tegra = container_of(hw, struct tegra_clk_emc, hw);
320*3dcbd36fSThierry Reding 
321*3dcbd36fSThierry Reding 	if (clk_hw_get_rate(hw) == rate)
322*3dcbd36fSThierry Reding 		return 0;
323*3dcbd36fSThierry Reding 
324*3dcbd36fSThierry Reding 	/*
325*3dcbd36fSThierry Reding 	 * When emc_set_timing changes the parent rate, CCF will propagate
326*3dcbd36fSThierry Reding 	 * that downward to us, so ignore any set_rate calls while a rate
327*3dcbd36fSThierry Reding 	 * change is already going on.
328*3dcbd36fSThierry Reding 	 */
329*3dcbd36fSThierry Reding 	if (tegra->changing_timing)
330*3dcbd36fSThierry Reding 		return 0;
331*3dcbd36fSThierry Reding 
332*3dcbd36fSThierry Reding 	for (i = 0; i < tegra->num_timings; i++) {
333*3dcbd36fSThierry Reding 		if (tegra->timings[i].rate == rate &&
334*3dcbd36fSThierry Reding 		    tegra->timings[i].ram_code == ram_code) {
335*3dcbd36fSThierry Reding 			timing = tegra->timings + i;
336*3dcbd36fSThierry Reding 			break;
337*3dcbd36fSThierry Reding 		}
338*3dcbd36fSThierry Reding 	}
339*3dcbd36fSThierry Reding 
340*3dcbd36fSThierry Reding 	if (!timing) {
341*3dcbd36fSThierry Reding 		pr_err("cannot switch to rate %ld without emc table\n", rate);
342*3dcbd36fSThierry Reding 		return -EINVAL;
343*3dcbd36fSThierry Reding 	}
344*3dcbd36fSThierry Reding 
345*3dcbd36fSThierry Reding 	if (emc_parent_clk_sources[emc_get_parent(hw)] ==
346*3dcbd36fSThierry Reding 	    emc_parent_clk_sources[timing->parent_index] &&
347*3dcbd36fSThierry Reding 	    clk_get_rate(timing->parent) != timing->parent_rate) {
348*3dcbd36fSThierry Reding 		/*
349*3dcbd36fSThierry Reding 		 * Parent clock source not changed but parent rate has changed,
350*3dcbd36fSThierry Reding 		 * need to temporarily switch to another parent
351*3dcbd36fSThierry Reding 		 */
352*3dcbd36fSThierry Reding 
353*3dcbd36fSThierry Reding 		struct emc_timing *backup_timing;
354*3dcbd36fSThierry Reding 
355*3dcbd36fSThierry Reding 		backup_timing = get_backup_timing(tegra, i);
356*3dcbd36fSThierry Reding 		if (!backup_timing) {
357*3dcbd36fSThierry Reding 			pr_err("cannot find backup timing\n");
358*3dcbd36fSThierry Reding 			return -EINVAL;
359*3dcbd36fSThierry Reding 		}
360*3dcbd36fSThierry Reding 
361*3dcbd36fSThierry Reding 		pr_debug("using %ld as backup rate when going to %ld\n",
362*3dcbd36fSThierry Reding 			 backup_timing->rate, rate);
363*3dcbd36fSThierry Reding 
364*3dcbd36fSThierry Reding 		err = emc_set_timing(tegra, backup_timing);
365*3dcbd36fSThierry Reding 		if (err) {
366*3dcbd36fSThierry Reding 			pr_err("cannot set backup timing: %d\n", err);
367*3dcbd36fSThierry Reding 			return err;
368*3dcbd36fSThierry Reding 		}
369*3dcbd36fSThierry Reding 	}
370*3dcbd36fSThierry Reding 
371*3dcbd36fSThierry Reding 	return emc_set_timing(tegra, timing);
372*3dcbd36fSThierry Reding }
373*3dcbd36fSThierry Reding 
374*3dcbd36fSThierry Reding /* Initialization and deinitialization */
375*3dcbd36fSThierry Reding 
376*3dcbd36fSThierry Reding static int load_one_timing_from_dt(struct tegra_clk_emc *tegra,
377*3dcbd36fSThierry Reding 				   struct emc_timing *timing,
378*3dcbd36fSThierry Reding 				   struct device_node *node)
379*3dcbd36fSThierry Reding {
380*3dcbd36fSThierry Reding 	int err, i;
381*3dcbd36fSThierry Reding 	u32 tmp;
382*3dcbd36fSThierry Reding 
383*3dcbd36fSThierry Reding 	err = of_property_read_u32(node, "clock-frequency", &tmp);
384*3dcbd36fSThierry Reding 	if (err) {
385*3dcbd36fSThierry Reding 		pr_err("timing %pOF: failed to read rate\n", node);
386*3dcbd36fSThierry Reding 		return err;
387*3dcbd36fSThierry Reding 	}
388*3dcbd36fSThierry Reding 
389*3dcbd36fSThierry Reding 	timing->rate = tmp;
390*3dcbd36fSThierry Reding 
391*3dcbd36fSThierry Reding 	err = of_property_read_u32(node, "nvidia,parent-clock-frequency", &tmp);
392*3dcbd36fSThierry Reding 	if (err) {
393*3dcbd36fSThierry Reding 		pr_err("timing %pOF: failed to read parent rate\n", node);
394*3dcbd36fSThierry Reding 		return err;
395*3dcbd36fSThierry Reding 	}
396*3dcbd36fSThierry Reding 
397*3dcbd36fSThierry Reding 	timing->parent_rate = tmp;
398*3dcbd36fSThierry Reding 
399*3dcbd36fSThierry Reding 	timing->parent = of_clk_get_by_name(node, "emc-parent");
400*3dcbd36fSThierry Reding 	if (IS_ERR(timing->parent)) {
401*3dcbd36fSThierry Reding 		pr_err("timing %pOF: failed to get parent clock\n", node);
402*3dcbd36fSThierry Reding 		return PTR_ERR(timing->parent);
403*3dcbd36fSThierry Reding 	}
404*3dcbd36fSThierry Reding 
405*3dcbd36fSThierry Reding 	timing->parent_index = 0xff;
406*3dcbd36fSThierry Reding 	i = match_string(emc_parent_clk_names, ARRAY_SIZE(emc_parent_clk_names),
407*3dcbd36fSThierry Reding 			 __clk_get_name(timing->parent));
408*3dcbd36fSThierry Reding 	if (i < 0) {
409*3dcbd36fSThierry Reding 		pr_err("timing %pOF: %s is not a valid parent\n",
410*3dcbd36fSThierry Reding 		       node, __clk_get_name(timing->parent));
411*3dcbd36fSThierry Reding 		clk_put(timing->parent);
412*3dcbd36fSThierry Reding 		return -EINVAL;
413*3dcbd36fSThierry Reding 	}
414*3dcbd36fSThierry Reding 
415*3dcbd36fSThierry Reding 	timing->parent_index = i;
416*3dcbd36fSThierry Reding 	return 0;
417*3dcbd36fSThierry Reding }
418*3dcbd36fSThierry Reding 
419*3dcbd36fSThierry Reding static int cmp_timings(const void *_a, const void *_b)
420*3dcbd36fSThierry Reding {
421*3dcbd36fSThierry Reding 	const struct emc_timing *a = _a;
422*3dcbd36fSThierry Reding 	const struct emc_timing *b = _b;
423*3dcbd36fSThierry Reding 
424*3dcbd36fSThierry Reding 	if (a->rate < b->rate)
425*3dcbd36fSThierry Reding 		return -1;
426*3dcbd36fSThierry Reding 	else if (a->rate == b->rate)
427*3dcbd36fSThierry Reding 		return 0;
428*3dcbd36fSThierry Reding 	else
429*3dcbd36fSThierry Reding 		return 1;
430*3dcbd36fSThierry Reding }
431*3dcbd36fSThierry Reding 
432*3dcbd36fSThierry Reding static int load_timings_from_dt(struct tegra_clk_emc *tegra,
433*3dcbd36fSThierry Reding 				struct device_node *node,
434*3dcbd36fSThierry Reding 				u32 ram_code)
435*3dcbd36fSThierry Reding {
436*3dcbd36fSThierry Reding 	struct emc_timing *timings_ptr;
437*3dcbd36fSThierry Reding 	struct device_node *child;
438*3dcbd36fSThierry Reding 	int child_count = of_get_child_count(node);
439*3dcbd36fSThierry Reding 	int i = 0, err;
440*3dcbd36fSThierry Reding 	size_t size;
441*3dcbd36fSThierry Reding 
442*3dcbd36fSThierry Reding 	size = (tegra->num_timings + child_count) * sizeof(struct emc_timing);
443*3dcbd36fSThierry Reding 
444*3dcbd36fSThierry Reding 	tegra->timings = krealloc(tegra->timings, size, GFP_KERNEL);
445*3dcbd36fSThierry Reding 	if (!tegra->timings)
446*3dcbd36fSThierry Reding 		return -ENOMEM;
447*3dcbd36fSThierry Reding 
448*3dcbd36fSThierry Reding 	timings_ptr = tegra->timings + tegra->num_timings;
449*3dcbd36fSThierry Reding 	tegra->num_timings += child_count;
450*3dcbd36fSThierry Reding 
451*3dcbd36fSThierry Reding 	for_each_child_of_node(node, child) {
452*3dcbd36fSThierry Reding 		struct emc_timing *timing = timings_ptr + (i++);
453*3dcbd36fSThierry Reding 
454*3dcbd36fSThierry Reding 		err = load_one_timing_from_dt(tegra, timing, child);
455*3dcbd36fSThierry Reding 		if (err) {
456*3dcbd36fSThierry Reding 			of_node_put(child);
457*3dcbd36fSThierry Reding 			return err;
458*3dcbd36fSThierry Reding 		}
459*3dcbd36fSThierry Reding 
460*3dcbd36fSThierry Reding 		timing->ram_code = ram_code;
461*3dcbd36fSThierry Reding 	}
462*3dcbd36fSThierry Reding 
463*3dcbd36fSThierry Reding 	sort(timings_ptr, child_count, sizeof(struct emc_timing),
464*3dcbd36fSThierry Reding 	     cmp_timings, NULL);
465*3dcbd36fSThierry Reding 
466*3dcbd36fSThierry Reding 	return 0;
467*3dcbd36fSThierry Reding }
468*3dcbd36fSThierry Reding 
469*3dcbd36fSThierry Reding static const struct clk_ops tegra_clk_emc_ops = {
470*3dcbd36fSThierry Reding 	.recalc_rate = emc_recalc_rate,
471*3dcbd36fSThierry Reding 	.determine_rate = emc_determine_rate,
472*3dcbd36fSThierry Reding 	.set_rate = emc_set_rate,
473*3dcbd36fSThierry Reding 	.get_parent = emc_get_parent,
474*3dcbd36fSThierry Reding };
475*3dcbd36fSThierry Reding 
476*3dcbd36fSThierry Reding struct clk *tegra_clk_register_emc(void __iomem *base, struct device_node *np,
477*3dcbd36fSThierry Reding 				   spinlock_t *lock)
478*3dcbd36fSThierry Reding {
479*3dcbd36fSThierry Reding 	struct tegra_clk_emc *tegra;
480*3dcbd36fSThierry Reding 	struct clk_init_data init;
481*3dcbd36fSThierry Reding 	struct device_node *node;
482*3dcbd36fSThierry Reding 	u32 node_ram_code;
483*3dcbd36fSThierry Reding 	struct clk *clk;
484*3dcbd36fSThierry Reding 	int err;
485*3dcbd36fSThierry Reding 
486*3dcbd36fSThierry Reding 	tegra = kcalloc(1, sizeof(*tegra), GFP_KERNEL);
487*3dcbd36fSThierry Reding 	if (!tegra)
488*3dcbd36fSThierry Reding 		return ERR_PTR(-ENOMEM);
489*3dcbd36fSThierry Reding 
490*3dcbd36fSThierry Reding 	tegra->clk_regs = base;
491*3dcbd36fSThierry Reding 	tegra->lock = lock;
492*3dcbd36fSThierry Reding 
493*3dcbd36fSThierry Reding 	tegra->num_timings = 0;
494*3dcbd36fSThierry Reding 
495*3dcbd36fSThierry Reding 	for_each_child_of_node(np, node) {
496*3dcbd36fSThierry Reding 		err = of_property_read_u32(node, "nvidia,ram-code",
497*3dcbd36fSThierry Reding 					   &node_ram_code);
498*3dcbd36fSThierry Reding 		if (err)
499*3dcbd36fSThierry Reding 			continue;
500*3dcbd36fSThierry Reding 
501*3dcbd36fSThierry Reding 		/*
502*3dcbd36fSThierry Reding 		 * Store timings for all ram codes as we cannot read the
503*3dcbd36fSThierry Reding 		 * fuses until the apbmisc driver is loaded.
504*3dcbd36fSThierry Reding 		 */
505*3dcbd36fSThierry Reding 		err = load_timings_from_dt(tegra, node, node_ram_code);
506*3dcbd36fSThierry Reding 		if (err) {
507*3dcbd36fSThierry Reding 			of_node_put(node);
508*3dcbd36fSThierry Reding 			return ERR_PTR(err);
509*3dcbd36fSThierry Reding 		}
510*3dcbd36fSThierry Reding 	}
511*3dcbd36fSThierry Reding 
512*3dcbd36fSThierry Reding 	if (tegra->num_timings == 0)
513*3dcbd36fSThierry Reding 		pr_warn("%s: no memory timings registered\n", __func__);
514*3dcbd36fSThierry Reding 
515*3dcbd36fSThierry Reding 	tegra->emc_node = of_parse_phandle(np,
516*3dcbd36fSThierry Reding 			"nvidia,external-memory-controller", 0);
517*3dcbd36fSThierry Reding 	if (!tegra->emc_node)
518*3dcbd36fSThierry Reding 		pr_warn("%s: couldn't find node for EMC driver\n", __func__);
519*3dcbd36fSThierry Reding 
520*3dcbd36fSThierry Reding 	init.name = "emc";
521*3dcbd36fSThierry Reding 	init.ops = &tegra_clk_emc_ops;
522*3dcbd36fSThierry Reding 	init.flags = CLK_IS_CRITICAL;
523*3dcbd36fSThierry Reding 	init.parent_names = emc_parent_clk_names;
524*3dcbd36fSThierry Reding 	init.num_parents = ARRAY_SIZE(emc_parent_clk_names);
525*3dcbd36fSThierry Reding 
526*3dcbd36fSThierry Reding 	tegra->hw.init = &init;
527*3dcbd36fSThierry Reding 
528*3dcbd36fSThierry Reding 	clk = clk_register(NULL, &tegra->hw);
529*3dcbd36fSThierry Reding 	if (IS_ERR(clk))
530*3dcbd36fSThierry Reding 		return clk;
531*3dcbd36fSThierry Reding 
532*3dcbd36fSThierry Reding 	tegra->prev_parent = clk_hw_get_parent_by_index(
533*3dcbd36fSThierry Reding 		&tegra->hw, emc_get_parent(&tegra->hw))->clk;
534*3dcbd36fSThierry Reding 	tegra->changing_timing = false;
535*3dcbd36fSThierry Reding 
536*3dcbd36fSThierry Reding 	/* Allow debugging tools to see the EMC clock */
537*3dcbd36fSThierry Reding 	clk_register_clkdev(clk, "emc", "tegra-clk-debug");
538*3dcbd36fSThierry Reding 
539*3dcbd36fSThierry Reding 	return clk;
540*3dcbd36fSThierry Reding };
541