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