1 // SPDX-License-Identifier: GPL-2.0+
5 * Clock initialization for AM33XX boards.
6 * Derived from OMAP4 boards
8 * Copyright (C) 2013, Texas Instruments, Incorporated - http://www.ti.com/
12 #include <asm/arch/cpu.h>
13 #include <asm/arch/clock.h>
14 #include <asm/arch/hardware.h>
15 #include <asm/arch/sys_proto.h>
18 static void setup_post_dividers(const struct dpll_regs *dpll_regs,
19 const struct dpll_params *params)
21 /* Setup post-dividers */
23 writel(params->m2, dpll_regs->cm_div_m2_dpll);
25 writel(params->m3, dpll_regs->cm_div_m3_dpll);
27 writel(params->m4, dpll_regs->cm_div_m4_dpll);
29 writel(params->m5, dpll_regs->cm_div_m5_dpll);
31 writel(params->m6, dpll_regs->cm_div_m6_dpll);
34 static inline void do_lock_dpll(const struct dpll_regs *dpll_regs)
36 clrsetbits_le32(dpll_regs->cm_clkmode_dpll,
37 CM_CLKMODE_DPLL_DPLL_EN_MASK,
38 DPLL_EN_LOCK << CM_CLKMODE_DPLL_EN_SHIFT);
41 static inline void wait_for_lock(const struct dpll_regs *dpll_regs)
43 if (!wait_on_value(ST_DPLL_CLK_MASK, ST_DPLL_CLK_MASK,
44 (void *)dpll_regs->cm_idlest_dpll, LDELAY)) {
45 printf("DPLL locking failed for 0x%x\n",
46 dpll_regs->cm_clkmode_dpll);
51 static inline void do_bypass_dpll(const struct dpll_regs *dpll_regs)
53 clrsetbits_le32(dpll_regs->cm_clkmode_dpll,
54 CM_CLKMODE_DPLL_DPLL_EN_MASK,
55 DPLL_EN_MN_BYPASS << CM_CLKMODE_DPLL_EN_SHIFT);
58 static inline void wait_for_bypass(const struct dpll_regs *dpll_regs)
60 if (!wait_on_value(ST_DPLL_CLK_MASK, 0,
61 (void *)dpll_regs->cm_idlest_dpll, LDELAY)) {
62 printf("Bypassing DPLL failed 0x%x\n",
63 dpll_regs->cm_clkmode_dpll);
67 static void bypass_dpll(const struct dpll_regs *dpll_regs)
69 do_bypass_dpll(dpll_regs);
70 wait_for_bypass(dpll_regs);
73 void do_setup_dpll(const struct dpll_regs *dpll_regs,
74 const struct dpll_params *params)
81 temp = readl(dpll_regs->cm_clksel_dpll);
83 bypass_dpll(dpll_regs);
86 temp &= ~CM_CLKSEL_DPLL_M_MASK;
87 temp |= (params->m << CM_CLKSEL_DPLL_M_SHIFT) & CM_CLKSEL_DPLL_M_MASK;
89 temp &= ~CM_CLKSEL_DPLL_N_MASK;
90 temp |= (params->n << CM_CLKSEL_DPLL_N_SHIFT) & CM_CLKSEL_DPLL_N_MASK;
92 writel(temp, dpll_regs->cm_clksel_dpll);
94 setup_post_dividers(dpll_regs, params);
96 /* Wait till the DPLL locks */
97 do_lock_dpll(dpll_regs);
98 wait_for_lock(dpll_regs);
101 static void setup_dplls(void)
103 const struct dpll_params *params;
105 params = get_dpll_core_params();
106 do_setup_dpll(&dpll_core_regs, params);
108 params = get_dpll_mpu_params();
109 do_setup_dpll(&dpll_mpu_regs, params);
111 params = get_dpll_per_params();
112 do_setup_dpll(&dpll_per_regs, params);
113 writel(0x300, &cmwkup->clkdcoldodpllper);
115 params = get_dpll_ddr_params();
116 do_setup_dpll(&dpll_ddr_regs, params);
119 static inline void wait_for_clk_enable(u32 *clkctrl_addr)
121 u32 clkctrl, idlest = MODULE_CLKCTRL_IDLEST_DISABLED;
124 while ((idlest == MODULE_CLKCTRL_IDLEST_DISABLED) ||
125 (idlest == MODULE_CLKCTRL_IDLEST_TRANSITIONING)) {
126 clkctrl = readl(clkctrl_addr);
127 idlest = (clkctrl & MODULE_CLKCTRL_IDLEST_MASK) >>
128 MODULE_CLKCTRL_IDLEST_SHIFT;
130 printf("Clock enable failed for 0x%p idlest 0x%x\n",
131 clkctrl_addr, clkctrl);
137 static inline void enable_clock_module(u32 *const clkctrl_addr, u32 enable_mode,
140 clrsetbits_le32(clkctrl_addr, MODULE_CLKCTRL_MODULEMODE_MASK,
141 enable_mode << MODULE_CLKCTRL_MODULEMODE_SHIFT);
142 debug("Enable clock module - %p\n", clkctrl_addr);
144 wait_for_clk_enable(clkctrl_addr);
147 static inline void wait_for_clk_disable(u32 *clkctrl_addr)
149 u32 clkctrl, idlest = MODULE_CLKCTRL_IDLEST_FULLY_FUNCTIONAL;
152 while ((idlest != MODULE_CLKCTRL_IDLEST_DISABLED)) {
153 clkctrl = readl(clkctrl_addr);
154 idlest = (clkctrl & MODULE_CLKCTRL_IDLEST_MASK) >>
155 MODULE_CLKCTRL_IDLEST_SHIFT;
157 printf("Clock disable failed for 0x%p idlest 0x%x\n",
158 clkctrl_addr, clkctrl);
163 static inline void disable_clock_module(u32 *const clkctrl_addr,
164 u32 wait_for_disable)
166 clrsetbits_le32(clkctrl_addr, MODULE_CLKCTRL_MODULEMODE_MASK,
167 MODULE_CLKCTRL_MODULEMODE_SW_DISABLE <<
168 MODULE_CLKCTRL_MODULEMODE_SHIFT);
169 debug("Disable clock module - %p\n", clkctrl_addr);
170 if (wait_for_disable)
171 wait_for_clk_disable(clkctrl_addr);
174 static inline void enable_clock_domain(u32 *const clkctrl_reg, u32 enable_mode)
176 clrsetbits_le32(clkctrl_reg, CD_CLKCTRL_CLKTRCTRL_MASK,
177 enable_mode << CD_CLKCTRL_CLKTRCTRL_SHIFT);
178 debug("Enable clock domain - %p\n", clkctrl_reg);
181 static inline void disable_clock_domain(u32 *const clkctrl_reg)
183 clrsetbits_le32(clkctrl_reg, CD_CLKCTRL_CLKTRCTRL_MASK,
184 CD_CLKCTRL_CLKTRCTRL_SW_SLEEP <<
185 CD_CLKCTRL_CLKTRCTRL_SHIFT);
186 debug("Disable clock domain - %p\n", clkctrl_reg);
189 void do_enable_clocks(u32 *const *clk_domains,
190 u32 *const *clk_modules_explicit_en, u8 wait_for_enable)
194 /* Put the clock domains in SW_WKUP mode */
195 for (i = 0; (i < max) && clk_domains[i]; i++) {
196 enable_clock_domain(clk_domains[i],
197 CD_CLKCTRL_CLKTRCTRL_SW_WKUP);
200 /* Clock modules that need to be put in SW_EXPLICIT_EN mode */
201 for (i = 0; (i < max) && clk_modules_explicit_en[i]; i++) {
202 enable_clock_module(clk_modules_explicit_en[i],
203 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN,
208 void do_disable_clocks(u32 *const *clk_domains,
209 u32 *const *clk_modules_disable,
215 /* Clock modules that need to be put in SW_DISABLE */
216 for (i = 0; (i < max) && clk_modules_disable[i]; i++)
217 disable_clock_module(clk_modules_disable[i],
220 /* Put the clock domains in SW_SLEEP mode */
221 for (i = 0; (i < max) && clk_domains[i]; i++)
222 disable_clock_domain(clk_domains[i]);
226 * Before scaling up the clocks we need to have the PMIC scale up the
227 * voltages first. This will be dependent on which PMIC is in use
228 * and in some cases we may not be scaling things up at all and thus not
229 * need to do anything here.
231 __weak void scale_vcores(void)
235 void setup_early_clocks(void)
237 setup_clocks_for_console();
238 enable_basic_clocks();
248 void rtc_only_prcm_init(void)
250 const struct dpll_params *params;
252 rtc_only_enable_basic_clocks();
254 params = get_dpll_ddr_params();
255 do_setup_dpll(&dpll_ddr_regs, params);