blob: 590826072b8a27161fe2554db8783c6a38841e42 [file] [log] [blame]
Tom Warrenf29f0862013-01-23 14:01:01 -07001/*
Jimmy Zhangb9dd6212014-01-24 10:37:36 -07002 * Copyright (c) 2010-2014, NVIDIA CORPORATION. All rights reserved.
Tom Warrenf29f0862013-01-23 14:01:01 -07003 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17/* Tegra SoC common clock control functions */
18
19#include <common.h>
Simon Glass746dc762015-06-05 14:39:36 -060020#include <errno.h>
Tom Warrenf29f0862013-01-23 14:01:01 -070021#include <asm/io.h>
22#include <asm/arch/clock.h>
23#include <asm/arch/tegra.h>
Stephen Warren73c38932015-01-19 16:25:52 -070024#include <asm/arch-tegra/ap.h>
Tom Warrenf29f0862013-01-23 14:01:01 -070025#include <asm/arch-tegra/clk_rst.h>
Simon Glass746dc762015-06-05 14:39:36 -060026#include <asm/arch-tegra/pmc.h>
Tom Warrenf29f0862013-01-23 14:01:01 -070027#include <asm/arch-tegra/timer.h>
28#include <div64.h>
29#include <fdtdec.h>
30
31/*
32 * This is our record of the current clock rate of each clock. We don't
33 * fill all of these in since we are only really interested in clocks which
34 * we use as parents.
35 */
36static unsigned pll_rate[CLOCK_ID_COUNT];
37
38/*
39 * The oscillator frequency is fixed to one of four set values. Based on this
40 * the other clocks are set up appropriately.
41 */
42static unsigned osc_freq[CLOCK_OSC_FREQ_COUNT] = {
43 13000000,
44 19200000,
45 12000000,
46 26000000,
47};
48
49/* return 1 if a peripheral ID is in range */
50#define clock_type_id_isvalid(id) ((id) >= 0 && \
51 (id) < CLOCK_TYPE_COUNT)
52
53char pllp_valid = 1; /* PLLP is set up correctly */
54
55/* return 1 if a periphc_internal_id is in range */
56#define periphc_internal_id_isvalid(id) ((id) >= 0 && \
57 (id) < PERIPHC_COUNT)
58
59/* number of clock outputs of a PLL */
60static const u8 pll_num_clkouts[] = {
61 1, /* PLLC */
62 1, /* PLLM */
63 4, /* PLLP */
64 1, /* PLLA */
65 0, /* PLLU */
66 0, /* PLLD */
67};
68
69int clock_get_osc_bypass(void)
70{
71 struct clk_rst_ctlr *clkrst =
72 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
73 u32 reg;
74
75 reg = readl(&clkrst->crc_osc_ctrl);
76 return (reg & OSC_XOBP_MASK) >> OSC_XOBP_SHIFT;
77}
78
79/* Returns a pointer to the registers of the given pll */
80static struct clk_pll *get_pll(enum clock_id clkid)
81{
82 struct clk_rst_ctlr *clkrst =
83 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
84
85 assert(clock_id_is_pll(clkid));
Simon Glass801b05c2015-04-14 21:03:32 -060086 if (clkid >= (enum clock_id)TEGRA_CLK_PLLS) {
87 debug("%s: Invalid PLL\n", __func__);
88 return NULL;
89 }
Tom Warrenf29f0862013-01-23 14:01:01 -070090 return &clkrst->crc_pll[clkid];
91}
92
Simon Glass801b05c2015-04-14 21:03:32 -060093__weak struct clk_pll_simple *clock_get_simple_pll(enum clock_id clkid)
94{
95 return NULL;
96}
97
Tom Warrenf29f0862013-01-23 14:01:01 -070098int clock_ll_read_pll(enum clock_id clkid, u32 *divm, u32 *divn,
99 u32 *divp, u32 *cpcon, u32 *lfcon)
100{
101 struct clk_pll *pll = get_pll(clkid);
102 u32 data;
103
104 assert(clkid != CLOCK_ID_USB);
105
106 /* Safety check, adds to code size but is small */
107 if (!clock_id_is_pll(clkid) || clkid == CLOCK_ID_USB)
108 return -1;
109 data = readl(&pll->pll_base);
110 *divm = (data & PLL_DIVM_MASK) >> PLL_DIVM_SHIFT;
111 *divn = (data & PLL_DIVN_MASK) >> PLL_DIVN_SHIFT;
112 *divp = (data & PLL_DIVP_MASK) >> PLL_DIVP_SHIFT;
113 data = readl(&pll->pll_misc);
114 *cpcon = (data & PLL_CPCON_MASK) >> PLL_CPCON_SHIFT;
115 *lfcon = (data & PLL_LFCON_MASK) >> PLL_LFCON_SHIFT;
116
117 return 0;
118}
119
120unsigned long clock_start_pll(enum clock_id clkid, u32 divm, u32 divn,
121 u32 divp, u32 cpcon, u32 lfcon)
122{
123 struct clk_pll *pll = get_pll(clkid);
Simon Glass801b05c2015-04-14 21:03:32 -0600124 u32 misc_data, data;
Tom Warrenf29f0862013-01-23 14:01:01 -0700125
126 /*
127 * We cheat by treating all PLL (except PLLU) in the same fashion.
128 * This works only because:
129 * - same fields are always mapped at same offsets, except DCCON
130 * - DCCON is always 0, doesn't conflict
131 * - M,N, P of PLLP values are ignored for PLLP
132 */
Simon Glass801b05c2015-04-14 21:03:32 -0600133 misc_data = (cpcon << PLL_CPCON_SHIFT) | (lfcon << PLL_LFCON_SHIFT);
Tom Warrenf29f0862013-01-23 14:01:01 -0700134
135 data = (divm << PLL_DIVM_SHIFT) | (divn << PLL_DIVN_SHIFT) |
136 (0 << PLL_BYPASS_SHIFT) | (1 << PLL_ENABLE_SHIFT);
137
138 if (clkid == CLOCK_ID_USB)
139 data |= divp << PLLU_VCO_FREQ_SHIFT;
140 else
141 data |= divp << PLL_DIVP_SHIFT;
Simon Glass801b05c2015-04-14 21:03:32 -0600142 if (pll) {
143 writel(misc_data, &pll->pll_misc);
144 writel(data, &pll->pll_base);
145 } else {
146 struct clk_pll_simple *pll = clock_get_simple_pll(clkid);
147
148 if (!pll) {
149 debug("%s: Uknown simple PLL %d\n", __func__, clkid);
150 return 0;
151 }
152 writel(misc_data, &pll->pll_misc);
153 writel(data, &pll->pll_base);
154 }
Tom Warrenf29f0862013-01-23 14:01:01 -0700155
156 /* calculate the stable time */
157 return timer_get_us() + CLOCK_PLL_STABLE_DELAY_US;
158}
159
160void clock_ll_set_source_divisor(enum periph_id periph_id, unsigned source,
161 unsigned divisor)
162{
163 u32 *reg = get_periph_source_reg(periph_id);
164 u32 value;
165
166 value = readl(reg);
167
Stephen Warren9cb0c6d2014-01-24 10:16:19 -0700168 value &= ~OUT_CLK_SOURCE_31_30_MASK;
169 value |= source << OUT_CLK_SOURCE_31_30_SHIFT;
Tom Warrenf29f0862013-01-23 14:01:01 -0700170
171 value &= ~OUT_CLK_DIVISOR_MASK;
172 value |= divisor << OUT_CLK_DIVISOR_SHIFT;
173
174 writel(value, reg);
175}
176
Simon Glass7bb61992015-04-14 21:03:33 -0600177int clock_ll_set_source_bits(enum periph_id periph_id, int mux_bits,
178 unsigned source)
Tom Warrenf29f0862013-01-23 14:01:01 -0700179{
180 u32 *reg = get_periph_source_reg(periph_id);
181
Simon Glass7bb61992015-04-14 21:03:33 -0600182 switch (mux_bits) {
183 case MASK_BITS_31_30:
184 clrsetbits_le32(reg, OUT_CLK_SOURCE_31_30_MASK,
185 source << OUT_CLK_SOURCE_31_30_SHIFT);
186 break;
187
188 case MASK_BITS_31_29:
189 clrsetbits_le32(reg, OUT_CLK_SOURCE_31_29_MASK,
190 source << OUT_CLK_SOURCE_31_29_SHIFT);
191 break;
192
193 case MASK_BITS_31_28:
194 clrsetbits_le32(reg, OUT_CLK_SOURCE_31_28_MASK,
195 source << OUT_CLK_SOURCE_31_28_SHIFT);
196 break;
197
198 default:
199 return -1;
200 }
201
202 return 0;
203}
204
205void clock_ll_set_source(enum periph_id periph_id, unsigned source)
206{
207 clock_ll_set_source_bits(periph_id, MASK_BITS_31_30, source);
Tom Warrenf29f0862013-01-23 14:01:01 -0700208}
209
210/**
211 * Given the parent's rate and the required rate for the children, this works
212 * out the peripheral clock divider to use, in 7.1 binary format.
213 *
214 * @param divider_bits number of divider bits (8 or 16)
215 * @param parent_rate clock rate of parent clock in Hz
216 * @param rate required clock rate for this clock
217 * @return divider which should be used
218 */
219static int clk_get_divider(unsigned divider_bits, unsigned long parent_rate,
220 unsigned long rate)
221{
222 u64 divider = parent_rate * 2;
223 unsigned max_divider = 1 << divider_bits;
224
225 divider += rate - 1;
226 do_div(divider, rate);
227
228 if ((s64)divider - 2 < 0)
229 return 0;
230
231 if ((s64)divider - 2 >= max_divider)
232 return -1;
233
234 return divider - 2;
235}
236
237int clock_set_pllout(enum clock_id clkid, enum pll_out_id pllout, unsigned rate)
238{
239 struct clk_pll *pll = get_pll(clkid);
240 int data = 0, div = 0, offset = 0;
241
242 if (!clock_id_is_pll(clkid))
243 return -1;
244
245 if (pllout + 1 > pll_num_clkouts[clkid])
246 return -1;
247
248 div = clk_get_divider(8, pll_rate[clkid], rate);
249
250 if (div < 0)
251 return -1;
252
253 /* out2 and out4 are in the high part of the register */
254 if (pllout == PLL_OUT2 || pllout == PLL_OUT4)
255 offset = 16;
256
257 data = (div << PLL_OUT_RATIO_SHIFT) |
258 PLL_OUT_OVRRIDE | PLL_OUT_CLKEN | PLL_OUT_RSTN;
259 clrsetbits_le32(&pll->pll_out[pllout >> 1],
260 PLL_OUT_RATIO_MASK << offset, data << offset);
261
262 return 0;
263}
264
265/**
266 * Given the parent's rate and the divider in 7.1 format, this works out the
267 * resulting peripheral clock rate.
268 *
269 * @param parent_rate clock rate of parent clock in Hz
270 * @param divider which should be used in 7.1 format
271 * @return effective clock rate of peripheral
272 */
273static unsigned long get_rate_from_divider(unsigned long parent_rate,
274 int divider)
275{
276 u64 rate;
277
278 rate = (u64)parent_rate * 2;
279 do_div(rate, divider + 2);
280 return rate;
281}
282
283unsigned long clock_get_periph_rate(enum periph_id periph_id,
284 enum clock_id parent)
285{
286 u32 *reg = get_periph_source_reg(periph_id);
287
288 return get_rate_from_divider(pll_rate[parent],
289 (readl(reg) & OUT_CLK_DIVISOR_MASK) >> OUT_CLK_DIVISOR_SHIFT);
290}
291
292/**
293 * Find the best available 7.1 format divisor given a parent clock rate and
294 * required child clock rate. This function assumes that a second-stage
295 * divisor is available which can divide by powers of 2 from 1 to 256.
296 *
297 * @param divider_bits number of divider bits (8 or 16)
298 * @param parent_rate clock rate of parent clock in Hz
299 * @param rate required clock rate for this clock
300 * @param extra_div value for the second-stage divisor (not set if this
301 * function returns -1.
302 * @return divider which should be used, or -1 if nothing is valid
303 *
304 */
305static int find_best_divider(unsigned divider_bits, unsigned long parent_rate,
306 unsigned long rate, int *extra_div)
307{
308 int shift;
309 int best_divider = -1;
310 int best_error = rate;
311
312 /* try dividers from 1 to 256 and find closest match */
313 for (shift = 0; shift <= 8 && best_error > 0; shift++) {
314 unsigned divided_parent = parent_rate >> shift;
315 int divider = clk_get_divider(divider_bits, divided_parent,
316 rate);
317 unsigned effective_rate = get_rate_from_divider(divided_parent,
318 divider);
319 int error = rate - effective_rate;
320
321 /* Given a valid divider, look for the lowest error */
322 if (divider != -1 && error < best_error) {
323 best_error = error;
324 *extra_div = 1 << shift;
325 best_divider = divider;
326 }
327 }
328
329 /* return what we found - *extra_div will already be set */
330 return best_divider;
331}
332
333/**
334 * Adjust peripheral PLL to use the given divider and source.
335 *
336 * @param periph_id peripheral to adjust
337 * @param source Source number (0-3 or 0-7)
338 * @param mux_bits Number of mux bits (2 or 4)
339 * @param divider Required divider in 7.1 or 15.1 format
340 * @return 0 if ok, -1 on error (requesting a parent clock which is not valid
341 * for this peripheral)
342 */
343static int adjust_periph_pll(enum periph_id periph_id, int source,
344 int mux_bits, unsigned divider)
345{
346 u32 *reg = get_periph_source_reg(periph_id);
347
348 clrsetbits_le32(reg, OUT_CLK_DIVISOR_MASK,
349 divider << OUT_CLK_DIVISOR_SHIFT);
350 udelay(1);
351
352 /* work out the source clock and set it */
353 if (source < 0)
354 return -1;
Tom Warrenc82014d2014-01-24 10:16:22 -0700355
Simon Glass7bb61992015-04-14 21:03:33 -0600356 clock_ll_set_source_bits(periph_id, mux_bits, source);
Tom Warrenc82014d2014-01-24 10:16:22 -0700357
Tom Warrenf29f0862013-01-23 14:01:01 -0700358 udelay(2);
359 return 0;
360}
361
362unsigned clock_adjust_periph_pll_div(enum periph_id periph_id,
363 enum clock_id parent, unsigned rate, int *extra_div)
364{
365 unsigned effective_rate;
366 int mux_bits, divider_bits, source;
367 int divider;
Allen Martina51f7de2013-05-10 16:56:55 +0000368 int xdiv = 0;
Tom Warrenf29f0862013-01-23 14:01:01 -0700369
370 /* work out the source clock and set it */
371 source = get_periph_clock_source(periph_id, parent, &mux_bits,
372 &divider_bits);
373
Allen Martina51f7de2013-05-10 16:56:55 +0000374 divider = find_best_divider(divider_bits, pll_rate[parent],
375 rate, &xdiv);
Tom Warrenf29f0862013-01-23 14:01:01 -0700376 if (extra_div)
Allen Martina51f7de2013-05-10 16:56:55 +0000377 *extra_div = xdiv;
378
Tom Warrenf29f0862013-01-23 14:01:01 -0700379 assert(divider >= 0);
380 if (adjust_periph_pll(periph_id, source, mux_bits, divider))
381 return -1U;
382 debug("periph %d, rate=%d, reg=%p = %x\n", periph_id, rate,
383 get_periph_source_reg(periph_id),
384 readl(get_periph_source_reg(periph_id)));
385
386 /* Check what we ended up with. This shouldn't matter though */
387 effective_rate = clock_get_periph_rate(periph_id, parent);
388 if (extra_div)
389 effective_rate /= *extra_div;
390 if (rate != effective_rate)
391 debug("Requested clock rate %u not honored (got %u)\n",
392 rate, effective_rate);
393 return effective_rate;
394}
395
396unsigned clock_start_periph_pll(enum periph_id periph_id,
397 enum clock_id parent, unsigned rate)
398{
399 unsigned effective_rate;
400
401 reset_set_enable(periph_id, 1);
402 clock_enable(periph_id);
403
404 effective_rate = clock_adjust_periph_pll_div(periph_id, parent, rate,
405 NULL);
406
407 reset_set_enable(periph_id, 0);
408 return effective_rate;
409}
410
411void clock_enable(enum periph_id clkid)
412{
413 clock_set_enable(clkid, 1);
414}
415
416void clock_disable(enum periph_id clkid)
417{
418 clock_set_enable(clkid, 0);
419}
420
421void reset_periph(enum periph_id periph_id, int us_delay)
422{
423 /* Put peripheral into reset */
424 reset_set_enable(periph_id, 1);
425 udelay(us_delay);
426
427 /* Remove reset */
428 reset_set_enable(periph_id, 0);
429
430 udelay(us_delay);
431}
432
433void reset_cmplx_set_enable(int cpu, int which, int reset)
434{
435 struct clk_rst_ctlr *clkrst =
436 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
437 u32 mask;
438
439 /* Form the mask, which depends on the cpu chosen (2 or 4) */
440 assert(cpu >= 0 && cpu < MAX_NUM_CPU);
441 mask = which << cpu;
442
443 /* either enable or disable those reset for that CPU */
444 if (reset)
445 writel(mask, &clkrst->crc_cpu_cmplx_set);
446 else
447 writel(mask, &clkrst->crc_cpu_cmplx_clr);
448}
449
450unsigned clock_get_rate(enum clock_id clkid)
451{
452 struct clk_pll *pll;
453 u32 base;
454 u32 divm;
455 u64 parent_rate;
456 u64 rate;
457
458 parent_rate = osc_freq[clock_get_osc_freq()];
459 if (clkid == CLOCK_ID_OSC)
460 return parent_rate;
461
462 pll = get_pll(clkid);
Simon Glass801b05c2015-04-14 21:03:32 -0600463 if (!pll)
464 return 0;
Tom Warrenf29f0862013-01-23 14:01:01 -0700465 base = readl(&pll->pll_base);
466
467 /* Oh for bf_unpack()... */
468 rate = parent_rate * ((base & PLL_DIVN_MASK) >> PLL_DIVN_SHIFT);
469 divm = (base & PLL_DIVM_MASK) >> PLL_DIVM_SHIFT;
470 if (clkid == CLOCK_ID_USB)
471 divm <<= (base & PLLU_VCO_FREQ_MASK) >> PLLU_VCO_FREQ_SHIFT;
472 else
473 divm <<= (base & PLL_DIVP_MASK) >> PLL_DIVP_SHIFT;
474 do_div(rate, divm);
475 return rate;
476}
477
478/**
479 * Set the output frequency you want for each PLL clock.
480 * PLL output frequencies are programmed by setting their N, M and P values.
481 * The governing equations are:
482 * VCO = (Fi / m) * n, Fo = VCO / (2^p)
483 * where Fo is the output frequency from the PLL.
484 * Example: Set the output frequency to 216Mhz(Fo) with 12Mhz OSC(Fi)
485 * 216Mhz = ((12Mhz / m) * n) / (2^p) so n=432,m=12,p=1
486 * Please see Tegra TRM section 5.3 to get the detail for PLL Programming
487 *
488 * @param n PLL feedback divider(DIVN)
489 * @param m PLL input divider(DIVN)
490 * @param p post divider(DIVP)
491 * @param cpcon base PLL charge pump(CPCON)
492 * @return 0 if ok, -1 on error (the requested PLL is incorrect and cannot
493 * be overriden), 1 if PLL is already correct
494 */
495int clock_set_rate(enum clock_id clkid, u32 n, u32 m, u32 p, u32 cpcon)
496{
497 u32 base_reg;
498 u32 misc_reg;
499 struct clk_pll *pll;
500
501 pll = get_pll(clkid);
502
503 base_reg = readl(&pll->pll_base);
504
505 /* Set BYPASS, m, n and p to PLL_BASE */
506 base_reg &= ~PLL_DIVM_MASK;
507 base_reg |= m << PLL_DIVM_SHIFT;
508
509 base_reg &= ~PLL_DIVN_MASK;
510 base_reg |= n << PLL_DIVN_SHIFT;
511
512 base_reg &= ~PLL_DIVP_MASK;
513 base_reg |= p << PLL_DIVP_SHIFT;
514
515 if (clkid == CLOCK_ID_PERIPH) {
516 /*
517 * If the PLL is already set up, check that it is correct
518 * and record this info for clock_verify() to check.
519 */
520 if (base_reg & PLL_BASE_OVRRIDE_MASK) {
521 base_reg |= PLL_ENABLE_MASK;
522 if (base_reg != readl(&pll->pll_base))
523 pllp_valid = 0;
524 return pllp_valid ? 1 : -1;
525 }
526 base_reg |= PLL_BASE_OVRRIDE_MASK;
527 }
528
529 base_reg |= PLL_BYPASS_MASK;
530 writel(base_reg, &pll->pll_base);
531
532 /* Set cpcon to PLL_MISC */
533 misc_reg = readl(&pll->pll_misc);
534 misc_reg &= ~PLL_CPCON_MASK;
535 misc_reg |= cpcon << PLL_CPCON_SHIFT;
536 writel(misc_reg, &pll->pll_misc);
537
538 /* Enable PLL */
539 base_reg |= PLL_ENABLE_MASK;
540 writel(base_reg, &pll->pll_base);
541
542 /* Disable BYPASS */
543 base_reg &= ~PLL_BYPASS_MASK;
544 writel(base_reg, &pll->pll_base);
545
546 return 0;
547}
548
549void clock_ll_start_uart(enum periph_id periph_id)
550{
551 /* Assert UART reset and enable clock */
552 reset_set_enable(periph_id, 1);
553 clock_enable(periph_id);
554 clock_ll_set_source(periph_id, 0); /* UARTx_CLK_SRC = 00, PLLP_OUT0 */
555
556 /* wait for 2us */
557 udelay(2);
558
559 /* De-assert reset to UART */
560 reset_set_enable(periph_id, 0);
561}
562
563#ifdef CONFIG_OF_CONTROL
564int clock_decode_periph_id(const void *blob, int node)
565{
566 enum periph_id id;
567 u32 cell[2];
568 int err;
569
570 err = fdtdec_get_int_array(blob, node, "clocks", cell,
571 ARRAY_SIZE(cell));
572 if (err)
573 return -1;
574 id = clk_id_to_periph_id(cell[1]);
575 assert(clock_periph_id_isvalid(id));
576 return id;
577}
578#endif /* CONFIG_OF_CONTROL */
579
580int clock_verify(void)
581{
582 struct clk_pll *pll = get_pll(CLOCK_ID_PERIPH);
583 u32 reg = readl(&pll->pll_base);
584
585 if (!pllp_valid) {
586 printf("Warning: PLLP %x is not correct\n", reg);
587 return -1;
588 }
589 debug("PLLP %x is correct\n", reg);
590 return 0;
591}
592
593void clock_init(void)
594{
595 pll_rate[CLOCK_ID_MEMORY] = clock_get_rate(CLOCK_ID_MEMORY);
596 pll_rate[CLOCK_ID_PERIPH] = clock_get_rate(CLOCK_ID_PERIPH);
597 pll_rate[CLOCK_ID_CGENERAL] = clock_get_rate(CLOCK_ID_CGENERAL);
Simon Glass96e82a22015-04-14 21:03:34 -0600598 pll_rate[CLOCK_ID_DISPLAY] = clock_get_rate(CLOCK_ID_DISPLAY);
Tom Warrenf29f0862013-01-23 14:01:01 -0700599 pll_rate[CLOCK_ID_OSC] = clock_get_rate(CLOCK_ID_OSC);
600 pll_rate[CLOCK_ID_SFROM32KHZ] = 32768;
601 pll_rate[CLOCK_ID_XCPU] = clock_get_rate(CLOCK_ID_XCPU);
602 debug("Osc = %d\n", pll_rate[CLOCK_ID_OSC]);
603 debug("PLLM = %d\n", pll_rate[CLOCK_ID_MEMORY]);
604 debug("PLLP = %d\n", pll_rate[CLOCK_ID_PERIPH]);
605 debug("PLLC = %d\n", pll_rate[CLOCK_ID_CGENERAL]);
Simon Glass96e82a22015-04-14 21:03:34 -0600606 debug("PLLD = %d\n", pll_rate[CLOCK_ID_DISPLAY]);
Tom Warrenf29f0862013-01-23 14:01:01 -0700607 debug("PLLX = %d\n", pll_rate[CLOCK_ID_XCPU]);
Tom Warrenb40f7342013-04-01 15:48:54 -0700608
609 /* Do any special system timer/TSC setup */
Stephen Warren73c38932015-01-19 16:25:52 -0700610#if defined(CONFIG_TEGRA_SUPPORT_NON_SECURE)
611 if (!tegra_cpu_is_non_secure())
612#endif
613 arch_timer_init();
Tom Warrenf29f0862013-01-23 14:01:01 -0700614}
Jimmy Zhangb9dd6212014-01-24 10:37:36 -0700615
616static void set_avp_clock_source(u32 src)
617{
618 struct clk_rst_ctlr *clkrst =
619 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
620 u32 val;
621
622 val = (src << SCLK_SWAKEUP_FIQ_SOURCE_SHIFT) |
623 (src << SCLK_SWAKEUP_IRQ_SOURCE_SHIFT) |
624 (src << SCLK_SWAKEUP_RUN_SOURCE_SHIFT) |
625 (src << SCLK_SWAKEUP_IDLE_SOURCE_SHIFT) |
626 (SCLK_SYS_STATE_RUN << SCLK_SYS_STATE_SHIFT);
627 writel(val, &clkrst->crc_sclk_brst_pol);
628 udelay(3);
629}
630
631/*
632 * This function is useful on Tegra30, and any later SoCs that have compatible
633 * PLLP configuration registers.
634 */
635void tegra30_set_up_pllp(void)
636{
637 struct clk_rst_ctlr *clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
638 u32 reg;
639
640 /*
641 * Based on the Tegra TRM, the system clock (which is the AVP clock) can
642 * run up to 275MHz. On power on, the default sytem clock source is set
643 * to PLLP_OUT0. This function sets PLLP's (hence PLLP_OUT0's) rate to
644 * 408MHz which is beyond system clock's upper limit.
645 *
646 * The fix is to set the system clock to CLK_M before initializing PLLP,
647 * and then switch back to PLLP_OUT4, which has an appropriate divider
648 * configured, after PLLP has been configured
649 */
650 set_avp_clock_source(SCLK_SOURCE_CLKM);
651
652 /*
653 * PLLP output frequency set to 408Mhz
654 * PLLC output frequency set to 228Mhz
655 */
656 switch (clock_get_osc_freq()) {
657 case CLOCK_OSC_FREQ_12_0: /* OSC is 12Mhz */
658 clock_set_rate(CLOCK_ID_PERIPH, 408, 12, 0, 8);
659 clock_set_rate(CLOCK_ID_CGENERAL, 456, 12, 1, 8);
660 break;
661
662 case CLOCK_OSC_FREQ_26_0: /* OSC is 26Mhz */
663 clock_set_rate(CLOCK_ID_PERIPH, 408, 26, 0, 8);
664 clock_set_rate(CLOCK_ID_CGENERAL, 600, 26, 0, 8);
665 break;
666
667 case CLOCK_OSC_FREQ_13_0: /* OSC is 13Mhz */
668 clock_set_rate(CLOCK_ID_PERIPH, 408, 13, 0, 8);
669 clock_set_rate(CLOCK_ID_CGENERAL, 600, 13, 0, 8);
670 break;
671 case CLOCK_OSC_FREQ_19_2:
672 default:
673 /*
674 * These are not supported. It is too early to print a
675 * message and the UART likely won't work anyway due to the
676 * oscillator being wrong.
677 */
678 break;
679 }
680
681 /* Set PLLP_OUT1, 2, 3 & 4 freqs to 9.6, 48, 102 & 204MHz */
682
683 /* OUT1, 2 */
684 /* Assert RSTN before enable */
685 reg = PLLP_OUT2_RSTN_EN | PLLP_OUT1_RSTN_EN;
686 writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[0]);
687 /* Set divisor and reenable */
688 reg = (IN_408_OUT_48_DIVISOR << PLLP_OUT2_RATIO)
689 | PLLP_OUT2_OVR | PLLP_OUT2_CLKEN | PLLP_OUT2_RSTN_DIS
690 | (IN_408_OUT_9_6_DIVISOR << PLLP_OUT1_RATIO)
691 | PLLP_OUT1_OVR | PLLP_OUT1_CLKEN | PLLP_OUT1_RSTN_DIS;
692 writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[0]);
693
694 /* OUT3, 4 */
695 /* Assert RSTN before enable */
696 reg = PLLP_OUT4_RSTN_EN | PLLP_OUT3_RSTN_EN;
697 writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[1]);
698 /* Set divisor and reenable */
699 reg = (IN_408_OUT_204_DIVISOR << PLLP_OUT4_RATIO)
700 | PLLP_OUT4_OVR | PLLP_OUT4_CLKEN | PLLP_OUT4_RSTN_DIS
701 | (IN_408_OUT_102_DIVISOR << PLLP_OUT3_RATIO)
702 | PLLP_OUT3_OVR | PLLP_OUT3_CLKEN | PLLP_OUT3_RSTN_DIS;
703 writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[1]);
704
705 set_avp_clock_source(SCLK_SOURCE_PLLP_OUT4);
706}
Simon Glass746dc762015-06-05 14:39:36 -0600707
708int clock_external_output(int clk_id)
709{
710 struct pmc_ctlr *pmc = (struct pmc_ctlr *)NV_PA_PMC_BASE;
711
712 if (clk_id >= 1 && clk_id <= 3) {
713 setbits_le32(&pmc->pmc_clk_out_cntrl,
714 1 << (2 + (clk_id - 1) * 8));
715 } else {
716 printf("%s: Unknown output clock id %d\n", __func__, clk_id);
717 return -EINVAL;
718 }
719
720 return 0;
721}