2 * Copyright (c) 2011 The Chromium OS Authors.
3 * See file CREDITS for list of people who contributed to this
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 /* Tegra2 Clock control functions */
25 #include <asm/arch/clk_rst.h>
26 #include <asm/arch/clock.h>
27 #include <asm/arch/timer.h>
28 #include <asm/arch/tegra2.h>
33 * This is our record of the current clock rate of each clock. We don't
34 * fill all of these in since we are only really interested in clocks which
37 static unsigned pll_rate[CLOCK_ID_COUNT];
40 * The oscillator frequency is fixed to one of four set values. Based on this
41 * the other clocks are set up appropriately.
43 static unsigned osc_freq[CLOCK_OSC_FREQ_COUNT] = {
51 * Clock types that we can use as a source. The Tegra2 has muxes for the
52 * peripheral clocks, and in most cases there are four options for the clock
53 * source. This gives us a clock 'type' and exploits what commonality exists
56 * Letters are obvious, except for T which means CLK_M, and S which means the
57 * clock derived from 32KHz. Beware that CLK_M (also called OSC in the
58 * datasheet) and PLL_M are different things. The former is the basic
59 * clock supplied to the SOC from an external oscillator. The latter is the
62 * See definitions in clock_id in the header file.
65 CLOCK_TYPE_AXPT, /* PLL_A, PLL_X, PLL_P, CLK_M */
66 CLOCK_TYPE_MCPA, /* and so on */
74 CLOCK_TYPE_NONE = -1, /* invalid clock type */
77 /* return 1 if a peripheral ID is in range */
78 #define clock_type_id_isvalid(id) ((id) >= 0 && \
79 (id) < CLOCK_TYPE_COUNT)
81 char pllp_valid = 1; /* PLLP is set up correctly */
84 CLOCK_MAX_MUX = 4 /* number of source options for each clock */
88 * Clock source mux for each clock type. This just converts our enum into
89 * a list of mux sources for use by the code. Note that CLOCK_TYPE_PCXTS
90 * is special as it has 5 sources. Since it also has a different number of
91 * bits in its register for the source, we just handle it with a special
94 #define CLK(x) CLOCK_ID_ ## x
95 static enum clock_id clock_source[CLOCK_TYPE_COUNT][CLOCK_MAX_MUX] = {
96 { CLK(AUDIO), CLK(XCPU), CLK(PERIPH), CLK(OSC) },
97 { CLK(MEMORY), CLK(CGENERAL), CLK(PERIPH), CLK(AUDIO) },
98 { CLK(MEMORY), CLK(CGENERAL), CLK(PERIPH), CLK(OSC) },
99 { CLK(PERIPH), CLK(CGENERAL), CLK(MEMORY), CLK(NONE) },
100 { CLK(PERIPH), CLK(CGENERAL), CLK(MEMORY), CLK(OSC) },
101 { CLK(PERIPH), CLK(CGENERAL), CLK(XCPU), CLK(OSC) },
102 { CLK(PERIPH), CLK(DISPLAY), CLK(CGENERAL), CLK(OSC) },
106 * Clock peripheral IDs which sadly don't match up with PERIPH_ID. This is
107 * not in the header file since it is for purely internal use - we want
108 * callers to use the PERIPH_ID for all access to peripheral clocks to avoid
109 * confusion bewteen PERIPH_ID_... and PERIPHC_...
111 * We don't call this CLOCK_PERIPH_ID or PERIPH_CLOCK_ID as it would just be
114 * Note to SOC vendors: perhaps define a unified numbering for peripherals and
115 * use it for reset, clock enable, clock source/divider and even pinmuxing
118 enum periphc_internal_id {
135 PERIPHC_10, /* PERIPHC_SPI1, what is this really? */
192 /* return 1 if a periphc_internal_id is in range */
193 #define periphc_internal_id_isvalid(id) ((id) >= 0 && \
194 (id) < PERIPHC_COUNT)
197 * Clock type for each peripheral clock source. We put the name in each
198 * record just so it is easy to match things up
200 #define TYPE(name, type) type
201 static enum clock_type_id clock_periph_type[PERIPHC_COUNT] = {
203 TYPE(PERIPHC_I2S1, CLOCK_TYPE_AXPT),
204 TYPE(PERIPHC_I2S2, CLOCK_TYPE_AXPT),
205 TYPE(PERIPHC_SPDIF_OUT, CLOCK_TYPE_AXPT),
206 TYPE(PERIPHC_SPDIF_IN, CLOCK_TYPE_PCM),
207 TYPE(PERIPHC_PWM, CLOCK_TYPE_PCXTS),
208 TYPE(PERIPHC_SPI1, CLOCK_TYPE_PCMT),
209 TYPE(PERIPHC_SPI22, CLOCK_TYPE_PCMT),
210 TYPE(PERIPHC_SPI3, CLOCK_TYPE_PCMT),
213 TYPE(PERIPHC_XIO, CLOCK_TYPE_PCMT),
214 TYPE(PERIPHC_I2C1, CLOCK_TYPE_PCMT),
215 TYPE(PERIPHC_DVC_I2C, CLOCK_TYPE_PCMT),
216 TYPE(PERIPHC_TWC, CLOCK_TYPE_PCMT),
217 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
218 TYPE(PERIPHC_SPI1, CLOCK_TYPE_PCMT),
219 TYPE(PERIPHC_DISP1, CLOCK_TYPE_PDCT),
220 TYPE(PERIPHC_DISP2, CLOCK_TYPE_PDCT),
223 TYPE(PERIPHC_CVE, CLOCK_TYPE_PDCT),
224 TYPE(PERIPHC_IDE0, CLOCK_TYPE_PCMT),
225 TYPE(PERIPHC_VI, CLOCK_TYPE_MCPA),
226 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
227 TYPE(PERIPHC_SDMMC1, CLOCK_TYPE_PCMT),
228 TYPE(PERIPHC_SDMMC2, CLOCK_TYPE_PCMT),
229 TYPE(PERIPHC_G3D, CLOCK_TYPE_MCPA),
230 TYPE(PERIPHC_G2D, CLOCK_TYPE_MCPA),
233 TYPE(PERIPHC_NDFLASH, CLOCK_TYPE_PCMT),
234 TYPE(PERIPHC_SDMMC4, CLOCK_TYPE_PCMT),
235 TYPE(PERIPHC_VFIR, CLOCK_TYPE_PCMT),
236 TYPE(PERIPHC_EPP, CLOCK_TYPE_MCPA),
237 TYPE(PERIPHC_MPE, CLOCK_TYPE_MCPA),
238 TYPE(PERIPHC_MIPI, CLOCK_TYPE_PCMT),
239 TYPE(PERIPHC_UART1, CLOCK_TYPE_PCMT),
240 TYPE(PERIPHC_UART2, CLOCK_TYPE_PCMT),
243 TYPE(PERIPHC_HOST1X, CLOCK_TYPE_MCPA),
244 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
245 TYPE(PERIPHC_TVO, CLOCK_TYPE_PDCT),
246 TYPE(PERIPHC_HDMI, CLOCK_TYPE_PDCT),
247 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
248 TYPE(PERIPHC_TVDAC, CLOCK_TYPE_PDCT),
249 TYPE(PERIPHC_I2C2, CLOCK_TYPE_PCMT),
250 TYPE(PERIPHC_EMC, CLOCK_TYPE_MCPT),
253 TYPE(PERIPHC_UART3, CLOCK_TYPE_PCMT),
254 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
255 TYPE(PERIPHC_VI, CLOCK_TYPE_MCPA),
256 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
257 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
258 TYPE(PERIPHC_SPI4, CLOCK_TYPE_PCMT),
259 TYPE(PERIPHC_I2C3, CLOCK_TYPE_PCMT),
260 TYPE(PERIPHC_SDMMC3, CLOCK_TYPE_PCMT),
263 TYPE(PERIPHC_UART4, CLOCK_TYPE_PCMT),
264 TYPE(PERIPHC_UART5, CLOCK_TYPE_PCMT),
265 TYPE(PERIPHC_VDE, CLOCK_TYPE_PCMT),
266 TYPE(PERIPHC_OWR, CLOCK_TYPE_PCMT),
267 TYPE(PERIPHC_NOR, CLOCK_TYPE_PCMT),
268 TYPE(PERIPHC_CSITE, CLOCK_TYPE_PCMT),
272 * This array translates a periph_id to a periphc_internal_id
274 * Not present/matched up:
275 * uint vi_sensor; _VI_SENSOR_0, 0x1A8
276 * SPDIF - which is both 0x08 and 0x0c
279 #define NONE(name) (-1)
280 #define OFFSET(name, value) PERIPHC_ ## name
281 static s8 periph_id_to_internal_id[PERIPH_ID_COUNT] = {
290 PERIPHC_UART2, /* and vfir 0x68 */
295 NONE(SPDIF), /* 0x08 and 0x0c, unclear which to use */
322 /* Middle word: 63:32 */
334 NONE(SBC1), /* SBC1, 0x34, is this SPI1? */
344 PERIPHC_TVO, /* also CVE 0x40 */
362 /* Upper word 95:64 */
397 * Get the oscillator frequency, from the corresponding hardware configuration
400 enum clock_osc_freq clock_get_osc_freq(void)
402 struct clk_rst_ctlr *clkrst =
403 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
406 reg = readl(&clkrst->crc_osc_ctrl);
407 return (reg & OSC_FREQ_MASK) >> OSC_FREQ_SHIFT;
410 /* Returns a pointer to the registers of the given pll */
411 static struct clk_pll *get_pll(enum clock_id clkid)
413 struct clk_rst_ctlr *clkrst =
414 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
416 assert(clock_id_isvalid(clkid));
417 return &clkrst->crc_pll[clkid];
420 unsigned long clock_start_pll(enum clock_id clkid, u32 divm, u32 divn,
421 u32 divp, u32 cpcon, u32 lfcon)
423 struct clk_pll *pll = get_pll(clkid);
427 * We cheat by treating all PLL (except PLLU) in the same fashion.
428 * This works only because:
429 * - same fields are always mapped at same offsets, except DCCON
430 * - DCCON is always 0, doesn't conflict
431 * - M,N, P of PLLP values are ignored for PLLP
433 data = (cpcon << PLL_CPCON_SHIFT) | (lfcon << PLL_LFCON_SHIFT);
434 writel(data, &pll->pll_misc);
436 data = (divm << PLL_DIVM_SHIFT) | (divn << PLL_DIVN_SHIFT) |
437 (0 << PLL_BYPASS_SHIFT) | (1 << PLL_ENABLE_SHIFT);
439 if (clkid == CLOCK_ID_USB)
440 data |= divp << PLLU_VCO_FREQ_SHIFT;
442 data |= divp << PLL_DIVP_SHIFT;
443 writel(data, &pll->pll_base);
445 /* calculate the stable time */
446 return timer_get_us() + CLOCK_PLL_STABLE_DELAY_US;
449 /* return 1 if a peripheral ID is in range and valid */
450 static int clock_periph_id_isvalid(enum periph_id id)
452 if (id < PERIPH_ID_FIRST || id >= PERIPH_ID_COUNT)
453 printf("Peripheral id %d out of range\n", id);
456 case PERIPH_ID_RESERVED1:
457 case PERIPH_ID_RESERVED2:
458 case PERIPH_ID_RESERVED30:
459 case PERIPH_ID_RESERVED35:
460 case PERIPH_ID_RESERVED56:
461 case PERIPH_ID_RESERVED74:
462 case PERIPH_ID_RESERVED76:
463 case PERIPH_ID_RESERVED77:
464 case PERIPH_ID_RESERVED78:
465 case PERIPH_ID_RESERVED79:
466 case PERIPH_ID_RESERVED80:
467 case PERIPH_ID_RESERVED81:
468 case PERIPH_ID_RESERVED82:
469 case PERIPH_ID_RESERVED83:
470 printf("Peripheral id %d is reserved\n", id);
479 /* Returns a pointer to the clock source register for a peripheral */
480 static u32 *get_periph_source_reg(enum periph_id periph_id)
482 struct clk_rst_ctlr *clkrst =
483 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
484 enum periphc_internal_id internal_id;
486 assert(clock_periph_id_isvalid(periph_id));
487 internal_id = periph_id_to_internal_id[periph_id];
488 assert(internal_id != -1);
489 return &clkrst->crc_clk_src[internal_id];
492 void clock_ll_set_source_divisor(enum periph_id periph_id, unsigned source,
495 u32 *reg = get_periph_source_reg(periph_id);
500 value &= ~OUT_CLK_SOURCE_MASK;
501 value |= source << OUT_CLK_SOURCE_SHIFT;
503 value &= ~OUT_CLK_DIVISOR_MASK;
504 value |= divisor << OUT_CLK_DIVISOR_SHIFT;
509 void clock_ll_set_source(enum periph_id periph_id, unsigned source)
511 u32 *reg = get_periph_source_reg(periph_id);
513 clrsetbits_le32(reg, OUT_CLK_SOURCE_MASK,
514 source << OUT_CLK_SOURCE_SHIFT);
518 * Given the parent's rate and the required rate for the children, this works
519 * out the peripheral clock divider to use, in 7.1 binary format.
521 * @param parent_rate clock rate of parent clock in Hz
522 * @param rate required clock rate for this clock
523 * @return divider which should be used
525 static int clk_div7_1_get_divider(unsigned long parent_rate,
528 u64 divider = parent_rate * 2;
531 do_div(divider, rate);
533 if ((s64)divider - 2 < 0)
536 if ((s64)divider - 2 > 255)
543 * Given the parent's rate and the divider in 7.1 format, this works out the
544 * resulting peripheral clock rate.
546 * @param parent_rate clock rate of parent clock in Hz
547 * @param divider which should be used in 7.1 format
548 * @return effective clock rate of peripheral
550 static unsigned long get_rate_from_divider(unsigned long parent_rate,
555 rate = (u64)parent_rate * 2;
556 do_div(rate, divider + 2);
560 unsigned long clock_get_periph_rate(enum periph_id periph_id,
561 enum clock_id parent)
563 u32 *reg = get_periph_source_reg(periph_id);
565 return get_rate_from_divider(pll_rate[parent],
566 (readl(reg) & OUT_CLK_DIVISOR_MASK) >> OUT_CLK_DIVISOR_SHIFT);
570 * Find the best available 7.1 format divisor given a parent clock rate and
571 * required child clock rate. This function assumes that a second-stage
572 * divisor is available which can divide by powers of 2 from 1 to 256.
574 * @param parent_rate clock rate of parent clock in Hz
575 * @param rate required clock rate for this clock
576 * @param extra_div value for the second-stage divisor (not set if this
577 * function returns -1.
578 * @return divider which should be used, or -1 if nothing is valid
581 static int find_best_divider(unsigned long parent_rate, unsigned long rate,
585 int best_divider = -1;
586 int best_error = rate;
588 /* try dividers from 1 to 256 and find closest match */
589 for (shift = 0; shift <= 8 && best_error > 0; shift++) {
590 unsigned divided_parent = parent_rate >> shift;
591 int divider = clk_div7_1_get_divider(divided_parent, rate);
592 unsigned effective_rate = get_rate_from_divider(divided_parent,
594 int error = rate - effective_rate;
596 /* Given a valid divider, look for the lowest error */
597 if (divider != -1 && error < best_error) {
599 *extra_div = 1 << shift;
600 best_divider = divider;
604 /* return what we found - *extra_div will already be set */
609 * Given a peripheral ID and the required source clock, this returns which
610 * value should be programmed into the source mux for that peripheral.
612 * There is special code here to handle the one source type with 5 sources.
614 * @param periph_id peripheral to start
615 * @param source PLL id of required parent clock
616 * @param mux_bits Set to number of bits in mux register: 2 or 4
617 * @return mux value (0-4, or -1 if not found)
619 static int get_periph_clock_source(enum periph_id periph_id,
620 enum clock_id parent, int *mux_bits)
622 enum clock_type_id type;
623 enum periphc_internal_id internal_id;
626 assert(clock_periph_id_isvalid(periph_id));
628 internal_id = periph_id_to_internal_id[periph_id];
629 assert(periphc_internal_id_isvalid(internal_id));
631 type = clock_periph_type[internal_id];
632 assert(clock_type_id_isvalid(type));
634 /* Special case here for the clock with a 4-bit source mux */
635 if (type == CLOCK_TYPE_PCXTS)
640 for (mux = 0; mux < CLOCK_MAX_MUX; mux++)
641 if (clock_source[type][mux] == parent)
645 * Not found: it might be looking for the 'S' in CLOCK_TYPE_PCXTS
646 * which is not in our table. If not, then they are asking for a
647 * source which this peripheral can't access through its mux.
649 assert(type == CLOCK_TYPE_PCXTS);
650 assert(parent == CLOCK_ID_SFROM32KHZ);
651 if (type == CLOCK_TYPE_PCXTS && parent == CLOCK_ID_SFROM32KHZ)
652 return 4; /* mux value for this clock */
654 /* if we get here, either us or the caller has made a mistake */
655 printf("Caller requested bad clock: periph=%d, parent=%d\n", periph_id,
661 * Adjust peripheral PLL to use the given divider and source.
663 * @param periph_id peripheral to adjust
664 * @param parent Required parent clock (for source mux)
665 * @param divider Required divider in 7.1 format
666 * @return 0 if ok, -1 on error (requesting a parent clock which is not valid
667 * for this peripheral)
669 static int adjust_periph_pll(enum periph_id periph_id,
670 enum clock_id parent, unsigned divider)
672 u32 *reg = get_periph_source_reg(periph_id);
676 clrsetbits_le32(reg, OUT_CLK_DIVISOR_MASK,
677 divider << OUT_CLK_DIVISOR_SHIFT);
680 /* work out the source clock and set it */
681 source = get_periph_clock_source(periph_id, parent, &mux_bits);
685 clrsetbits_le32(reg, OUT_CLK_SOURCE4_MASK,
686 source << OUT_CLK_SOURCE4_SHIFT);
688 clrsetbits_le32(reg, OUT_CLK_SOURCE_MASK,
689 source << OUT_CLK_SOURCE_SHIFT);
695 unsigned clock_adjust_periph_pll_div(enum periph_id periph_id,
696 enum clock_id parent, unsigned rate, int *extra_div)
698 unsigned effective_rate;
702 divider = find_best_divider(pll_rate[parent], rate, extra_div);
704 divider = clk_div7_1_get_divider(pll_rate[parent], rate);
705 assert(divider >= 0);
706 if (adjust_periph_pll(periph_id, parent, divider))
708 debug("periph %d, rate=%d, reg=%p = %x\n", periph_id, rate,
709 get_periph_source_reg(periph_id),
710 readl(get_periph_source_reg(periph_id)));
712 /* Check what we ended up with. This shouldn't matter though */
713 effective_rate = clock_get_periph_rate(periph_id, parent);
715 effective_rate /= *extra_div;
716 if (rate != effective_rate)
717 debug("Requested clock rate %u not honored (got %u)\n",
718 rate, effective_rate);
719 return effective_rate;
722 unsigned clock_start_periph_pll(enum periph_id periph_id,
723 enum clock_id parent, unsigned rate)
725 unsigned effective_rate;
727 reset_set_enable(periph_id, 1);
728 clock_enable(periph_id);
730 effective_rate = clock_adjust_periph_pll_div(periph_id, parent, rate,
733 reset_set_enable(periph_id, 0);
734 return effective_rate;
737 void clock_set_enable(enum periph_id periph_id, int enable)
739 struct clk_rst_ctlr *clkrst =
740 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
741 u32 *clk = &clkrst->crc_clk_out_enb[PERIPH_REG(periph_id)];
744 /* Enable/disable the clock to this peripheral */
745 assert(clock_periph_id_isvalid(periph_id));
748 reg |= PERIPH_MASK(periph_id);
750 reg &= ~PERIPH_MASK(periph_id);
754 void clock_enable(enum periph_id clkid)
756 clock_set_enable(clkid, 1);
759 void clock_disable(enum periph_id clkid)
761 clock_set_enable(clkid, 0);
764 void reset_set_enable(enum periph_id periph_id, int enable)
766 struct clk_rst_ctlr *clkrst =
767 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
768 u32 *reset = &clkrst->crc_rst_dev[PERIPH_REG(periph_id)];
771 /* Enable/disable reset to the peripheral */
772 assert(clock_periph_id_isvalid(periph_id));
775 reg |= PERIPH_MASK(periph_id);
777 reg &= ~PERIPH_MASK(periph_id);
781 void reset_periph(enum periph_id periph_id, int us_delay)
783 /* Put peripheral into reset */
784 reset_set_enable(periph_id, 1);
788 reset_set_enable(periph_id, 0);
793 void reset_cmplx_set_enable(int cpu, int which, int reset)
795 struct clk_rst_ctlr *clkrst =
796 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
799 /* Form the mask, which depends on the cpu chosen. Tegra2 has 2 */
800 assert(cpu >= 0 && cpu < 2);
803 /* either enable or disable those reset for that CPU */
805 writel(mask, &clkrst->crc_cpu_cmplx_set);
807 writel(mask, &clkrst->crc_cpu_cmplx_clr);
810 unsigned clock_get_rate(enum clock_id clkid)
818 parent_rate = osc_freq[clock_get_osc_freq()];
819 if (clkid == CLOCK_ID_OSC)
822 pll = get_pll(clkid);
823 base = readl(&pll->pll_base);
825 /* Oh for bf_unpack()... */
826 rate = parent_rate * ((base & PLL_DIVN_MASK) >> PLL_DIVN_SHIFT);
827 divm = (base & PLL_DIVM_MASK) >> PLL_DIVM_SHIFT;
828 if (clkid == CLOCK_ID_USB)
829 divm <<= (base & PLLU_VCO_FREQ_MASK) >> PLLU_VCO_FREQ_SHIFT;
831 divm <<= (base & PLL_DIVP_MASK) >> PLL_DIVP_SHIFT;
837 * Set the output frequency you want for each PLL clock.
838 * PLL output frequencies are programmed by setting their N, M and P values.
839 * The governing equations are:
840 * VCO = (Fi / m) * n, Fo = VCO / (2^p)
841 * where Fo is the output frequency from the PLL.
842 * Example: Set the output frequency to 216Mhz(Fo) with 12Mhz OSC(Fi)
843 * 216Mhz = ((12Mhz / m) * n) / (2^p) so n=432,m=12,p=1
844 * Please see Tegra TRM section 5.3 to get the detail for PLL Programming
846 * @param n PLL feedback divider(DIVN)
847 * @param m PLL input divider(DIVN)
848 * @param p post divider(DIVP)
849 * @param cpcon base PLL charge pump(CPCON)
850 * @return 0 if ok, -1 on error (the requested PLL is incorrect and cannot
851 * be overriden), 1 if PLL is already correct
853 static int clock_set_rate(enum clock_id clkid, u32 n, u32 m, u32 p, u32 cpcon)
859 pll = get_pll(clkid);
861 base_reg = readl(&pll->pll_base);
863 /* Set BYPASS, m, n and p to PLL_BASE */
864 base_reg &= ~PLL_DIVM_MASK;
865 base_reg |= m << PLL_DIVM_SHIFT;
867 base_reg &= ~PLL_DIVN_MASK;
868 base_reg |= n << PLL_DIVN_SHIFT;
870 base_reg &= ~PLL_DIVP_MASK;
871 base_reg |= p << PLL_DIVP_SHIFT;
873 if (clkid == CLOCK_ID_PERIPH) {
875 * If the PLL is already set up, check that it is correct
876 * and record this info for clock_verify() to check.
878 if (base_reg & PLL_BASE_OVRRIDE_MASK) {
879 base_reg |= PLL_ENABLE_MASK;
880 if (base_reg != readl(&pll->pll_base))
882 return pllp_valid ? 1 : -1;
884 base_reg |= PLL_BASE_OVRRIDE_MASK;
887 base_reg |= PLL_BYPASS_MASK;
888 writel(base_reg, &pll->pll_base);
890 /* Set cpcon to PLL_MISC */
891 misc_reg = readl(&pll->pll_misc);
892 misc_reg &= ~PLL_CPCON_MASK;
893 misc_reg |= cpcon << PLL_CPCON_SHIFT;
894 writel(misc_reg, &pll->pll_misc);
897 base_reg |= PLL_ENABLE_MASK;
898 writel(base_reg, &pll->pll_base);
901 base_reg &= ~PLL_BYPASS_MASK;
902 writel(base_reg, &pll->pll_base);
907 int clock_verify(void)
909 struct clk_pll *pll = get_pll(CLOCK_ID_PERIPH);
910 u32 reg = readl(&pll->pll_base);
913 printf("Warning: PLLP %x is not correct\n", reg);
916 debug("PLLX %x is correct\n", reg);
920 void clock_early_init(void)
923 * PLLP output frequency set to 216MHz
924 * PLLC output frequency set to 600Mhz
926 * TODO: Can we calculate these values instead of hard-coding?
928 switch (clock_get_osc_freq()) {
929 case CLOCK_OSC_FREQ_12_0: /* OSC is 12Mhz */
930 clock_set_rate(CLOCK_ID_PERIPH, 432, 12, 1, 8);
931 clock_set_rate(CLOCK_ID_CGENERAL, 600, 12, 0, 8);
934 case CLOCK_OSC_FREQ_26_0: /* OSC is 26Mhz */
935 clock_set_rate(CLOCK_ID_PERIPH, 432, 26, 1, 8);
936 clock_set_rate(CLOCK_ID_CGENERAL, 600, 26, 0, 8);
939 case CLOCK_OSC_FREQ_13_0:
940 case CLOCK_OSC_FREQ_19_2:
943 * These are not supported. It is too early to print a
944 * message and the UART likely won't work anyway due to the
945 * oscillator being wrong.
951 void clock_init(void)
953 pll_rate[CLOCK_ID_MEMORY] = clock_get_rate(CLOCK_ID_MEMORY);
954 pll_rate[CLOCK_ID_PERIPH] = clock_get_rate(CLOCK_ID_PERIPH);
955 pll_rate[CLOCK_ID_CGENERAL] = clock_get_rate(CLOCK_ID_CGENERAL);
956 pll_rate[CLOCK_ID_OSC] = clock_get_rate(CLOCK_ID_OSC);
957 pll_rate[CLOCK_ID_SFROM32KHZ] = 32768;
958 debug("Osc = %d\n", pll_rate[CLOCK_ID_OSC]);
959 debug("PLLM = %d\n", pll_rate[CLOCK_ID_MEMORY]);
960 debug("PLLP = %d\n", pll_rate[CLOCK_ID_PERIPH]);