1 /*
2  * Copyright 2020, Data61, CSIRO (ABN 41 687 119 230)
3  *
4  * SPDX-License-Identifier: GPL-2.0-only
5  */
6 
7 #include <config.h>
8 #include <types.h>
9 #include <plat/machine.h>
10 #include <linker.h>
11 #include <drivers/timer/mct.h>
12 
13 timer_t *mct = (timer_t *) EXYNOS_MCT_PPTR;
14 
15 #ifdef CONFIG_KERNEL_MCS
16 
initTimer(void)17 BOOT_CODE void initTimer(void)
18 {
19 
20     mct_clear_write_status();
21     mct->global.comp0_add_inc = 0;
22     /* Enable interrupts */
23     mct->global.int_en = GINT_COMP0_IRQ;
24     while (mct->global.wstat != (GWSTAT_COMP0_ADD_INC));
25     mct->global.wstat = (GWSTAT_COMP0_ADD_INC);
26     /* enable interrupts */
27     mct->global.tcon = GTCON_EN | GTCON_COMP0_EN;
28     while (mct->global.wstat != GWSTAT_TCON);
29     mct->global.wstat = GWSTAT_TCON;
30 }
31 
32 #else /* CONFIG_KERNEL_MCS */
33 
initTimer(void)34 BOOT_CODE void initTimer(void)
35 {
36 
37     mct_clear_write_status();
38 
39     /* Configure the comparator */
40     mct->global.comp0_add_inc = TIMER_RELOAD;
41 
42     uint64_t  comparator_value = ((((uint64_t) mct->global.cnth) << 32llu)
43                                   | mct->global.cntl) + TIMER_RELOAD;
44     mct->global.comp0h = (uint32_t)(comparator_value >> 32u);
45     mct->global.comp0l = (uint32_t) comparator_value;
46     /* Enable interrupts */
47     mct->global.int_en = GINT_COMP0_IRQ;
48 
49     /* Wait for update */
50     while (mct->global.wstat != (GWSTAT_COMP0H | GWSTAT_COMP0L | GWSTAT_COMP0_ADD_INC));
51     mct->global.wstat = (GWSTAT_COMP0H | GWSTAT_COMP0L | GWSTAT_COMP0_ADD_INC);
52 
53     /* enable interrupts */
54     mct->global.tcon = GTCON_EN | GTCON_COMP0_EN | GTCON_COMP0_AUTOINC;
55     while (mct->global.wstat != GWSTAT_TCON);
56     mct->global.wstat = GWSTAT_TCON;
57 }
58 #endif /* CONFIG_KERNEL_MCS */
59