1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2983e3700STom Rini /*
3983e3700STom Rini * clock_am33xx.c
4983e3700STom Rini *
5983e3700STom Rini * clocks for AM33XX based boards
6983e3700STom Rini *
7983e3700STom Rini * Copyright (C) 2013, Texas Instruments, Incorporated - http://www.ti.com/
8983e3700STom Rini */
9983e3700STom Rini
10983e3700STom Rini #include <common.h>
11983e3700STom Rini #include <asm/arch/cpu.h>
12fbd6295dSLokesh Vutla #include <asm/arch/sys_proto.h>
13983e3700STom Rini #include <asm/arch/clock.h>
14983e3700STom Rini #include <asm/arch/hardware.h>
15983e3700STom Rini #include <asm/io.h>
16983e3700STom Rini
17983e3700STom Rini #define OSC (V_OSCK/1000000)
18983e3700STom Rini
19983e3700STom Rini struct cm_perpll *const cmper = (struct cm_perpll *)CM_PER;
20983e3700STom Rini struct cm_wkuppll *const cmwkup = (struct cm_wkuppll *)CM_WKUP;
21983e3700STom Rini struct cm_dpll *const cmdpll = (struct cm_dpll *)CM_DPLL;
22983e3700STom Rini struct cm_rtc *const cmrtc = (struct cm_rtc *)CM_RTC;
23983e3700STom Rini
24983e3700STom Rini const struct dpll_regs dpll_mpu_regs = {
25983e3700STom Rini .cm_clkmode_dpll = CM_WKUP + 0x88,
26983e3700STom Rini .cm_idlest_dpll = CM_WKUP + 0x20,
27983e3700STom Rini .cm_clksel_dpll = CM_WKUP + 0x2C,
28983e3700STom Rini .cm_div_m2_dpll = CM_WKUP + 0xA8,
29983e3700STom Rini };
30983e3700STom Rini
31983e3700STom Rini const struct dpll_regs dpll_core_regs = {
32983e3700STom Rini .cm_clkmode_dpll = CM_WKUP + 0x90,
33983e3700STom Rini .cm_idlest_dpll = CM_WKUP + 0x5C,
34983e3700STom Rini .cm_clksel_dpll = CM_WKUP + 0x68,
35983e3700STom Rini .cm_div_m4_dpll = CM_WKUP + 0x80,
36983e3700STom Rini .cm_div_m5_dpll = CM_WKUP + 0x84,
37983e3700STom Rini .cm_div_m6_dpll = CM_WKUP + 0xD8,
38983e3700STom Rini };
39983e3700STom Rini
40983e3700STom Rini const struct dpll_regs dpll_per_regs = {
41983e3700STom Rini .cm_clkmode_dpll = CM_WKUP + 0x8C,
42983e3700STom Rini .cm_idlest_dpll = CM_WKUP + 0x70,
43983e3700STom Rini .cm_clksel_dpll = CM_WKUP + 0x9C,
44983e3700STom Rini .cm_div_m2_dpll = CM_WKUP + 0xAC,
45983e3700STom Rini };
46983e3700STom Rini
47983e3700STom Rini const struct dpll_regs dpll_ddr_regs = {
48983e3700STom Rini .cm_clkmode_dpll = CM_WKUP + 0x94,
49983e3700STom Rini .cm_idlest_dpll = CM_WKUP + 0x34,
50983e3700STom Rini .cm_clksel_dpll = CM_WKUP + 0x40,
51983e3700STom Rini .cm_div_m2_dpll = CM_WKUP + 0xA0,
52983e3700STom Rini };
53983e3700STom Rini
5432151929SHannes Schmelzer const struct dpll_regs dpll_disp_regs = {
5532151929SHannes Schmelzer .cm_clkmode_dpll = CM_WKUP + 0x98,
5632151929SHannes Schmelzer .cm_idlest_dpll = CM_WKUP + 0x48,
5732151929SHannes Schmelzer .cm_clksel_dpll = CM_WKUP + 0x54,
5832151929SHannes Schmelzer .cm_div_m2_dpll = CM_WKUP + 0xA4,
5932151929SHannes Schmelzer };
6032151929SHannes Schmelzer
61983e3700STom Rini struct dpll_params dpll_mpu_opp100 = {
62983e3700STom Rini CONFIG_SYS_MPUCLK, OSC-1, 1, -1, -1, -1, -1};
63983e3700STom Rini const struct dpll_params dpll_core_opp100 = {
64983e3700STom Rini 1000, OSC-1, -1, -1, 10, 8, 4};
65983e3700STom Rini
66fbd6295dSLokesh Vutla const struct dpll_params dpll_mpu_opp[NUM_CRYSTAL_FREQ][NUM_OPPS] = {
67fbd6295dSLokesh Vutla { /* 19.2 MHz */
68fbd6295dSLokesh Vutla {125, 3, 2, -1, -1, -1, -1}, /* OPP 50 */
69fbd6295dSLokesh Vutla {-1, -1, -1, -1, -1, -1, -1}, /* OPP RESERVED */
70fbd6295dSLokesh Vutla {125, 3, 1, -1, -1, -1, -1}, /* OPP 100 */
71fbd6295dSLokesh Vutla {150, 3, 1, -1, -1, -1, -1}, /* OPP 120 */
72fbd6295dSLokesh Vutla {125, 2, 1, -1, -1, -1, -1}, /* OPP TB */
73fbd6295dSLokesh Vutla {625, 11, 1, -1, -1, -1, -1} /* OPP NT */
74fbd6295dSLokesh Vutla },
75fbd6295dSLokesh Vutla { /* 24 MHz */
76fbd6295dSLokesh Vutla {25, 0, 2, -1, -1, -1, -1}, /* OPP 50 */
77fbd6295dSLokesh Vutla {-1, -1, -1, -1, -1, -1, -1}, /* OPP RESERVED */
78fbd6295dSLokesh Vutla {25, 0, 1, -1, -1, -1, -1}, /* OPP 100 */
79fbd6295dSLokesh Vutla {30, 0, 1, -1, -1, -1, -1}, /* OPP 120 */
80fbd6295dSLokesh Vutla {100, 3, 1, -1, -1, -1, -1}, /* OPP TB */
81fbd6295dSLokesh Vutla {125, 2, 1, -1, -1, -1, -1} /* OPP NT */
82fbd6295dSLokesh Vutla },
83fbd6295dSLokesh Vutla { /* 25 MHz */
84fbd6295dSLokesh Vutla {24, 0, 2, -1, -1, -1, -1}, /* OPP 50 */
85fbd6295dSLokesh Vutla {-1, -1, -1, -1, -1, -1, -1}, /* OPP RESERVED */
86fbd6295dSLokesh Vutla {24, 0, 1, -1, -1, -1, -1}, /* OPP 100 */
87fbd6295dSLokesh Vutla {144, 4, 1, -1, -1, -1, -1}, /* OPP 120 */
88fbd6295dSLokesh Vutla {32, 0, 1, -1, -1, -1, -1}, /* OPP TB */
89fbd6295dSLokesh Vutla {40, 0, 1, -1, -1, -1, -1} /* OPP NT */
90fbd6295dSLokesh Vutla },
91fbd6295dSLokesh Vutla { /* 26 MHz */
92fbd6295dSLokesh Vutla {300, 12, 2, -1, -1, -1, -1}, /* OPP 50 */
93fbd6295dSLokesh Vutla {-1, -1, -1, -1, -1, -1, -1}, /* OPP RESERVED */
94fbd6295dSLokesh Vutla {300, 12, 1, -1, -1, -1, -1}, /* OPP 100 */
95fbd6295dSLokesh Vutla {360, 12, 1, -1, -1, -1, -1}, /* OPP 120 */
96fbd6295dSLokesh Vutla {400, 12, 1, -1, -1, -1, -1}, /* OPP TB */
97fbd6295dSLokesh Vutla {500, 12, 1, -1, -1, -1, -1} /* OPP NT */
98fbd6295dSLokesh Vutla },
99fbd6295dSLokesh Vutla };
100fbd6295dSLokesh Vutla
101fbd6295dSLokesh Vutla const struct dpll_params dpll_core_1000MHz[NUM_CRYSTAL_FREQ] = {
102fbd6295dSLokesh Vutla {625, 11, -1, -1, 10, 8, 4}, /* 19.2 MHz */
103fbd6295dSLokesh Vutla {125, 2, -1, -1, 10, 8, 4}, /* 24 MHz */
104fbd6295dSLokesh Vutla {40, 0, -1, -1, 10, 8, 4}, /* 25 MHz */
105fbd6295dSLokesh Vutla {500, 12, -1, -1, 10, 8, 4} /* 26 MHz */
106fbd6295dSLokesh Vutla };
107fbd6295dSLokesh Vutla
108fbd6295dSLokesh Vutla const struct dpll_params dpll_per_192MHz[NUM_CRYSTAL_FREQ] = {
109fbd6295dSLokesh Vutla {400, 7, 5, -1, -1, -1, -1}, /* 19.2 MHz */
110fbd6295dSLokesh Vutla {400, 9, 5, -1, -1, -1, -1}, /* 24 MHz */
111fbd6295dSLokesh Vutla {384, 9, 5, -1, -1, -1, -1}, /* 25 MHz */
112fbd6295dSLokesh Vutla {480, 12, 5, -1, -1, -1, -1} /* 26 MHz */
113fbd6295dSLokesh Vutla };
114fbd6295dSLokesh Vutla
115fbd6295dSLokesh Vutla const struct dpll_params dpll_ddr3_303MHz[NUM_CRYSTAL_FREQ] = {
116fbd6295dSLokesh Vutla {505, 15, 2, -1, -1, -1, -1}, /*19.2*/
117fbd6295dSLokesh Vutla {101, 3, 2, -1, -1, -1, -1}, /* 24 MHz */
1189b88a4bdSLokesh Vutla {303, 24, 1, -1, -1, -1, -1}, /* 25 MHz */
1199b88a4bdSLokesh Vutla {303, 12, 2, -1, -1, -1, -1} /* 26 MHz */
120fbd6295dSLokesh Vutla };
121fbd6295dSLokesh Vutla
122fbd6295dSLokesh Vutla const struct dpll_params dpll_ddr3_400MHz[NUM_CRYSTAL_FREQ] = {
123fbd6295dSLokesh Vutla {125, 5, 1, -1, -1, -1, -1}, /*19.2*/
124fbd6295dSLokesh Vutla {50, 2, 1, -1, -1, -1, -1}, /* 24 MHz */
1259b88a4bdSLokesh Vutla {16, 0, 1, -1, -1, -1, -1}, /* 25 MHz */
1269b88a4bdSLokesh Vutla {200, 12, 1, -1, -1, -1, -1} /* 26 MHz */
127fbd6295dSLokesh Vutla };
128fbd6295dSLokesh Vutla
129fbd6295dSLokesh Vutla const struct dpll_params dpll_ddr2_266MHz[NUM_CRYSTAL_FREQ] = {
130fbd6295dSLokesh Vutla {665, 47, 1, -1, -1, -1, -1}, /*19.2*/
131fbd6295dSLokesh Vutla {133, 11, 1, -1, -1, -1, -1}, /* 24 MHz */
1329b88a4bdSLokesh Vutla {266, 24, 1, -1, -1, -1, -1}, /* 25 MHz */
1339b88a4bdSLokesh Vutla {133, 12, 1, -1, -1, -1, -1} /* 26 MHz */
134fbd6295dSLokesh Vutla };
135fbd6295dSLokesh Vutla
get_dpll_mpu_params(void)136fbd6295dSLokesh Vutla __weak const struct dpll_params *get_dpll_mpu_params(void)
137983e3700STom Rini {
138fbd6295dSLokesh Vutla return &dpll_mpu_opp100;
139983e3700STom Rini }
140983e3700STom Rini
get_dpll_core_params(void)141983e3700STom Rini const struct dpll_params *get_dpll_core_params(void)
142983e3700STom Rini {
143fbd6295dSLokesh Vutla int ind = get_sys_clk_index();
144fbd6295dSLokesh Vutla
145fbd6295dSLokesh Vutla return &dpll_core_1000MHz[ind];
146983e3700STom Rini }
147983e3700STom Rini
get_dpll_per_params(void)148983e3700STom Rini const struct dpll_params *get_dpll_per_params(void)
149983e3700STom Rini {
150fbd6295dSLokesh Vutla int ind = get_sys_clk_index();
151fbd6295dSLokesh Vutla
152fbd6295dSLokesh Vutla return &dpll_per_192MHz[ind];
153983e3700STom Rini }
154983e3700STom Rini
setup_clocks_for_console(void)155983e3700STom Rini void setup_clocks_for_console(void)
156983e3700STom Rini {
157983e3700STom Rini clrsetbits_le32(&cmwkup->wkclkstctrl, CD_CLKCTRL_CLKTRCTRL_MASK,
158983e3700STom Rini CD_CLKCTRL_CLKTRCTRL_SW_WKUP <<
159983e3700STom Rini CD_CLKCTRL_CLKTRCTRL_SHIFT);
160983e3700STom Rini
161983e3700STom Rini clrsetbits_le32(&cmper->l4hsclkstctrl, CD_CLKCTRL_CLKTRCTRL_MASK,
162983e3700STom Rini CD_CLKCTRL_CLKTRCTRL_SW_WKUP <<
163983e3700STom Rini CD_CLKCTRL_CLKTRCTRL_SHIFT);
164983e3700STom Rini
165983e3700STom Rini clrsetbits_le32(&cmwkup->wkup_uart0ctrl,
166983e3700STom Rini MODULE_CLKCTRL_MODULEMODE_MASK,
167983e3700STom Rini MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
168983e3700STom Rini MODULE_CLKCTRL_MODULEMODE_SHIFT);
169983e3700STom Rini clrsetbits_le32(&cmper->uart1clkctrl,
170983e3700STom Rini MODULE_CLKCTRL_MODULEMODE_MASK,
171983e3700STom Rini MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
172983e3700STom Rini MODULE_CLKCTRL_MODULEMODE_SHIFT);
173983e3700STom Rini clrsetbits_le32(&cmper->uart2clkctrl,
174983e3700STom Rini MODULE_CLKCTRL_MODULEMODE_MASK,
175983e3700STom Rini MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
176983e3700STom Rini MODULE_CLKCTRL_MODULEMODE_SHIFT);
177983e3700STom Rini clrsetbits_le32(&cmper->uart3clkctrl,
178983e3700STom Rini MODULE_CLKCTRL_MODULEMODE_MASK,
179983e3700STom Rini MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
180983e3700STom Rini MODULE_CLKCTRL_MODULEMODE_SHIFT);
181983e3700STom Rini clrsetbits_le32(&cmper->uart4clkctrl,
182983e3700STom Rini MODULE_CLKCTRL_MODULEMODE_MASK,
183983e3700STom Rini MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
184983e3700STom Rini MODULE_CLKCTRL_MODULEMODE_SHIFT);
185983e3700STom Rini clrsetbits_le32(&cmper->uart5clkctrl,
186983e3700STom Rini MODULE_CLKCTRL_MODULEMODE_MASK,
187983e3700STom Rini MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
188983e3700STom Rini MODULE_CLKCTRL_MODULEMODE_SHIFT);
189983e3700STom Rini }
190983e3700STom Rini
enable_basic_clocks(void)191983e3700STom Rini void enable_basic_clocks(void)
192983e3700STom Rini {
193983e3700STom Rini u32 *const clk_domains[] = {
194983e3700STom Rini &cmper->l3clkstctrl,
195983e3700STom Rini &cmper->l4fwclkstctrl,
196983e3700STom Rini &cmper->l3sclkstctrl,
197983e3700STom Rini &cmper->l4lsclkstctrl,
198983e3700STom Rini &cmwkup->wkclkstctrl,
199983e3700STom Rini &cmper->emiffwclkctrl,
200983e3700STom Rini &cmrtc->clkstctrl,
201983e3700STom Rini 0
202983e3700STom Rini };
203983e3700STom Rini
204983e3700STom Rini u32 *const clk_modules_explicit_en[] = {
205983e3700STom Rini &cmper->l3clkctrl,
206983e3700STom Rini &cmper->l4lsclkctrl,
207983e3700STom Rini &cmper->l4fwclkctrl,
208983e3700STom Rini &cmwkup->wkl4wkclkctrl,
209983e3700STom Rini &cmper->l3instrclkctrl,
210983e3700STom Rini &cmper->l4hsclkctrl,
211983e3700STom Rini &cmwkup->wkgpio0clkctrl,
212983e3700STom Rini &cmwkup->wkctrlclkctrl,
213983e3700STom Rini &cmper->timer2clkctrl,
214983e3700STom Rini &cmper->gpmcclkctrl,
215983e3700STom Rini &cmper->elmclkctrl,
216983e3700STom Rini &cmper->mmc0clkctrl,
217983e3700STom Rini &cmper->mmc1clkctrl,
218983e3700STom Rini &cmwkup->wkup_i2c0ctrl,
219983e3700STom Rini &cmper->gpio1clkctrl,
220983e3700STom Rini &cmper->gpio2clkctrl,
221983e3700STom Rini &cmper->gpio3clkctrl,
222983e3700STom Rini &cmper->i2c1clkctrl,
223983e3700STom Rini &cmper->cpgmac0clkctrl,
224983e3700STom Rini &cmper->spi0clkctrl,
225983e3700STom Rini &cmrtc->rtcclkctrl,
226983e3700STom Rini &cmper->usb0clkctrl,
227983e3700STom Rini &cmper->emiffwclkctrl,
228983e3700STom Rini &cmper->emifclkctrl,
229983e3700STom Rini 0
230983e3700STom Rini };
231983e3700STom Rini
232983e3700STom Rini do_enable_clocks(clk_domains, clk_modules_explicit_en, 1);
233983e3700STom Rini
234983e3700STom Rini /* Select the Master osc 24 MHZ as Timer2 clock source */
235983e3700STom Rini writel(0x1, &cmdpll->clktimer2clk);
236983e3700STom Rini }
237983e3700STom Rini
238983e3700STom Rini /*
239983e3700STom Rini * Enable Spread Spectrum for the MPU by calculating the required
240983e3700STom Rini * values and setting the registers accordingly.
241983e3700STom Rini * @param permille The spreading in permille (10th of a percent)
242983e3700STom Rini */
set_mpu_spreadspectrum(int permille)243983e3700STom Rini void set_mpu_spreadspectrum(int permille)
244983e3700STom Rini {
245983e3700STom Rini u32 multiplier_m;
246983e3700STom Rini u32 predivider_n;
247983e3700STom Rini u32 cm_clksel_dpll_mpu;
248983e3700STom Rini u32 cm_clkmode_dpll_mpu;
249983e3700STom Rini u32 ref_clock;
250983e3700STom Rini u32 pll_bandwidth;
251983e3700STom Rini u32 mod_freq_divider;
252983e3700STom Rini u32 exponent;
253983e3700STom Rini u32 mantissa;
254983e3700STom Rini u32 delta_m_step;
255983e3700STom Rini
256983e3700STom Rini printf("Enabling Spread Spectrum of %d permille for MPU\n",
257983e3700STom Rini permille);
258983e3700STom Rini
259983e3700STom Rini /* Read PLL parameter m and n */
260983e3700STom Rini cm_clksel_dpll_mpu = readl(&cmwkup->clkseldpllmpu);
261983e3700STom Rini multiplier_m = (cm_clksel_dpll_mpu >> 8) & 0x3FF;
262983e3700STom Rini predivider_n = cm_clksel_dpll_mpu & 0x7F;
263983e3700STom Rini
264983e3700STom Rini /*
265983e3700STom Rini * Calculate reference clock (clock after pre-divider),
266983e3700STom Rini * its max. PLL bandwidth,
267983e3700STom Rini * and resulting mod_freq_divider
268983e3700STom Rini */
269983e3700STom Rini ref_clock = V_OSCK / (predivider_n + 1);
270983e3700STom Rini pll_bandwidth = ref_clock / 70;
271983e3700STom Rini mod_freq_divider = ref_clock / (4 * pll_bandwidth);
272983e3700STom Rini
273983e3700STom Rini /* Calculate Mantissa/Exponent */
274983e3700STom Rini exponent = 0;
275983e3700STom Rini mantissa = mod_freq_divider;
276983e3700STom Rini while ((mantissa > 127) && (exponent < 7)) {
277983e3700STom Rini exponent++;
278983e3700STom Rini mantissa /= 2;
279983e3700STom Rini }
280983e3700STom Rini if (mantissa > 127)
281983e3700STom Rini mantissa = 127;
282983e3700STom Rini
283983e3700STom Rini mod_freq_divider = mantissa << exponent;
284983e3700STom Rini
285983e3700STom Rini /*
286983e3700STom Rini * Calculate Modulation steps
287983e3700STom Rini * As we use Downspread only, the spread is twice the value of
288983e3700STom Rini * permille, so Div2!
289983e3700STom Rini * As it takes the value in percent, divide by ten!
290983e3700STom Rini */
291983e3700STom Rini delta_m_step = ((u32)((multiplier_m * permille) / 10 / 2)) << 18;
292983e3700STom Rini delta_m_step /= 100;
293983e3700STom Rini delta_m_step /= mod_freq_divider;
294983e3700STom Rini if (delta_m_step > 0xFFFFF)
295983e3700STom Rini delta_m_step = 0xFFFFF;
296983e3700STom Rini
297983e3700STom Rini /* Setup Spread Spectrum */
298983e3700STom Rini writel(delta_m_step, &cmwkup->sscdeltamstepdllmpu);
299983e3700STom Rini writel((exponent << 8) | mantissa, &cmwkup->sscmodfreqdivdpllmpu);
300983e3700STom Rini cm_clkmode_dpll_mpu = readl(&cmwkup->clkmoddpllmpu);
301983e3700STom Rini /* clear all SSC flags */
302983e3700STom Rini cm_clkmode_dpll_mpu &= ~(0xF << CM_CLKMODE_DPLL_SSC_EN_SHIFT);
303983e3700STom Rini /* enable SSC with Downspread only */
304983e3700STom Rini cm_clkmode_dpll_mpu |= CM_CLKMODE_DPLL_SSC_EN_MASK |
305983e3700STom Rini CM_CLKMODE_DPLL_SSC_DOWNSPREAD_MASK;
306983e3700STom Rini writel(cm_clkmode_dpll_mpu, &cmwkup->clkmoddpllmpu);
307983e3700STom Rini while (!(readl(&cmwkup->clkmoddpllmpu) & 0x2000))
308983e3700STom Rini ;
309983e3700STom Rini }
310