blob: 1cce511bcb26d7cf89f291116d3e70c1cc60cc45 [file] [log] [blame]
Angel Pons1ddb8942020-04-04 18:51:26 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -08002
Kyösti Mälkki13f66502019-03-03 08:01:05 +02003#include <device/mmio.h>
Stefan Reinauer08dc3572013-05-14 16:57:50 -07004#include <assert.h>
Julius Werner1ed0c8c2014-10-20 13:16:29 -07005#include <console/console.h>
6#include <soc/clk.h>
7#include <soc/periph.h>
Edward O'Callaghan9b152b22014-12-26 12:36:47 +11008#include <timer.h>
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -08009
10/* input clock of PLL: SMDK5250 has 24MHz input clock */
Elyes HAOUAS251514d2019-01-23 11:36:44 +010011#define CONF_SYS_CLK_FREQ 24000000
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080012
Stefan Reinauer08dc3572013-05-14 16:57:50 -070013static struct arm_clk_ratios arm_clk_ratios[] = {
David Hendricks0d4f97e2013-02-03 18:09:58 -080014 {
15 .arm_freq_mhz = 600,
16
17 .apll_mdiv = 0xc8,
18 .apll_pdiv = 0x4,
19 .apll_sdiv = 0x1,
20
21 .arm2_ratio = 0x0,
22 .apll_ratio = 0x1,
23 .pclk_dbg_ratio = 0x1,
24 .atb_ratio = 0x2,
25 .periph_ratio = 0x7,
26 .acp_ratio = 0x7,
27 .cpud_ratio = 0x1,
28 .arm_ratio = 0x0,
29 }, {
30 .arm_freq_mhz = 800,
31
32 .apll_mdiv = 0x64,
33 .apll_pdiv = 0x3,
34 .apll_sdiv = 0x0,
35
36 .arm2_ratio = 0x0,
37 .apll_ratio = 0x1,
38 .pclk_dbg_ratio = 0x1,
39 .atb_ratio = 0x3,
40 .periph_ratio = 0x7,
41 .acp_ratio = 0x7,
42 .cpud_ratio = 0x2,
43 .arm_ratio = 0x0,
44 }, {
45 .arm_freq_mhz = 1000,
46
47 .apll_mdiv = 0x7d,
48 .apll_pdiv = 0x3,
49 .apll_sdiv = 0x0,
50
51 .arm2_ratio = 0x0,
52 .apll_ratio = 0x1,
53 .pclk_dbg_ratio = 0x1,
54 .atb_ratio = 0x4,
55 .periph_ratio = 0x7,
56 .acp_ratio = 0x7,
57 .cpud_ratio = 0x2,
58 .arm_ratio = 0x0,
59 }, {
60 .arm_freq_mhz = 1200,
61
62 .apll_mdiv = 0x96,
63 .apll_pdiv = 0x3,
64 .apll_sdiv = 0x0,
65
66 .arm2_ratio = 0x0,
67 .apll_ratio = 0x3,
68 .pclk_dbg_ratio = 0x1,
69 .atb_ratio = 0x5,
70 .periph_ratio = 0x7,
71 .acp_ratio = 0x7,
72 .cpud_ratio = 0x3,
73 .arm_ratio = 0x0,
74 }, {
75 .arm_freq_mhz = 1400,
76
77 .apll_mdiv = 0xaf,
78 .apll_pdiv = 0x3,
79 .apll_sdiv = 0x0,
80
81 .arm2_ratio = 0x0,
82 .apll_ratio = 0x3,
83 .pclk_dbg_ratio = 0x1,
84 .atb_ratio = 0x6,
85 .periph_ratio = 0x7,
86 .acp_ratio = 0x7,
87 .cpud_ratio = 0x3,
88 .arm_ratio = 0x0,
89 }, {
90 .arm_freq_mhz = 1700,
91
92 .apll_mdiv = 0x1a9,
93 .apll_pdiv = 0x6,
94 .apll_sdiv = 0x0,
95
96 .arm2_ratio = 0x0,
97 .apll_ratio = 0x3,
98 .pclk_dbg_ratio = 0x1,
99 .atb_ratio = 0x6,
100 .periph_ratio = 0x7,
101 .acp_ratio = 0x7,
102 .cpud_ratio = 0x3,
103 .arm_ratio = 0x0,
104 }
105};
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800106
107/* src_bit div_bit prediv_bit */
108static struct clk_bit_info clk_bit_info[PERIPH_ID_COUNT] = {
109 {0, 4, 0, -1},
110 {4, 4, 4, -1},
111 {8, 4, 8, -1},
112 {12, 4, 12, -1},
113 {0, 4, 0, 8},
114 {4, 4, 16, 24},
115 {8, 4, 0, 8},
116 {12, 4, 16, 24},
117 {-1, -1, -1, -1},
118 {16, 4, 0, 8}, /* PERIPH_ID_SROMC */
119 {20, 4, 16, 24},
120 {24, 4, 0, 8},
121 {0, 4, 0, 4},
122 {4, 4, 12, 16},
123 {-1, 4, -1, -1},
124 {-1, 4, -1, -1},
125 {-1, 4, 24, 0},
126 {-1, 4, 24, 0},
127 {-1, 4, 24, 0},
128 {-1, 4, 24, 0},
129 {-1, 4, 24, 0},
130 {-1, 4, 24, 0},
131 {-1, 4, 24, 0},
132 {-1, 4, 24, 0},
133 {24, 4, 0, -1},
134 {24, 4, 0, -1},
135 {24, 4, 0, -1},
136 {24, 4, 0, -1},
137 {24, 4, 0, -1},
138 {-1, -1, -1, -1},
139 {-1, -1, -1, -1},
140 {-1, -1, -1, -1}, /* PERIPH_ID_I2S1 */
141 {24, 1, 20, -1}, /* PERIPH_ID_SATA */
142};
143
Martin Roth4c3ab732013-07-08 16:23:54 -0600144/* Epll Clock division values to achieve different frequency output */
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800145static struct st_epll_con_val epll_div[] = {
146 { 192000000, 0, 48, 3, 1, 0 },
147 { 180000000, 0, 45, 3, 1, 0 },
148 { 73728000, 1, 73, 3, 3, 47710 },
149 { 67737600, 1, 90, 4, 3, 20762 },
150 { 49152000, 0, 49, 3, 3, 9961 },
151 { 45158400, 0, 45, 3, 3, 10381 },
152 { 180633600, 0, 45, 3, 1, 10381 }
153};
154
155/* exynos5: return pll clock frequency */
156unsigned long get_pll_clk(int pllreg)
157{
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800158 unsigned long r, m, p, s, k = 0, mask, fout;
159 unsigned int freq;
160
161 switch (pllreg) {
162 case APLL:
Julius Werner2f37bd62015-02-19 14:51:15 -0800163 r = read32(&exynos_clock->apll_con0);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800164 break;
165 case BPLL:
Julius Werner2f37bd62015-02-19 14:51:15 -0800166 r = read32(&exynos_clock->bpll_con0);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800167 break;
168 case MPLL:
Julius Werner2f37bd62015-02-19 14:51:15 -0800169 r = read32(&exynos_clock->mpll_con0);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800170 break;
171 case EPLL:
Julius Werner2f37bd62015-02-19 14:51:15 -0800172 r = read32(&exynos_clock->epll_con0);
173 k = read32(&exynos_clock->epll_con1);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800174 break;
175 case VPLL:
Julius Werner2f37bd62015-02-19 14:51:15 -0800176 r = read32(&exynos_clock->vpll_con0);
177 k = read32(&exynos_clock->vpll_con1);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800178 break;
179 default:
180 printk(BIOS_DEBUG, "Unsupported PLL (%d)\n", pllreg);
181 return 0;
182 }
183
184 /*
185 * APLL_CON: MIDV [25:16]
186 * MPLL_CON: MIDV [25:16]
187 * EPLL_CON: MIDV [24:16]
188 * VPLL_CON: MIDV [24:16]
189 */
190 if (pllreg == APLL || pllreg == BPLL || pllreg == MPLL)
191 mask = 0x3ff;
192 else
193 mask = 0x1ff;
194
195 m = (r >> 16) & mask;
196
197 /* PDIV [13:8] */
198 p = (r >> 8) & 0x3f;
199 /* SDIV [2:0] */
200 s = r & 0x7;
201
Elyes HAOUAS251514d2019-01-23 11:36:44 +0100202 freq = CONF_SYS_CLK_FREQ;
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800203
204 if (pllreg == EPLL) {
205 k = k & 0xffff;
206 /* FOUT = (MDIV + K / 65536) * FIN / (PDIV * 2^SDIV) */
207 fout = (m + k / 65536) * (freq / (p * (1 << s)));
208 } else if (pllreg == VPLL) {
209 k = k & 0xfff;
210 /* FOUT = (MDIV + K / 1024) * FIN / (PDIV * 2^SDIV) */
211 fout = (m + k / 1024) * (freq / (p * (1 << s)));
212 } else {
213 /* FOUT = MDIV * FIN / (PDIV * 2^SDIV) */
214 fout = m * (freq / (p * (1 << s)));
215 }
216
217 return fout;
218}
219
220unsigned long clock_get_periph_rate(enum periph_id peripheral)
221{
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800222 struct clk_bit_info *bit_info = &clk_bit_info[peripheral];
223 unsigned long sclk, sub_clk;
224 unsigned int src, div, sub_div;
225
226 switch (peripheral) {
227 case PERIPH_ID_UART0:
228 case PERIPH_ID_UART1:
229 case PERIPH_ID_UART2:
230 case PERIPH_ID_UART3:
Julius Werner2f37bd62015-02-19 14:51:15 -0800231 src = read32(&exynos_clock->src_peric0);
232 div = read32(&exynos_clock->div_peric0);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800233 break;
234 case PERIPH_ID_PWM0:
235 case PERIPH_ID_PWM1:
236 case PERIPH_ID_PWM2:
237 case PERIPH_ID_PWM3:
238 case PERIPH_ID_PWM4:
Julius Werner2f37bd62015-02-19 14:51:15 -0800239 src = read32(&exynos_clock->src_peric0);
240 div = read32(&exynos_clock->div_peric3);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800241 break;
242 case PERIPH_ID_SPI0:
243 case PERIPH_ID_SPI1:
Julius Werner2f37bd62015-02-19 14:51:15 -0800244 src = read32(&exynos_clock->src_peric1);
245 div = read32(&exynos_clock->div_peric1);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800246 break;
247 case PERIPH_ID_SPI2:
Julius Werner2f37bd62015-02-19 14:51:15 -0800248 src = read32(&exynos_clock->src_peric1);
249 div = read32(&exynos_clock->div_peric2);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800250 break;
251 case PERIPH_ID_SPI3:
252 case PERIPH_ID_SPI4:
Julius Werner2f37bd62015-02-19 14:51:15 -0800253 src = read32(&exynos_clock->sclk_src_isp);
254 div = read32(&exynos_clock->sclk_div_isp);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800255 break;
256 case PERIPH_ID_SATA:
Julius Werner2f37bd62015-02-19 14:51:15 -0800257 src = read32(&exynos_clock->src_fsys);
258 div = read32(&exynos_clock->div_fsys0);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800259 break;
260 case PERIPH_ID_SDMMC0:
261 case PERIPH_ID_SDMMC1:
262 case PERIPH_ID_SDMMC2:
263 case PERIPH_ID_SDMMC3:
Julius Werner2f37bd62015-02-19 14:51:15 -0800264 src = read32(&exynos_clock->src_fsys);
265 div = read32(&exynos_clock->div_fsys1);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800266 break;
267 case PERIPH_ID_I2C0:
268 case PERIPH_ID_I2C1:
269 case PERIPH_ID_I2C2:
270 case PERIPH_ID_I2C3:
271 case PERIPH_ID_I2C4:
272 case PERIPH_ID_I2C5:
273 case PERIPH_ID_I2C6:
274 case PERIPH_ID_I2C7:
275 sclk = get_pll_clk(MPLL);
Julius Werner2f37bd62015-02-19 14:51:15 -0800276 sub_div = ((read32(&exynos_clock->div_top1)
Julius Wernerfa938c72013-08-29 14:17:36 -0700277 >> bit_info->div_bit) & 0x7) + 1;
Julius Werner2f37bd62015-02-19 14:51:15 -0800278 div = ((read32(&exynos_clock->div_top0)
Julius Wernerfa938c72013-08-29 14:17:36 -0700279 >> bit_info->prediv_bit) & 0x7) + 1;
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800280 return (sclk / sub_div) / div;
281 default:
282 printk(BIOS_DEBUG, "%s: invalid peripheral %d", __func__, peripheral);
283 return -1;
284 };
285
286 src = (src >> bit_info->src_bit) & ((1 << bit_info->n_src_bits) - 1);
287 if (peripheral == PERIPH_ID_SATA) {
288 if (src)
289 sclk = get_pll_clk(BPLL);
290 else
291 sclk = get_pll_clk(MPLL);
292 } else {
293 if (src == SRC_MPLL)
294 sclk = get_pll_clk(MPLL);
295 else if (src == SRC_EPLL)
296 sclk = get_pll_clk(EPLL);
297 else if (src == SRC_VPLL)
298 sclk = get_pll_clk(VPLL);
299 else
300 return 0;
301 }
302
303 sub_div = (div >> bit_info->div_bit) & 0xf;
304 sub_clk = sclk / (sub_div + 1);
305
306 if (peripheral == PERIPH_ID_SDMMC0 || peripheral == PERIPH_ID_SDMMC2) {
307 div = (div >> bit_info->prediv_bit) & 0xff;
308 return sub_clk / (div + 1);
309 }
310
311 return sub_clk;
312}
313
314/* exynos5: return ARM clock frequency */
315unsigned long get_arm_clk(void)
316{
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800317 unsigned long div;
318 unsigned long armclk;
319 unsigned int arm_ratio;
320 unsigned int arm2_ratio;
321
Julius Werner2f37bd62015-02-19 14:51:15 -0800322 div = read32(&exynos_clock->div_cpu0);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800323
324 /* ARM_RATIO: [2:0], ARM2_RATIO: [30:28] */
325 arm_ratio = (div >> 0) & 0x7;
326 arm2_ratio = (div >> 28) & 0x7;
327
328 armclk = get_pll_clk(APLL) / (arm_ratio + 1);
329 armclk /= (arm2_ratio + 1);
330
331 return armclk;
332}
333
David Hendricks0d4f97e2013-02-03 18:09:58 -0800334struct arm_clk_ratios *get_arm_clk_ratios(void)
335{
336 struct arm_clk_ratios *arm_ratio;
337 unsigned long arm_freq = 1700; /* FIXME: use get_arm_clk() */
338 int i;
339
340 for (i = 0, arm_ratio = arm_clk_ratios; i < ARRAY_SIZE(arm_clk_ratios);
341 i++, arm_ratio++) {
342 if (arm_ratio->arm_freq_mhz == arm_freq)
343 return arm_ratio;
344 }
345
346 return NULL;
347}
348
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800349/* exynos5: set the mmc clock */
350void set_mmc_clk(int dev_index, unsigned int div)
351{
David Hendricks086b3692013-04-08 20:01:18 -0700352 unsigned int *addr;
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800353 unsigned int val;
354
355 /*
356 * CLK_DIV_FSYS1
357 * MMC0_PRE_RATIO [15:8], MMC1_PRE_RATIO [31:24]
358 * CLK_DIV_FSYS2
359 * MMC2_PRE_RATIO [15:8], MMC3_PRE_RATIO [31:24]
360 */
361 if (dev_index < 2) {
Julius Wernerfa938c72013-08-29 14:17:36 -0700362 addr = &exynos_clock->div_fsys1;
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800363 } else {
Julius Wernerfa938c72013-08-29 14:17:36 -0700364 addr = &exynos_clock->div_fsys2;
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800365 dev_index -= 2;
366 }
367
Julius Werner2f37bd62015-02-19 14:51:15 -0800368 val = read32(addr);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800369 val &= ~(0xff << ((dev_index << 4) + 8));
370 val |= (div & 0xff) << ((dev_index << 4) + 8);
Julius Werner2f37bd62015-02-19 14:51:15 -0800371 write32(addr, val);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800372}
373
Martin Roth57e89092019-10-23 21:45:23 -0600374void clock_ll_set_pre_ratio(enum periph_id periph_id, unsigned int divisor)
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800375{
Martin Roth57e89092019-10-23 21:45:23 -0600376 unsigned int shift;
377 unsigned int mask = 0xff;
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800378 u32 *reg;
379
380 /*
Martin Roth4c3ab732013-07-08 16:23:54 -0600381 * For now we only handle a very small subset of peripherals here.
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800382 * Others will need to (and do) mangle the clock registers
383 * themselves, At some point it is hoped that this function can work
384 * from a table or calculated register offset / mask. For now this
385 * is at least better than spreading clock control code around
386 * U-Boot.
387 */
388 switch (periph_id) {
389 case PERIPH_ID_SPI0:
Julius Wernerfa938c72013-08-29 14:17:36 -0700390 reg = &exynos_clock->div_peric1;
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800391 shift = 8;
392 break;
393 case PERIPH_ID_SPI1:
Julius Wernerfa938c72013-08-29 14:17:36 -0700394 reg = &exynos_clock->div_peric1;
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800395 shift = 24;
396 break;
397 case PERIPH_ID_SPI2:
Julius Wernerfa938c72013-08-29 14:17:36 -0700398 reg = &exynos_clock->div_peric2;
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800399 shift = 8;
400 break;
401 case PERIPH_ID_SPI3:
Julius Wernerfa938c72013-08-29 14:17:36 -0700402 reg = &exynos_clock->sclk_div_isp;
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800403 shift = 4;
404 break;
405 case PERIPH_ID_SPI4:
Julius Wernerfa938c72013-08-29 14:17:36 -0700406 reg = &exynos_clock->sclk_div_isp;
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800407 shift = 16;
408 break;
409 default:
Stefan Reinauer08dc3572013-05-14 16:57:50 -0700410 printk(BIOS_DEBUG, "%s: Unsupported peripheral ID %d\n", __func__,
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800411 periph_id);
412 return;
413 }
Julius Werner55009af2019-12-02 22:03:27 -0800414 clrsetbits32(reg, mask << shift, (divisor & mask) << shift);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800415}
416
Martin Roth57e89092019-10-23 21:45:23 -0600417void clock_ll_set_ratio(enum periph_id periph_id, unsigned int divisor)
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800418{
Martin Roth57e89092019-10-23 21:45:23 -0600419 unsigned int shift;
420 unsigned int mask = 0xff;
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800421 u32 *reg;
422
423 switch (periph_id) {
424 case PERIPH_ID_SPI0:
Julius Wernerfa938c72013-08-29 14:17:36 -0700425 reg = &exynos_clock->div_peric1;
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800426 shift = 0;
427 break;
428 case PERIPH_ID_SPI1:
Julius Wernerfa938c72013-08-29 14:17:36 -0700429 reg = &exynos_clock->div_peric1;
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800430 shift = 16;
431 break;
432 case PERIPH_ID_SPI2:
Julius Wernerfa938c72013-08-29 14:17:36 -0700433 reg = &exynos_clock->div_peric2;
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800434 shift = 0;
435 break;
436 case PERIPH_ID_SPI3:
Julius Wernerfa938c72013-08-29 14:17:36 -0700437 reg = &exynos_clock->sclk_div_isp;
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800438 shift = 0;
439 break;
440 case PERIPH_ID_SPI4:
Julius Wernerfa938c72013-08-29 14:17:36 -0700441 reg = &exynos_clock->sclk_div_isp;
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800442 shift = 12;
443 break;
444 default:
Stefan Reinauer08dc3572013-05-14 16:57:50 -0700445 printk(BIOS_DEBUG, "%s: Unsupported peripheral ID %d\n", __func__,
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800446 periph_id);
447 return;
448 }
Julius Werner55009af2019-12-02 22:03:27 -0800449 clrsetbits32(reg, mask << shift, (divisor & mask) << shift);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800450}
451
452/**
453 * Linearly searches for the most accurate main and fine stage clock scalars
454 * (divisors) for a specified target frequency and scalar bit sizes by checking
455 * all multiples of main_scalar_bits values. Will always return scalars up to or
456 * slower than target.
457 *
458 * @param main_scalar_bits Number of main scalar bits, must be > 0 and < 32
459 * @param fine_scalar_bits Number of fine scalar bits, must be > 0 and < 32
Martin Roth32bc6b62015-01-04 16:54:35 -0700460 * @param input_rate Clock frequency to be scaled in Hz
461 * @param target_rate Desired clock frequency in Hz
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800462 * @param best_fine_scalar Pointer to store the fine stage divisor
463 *
464 * @return best_main_scalar Main scalar for desired frequency or -1 if none
465 * found
466 */
467static int clock_calc_best_scalar(unsigned int main_scaler_bits,
468 unsigned int fine_scalar_bits, unsigned int input_rate,
469 unsigned int target_rate, unsigned int *best_fine_scalar)
470{
471 int i;
472 int best_main_scalar = -1;
473 unsigned int best_error = target_rate;
474 const unsigned int cap = (1 << fine_scalar_bits) - 1;
475 const unsigned int loops = 1 << main_scaler_bits;
476
Stefan Reinauer08dc3572013-05-14 16:57:50 -0700477 printk(BIOS_DEBUG, "Input Rate is %u, Target is %u, Cap is %u\n", input_rate,
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800478 target_rate, cap);
479
Stefan Reinauer08dc3572013-05-14 16:57:50 -0700480 ASSERT(best_fine_scalar != NULL);
481 ASSERT(main_scaler_bits <= fine_scalar_bits);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800482
483 *best_fine_scalar = 1;
484
485 if (input_rate == 0 || target_rate == 0)
486 return -1;
487
488 if (target_rate >= input_rate)
489 return 1;
490
491 for (i = 1; i <= loops; i++) {
492 const unsigned int effective_div = MAX(MIN(input_rate / i /
493 target_rate, cap), 1);
494 const unsigned int effective_rate = input_rate / i /
495 effective_div;
496 const int error = target_rate - effective_rate;
497
Stefan Reinauer08dc3572013-05-14 16:57:50 -0700498 printk(BIOS_DEBUG, "%d|effdiv:%u, effrate:%u, error:%d\n", i, effective_div,
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800499 effective_rate, error);
500
501 if (error >= 0 && error <= best_error) {
502 best_error = error;
503 best_main_scalar = i;
504 *best_fine_scalar = effective_div;
505 }
506 }
507
508 return best_main_scalar;
509}
510
511int clock_set_rate(enum periph_id periph_id, unsigned int rate)
512{
Patrick Georgi905e6f22014-08-14 20:23:51 +0200513 int main_scalar;
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800514 unsigned int fine;
515
516 switch (periph_id) {
517 case PERIPH_ID_SPI0:
518 case PERIPH_ID_SPI1:
519 case PERIPH_ID_SPI2:
520 case PERIPH_ID_SPI3:
521 case PERIPH_ID_SPI4:
Patrick Georgi905e6f22014-08-14 20:23:51 +0200522 main_scalar = clock_calc_best_scalar(4, 8, 400000000, rate, &fine);
523 if (main_scalar < 0) {
Stefan Reinauer08dc3572013-05-14 16:57:50 -0700524 printk(BIOS_DEBUG, "%s: Cannot set clock rate for periph %d",
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800525 __func__, periph_id);
526 return -1;
527 }
Patrick Georgi905e6f22014-08-14 20:23:51 +0200528 clock_ll_set_ratio(periph_id, main_scalar - 1);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800529 clock_ll_set_pre_ratio(periph_id, fine - 1);
530 break;
531 default:
Stefan Reinauer08dc3572013-05-14 16:57:50 -0700532 printk(BIOS_DEBUG, "%s: Unsupported peripheral ID %d\n", __func__,
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800533 periph_id);
534 return -1;
535 }
536
537 return 0;
538}
539
540int clock_set_mshci(enum periph_id peripheral)
541{
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800542 u32 *addr;
543 unsigned int clock;
544 unsigned int tmp;
545 unsigned int i;
546
547 /* get mpll clock */
548 clock = get_pll_clk(MPLL) / 1000000;
549
550 /*
551 * CLK_DIV_FSYS1
552 * MMC0_PRE_RATIO [15:8], MMC0_RATIO [3:0]
553 * CLK_DIV_FSYS2
554 * MMC2_PRE_RATIO [15:8], MMC2_RATIO [3:0]
555 */
556 switch (peripheral) {
557 case PERIPH_ID_SDMMC0:
Julius Wernerfa938c72013-08-29 14:17:36 -0700558 addr = &exynos_clock->div_fsys1;
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800559 break;
560 case PERIPH_ID_SDMMC2:
Julius Wernerfa938c72013-08-29 14:17:36 -0700561 addr = &exynos_clock->div_fsys2;
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800562 break;
563 default:
Stefan Reinauer08dc3572013-05-14 16:57:50 -0700564 printk(BIOS_DEBUG, "invalid peripheral\n");
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800565 return -1;
566 }
Julius Werner2f37bd62015-02-19 14:51:15 -0800567 tmp = read32(addr) & ~0xff0f;
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800568 for (i = 0; i <= 0xf; i++) {
569 if ((clock / (i + 1)) <= 400) {
Julius Werner2f37bd62015-02-19 14:51:15 -0800570 write32(addr, tmp | i << 0);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800571 break;
572 }
573 }
574 return 0;
575}
576
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800577int clock_epll_set_rate(unsigned long rate)
578{
579 unsigned int epll_con, epll_con_k;
580 unsigned int i;
581 unsigned int lockcnt;
Aaron Durbin43933462014-09-24 10:27:29 -0500582 struct stopwatch sw;
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800583
Julius Werner2f37bd62015-02-19 14:51:15 -0800584 epll_con = read32(&exynos_clock->epll_con0);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800585 epll_con &= ~((EPLL_CON0_LOCK_DET_EN_MASK <<
586 EPLL_CON0_LOCK_DET_EN_SHIFT) |
587 EPLL_CON0_MDIV_MASK << EPLL_CON0_MDIV_SHIFT |
588 EPLL_CON0_PDIV_MASK << EPLL_CON0_PDIV_SHIFT |
589 EPLL_CON0_SDIV_MASK << EPLL_CON0_SDIV_SHIFT);
590
591 for (i = 0; i < ARRAY_SIZE(epll_div); i++) {
592 if (epll_div[i].freq_out == rate)
593 break;
594 }
595
596 if (i == ARRAY_SIZE(epll_div))
597 return -1;
598
599 epll_con_k = epll_div[i].k_dsm << 0;
600 epll_con |= epll_div[i].en_lock_det << EPLL_CON0_LOCK_DET_EN_SHIFT;
601 epll_con |= epll_div[i].m_div << EPLL_CON0_MDIV_SHIFT;
602 epll_con |= epll_div[i].p_div << EPLL_CON0_PDIV_SHIFT;
603 epll_con |= epll_div[i].s_div << EPLL_CON0_SDIV_SHIFT;
604
605 /*
Elyes HAOUASa342f392018-10-17 10:56:26 +0200606 * Required period (in cycles) to generate a stable clock output.
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800607 * The maximum clock time can be up to 3000 * PDIV cycles of PLLs
608 * frequency input (as per spec)
609 */
610 lockcnt = 3000 * epll_div[i].p_div;
611
Julius Werner2f37bd62015-02-19 14:51:15 -0800612 write32(&exynos_clock->epll_lock, lockcnt);
613 write32(&exynos_clock->epll_con0, epll_con);
614 write32(&exynos_clock->epll_con1, epll_con_k);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800615
Aaron Durbin43933462014-09-24 10:27:29 -0500616 stopwatch_init_msecs_expire(&sw, TIMEOUT_EPLL_LOCK);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800617
Julius Werner2f37bd62015-02-19 14:51:15 -0800618 while (!(read32(&exynos_clock->epll_con0) &
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800619 (0x1 << EXYNOS5_EPLLCON0_LOCKED_SHIFT))) {
Aaron Durbin43933462014-09-24 10:27:29 -0500620 if (stopwatch_expired(&sw)) {
621 printk(BIOS_DEBUG,
622 "%s: Timeout waiting for EPLL lock\n",
623 __func__);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800624 return -1;
625 }
626 }
627
628 return 0;
629}
630
631void clock_select_i2s_clk_source(void)
632{
Julius Werner55009af2019-12-02 22:03:27 -0800633 clrsetbits32(&exynos_clock->src_peric1, AUDIO1_SEL_MASK,
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800634 (CLK_SRC_SCLK_EPLL));
635}
636
637int clock_set_i2s_clk_prescaler(unsigned int src_frq, unsigned int dst_frq)
638{
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800639 unsigned int div ;
640
641 if ((dst_frq == 0) || (src_frq == 0)) {
Martin Roth4c3ab732013-07-08 16:23:54 -0600642 printk(BIOS_DEBUG, "%s: Invalid frequency input for prescaler\n", __func__);
Stefan Reinauer08dc3572013-05-14 16:57:50 -0700643 printk(BIOS_DEBUG, "src frq = %d des frq = %d ", src_frq, dst_frq);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800644 return -1;
645 }
646
647 div = (src_frq / dst_frq);
648 if (div > AUDIO_1_RATIO_MASK) {
Stefan Reinauer08dc3572013-05-14 16:57:50 -0700649 printk(BIOS_DEBUG, "%s: Frequency ratio is out of range\n", __func__);
650 printk(BIOS_DEBUG, "src frq = %d des frq = %d ", src_frq, dst_frq);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800651 return -1;
652 }
Julius Werner55009af2019-12-02 22:03:27 -0800653 clrsetbits32(&exynos_clock->div_peric4, AUDIO_1_RATIO_MASK,
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800654 (div & AUDIO_1_RATIO_MASK));
655 return 0;
656}