blob: c33d974dccdd60509c31da39904a8bba2a59989b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Chandan Nath4ba33452011-10-14 02:58:23 +00002/*
Matt Porter57da6662013-03-15 10:07:04 +00003 * clock_am33xx.c
Chandan Nath4ba33452011-10-14 02:58:23 +00004 *
5 * clocks for AM33XX based boards
6 *
Nishanth Menoneaa39c62023-11-01 15:56:03 -05007 * Copyright (C) 2013, Texas Instruments, Incorporated - https://www.ti.com/
Chandan Nath4ba33452011-10-14 02:58:23 +00008 */
9
Chandan Nath4ba33452011-10-14 02:58:23 +000010#include <asm/arch/cpu.h>
Lokesh Vutla6302e532017-05-05 12:59:10 +053011#include <asm/arch/sys_proto.h>
Chandan Nath4ba33452011-10-14 02:58:23 +000012#include <asm/arch/clock.h>
13#include <asm/arch/hardware.h>
14#include <asm/io.h>
15
Matt Porter57da6662013-03-15 10:07:04 +000016#define OSC (V_OSCK/1000000)
17
Lokesh Vutlab1b6fba2013-07-30 10:48:53 +053018struct cm_perpll *const cmper = (struct cm_perpll *)CM_PER;
19struct cm_wkuppll *const cmwkup = (struct cm_wkuppll *)CM_WKUP;
20struct cm_dpll *const cmdpll = (struct cm_dpll *)CM_DPLL;
21struct cm_rtc *const cmrtc = (struct cm_rtc *)CM_RTC;
Matt Porter57da6662013-03-15 10:07:04 +000022
Lokesh Vutla89a83bf2013-07-30 10:48:52 +053023const struct dpll_regs dpll_mpu_regs = {
24 .cm_clkmode_dpll = CM_WKUP + 0x88,
25 .cm_idlest_dpll = CM_WKUP + 0x20,
26 .cm_clksel_dpll = CM_WKUP + 0x2C,
27 .cm_div_m2_dpll = CM_WKUP + 0xA8,
28};
Matt Porter57da6662013-03-15 10:07:04 +000029
Lokesh Vutla89a83bf2013-07-30 10:48:52 +053030const struct dpll_regs dpll_core_regs = {
31 .cm_clkmode_dpll = CM_WKUP + 0x90,
32 .cm_idlest_dpll = CM_WKUP + 0x5C,
33 .cm_clksel_dpll = CM_WKUP + 0x68,
34 .cm_div_m4_dpll = CM_WKUP + 0x80,
35 .cm_div_m5_dpll = CM_WKUP + 0x84,
36 .cm_div_m6_dpll = CM_WKUP + 0xD8,
37};
Matt Porter57da6662013-03-15 10:07:04 +000038
Lokesh Vutla89a83bf2013-07-30 10:48:52 +053039const struct dpll_regs dpll_per_regs = {
40 .cm_clkmode_dpll = CM_WKUP + 0x8C,
41 .cm_idlest_dpll = CM_WKUP + 0x70,
42 .cm_clksel_dpll = CM_WKUP + 0x9C,
43 .cm_div_m2_dpll = CM_WKUP + 0xAC,
44};
Matt Porter57da6662013-03-15 10:07:04 +000045
Lokesh Vutla89a83bf2013-07-30 10:48:52 +053046const struct dpll_regs dpll_ddr_regs = {
47 .cm_clkmode_dpll = CM_WKUP + 0x94,
48 .cm_idlest_dpll = CM_WKUP + 0x34,
49 .cm_clksel_dpll = CM_WKUP + 0x40,
50 .cm_div_m2_dpll = CM_WKUP + 0xA0,
51};
Matt Porter57da6662013-03-15 10:07:04 +000052
Hannes Schmelzerf29f8f42018-01-09 19:01:31 +010053const struct dpll_regs dpll_disp_regs = {
54 .cm_clkmode_dpll = CM_WKUP + 0x98,
55 .cm_idlest_dpll = CM_WKUP + 0x48,
56 .cm_clksel_dpll = CM_WKUP + 0x54,
57 .cm_div_m2_dpll = CM_WKUP + 0xA4,
58};
59
Steve Kipisz5adac352013-08-14 10:51:31 -040060struct dpll_params dpll_mpu_opp100 = {
Lokesh Vutla89a83bf2013-07-30 10:48:52 +053061 CONFIG_SYS_MPUCLK, OSC-1, 1, -1, -1, -1, -1};
Steve Kipisz5adac352013-08-14 10:51:31 -040062const struct dpll_params dpll_core_opp100 = {
Lokesh Vutla89a83bf2013-07-30 10:48:52 +053063 1000, OSC-1, -1, -1, 10, 8, 4};
Chandan Nath4ba33452011-10-14 02:58:23 +000064
Lokesh Vutla6302e532017-05-05 12:59:10 +053065const struct dpll_params dpll_mpu_opp[NUM_CRYSTAL_FREQ][NUM_OPPS] = {
66 { /* 19.2 MHz */
67 {125, 3, 2, -1, -1, -1, -1}, /* OPP 50 */
68 {-1, -1, -1, -1, -1, -1, -1}, /* OPP RESERVED */
69 {125, 3, 1, -1, -1, -1, -1}, /* OPP 100 */
70 {150, 3, 1, -1, -1, -1, -1}, /* OPP 120 */
71 {125, 2, 1, -1, -1, -1, -1}, /* OPP TB */
72 {625, 11, 1, -1, -1, -1, -1} /* OPP NT */
73 },
74 { /* 24 MHz */
75 {25, 0, 2, -1, -1, -1, -1}, /* OPP 50 */
76 {-1, -1, -1, -1, -1, -1, -1}, /* OPP RESERVED */
77 {25, 0, 1, -1, -1, -1, -1}, /* OPP 100 */
78 {30, 0, 1, -1, -1, -1, -1}, /* OPP 120 */
Dario Binacchi18f2de52019-12-03 20:55:03 +010079 {100, 2, 1, -1, -1, -1, -1}, /* OPP TB */
Lokesh Vutla6302e532017-05-05 12:59:10 +053080 {125, 2, 1, -1, -1, -1, -1} /* OPP NT */
81 },
82 { /* 25 MHz */
83 {24, 0, 2, -1, -1, -1, -1}, /* OPP 50 */
84 {-1, -1, -1, -1, -1, -1, -1}, /* OPP RESERVED */
85 {24, 0, 1, -1, -1, -1, -1}, /* OPP 100 */
86 {144, 4, 1, -1, -1, -1, -1}, /* OPP 120 */
87 {32, 0, 1, -1, -1, -1, -1}, /* OPP TB */
88 {40, 0, 1, -1, -1, -1, -1} /* OPP NT */
89 },
90 { /* 26 MHz */
91 {300, 12, 2, -1, -1, -1, -1}, /* OPP 50 */
92 {-1, -1, -1, -1, -1, -1, -1}, /* OPP RESERVED */
93 {300, 12, 1, -1, -1, -1, -1}, /* OPP 100 */
94 {360, 12, 1, -1, -1, -1, -1}, /* OPP 120 */
95 {400, 12, 1, -1, -1, -1, -1}, /* OPP TB */
96 {500, 12, 1, -1, -1, -1, -1} /* OPP NT */
97 },
98};
99
100const struct dpll_params dpll_core_1000MHz[NUM_CRYSTAL_FREQ] = {
101 {625, 11, -1, -1, 10, 8, 4}, /* 19.2 MHz */
102 {125, 2, -1, -1, 10, 8, 4}, /* 24 MHz */
103 {40, 0, -1, -1, 10, 8, 4}, /* 25 MHz */
104 {500, 12, -1, -1, 10, 8, 4} /* 26 MHz */
105};
106
107const struct dpll_params dpll_per_192MHz[NUM_CRYSTAL_FREQ] = {
108 {400, 7, 5, -1, -1, -1, -1}, /* 19.2 MHz */
109 {400, 9, 5, -1, -1, -1, -1}, /* 24 MHz */
110 {384, 9, 5, -1, -1, -1, -1}, /* 25 MHz */
111 {480, 12, 5, -1, -1, -1, -1} /* 26 MHz */
112};
113
114const struct dpll_params dpll_ddr3_303MHz[NUM_CRYSTAL_FREQ] = {
115 {505, 15, 2, -1, -1, -1, -1}, /*19.2*/
116 {101, 3, 2, -1, -1, -1, -1}, /* 24 MHz */
Lokesh Vutla706dbd72017-12-28 20:40:01 +0530117 {303, 24, 1, -1, -1, -1, -1}, /* 25 MHz */
118 {303, 12, 2, -1, -1, -1, -1} /* 26 MHz */
Lokesh Vutla6302e532017-05-05 12:59:10 +0530119};
120
121const struct dpll_params dpll_ddr3_400MHz[NUM_CRYSTAL_FREQ] = {
122 {125, 5, 1, -1, -1, -1, -1}, /*19.2*/
123 {50, 2, 1, -1, -1, -1, -1}, /* 24 MHz */
Lokesh Vutla706dbd72017-12-28 20:40:01 +0530124 {16, 0, 1, -1, -1, -1, -1}, /* 25 MHz */
125 {200, 12, 1, -1, -1, -1, -1} /* 26 MHz */
Lokesh Vutla6302e532017-05-05 12:59:10 +0530126};
127
128const struct dpll_params dpll_ddr2_266MHz[NUM_CRYSTAL_FREQ] = {
129 {665, 47, 1, -1, -1, -1, -1}, /*19.2*/
130 {133, 11, 1, -1, -1, -1, -1}, /* 24 MHz */
Lokesh Vutla706dbd72017-12-28 20:40:01 +0530131 {266, 24, 1, -1, -1, -1, -1}, /* 25 MHz */
132 {133, 12, 1, -1, -1, -1, -1} /* 26 MHz */
Lokesh Vutla6302e532017-05-05 12:59:10 +0530133};
134
135__weak const struct dpll_params *get_dpll_mpu_params(void)
Lokesh Vutla42c213a2013-12-10 15:02:20 +0530136{
Lokesh Vutla6302e532017-05-05 12:59:10 +0530137 return &dpll_mpu_opp100;
Lokesh Vutla42c213a2013-12-10 15:02:20 +0530138}
139
140const struct dpll_params *get_dpll_core_params(void)
141{
Lokesh Vutla6302e532017-05-05 12:59:10 +0530142 int ind = get_sys_clk_index();
143
144 return &dpll_core_1000MHz[ind];
Lokesh Vutla42c213a2013-12-10 15:02:20 +0530145}
146
147const struct dpll_params *get_dpll_per_params(void)
148{
Lokesh Vutla6302e532017-05-05 12:59:10 +0530149 int ind = get_sys_clk_index();
150
151 return &dpll_per_192MHz[ind];
Lokesh Vutla42c213a2013-12-10 15:02:20 +0530152}
153
Lokesh Vutlab1b6fba2013-07-30 10:48:53 +0530154void setup_clocks_for_console(void)
Chandan Nath4ba33452011-10-14 02:58:23 +0000155{
Lokesh Vutlab1b6fba2013-07-30 10:48:53 +0530156 clrsetbits_le32(&cmwkup->wkclkstctrl, CD_CLKCTRL_CLKTRCTRL_MASK,
157 CD_CLKCTRL_CLKTRCTRL_SW_WKUP <<
158 CD_CLKCTRL_CLKTRCTRL_SHIFT);
Chandan Nath4ba33452011-10-14 02:58:23 +0000159
Lokesh Vutlab1b6fba2013-07-30 10:48:53 +0530160 clrsetbits_le32(&cmper->l4hsclkstctrl, CD_CLKCTRL_CLKTRCTRL_MASK,
161 CD_CLKCTRL_CLKTRCTRL_SW_WKUP <<
162 CD_CLKCTRL_CLKTRCTRL_SHIFT);
Chandan Nath4ba33452011-10-14 02:58:23 +0000163
Lokesh Vutlab1b6fba2013-07-30 10:48:53 +0530164 clrsetbits_le32(&cmwkup->wkup_uart0ctrl,
165 MODULE_CLKCTRL_MODULEMODE_MASK,
166 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
167 MODULE_CLKCTRL_MODULEMODE_SHIFT);
168 clrsetbits_le32(&cmper->uart1clkctrl,
169 MODULE_CLKCTRL_MODULEMODE_MASK,
170 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
171 MODULE_CLKCTRL_MODULEMODE_SHIFT);
172 clrsetbits_le32(&cmper->uart2clkctrl,
173 MODULE_CLKCTRL_MODULEMODE_MASK,
174 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
175 MODULE_CLKCTRL_MODULEMODE_SHIFT);
176 clrsetbits_le32(&cmper->uart3clkctrl,
177 MODULE_CLKCTRL_MODULEMODE_MASK,
178 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
179 MODULE_CLKCTRL_MODULEMODE_SHIFT);
180 clrsetbits_le32(&cmper->uart4clkctrl,
181 MODULE_CLKCTRL_MODULEMODE_MASK,
182 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
183 MODULE_CLKCTRL_MODULEMODE_SHIFT);
184 clrsetbits_le32(&cmper->uart5clkctrl,
185 MODULE_CLKCTRL_MODULEMODE_MASK,
186 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
187 MODULE_CLKCTRL_MODULEMODE_SHIFT);
Chandan Nath4ba33452011-10-14 02:58:23 +0000188}
189
Lokesh Vutlab1b6fba2013-07-30 10:48:53 +0530190void enable_basic_clocks(void)
Chandan Nath4ba33452011-10-14 02:58:23 +0000191{
Lokesh Vutlab1b6fba2013-07-30 10:48:53 +0530192 u32 *const clk_domains[] = {
193 &cmper->l3clkstctrl,
194 &cmper->l4fwclkstctrl,
195 &cmper->l3sclkstctrl,
196 &cmper->l4lsclkstctrl,
197 &cmwkup->wkclkstctrl,
198 &cmper->emiffwclkctrl,
199 &cmrtc->clkstctrl,
200 0
201 };
Chandan Nath4ba33452011-10-14 02:58:23 +0000202
Lokesh Vutlab1b6fba2013-07-30 10:48:53 +0530203 u32 *const clk_modules_explicit_en[] = {
204 &cmper->l3clkctrl,
205 &cmper->l4lsclkctrl,
206 &cmper->l4fwclkctrl,
207 &cmwkup->wkl4wkclkctrl,
208 &cmper->l3instrclkctrl,
209 &cmper->l4hsclkctrl,
210 &cmwkup->wkgpio0clkctrl,
211 &cmwkup->wkctrlclkctrl,
212 &cmper->timer2clkctrl,
213 &cmper->gpmcclkctrl,
214 &cmper->elmclkctrl,
215 &cmper->mmc0clkctrl,
216 &cmper->mmc1clkctrl,
217 &cmwkup->wkup_i2c0ctrl,
218 &cmper->gpio1clkctrl,
219 &cmper->gpio2clkctrl,
220 &cmper->gpio3clkctrl,
221 &cmper->i2c1clkctrl,
Kory Maincent4c1a71d2021-05-04 19:31:29 +0200222 &cmper->i2c2clkctrl,
Lokesh Vutlab1b6fba2013-07-30 10:48:53 +0530223 &cmper->cpgmac0clkctrl,
224 &cmper->spi0clkctrl,
225 &cmrtc->rtcclkctrl,
226 &cmper->usb0clkctrl,
227 &cmper->emiffwclkctrl,
228 &cmper->emifclkctrl,
229 0
230 };
Chandan Nath4ba33452011-10-14 02:58:23 +0000231
Lokesh Vutlab1b6fba2013-07-30 10:48:53 +0530232 do_enable_clocks(clk_domains, clk_modules_explicit_en, 1);
Chandan Nath4ba33452011-10-14 02:58:23 +0000233
Chandan Nath5b5c2122012-01-09 20:38:56 +0000234 /* Select the Master osc 24 MHZ as Timer2 clock source */
235 writel(0x1, &cmdpll->clktimer2clk);
Chandan Nath4ba33452011-10-14 02:58:23 +0000236}
Heiko Schocher85754732016-06-07 08:31:19 +0200237
238/*
239 * Enable Spread Spectrum for the MPU by calculating the required
240 * values and setting the registers accordingly.
241 * @param permille The spreading in permille (10th of a percent)
242 */
243void set_mpu_spreadspectrum(int permille)
244{
245 u32 multiplier_m;
246 u32 predivider_n;
247 u32 cm_clksel_dpll_mpu;
248 u32 cm_clkmode_dpll_mpu;
249 u32 ref_clock;
250 u32 pll_bandwidth;
251 u32 mod_freq_divider;
252 u32 exponent;
253 u32 mantissa;
254 u32 delta_m_step;
255
256 printf("Enabling Spread Spectrum of %d permille for MPU\n",
257 permille);
258
259 /* Read PLL parameter m and n */
260 cm_clksel_dpll_mpu = readl(&cmwkup->clkseldpllmpu);
261 multiplier_m = (cm_clksel_dpll_mpu >> 8) & 0x3FF;
262 predivider_n = cm_clksel_dpll_mpu & 0x7F;
263
264 /*
265 * Calculate reference clock (clock after pre-divider),
266 * its max. PLL bandwidth,
267 * and resulting mod_freq_divider
268 */
269 ref_clock = V_OSCK / (predivider_n + 1);
270 pll_bandwidth = ref_clock / 70;
271 mod_freq_divider = ref_clock / (4 * pll_bandwidth);
272
273 /* Calculate Mantissa/Exponent */
274 exponent = 0;
275 mantissa = mod_freq_divider;
276 while ((mantissa > 127) && (exponent < 7)) {
277 exponent++;
278 mantissa /= 2;
279 }
280 if (mantissa > 127)
281 mantissa = 127;
282
283 mod_freq_divider = mantissa << exponent;
284
285 /*
286 * Calculate Modulation steps
287 * As we use Downspread only, the spread is twice the value of
288 * permille, so Div2!
289 * As it takes the value in percent, divide by ten!
290 */
291 delta_m_step = ((u32)((multiplier_m * permille) / 10 / 2)) << 18;
292 delta_m_step /= 100;
293 delta_m_step /= mod_freq_divider;
294 if (delta_m_step > 0xFFFFF)
295 delta_m_step = 0xFFFFF;
296
297 /* Setup Spread Spectrum */
298 writel(delta_m_step, &cmwkup->sscdeltamstepdllmpu);
299 writel((exponent << 8) | mantissa, &cmwkup->sscmodfreqdivdpllmpu);
300 cm_clkmode_dpll_mpu = readl(&cmwkup->clkmoddpllmpu);
301 /* clear all SSC flags */
302 cm_clkmode_dpll_mpu &= ~(0xF << CM_CLKMODE_DPLL_SSC_EN_SHIFT);
303 /* enable SSC with Downspread only */
304 cm_clkmode_dpll_mpu |= CM_CLKMODE_DPLL_SSC_EN_MASK |
305 CM_CLKMODE_DPLL_SSC_DOWNSPREAD_MASK;
306 writel(cm_clkmode_dpll_mpu, &cmwkup->clkmoddpllmpu);
307 while (!(readl(&cmwkup->clkmoddpllmpu) & 0x2000))
308 ;
309}