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