13bd88451SPaolo Bonzini /*
23bd88451SPaolo Bonzini * Samsung exynos4210 Multi Core timer
33bd88451SPaolo Bonzini *
43bd88451SPaolo Bonzini * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
53bd88451SPaolo Bonzini * All rights reserved.
63bd88451SPaolo Bonzini *
73bd88451SPaolo Bonzini * Evgeny Voevodin <e.voevodin@samsung.com>
83bd88451SPaolo Bonzini *
93bd88451SPaolo Bonzini * This program is free software; you can redistribute it and/or modify it
103bd88451SPaolo Bonzini * under the terms of the GNU General Public License as published by the
113bd88451SPaolo Bonzini * Free Software Foundation; either version 2 of the License, or (at your
123bd88451SPaolo Bonzini * option) any later version.
133bd88451SPaolo Bonzini *
143bd88451SPaolo Bonzini * This program is distributed in the hope that it will be useful,
153bd88451SPaolo Bonzini * but WITHOUT ANY WARRANTY; without even the implied warranty of
163bd88451SPaolo Bonzini * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
173bd88451SPaolo Bonzini * See the GNU General Public License for more details.
183bd88451SPaolo Bonzini *
193bd88451SPaolo Bonzini * You should have received a copy of the GNU General Public License along
203bd88451SPaolo Bonzini * with this program; if not, see <http://www.gnu.org/licenses/>.
213bd88451SPaolo Bonzini */
223bd88451SPaolo Bonzini
233bd88451SPaolo Bonzini /*
243bd88451SPaolo Bonzini * Global Timer:
253bd88451SPaolo Bonzini *
263bd88451SPaolo Bonzini * Consists of two timers. First represents Free Running Counter and second
273bd88451SPaolo Bonzini * is used to measure interval from FRC to nearest comparator.
283bd88451SPaolo Bonzini *
293bd88451SPaolo Bonzini * 0 UINT64_MAX
303bd88451SPaolo Bonzini * | timer0 |
313bd88451SPaolo Bonzini * | <-------------------------------------------------------------- |
323bd88451SPaolo Bonzini * | --------------------------------------------frc---------------> |
333bd88451SPaolo Bonzini * |______________________________________________|__________________|
343bd88451SPaolo Bonzini * CMP0 CMP1 CMP2 | CMP3
353bd88451SPaolo Bonzini * __| |_
363bd88451SPaolo Bonzini * | timer1 |
373bd88451SPaolo Bonzini * | -------------> |
383bd88451SPaolo Bonzini * frc CMPx
393bd88451SPaolo Bonzini *
403bd88451SPaolo Bonzini * Problem: when implementing global timer as is, overflow arises.
413bd88451SPaolo Bonzini * next_time = cur_time + period * count;
423bd88451SPaolo Bonzini * period and count are 64 bits width.
433bd88451SPaolo Bonzini * Lets arm timer for MCT_GT_COUNTER_STEP count and update internal G_CNT
443bd88451SPaolo Bonzini * register during each event.
453bd88451SPaolo Bonzini *
463bd88451SPaolo Bonzini * Problem: both timers need to be implemented using MCT_XT_COUNTER_STEP because
473bd88451SPaolo Bonzini * local timer contains two counters: TCNT and ICNT. TCNT == 0 -> ICNT--.
483bd88451SPaolo Bonzini * IRQ is generated when ICNT riches zero. Implementation where TCNT == 0
493bd88451SPaolo Bonzini * generates IRQs suffers from too frequently events. Better to have one
503bd88451SPaolo Bonzini * uint64_t counter equal to TCNT*ICNT and arm ptimer.c for a minimum(TCNT*ICNT,
513bd88451SPaolo Bonzini * MCT_GT_COUNTER_STEP); (yes, if target tunes ICNT * TCNT to be too low values,
523bd88451SPaolo Bonzini * there is no way to avoid frequently events).
533bd88451SPaolo Bonzini */
543bd88451SPaolo Bonzini
558ef94f0bSPeter Maydell #include "qemu/osdep.h"
56f2ad5140SKrzysztof Kozlowski #include "qemu/log.h"
573bd88451SPaolo Bonzini #include "hw/sysbus.h"
58d6454270SMarkus Armbruster #include "migration/vmstate.h"
593bd88451SPaolo Bonzini #include "qemu/timer.h"
600b8fa32fSMarkus Armbruster #include "qemu/module.h"
613bd88451SPaolo Bonzini #include "hw/ptimer.h"
623bd88451SPaolo Bonzini
633bd88451SPaolo Bonzini #include "hw/arm/exynos4210.h"
6464552b6bSMarkus Armbruster #include "hw/irq.h"
65db1015e9SEduardo Habkost #include "qom/object.h"
663bd88451SPaolo Bonzini
673bd88451SPaolo Bonzini //#define DEBUG_MCT
683bd88451SPaolo Bonzini
693bd88451SPaolo Bonzini #ifdef DEBUG_MCT
703bd88451SPaolo Bonzini #define DPRINTF(fmt, ...) \
713bd88451SPaolo Bonzini do { fprintf(stdout, "MCT: [%24s:%5d] " fmt, __func__, __LINE__, \
723bd88451SPaolo Bonzini ## __VA_ARGS__); } while (0)
733bd88451SPaolo Bonzini #else
743bd88451SPaolo Bonzini #define DPRINTF(fmt, ...) do {} while (0)
753bd88451SPaolo Bonzini #endif
763bd88451SPaolo Bonzini
773bd88451SPaolo Bonzini #define MCT_CFG 0x000
783bd88451SPaolo Bonzini #define G_CNT_L 0x100
793bd88451SPaolo Bonzini #define G_CNT_U 0x104
803bd88451SPaolo Bonzini #define G_CNT_WSTAT 0x110
813bd88451SPaolo Bonzini #define G_COMP0_L 0x200
823bd88451SPaolo Bonzini #define G_COMP0_U 0x204
833bd88451SPaolo Bonzini #define G_COMP0_ADD_INCR 0x208
843bd88451SPaolo Bonzini #define G_COMP1_L 0x210
853bd88451SPaolo Bonzini #define G_COMP1_U 0x214
863bd88451SPaolo Bonzini #define G_COMP1_ADD_INCR 0x218
873bd88451SPaolo Bonzini #define G_COMP2_L 0x220
883bd88451SPaolo Bonzini #define G_COMP2_U 0x224
893bd88451SPaolo Bonzini #define G_COMP2_ADD_INCR 0x228
903bd88451SPaolo Bonzini #define G_COMP3_L 0x230
913bd88451SPaolo Bonzini #define G_COMP3_U 0x234
923bd88451SPaolo Bonzini #define G_COMP3_ADD_INCR 0x238
933bd88451SPaolo Bonzini #define G_TCON 0x240
943bd88451SPaolo Bonzini #define G_INT_CSTAT 0x244
953bd88451SPaolo Bonzini #define G_INT_ENB 0x248
963bd88451SPaolo Bonzini #define G_WSTAT 0x24C
973bd88451SPaolo Bonzini #define L0_TCNTB 0x300
983bd88451SPaolo Bonzini #define L0_TCNTO 0x304
993bd88451SPaolo Bonzini #define L0_ICNTB 0x308
1003bd88451SPaolo Bonzini #define L0_ICNTO 0x30C
1013bd88451SPaolo Bonzini #define L0_FRCNTB 0x310
1023bd88451SPaolo Bonzini #define L0_FRCNTO 0x314
1033bd88451SPaolo Bonzini #define L0_TCON 0x320
1043bd88451SPaolo Bonzini #define L0_INT_CSTAT 0x330
1053bd88451SPaolo Bonzini #define L0_INT_ENB 0x334
1063bd88451SPaolo Bonzini #define L0_WSTAT 0x340
1073bd88451SPaolo Bonzini #define L1_TCNTB 0x400
1083bd88451SPaolo Bonzini #define L1_TCNTO 0x404
1093bd88451SPaolo Bonzini #define L1_ICNTB 0x408
1103bd88451SPaolo Bonzini #define L1_ICNTO 0x40C
1113bd88451SPaolo Bonzini #define L1_FRCNTB 0x410
1123bd88451SPaolo Bonzini #define L1_FRCNTO 0x414
1133bd88451SPaolo Bonzini #define L1_TCON 0x420
1143bd88451SPaolo Bonzini #define L1_INT_CSTAT 0x430
1153bd88451SPaolo Bonzini #define L1_INT_ENB 0x434
1163bd88451SPaolo Bonzini #define L1_WSTAT 0x440
1173bd88451SPaolo Bonzini
1183bd88451SPaolo Bonzini #define MCT_CFG_GET_PRESCALER(x) ((x) & 0xFF)
1193bd88451SPaolo Bonzini #define MCT_CFG_GET_DIVIDER(x) (1 << ((x) >> 8 & 7))
1203bd88451SPaolo Bonzini
1213bd88451SPaolo Bonzini #define GET_G_COMP_IDX(offset) (((offset) - G_COMP0_L) / 0x10)
1223bd88451SPaolo Bonzini #define GET_G_COMP_ADD_INCR_IDX(offset) (((offset) - G_COMP0_ADD_INCR) / 0x10)
1233bd88451SPaolo Bonzini
1243bd88451SPaolo Bonzini #define G_COMP_L(x) (G_COMP0_L + (x) * 0x10)
1253bd88451SPaolo Bonzini #define G_COMP_U(x) (G_COMP0_U + (x) * 0x10)
1263bd88451SPaolo Bonzini
1273bd88451SPaolo Bonzini #define G_COMP_ADD_INCR(x) (G_COMP0_ADD_INCR + (x) * 0x10)
1283bd88451SPaolo Bonzini
1293bd88451SPaolo Bonzini /* MCT bits */
1303bd88451SPaolo Bonzini #define G_TCON_COMP_ENABLE(x) (1 << 2 * (x))
1313bd88451SPaolo Bonzini #define G_TCON_AUTO_ICREMENT(x) (1 << (2 * (x) + 1))
1323bd88451SPaolo Bonzini #define G_TCON_TIMER_ENABLE (1 << 8)
1333bd88451SPaolo Bonzini
1343bd88451SPaolo Bonzini #define G_INT_ENABLE(x) (1 << (x))
1353bd88451SPaolo Bonzini #define G_INT_CSTAT_COMP(x) (1 << (x))
1363bd88451SPaolo Bonzini
1373bd88451SPaolo Bonzini #define G_CNT_WSTAT_L 1
1383bd88451SPaolo Bonzini #define G_CNT_WSTAT_U 2
1393bd88451SPaolo Bonzini
1403bd88451SPaolo Bonzini #define G_WSTAT_COMP_L(x) (1 << 4 * (x))
1413bd88451SPaolo Bonzini #define G_WSTAT_COMP_U(x) (1 << ((4 * (x)) + 1))
1423bd88451SPaolo Bonzini #define G_WSTAT_COMP_ADDINCR(x) (1 << ((4 * (x)) + 2))
1433bd88451SPaolo Bonzini #define G_WSTAT_TCON_WRITE (1 << 16)
1443bd88451SPaolo Bonzini
1453bd88451SPaolo Bonzini #define GET_L_TIMER_IDX(offset) ((((offset) & 0xF00) - L0_TCNTB) / 0x100)
1463bd88451SPaolo Bonzini #define GET_L_TIMER_CNT_REG_IDX(offset, lt_i) \
1473bd88451SPaolo Bonzini (((offset) - (L0_TCNTB + 0x100 * (lt_i))) >> 2)
1483bd88451SPaolo Bonzini
1493bd88451SPaolo Bonzini #define L_ICNTB_MANUAL_UPDATE (1 << 31)
1503bd88451SPaolo Bonzini
1513bd88451SPaolo Bonzini #define L_TCON_TICK_START (1)
1523bd88451SPaolo Bonzini #define L_TCON_INT_START (1 << 1)
1533bd88451SPaolo Bonzini #define L_TCON_INTERVAL_MODE (1 << 2)
1543bd88451SPaolo Bonzini #define L_TCON_FRC_START (1 << 3)
1553bd88451SPaolo Bonzini
1563bd88451SPaolo Bonzini #define L_INT_CSTAT_INTCNT (1 << 0)
1573bd88451SPaolo Bonzini #define L_INT_CSTAT_FRCCNT (1 << 1)
1583bd88451SPaolo Bonzini
1593bd88451SPaolo Bonzini #define L_INT_INTENB_ICNTEIE (1 << 0)
1603bd88451SPaolo Bonzini #define L_INT_INTENB_FRCEIE (1 << 1)
1613bd88451SPaolo Bonzini
1623bd88451SPaolo Bonzini #define L_WSTAT_TCNTB_WRITE (1 << 0)
1633bd88451SPaolo Bonzini #define L_WSTAT_ICNTB_WRITE (1 << 1)
1643bd88451SPaolo Bonzini #define L_WSTAT_FRCCNTB_WRITE (1 << 2)
1653bd88451SPaolo Bonzini #define L_WSTAT_TCON_WRITE (1 << 3)
1663bd88451SPaolo Bonzini
1673bd88451SPaolo Bonzini enum LocalTimerRegCntIndexes {
1683bd88451SPaolo Bonzini L_REG_CNT_TCNTB,
1693bd88451SPaolo Bonzini L_REG_CNT_TCNTO,
1703bd88451SPaolo Bonzini L_REG_CNT_ICNTB,
1713bd88451SPaolo Bonzini L_REG_CNT_ICNTO,
1723bd88451SPaolo Bonzini L_REG_CNT_FRCCNTB,
1733bd88451SPaolo Bonzini L_REG_CNT_FRCCNTO,
1743bd88451SPaolo Bonzini
1753bd88451SPaolo Bonzini L_REG_CNT_AMOUNT
1763bd88451SPaolo Bonzini };
1773bd88451SPaolo Bonzini
1783bd88451SPaolo Bonzini #define MCT_SFR_SIZE 0x444
1793bd88451SPaolo Bonzini
1803bd88451SPaolo Bonzini #define MCT_GT_CMP_NUM 4
1813bd88451SPaolo Bonzini
1823bd88451SPaolo Bonzini #define MCT_GT_COUNTER_STEP 0x100000000ULL
1833bd88451SPaolo Bonzini #define MCT_LT_COUNTER_STEP 0x100000000ULL
1843bd88451SPaolo Bonzini #define MCT_LT_CNT_LOW_LIMIT 0x100
1853bd88451SPaolo Bonzini
1863bd88451SPaolo Bonzini /* global timer */
1873bd88451SPaolo Bonzini typedef struct {
1883bd88451SPaolo Bonzini qemu_irq irq[MCT_GT_CMP_NUM];
1893bd88451SPaolo Bonzini
1903bd88451SPaolo Bonzini struct gregs {
1913bd88451SPaolo Bonzini uint64_t cnt;
1923bd88451SPaolo Bonzini uint32_t cnt_wstat;
1933bd88451SPaolo Bonzini uint32_t tcon;
1943bd88451SPaolo Bonzini uint32_t int_cstat;
1953bd88451SPaolo Bonzini uint32_t int_enb;
1963bd88451SPaolo Bonzini uint32_t wstat;
1973bd88451SPaolo Bonzini uint64_t comp[MCT_GT_CMP_NUM];
1983bd88451SPaolo Bonzini uint32_t comp_add_incr[MCT_GT_CMP_NUM];
1993bd88451SPaolo Bonzini } reg;
2003bd88451SPaolo Bonzini
2013bd88451SPaolo Bonzini uint64_t count; /* Value FRC was armed with */
2023bd88451SPaolo Bonzini int32_t curr_comp; /* Current comparator FRC is running to */
2033bd88451SPaolo Bonzini
2043bd88451SPaolo Bonzini ptimer_state *ptimer_frc; /* FRC timer */
2053bd88451SPaolo Bonzini
2063bd88451SPaolo Bonzini } Exynos4210MCTGT;
2073bd88451SPaolo Bonzini
2083bd88451SPaolo Bonzini /* local timer */
2093bd88451SPaolo Bonzini typedef struct {
2103bd88451SPaolo Bonzini int id; /* timer id */
2113bd88451SPaolo Bonzini qemu_irq irq; /* local timer irq */
2123bd88451SPaolo Bonzini
2133bd88451SPaolo Bonzini struct tick_timer {
2143bd88451SPaolo Bonzini uint32_t cnt_run; /* cnt timer is running */
2153bd88451SPaolo Bonzini uint32_t int_run; /* int timer is running */
2163bd88451SPaolo Bonzini
2173bd88451SPaolo Bonzini uint32_t last_icnto;
2183bd88451SPaolo Bonzini uint32_t last_tcnto;
2193bd88451SPaolo Bonzini uint32_t tcntb; /* initial value for TCNTB */
2203bd88451SPaolo Bonzini uint32_t icntb; /* initial value for ICNTB */
2213bd88451SPaolo Bonzini
2223bd88451SPaolo Bonzini /* for step mode */
2233bd88451SPaolo Bonzini uint64_t distance; /* distance to count to the next event */
2243bd88451SPaolo Bonzini uint64_t progress; /* progress when counting by steps */
2253bd88451SPaolo Bonzini uint64_t count; /* count to arm timer with */
2263bd88451SPaolo Bonzini
2273bd88451SPaolo Bonzini ptimer_state *ptimer_tick; /* timer for tick counter */
2283bd88451SPaolo Bonzini } tick_timer;
2293bd88451SPaolo Bonzini
2303bd88451SPaolo Bonzini /* use ptimer.c to represent count down timer */
2313bd88451SPaolo Bonzini
2323bd88451SPaolo Bonzini ptimer_state *ptimer_frc; /* timer for free running counter */
2333bd88451SPaolo Bonzini
2343bd88451SPaolo Bonzini /* registers */
2353bd88451SPaolo Bonzini struct lregs {
2363bd88451SPaolo Bonzini uint32_t cnt[L_REG_CNT_AMOUNT];
2373bd88451SPaolo Bonzini uint32_t tcon;
2383bd88451SPaolo Bonzini uint32_t int_cstat;
2393bd88451SPaolo Bonzini uint32_t int_enb;
2403bd88451SPaolo Bonzini uint32_t wstat;
2413bd88451SPaolo Bonzini } reg;
2423bd88451SPaolo Bonzini
2433bd88451SPaolo Bonzini } Exynos4210MCTLT;
2443bd88451SPaolo Bonzini
24581e1010dSAndreas Färber #define TYPE_EXYNOS4210_MCT "exynos4210.mct"
2468063396bSEduardo Habkost OBJECT_DECLARE_SIMPLE_TYPE(Exynos4210MCTState, EXYNOS4210_MCT)
24781e1010dSAndreas Färber
248db1015e9SEduardo Habkost struct Exynos4210MCTState {
24981e1010dSAndreas Färber SysBusDevice parent_obj;
25081e1010dSAndreas Färber
2513bd88451SPaolo Bonzini MemoryRegion iomem;
2523bd88451SPaolo Bonzini
2533bd88451SPaolo Bonzini /* Registers */
2543bd88451SPaolo Bonzini uint32_t reg_mct_cfg;
2553bd88451SPaolo Bonzini
2563bd88451SPaolo Bonzini Exynos4210MCTLT l_timer[2];
2573bd88451SPaolo Bonzini Exynos4210MCTGT g_timer;
2583bd88451SPaolo Bonzini
2593bd88451SPaolo Bonzini uint32_t freq; /* all timers tick frequency, TCLK */
260db1015e9SEduardo Habkost };
2613bd88451SPaolo Bonzini
2623bd88451SPaolo Bonzini /*** VMState ***/
2633bd88451SPaolo Bonzini static const VMStateDescription vmstate_tick_timer = {
2643bd88451SPaolo Bonzini .name = "exynos4210.mct.tick_timer",
2653bd88451SPaolo Bonzini .version_id = 1,
2663bd88451SPaolo Bonzini .minimum_version_id = 1,
267ba324b3fSRichard Henderson .fields = (const VMStateField[]) {
2683bd88451SPaolo Bonzini VMSTATE_UINT32(cnt_run, struct tick_timer),
2693bd88451SPaolo Bonzini VMSTATE_UINT32(int_run, struct tick_timer),
2703bd88451SPaolo Bonzini VMSTATE_UINT32(last_icnto, struct tick_timer),
2713bd88451SPaolo Bonzini VMSTATE_UINT32(last_tcnto, struct tick_timer),
2723bd88451SPaolo Bonzini VMSTATE_UINT32(tcntb, struct tick_timer),
2733bd88451SPaolo Bonzini VMSTATE_UINT32(icntb, struct tick_timer),
2743bd88451SPaolo Bonzini VMSTATE_UINT64(distance, struct tick_timer),
2753bd88451SPaolo Bonzini VMSTATE_UINT64(progress, struct tick_timer),
2763bd88451SPaolo Bonzini VMSTATE_UINT64(count, struct tick_timer),
2773bd88451SPaolo Bonzini VMSTATE_PTIMER(ptimer_tick, struct tick_timer),
2783bd88451SPaolo Bonzini VMSTATE_END_OF_LIST()
2793bd88451SPaolo Bonzini }
2803bd88451SPaolo Bonzini };
2813bd88451SPaolo Bonzini
2823bd88451SPaolo Bonzini static const VMStateDescription vmstate_lregs = {
2833bd88451SPaolo Bonzini .name = "exynos4210.mct.lregs",
2843bd88451SPaolo Bonzini .version_id = 1,
2853bd88451SPaolo Bonzini .minimum_version_id = 1,
286ba324b3fSRichard Henderson .fields = (const VMStateField[]) {
2873bd88451SPaolo Bonzini VMSTATE_UINT32_ARRAY(cnt, struct lregs, L_REG_CNT_AMOUNT),
2883bd88451SPaolo Bonzini VMSTATE_UINT32(tcon, struct lregs),
2893bd88451SPaolo Bonzini VMSTATE_UINT32(int_cstat, struct lregs),
2903bd88451SPaolo Bonzini VMSTATE_UINT32(int_enb, struct lregs),
2913bd88451SPaolo Bonzini VMSTATE_UINT32(wstat, struct lregs),
2923bd88451SPaolo Bonzini VMSTATE_END_OF_LIST()
2933bd88451SPaolo Bonzini }
2943bd88451SPaolo Bonzini };
2953bd88451SPaolo Bonzini
2963bd88451SPaolo Bonzini static const VMStateDescription vmstate_exynos4210_mct_lt = {
2973bd88451SPaolo Bonzini .name = "exynos4210.mct.lt",
2983bd88451SPaolo Bonzini .version_id = 1,
2993bd88451SPaolo Bonzini .minimum_version_id = 1,
300ba324b3fSRichard Henderson .fields = (const VMStateField[]) {
3013bd88451SPaolo Bonzini VMSTATE_INT32(id, Exynos4210MCTLT),
3023bd88451SPaolo Bonzini VMSTATE_STRUCT(tick_timer, Exynos4210MCTLT, 0,
3033bd88451SPaolo Bonzini vmstate_tick_timer,
3043bd88451SPaolo Bonzini struct tick_timer),
3053bd88451SPaolo Bonzini VMSTATE_PTIMER(ptimer_frc, Exynos4210MCTLT),
3063bd88451SPaolo Bonzini VMSTATE_STRUCT(reg, Exynos4210MCTLT, 0,
3073bd88451SPaolo Bonzini vmstate_lregs,
3083bd88451SPaolo Bonzini struct lregs),
3093bd88451SPaolo Bonzini VMSTATE_END_OF_LIST()
3103bd88451SPaolo Bonzini }
3113bd88451SPaolo Bonzini };
3123bd88451SPaolo Bonzini
3133bd88451SPaolo Bonzini static const VMStateDescription vmstate_gregs = {
3143bd88451SPaolo Bonzini .name = "exynos4210.mct.lregs",
3153bd88451SPaolo Bonzini .version_id = 1,
3163bd88451SPaolo Bonzini .minimum_version_id = 1,
317ba324b3fSRichard Henderson .fields = (const VMStateField[]) {
3183bd88451SPaolo Bonzini VMSTATE_UINT64(cnt, struct gregs),
3193bd88451SPaolo Bonzini VMSTATE_UINT32(cnt_wstat, struct gregs),
3203bd88451SPaolo Bonzini VMSTATE_UINT32(tcon, struct gregs),
3213bd88451SPaolo Bonzini VMSTATE_UINT32(int_cstat, struct gregs),
3223bd88451SPaolo Bonzini VMSTATE_UINT32(int_enb, struct gregs),
3233bd88451SPaolo Bonzini VMSTATE_UINT32(wstat, struct gregs),
3243bd88451SPaolo Bonzini VMSTATE_UINT64_ARRAY(comp, struct gregs, MCT_GT_CMP_NUM),
3253bd88451SPaolo Bonzini VMSTATE_UINT32_ARRAY(comp_add_incr, struct gregs,
3263bd88451SPaolo Bonzini MCT_GT_CMP_NUM),
3273bd88451SPaolo Bonzini VMSTATE_END_OF_LIST()
3283bd88451SPaolo Bonzini }
3293bd88451SPaolo Bonzini };
3303bd88451SPaolo Bonzini
3313bd88451SPaolo Bonzini static const VMStateDescription vmstate_exynos4210_mct_gt = {
3323bd88451SPaolo Bonzini .name = "exynos4210.mct.lt",
3333bd88451SPaolo Bonzini .version_id = 1,
3343bd88451SPaolo Bonzini .minimum_version_id = 1,
335ba324b3fSRichard Henderson .fields = (const VMStateField[]) {
3363bd88451SPaolo Bonzini VMSTATE_STRUCT(reg, Exynos4210MCTGT, 0, vmstate_gregs,
3373bd88451SPaolo Bonzini struct gregs),
3383bd88451SPaolo Bonzini VMSTATE_UINT64(count, Exynos4210MCTGT),
3393bd88451SPaolo Bonzini VMSTATE_INT32(curr_comp, Exynos4210MCTGT),
3403bd88451SPaolo Bonzini VMSTATE_PTIMER(ptimer_frc, Exynos4210MCTGT),
3413bd88451SPaolo Bonzini VMSTATE_END_OF_LIST()
3423bd88451SPaolo Bonzini }
3433bd88451SPaolo Bonzini };
3443bd88451SPaolo Bonzini
3453bd88451SPaolo Bonzini static const VMStateDescription vmstate_exynos4210_mct_state = {
3463bd88451SPaolo Bonzini .name = "exynos4210.mct",
3473bd88451SPaolo Bonzini .version_id = 1,
3483bd88451SPaolo Bonzini .minimum_version_id = 1,
349ba324b3fSRichard Henderson .fields = (const VMStateField[]) {
3503bd88451SPaolo Bonzini VMSTATE_UINT32(reg_mct_cfg, Exynos4210MCTState),
3513bd88451SPaolo Bonzini VMSTATE_STRUCT_ARRAY(l_timer, Exynos4210MCTState, 2, 0,
3523bd88451SPaolo Bonzini vmstate_exynos4210_mct_lt, Exynos4210MCTLT),
3533bd88451SPaolo Bonzini VMSTATE_STRUCT(g_timer, Exynos4210MCTState, 0,
3543bd88451SPaolo Bonzini vmstate_exynos4210_mct_gt, Exynos4210MCTGT),
3553bd88451SPaolo Bonzini VMSTATE_UINT32(freq, Exynos4210MCTState),
3563bd88451SPaolo Bonzini VMSTATE_END_OF_LIST()
3573bd88451SPaolo Bonzini }
3583bd88451SPaolo Bonzini };
3593bd88451SPaolo Bonzini
3603bd88451SPaolo Bonzini static void exynos4210_mct_update_freq(Exynos4210MCTState *s);
3613bd88451SPaolo Bonzini
3623bd88451SPaolo Bonzini /*
3633bd88451SPaolo Bonzini * Set counter of FRC global timer.
3649ede4ec0SPeter Maydell * Must be called within exynos4210_gfrc_tx_begin/commit block.
3653bd88451SPaolo Bonzini */
exynos4210_gfrc_set_count(Exynos4210MCTGT * s,uint64_t count)3663bd88451SPaolo Bonzini static void exynos4210_gfrc_set_count(Exynos4210MCTGT *s, uint64_t count)
3673bd88451SPaolo Bonzini {
3683bd88451SPaolo Bonzini s->count = count;
3693bd88451SPaolo Bonzini DPRINTF("global timer frc set count 0x%llx\n", count);
3703bd88451SPaolo Bonzini ptimer_set_count(s->ptimer_frc, count);
3713bd88451SPaolo Bonzini }
3723bd88451SPaolo Bonzini
3733bd88451SPaolo Bonzini /*
3743bd88451SPaolo Bonzini * Get counter of FRC global timer.
3753bd88451SPaolo Bonzini */
exynos4210_gfrc_get_count(Exynos4210MCTGT * s)3763bd88451SPaolo Bonzini static uint64_t exynos4210_gfrc_get_count(Exynos4210MCTGT *s)
3773bd88451SPaolo Bonzini {
3783bd88451SPaolo Bonzini uint64_t count = 0;
3793bd88451SPaolo Bonzini count = ptimer_get_count(s->ptimer_frc);
3803bd88451SPaolo Bonzini count = s->count - count;
3813bd88451SPaolo Bonzini return s->reg.cnt + count;
3823bd88451SPaolo Bonzini }
3833bd88451SPaolo Bonzini
3843bd88451SPaolo Bonzini /*
3853bd88451SPaolo Bonzini * Stop global FRC timer
3869ede4ec0SPeter Maydell * Must be called within exynos4210_gfrc_tx_begin/commit block.
3873bd88451SPaolo Bonzini */
exynos4210_gfrc_stop(Exynos4210MCTGT * s)3883bd88451SPaolo Bonzini static void exynos4210_gfrc_stop(Exynos4210MCTGT *s)
3893bd88451SPaolo Bonzini {
3903bd88451SPaolo Bonzini DPRINTF("global timer frc stop\n");
3913bd88451SPaolo Bonzini
3923bd88451SPaolo Bonzini ptimer_stop(s->ptimer_frc);
3933bd88451SPaolo Bonzini }
3943bd88451SPaolo Bonzini
3953bd88451SPaolo Bonzini /*
3963bd88451SPaolo Bonzini * Start global FRC timer
3979ede4ec0SPeter Maydell * Must be called within exynos4210_gfrc_tx_begin/commit block.
3983bd88451SPaolo Bonzini */
exynos4210_gfrc_start(Exynos4210MCTGT * s)3993bd88451SPaolo Bonzini static void exynos4210_gfrc_start(Exynos4210MCTGT *s)
4003bd88451SPaolo Bonzini {
4013bd88451SPaolo Bonzini DPRINTF("global timer frc start\n");
4023bd88451SPaolo Bonzini
4033bd88451SPaolo Bonzini ptimer_run(s->ptimer_frc, 1);
4043bd88451SPaolo Bonzini }
4053bd88451SPaolo Bonzini
4063bd88451SPaolo Bonzini /*
4079ede4ec0SPeter Maydell * Start ptimer transaction for global FRC timer; this is just for
4089ede4ec0SPeter Maydell * consistency with the way we wrap operations like stop and run.
4099ede4ec0SPeter Maydell */
exynos4210_gfrc_tx_begin(Exynos4210MCTGT * s)4109ede4ec0SPeter Maydell static void exynos4210_gfrc_tx_begin(Exynos4210MCTGT *s)
4119ede4ec0SPeter Maydell {
4129ede4ec0SPeter Maydell ptimer_transaction_begin(s->ptimer_frc);
4139ede4ec0SPeter Maydell }
4149ede4ec0SPeter Maydell
4159ede4ec0SPeter Maydell /* Commit ptimer transaction for global FRC timer. */
exynos4210_gfrc_tx_commit(Exynos4210MCTGT * s)4169ede4ec0SPeter Maydell static void exynos4210_gfrc_tx_commit(Exynos4210MCTGT *s)
4179ede4ec0SPeter Maydell {
4189ede4ec0SPeter Maydell ptimer_transaction_commit(s->ptimer_frc);
4199ede4ec0SPeter Maydell }
4209ede4ec0SPeter Maydell
4219ede4ec0SPeter Maydell /*
4223bd88451SPaolo Bonzini * Find next nearest Comparator. If current Comparator value equals to other
4233bd88451SPaolo Bonzini * Comparator value, skip them both
4243bd88451SPaolo Bonzini */
exynos4210_gcomp_find(Exynos4210MCTState * s)4253bd88451SPaolo Bonzini static int32_t exynos4210_gcomp_find(Exynos4210MCTState *s)
4263bd88451SPaolo Bonzini {
4273bd88451SPaolo Bonzini int res;
4283bd88451SPaolo Bonzini int i;
4293bd88451SPaolo Bonzini int enabled;
4303bd88451SPaolo Bonzini uint64_t min;
4313bd88451SPaolo Bonzini int min_comp_i;
4323bd88451SPaolo Bonzini uint64_t gfrc;
4333bd88451SPaolo Bonzini uint64_t distance;
4343bd88451SPaolo Bonzini uint64_t distance_min;
4353bd88451SPaolo Bonzini int comp_i;
4363bd88451SPaolo Bonzini
4373bd88451SPaolo Bonzini /* get gfrc count */
4383bd88451SPaolo Bonzini gfrc = exynos4210_gfrc_get_count(&s->g_timer);
4393bd88451SPaolo Bonzini
4403bd88451SPaolo Bonzini min = UINT64_MAX;
4413bd88451SPaolo Bonzini distance_min = UINT64_MAX;
4423bd88451SPaolo Bonzini comp_i = MCT_GT_CMP_NUM;
4433bd88451SPaolo Bonzini min_comp_i = MCT_GT_CMP_NUM;
4443bd88451SPaolo Bonzini enabled = 0;
4453bd88451SPaolo Bonzini
4463bd88451SPaolo Bonzini /* lookup for nearest comparator */
4473bd88451SPaolo Bonzini for (i = 0; i < MCT_GT_CMP_NUM; i++) {
4483bd88451SPaolo Bonzini
4493bd88451SPaolo Bonzini if (s->g_timer.reg.tcon & G_TCON_COMP_ENABLE(i)) {
4503bd88451SPaolo Bonzini
4513bd88451SPaolo Bonzini enabled = 1;
4523bd88451SPaolo Bonzini
4533bd88451SPaolo Bonzini if (s->g_timer.reg.comp[i] > gfrc) {
4543bd88451SPaolo Bonzini /* Comparator is upper then FRC */
4553bd88451SPaolo Bonzini distance = s->g_timer.reg.comp[i] - gfrc;
4563bd88451SPaolo Bonzini
4573bd88451SPaolo Bonzini if (distance <= distance_min) {
4583bd88451SPaolo Bonzini distance_min = distance;
4593bd88451SPaolo Bonzini comp_i = i;
4603bd88451SPaolo Bonzini }
4613bd88451SPaolo Bonzini } else {
4623bd88451SPaolo Bonzini /* Comparator is below FRC, find the smallest */
4633bd88451SPaolo Bonzini
4643bd88451SPaolo Bonzini if (s->g_timer.reg.comp[i] <= min) {
4653bd88451SPaolo Bonzini min = s->g_timer.reg.comp[i];
4663bd88451SPaolo Bonzini min_comp_i = i;
4673bd88451SPaolo Bonzini }
4683bd88451SPaolo Bonzini }
4693bd88451SPaolo Bonzini }
4703bd88451SPaolo Bonzini }
4713bd88451SPaolo Bonzini
4723bd88451SPaolo Bonzini if (!enabled) {
4733bd88451SPaolo Bonzini /* All Comparators disabled */
4743bd88451SPaolo Bonzini res = -1;
4753bd88451SPaolo Bonzini } else if (comp_i < MCT_GT_CMP_NUM) {
4763bd88451SPaolo Bonzini /* Found upper Comparator */
4773bd88451SPaolo Bonzini res = comp_i;
4783bd88451SPaolo Bonzini } else {
4793bd88451SPaolo Bonzini /* All Comparators are below or equal to FRC */
4803bd88451SPaolo Bonzini res = min_comp_i;
4813bd88451SPaolo Bonzini }
4823bd88451SPaolo Bonzini
483c47a80cdSFeng Jiang if (res >= 0) {
484c47a80cdSFeng Jiang DPRINTF("found comparator %d: "
485c47a80cdSFeng Jiang "comp 0x%llx distance 0x%llx, gfrc 0x%llx\n",
4863bd88451SPaolo Bonzini res,
4873bd88451SPaolo Bonzini s->g_timer.reg.comp[res],
4883bd88451SPaolo Bonzini distance_min,
4893bd88451SPaolo Bonzini gfrc);
490c47a80cdSFeng Jiang }
4913bd88451SPaolo Bonzini
4923bd88451SPaolo Bonzini return res;
4933bd88451SPaolo Bonzini }
4943bd88451SPaolo Bonzini
4953bd88451SPaolo Bonzini /*
4963bd88451SPaolo Bonzini * Get distance to nearest Comparator
4973bd88451SPaolo Bonzini */
exynos4210_gcomp_get_distance(Exynos4210MCTState * s,int32_t id)4983bd88451SPaolo Bonzini static uint64_t exynos4210_gcomp_get_distance(Exynos4210MCTState *s, int32_t id)
4993bd88451SPaolo Bonzini {
5003bd88451SPaolo Bonzini if (id == -1) {
5013bd88451SPaolo Bonzini /* no enabled Comparators, choose max distance */
5023bd88451SPaolo Bonzini return MCT_GT_COUNTER_STEP;
5033bd88451SPaolo Bonzini }
5043bd88451SPaolo Bonzini if (s->g_timer.reg.comp[id] - s->g_timer.reg.cnt < MCT_GT_COUNTER_STEP) {
5053bd88451SPaolo Bonzini return s->g_timer.reg.comp[id] - s->g_timer.reg.cnt;
5063bd88451SPaolo Bonzini } else {
5073bd88451SPaolo Bonzini return MCT_GT_COUNTER_STEP;
5083bd88451SPaolo Bonzini }
5093bd88451SPaolo Bonzini }
5103bd88451SPaolo Bonzini
5113bd88451SPaolo Bonzini /*
5123bd88451SPaolo Bonzini * Restart global FRC timer
5139ede4ec0SPeter Maydell * Must be called within exynos4210_gfrc_tx_begin/commit block.
5143bd88451SPaolo Bonzini */
exynos4210_gfrc_restart(Exynos4210MCTState * s)5153bd88451SPaolo Bonzini static void exynos4210_gfrc_restart(Exynos4210MCTState *s)
5163bd88451SPaolo Bonzini {
5173bd88451SPaolo Bonzini uint64_t distance;
5183bd88451SPaolo Bonzini
5193bd88451SPaolo Bonzini exynos4210_gfrc_stop(&s->g_timer);
5203bd88451SPaolo Bonzini
5213bd88451SPaolo Bonzini s->g_timer.curr_comp = exynos4210_gcomp_find(s);
5223bd88451SPaolo Bonzini
5233bd88451SPaolo Bonzini distance = exynos4210_gcomp_get_distance(s, s->g_timer.curr_comp);
5243bd88451SPaolo Bonzini
5253bd88451SPaolo Bonzini if (distance > MCT_GT_COUNTER_STEP || !distance) {
5263bd88451SPaolo Bonzini distance = MCT_GT_COUNTER_STEP;
5273bd88451SPaolo Bonzini }
5283bd88451SPaolo Bonzini
5293bd88451SPaolo Bonzini exynos4210_gfrc_set_count(&s->g_timer, distance);
5303bd88451SPaolo Bonzini exynos4210_gfrc_start(&s->g_timer);
5313bd88451SPaolo Bonzini }
5323bd88451SPaolo Bonzini
5333bd88451SPaolo Bonzini /*
5343bd88451SPaolo Bonzini * Raise global timer CMP IRQ
5353bd88451SPaolo Bonzini */
exynos4210_gcomp_raise_irq(void * opaque,uint32_t id)5363bd88451SPaolo Bonzini static void exynos4210_gcomp_raise_irq(void *opaque, uint32_t id)
5373bd88451SPaolo Bonzini {
5383bd88451SPaolo Bonzini Exynos4210MCTGT *s = opaque;
5393bd88451SPaolo Bonzini
5403bd88451SPaolo Bonzini /* If CSTAT is pending and IRQ is enabled */
5413bd88451SPaolo Bonzini if ((s->reg.int_cstat & G_INT_CSTAT_COMP(id)) &&
5423bd88451SPaolo Bonzini (s->reg.int_enb & G_INT_ENABLE(id))) {
54363192565SAlex Chen DPRINTF("gcmp timer[%u] IRQ\n", id);
5443bd88451SPaolo Bonzini qemu_irq_raise(s->irq[id]);
5453bd88451SPaolo Bonzini }
5463bd88451SPaolo Bonzini }
5473bd88451SPaolo Bonzini
5483bd88451SPaolo Bonzini /*
5493bd88451SPaolo Bonzini * Lower global timer CMP IRQ
5503bd88451SPaolo Bonzini */
exynos4210_gcomp_lower_irq(void * opaque,uint32_t id)5513bd88451SPaolo Bonzini static void exynos4210_gcomp_lower_irq(void *opaque, uint32_t id)
5523bd88451SPaolo Bonzini {
5533bd88451SPaolo Bonzini Exynos4210MCTGT *s = opaque;
5543bd88451SPaolo Bonzini qemu_irq_lower(s->irq[id]);
5553bd88451SPaolo Bonzini }
5563bd88451SPaolo Bonzini
5573bd88451SPaolo Bonzini /*
5583bd88451SPaolo Bonzini * Global timer FRC event handler.
5593bd88451SPaolo Bonzini * Each event occurs when internal counter reaches counter + MCT_GT_COUNTER_STEP
5603bd88451SPaolo Bonzini * Every time we arm global FRC timer to count for MCT_GT_COUNTER_STEP value
5613bd88451SPaolo Bonzini */
exynos4210_gfrc_event(void * opaque)5623bd88451SPaolo Bonzini static void exynos4210_gfrc_event(void *opaque)
5633bd88451SPaolo Bonzini {
5643bd88451SPaolo Bonzini Exynos4210MCTState *s = (Exynos4210MCTState *)opaque;
5653bd88451SPaolo Bonzini int i;
5663bd88451SPaolo Bonzini uint64_t distance;
5673bd88451SPaolo Bonzini
5683bd88451SPaolo Bonzini DPRINTF("\n");
5693bd88451SPaolo Bonzini
5703bd88451SPaolo Bonzini s->g_timer.reg.cnt += s->g_timer.count;
5713bd88451SPaolo Bonzini
5723bd88451SPaolo Bonzini /* Process all comparators */
5733bd88451SPaolo Bonzini for (i = 0; i < MCT_GT_CMP_NUM; i++) {
5743bd88451SPaolo Bonzini
5753bd88451SPaolo Bonzini if (s->g_timer.reg.cnt == s->g_timer.reg.comp[i]) {
5763bd88451SPaolo Bonzini /* reached nearest comparator */
5773bd88451SPaolo Bonzini
5783bd88451SPaolo Bonzini s->g_timer.reg.int_cstat |= G_INT_CSTAT_COMP(i);
5793bd88451SPaolo Bonzini
5803bd88451SPaolo Bonzini /* Auto increment */
5813bd88451SPaolo Bonzini if (s->g_timer.reg.tcon & G_TCON_AUTO_ICREMENT(i)) {
5823bd88451SPaolo Bonzini s->g_timer.reg.comp[i] += s->g_timer.reg.comp_add_incr[i];
5833bd88451SPaolo Bonzini }
5843bd88451SPaolo Bonzini
5853bd88451SPaolo Bonzini /* IRQ */
5863bd88451SPaolo Bonzini exynos4210_gcomp_raise_irq(&s->g_timer, i);
5873bd88451SPaolo Bonzini }
5883bd88451SPaolo Bonzini }
5893bd88451SPaolo Bonzini
5903bd88451SPaolo Bonzini /* Reload FRC to reach nearest comparator */
5913bd88451SPaolo Bonzini s->g_timer.curr_comp = exynos4210_gcomp_find(s);
5923bd88451SPaolo Bonzini distance = exynos4210_gcomp_get_distance(s, s->g_timer.curr_comp);
5933bd88451SPaolo Bonzini if (distance > MCT_GT_COUNTER_STEP || !distance) {
5943bd88451SPaolo Bonzini distance = MCT_GT_COUNTER_STEP;
5953bd88451SPaolo Bonzini }
5963bd88451SPaolo Bonzini exynos4210_gfrc_set_count(&s->g_timer, distance);
5973bd88451SPaolo Bonzini
5983bd88451SPaolo Bonzini exynos4210_gfrc_start(&s->g_timer);
5993bd88451SPaolo Bonzini }
6003bd88451SPaolo Bonzini
6013bd88451SPaolo Bonzini /*
6023bd88451SPaolo Bonzini * Get counter of FRC local timer.
6033bd88451SPaolo Bonzini */
exynos4210_lfrc_get_count(Exynos4210MCTLT * s)6043bd88451SPaolo Bonzini static uint64_t exynos4210_lfrc_get_count(Exynos4210MCTLT *s)
6053bd88451SPaolo Bonzini {
6063bd88451SPaolo Bonzini return ptimer_get_count(s->ptimer_frc);
6073bd88451SPaolo Bonzini }
6083bd88451SPaolo Bonzini
6093bd88451SPaolo Bonzini /*
6103bd88451SPaolo Bonzini * Set counter of FRC local timer.
61150f07d76SPeter Maydell * Must be called from within exynos4210_lfrc_tx_begin/commit block.
6123bd88451SPaolo Bonzini */
exynos4210_lfrc_update_count(Exynos4210MCTLT * s)6133bd88451SPaolo Bonzini static void exynos4210_lfrc_update_count(Exynos4210MCTLT *s)
6143bd88451SPaolo Bonzini {
6153bd88451SPaolo Bonzini if (!s->reg.cnt[L_REG_CNT_FRCCNTB]) {
6163bd88451SPaolo Bonzini ptimer_set_count(s->ptimer_frc, MCT_LT_COUNTER_STEP);
6173bd88451SPaolo Bonzini } else {
6183bd88451SPaolo Bonzini ptimer_set_count(s->ptimer_frc, s->reg.cnt[L_REG_CNT_FRCCNTB]);
6193bd88451SPaolo Bonzini }
6203bd88451SPaolo Bonzini }
6213bd88451SPaolo Bonzini
6223bd88451SPaolo Bonzini /*
6233bd88451SPaolo Bonzini * Start local FRC timer
62450f07d76SPeter Maydell * Must be called from within exynos4210_lfrc_tx_begin/commit block.
6253bd88451SPaolo Bonzini */
exynos4210_lfrc_start(Exynos4210MCTLT * s)6263bd88451SPaolo Bonzini static void exynos4210_lfrc_start(Exynos4210MCTLT *s)
6273bd88451SPaolo Bonzini {
6283bd88451SPaolo Bonzini ptimer_run(s->ptimer_frc, 1);
6293bd88451SPaolo Bonzini }
6303bd88451SPaolo Bonzini
6313bd88451SPaolo Bonzini /*
6323bd88451SPaolo Bonzini * Stop local FRC timer
63350f07d76SPeter Maydell * Must be called from within exynos4210_lfrc_tx_begin/commit block.
6343bd88451SPaolo Bonzini */
exynos4210_lfrc_stop(Exynos4210MCTLT * s)6353bd88451SPaolo Bonzini static void exynos4210_lfrc_stop(Exynos4210MCTLT *s)
6363bd88451SPaolo Bonzini {
6373bd88451SPaolo Bonzini ptimer_stop(s->ptimer_frc);
6383bd88451SPaolo Bonzini }
6393bd88451SPaolo Bonzini
64050f07d76SPeter Maydell /* Start ptimer transaction for local FRC timer */
exynos4210_lfrc_tx_begin(Exynos4210MCTLT * s)64150f07d76SPeter Maydell static void exynos4210_lfrc_tx_begin(Exynos4210MCTLT *s)
64250f07d76SPeter Maydell {
64350f07d76SPeter Maydell ptimer_transaction_begin(s->ptimer_frc);
64450f07d76SPeter Maydell }
64550f07d76SPeter Maydell
64650f07d76SPeter Maydell /* Commit ptimer transaction for local FRC timer */
exynos4210_lfrc_tx_commit(Exynos4210MCTLT * s)64750f07d76SPeter Maydell static void exynos4210_lfrc_tx_commit(Exynos4210MCTLT *s)
64850f07d76SPeter Maydell {
64950f07d76SPeter Maydell ptimer_transaction_commit(s->ptimer_frc);
65050f07d76SPeter Maydell }
65150f07d76SPeter Maydell
6523bd88451SPaolo Bonzini /*
6533bd88451SPaolo Bonzini * Local timer free running counter tick handler
6543bd88451SPaolo Bonzini */
exynos4210_lfrc_event(void * opaque)6553bd88451SPaolo Bonzini static void exynos4210_lfrc_event(void *opaque)
6563bd88451SPaolo Bonzini {
6573bd88451SPaolo Bonzini Exynos4210MCTLT * s = (Exynos4210MCTLT *)opaque;
6583bd88451SPaolo Bonzini
6593bd88451SPaolo Bonzini /* local frc expired */
6603bd88451SPaolo Bonzini
6613bd88451SPaolo Bonzini DPRINTF("\n");
6623bd88451SPaolo Bonzini
6633bd88451SPaolo Bonzini s->reg.int_cstat |= L_INT_CSTAT_FRCCNT;
6643bd88451SPaolo Bonzini
6653bd88451SPaolo Bonzini /* update frc counter */
6663bd88451SPaolo Bonzini exynos4210_lfrc_update_count(s);
6673bd88451SPaolo Bonzini
6683bd88451SPaolo Bonzini /* raise irq */
6693bd88451SPaolo Bonzini if (s->reg.int_enb & L_INT_INTENB_FRCEIE) {
6703bd88451SPaolo Bonzini qemu_irq_raise(s->irq);
6713bd88451SPaolo Bonzini }
6723bd88451SPaolo Bonzini
6733bd88451SPaolo Bonzini /* we reached here, this means that timer is enabled */
6743bd88451SPaolo Bonzini exynos4210_lfrc_start(s);
6753bd88451SPaolo Bonzini }
6763bd88451SPaolo Bonzini
6773bd88451SPaolo Bonzini static uint32_t exynos4210_ltick_int_get_cnto(struct tick_timer *s);
6783bd88451SPaolo Bonzini static uint32_t exynos4210_ltick_cnt_get_cnto(struct tick_timer *s);
6793bd88451SPaolo Bonzini static void exynos4210_ltick_recalc_count(struct tick_timer *s);
6803bd88451SPaolo Bonzini
6813bd88451SPaolo Bonzini /*
6823bd88451SPaolo Bonzini * Action on enabling local tick int timer
6833bd88451SPaolo Bonzini */
exynos4210_ltick_int_start(struct tick_timer * s)6843bd88451SPaolo Bonzini static void exynos4210_ltick_int_start(struct tick_timer *s)
6853bd88451SPaolo Bonzini {
6863bd88451SPaolo Bonzini if (!s->int_run) {
6873bd88451SPaolo Bonzini s->int_run = 1;
6883bd88451SPaolo Bonzini }
6893bd88451SPaolo Bonzini }
6903bd88451SPaolo Bonzini
6913bd88451SPaolo Bonzini /*
6923bd88451SPaolo Bonzini * Action on disabling local tick int timer
6933bd88451SPaolo Bonzini */
exynos4210_ltick_int_stop(struct tick_timer * s)6943bd88451SPaolo Bonzini static void exynos4210_ltick_int_stop(struct tick_timer *s)
6953bd88451SPaolo Bonzini {
6963bd88451SPaolo Bonzini if (s->int_run) {
6973bd88451SPaolo Bonzini s->last_icnto = exynos4210_ltick_int_get_cnto(s);
6983bd88451SPaolo Bonzini s->int_run = 0;
6993bd88451SPaolo Bonzini }
7003bd88451SPaolo Bonzini }
7013bd88451SPaolo Bonzini
7023bd88451SPaolo Bonzini /*
7033bd88451SPaolo Bonzini * Get count for INT timer
7043bd88451SPaolo Bonzini */
exynos4210_ltick_int_get_cnto(struct tick_timer * s)7053bd88451SPaolo Bonzini static uint32_t exynos4210_ltick_int_get_cnto(struct tick_timer *s)
7063bd88451SPaolo Bonzini {
7073bd88451SPaolo Bonzini uint32_t icnto;
7083bd88451SPaolo Bonzini uint64_t remain;
7093bd88451SPaolo Bonzini uint64_t count;
7103bd88451SPaolo Bonzini uint64_t counted;
7113bd88451SPaolo Bonzini uint64_t cur_progress;
7123bd88451SPaolo Bonzini
7133bd88451SPaolo Bonzini count = ptimer_get_count(s->ptimer_tick);
7143bd88451SPaolo Bonzini if (count) {
7153bd88451SPaolo Bonzini /* timer is still counting, called not from event */
7163bd88451SPaolo Bonzini counted = s->count - ptimer_get_count(s->ptimer_tick);
7173bd88451SPaolo Bonzini cur_progress = s->progress + counted;
7183bd88451SPaolo Bonzini } else {
7193bd88451SPaolo Bonzini /* timer expired earlier */
7203bd88451SPaolo Bonzini cur_progress = s->progress;
7213bd88451SPaolo Bonzini }
7223bd88451SPaolo Bonzini
7233bd88451SPaolo Bonzini remain = s->distance - cur_progress;
7243bd88451SPaolo Bonzini
7253bd88451SPaolo Bonzini if (!s->int_run) {
7263bd88451SPaolo Bonzini /* INT is stopped. */
7273bd88451SPaolo Bonzini icnto = s->last_icnto;
7283bd88451SPaolo Bonzini } else {
7293bd88451SPaolo Bonzini /* Both are counting */
7303bd88451SPaolo Bonzini icnto = remain / s->tcntb;
7313bd88451SPaolo Bonzini }
7323bd88451SPaolo Bonzini
7333bd88451SPaolo Bonzini return icnto;
7343bd88451SPaolo Bonzini }
7353bd88451SPaolo Bonzini
7363bd88451SPaolo Bonzini /*
7373bd88451SPaolo Bonzini * Start local tick cnt timer.
7386c27ee94SPeter Maydell * Must be called within exynos4210_ltick_tx_begin/commit block.
7393bd88451SPaolo Bonzini */
exynos4210_ltick_cnt_start(struct tick_timer * s)7403bd88451SPaolo Bonzini static void exynos4210_ltick_cnt_start(struct tick_timer *s)
7413bd88451SPaolo Bonzini {
7423bd88451SPaolo Bonzini if (!s->cnt_run) {
7433bd88451SPaolo Bonzini
7443bd88451SPaolo Bonzini exynos4210_ltick_recalc_count(s);
7453bd88451SPaolo Bonzini ptimer_set_count(s->ptimer_tick, s->count);
7463bd88451SPaolo Bonzini ptimer_run(s->ptimer_tick, 1);
7473bd88451SPaolo Bonzini
7483bd88451SPaolo Bonzini s->cnt_run = 1;
7493bd88451SPaolo Bonzini }
7503bd88451SPaolo Bonzini }
7513bd88451SPaolo Bonzini
7523bd88451SPaolo Bonzini /*
7533bd88451SPaolo Bonzini * Stop local tick cnt timer.
7546c27ee94SPeter Maydell * Must be called within exynos4210_ltick_tx_begin/commit block.
7553bd88451SPaolo Bonzini */
exynos4210_ltick_cnt_stop(struct tick_timer * s)7563bd88451SPaolo Bonzini static void exynos4210_ltick_cnt_stop(struct tick_timer *s)
7573bd88451SPaolo Bonzini {
7583bd88451SPaolo Bonzini if (s->cnt_run) {
7593bd88451SPaolo Bonzini
7603bd88451SPaolo Bonzini s->last_tcnto = exynos4210_ltick_cnt_get_cnto(s);
7613bd88451SPaolo Bonzini
7623bd88451SPaolo Bonzini if (s->int_run) {
7633bd88451SPaolo Bonzini exynos4210_ltick_int_stop(s);
7643bd88451SPaolo Bonzini }
7653bd88451SPaolo Bonzini
7663bd88451SPaolo Bonzini ptimer_stop(s->ptimer_tick);
7673bd88451SPaolo Bonzini
7683bd88451SPaolo Bonzini s->cnt_run = 0;
7693bd88451SPaolo Bonzini }
7703bd88451SPaolo Bonzini }
7713bd88451SPaolo Bonzini
7726c27ee94SPeter Maydell /* Start ptimer transaction for local tick timer */
exynos4210_ltick_tx_begin(struct tick_timer * s)7736c27ee94SPeter Maydell static void exynos4210_ltick_tx_begin(struct tick_timer *s)
7746c27ee94SPeter Maydell {
7756c27ee94SPeter Maydell ptimer_transaction_begin(s->ptimer_tick);
7766c27ee94SPeter Maydell }
7776c27ee94SPeter Maydell
7786c27ee94SPeter Maydell /* Commit ptimer transaction for local tick timer */
exynos4210_ltick_tx_commit(struct tick_timer * s)7796c27ee94SPeter Maydell static void exynos4210_ltick_tx_commit(struct tick_timer *s)
7806c27ee94SPeter Maydell {
7816c27ee94SPeter Maydell ptimer_transaction_commit(s->ptimer_tick);
7826c27ee94SPeter Maydell }
7836c27ee94SPeter Maydell
7843bd88451SPaolo Bonzini /*
7853bd88451SPaolo Bonzini * Get counter for CNT timer
7863bd88451SPaolo Bonzini */
exynos4210_ltick_cnt_get_cnto(struct tick_timer * s)7873bd88451SPaolo Bonzini static uint32_t exynos4210_ltick_cnt_get_cnto(struct tick_timer *s)
7883bd88451SPaolo Bonzini {
7893bd88451SPaolo Bonzini uint32_t tcnto;
7903bd88451SPaolo Bonzini uint32_t icnto;
7913bd88451SPaolo Bonzini uint64_t remain;
7923bd88451SPaolo Bonzini uint64_t counted;
7933bd88451SPaolo Bonzini uint64_t count;
7943bd88451SPaolo Bonzini uint64_t cur_progress;
7953bd88451SPaolo Bonzini
7963bd88451SPaolo Bonzini count = ptimer_get_count(s->ptimer_tick);
7973bd88451SPaolo Bonzini if (count) {
7983bd88451SPaolo Bonzini /* timer is still counting, called not from event */
7993bd88451SPaolo Bonzini counted = s->count - ptimer_get_count(s->ptimer_tick);
8003bd88451SPaolo Bonzini cur_progress = s->progress + counted;
8013bd88451SPaolo Bonzini } else {
8023bd88451SPaolo Bonzini /* timer expired earlier */
8033bd88451SPaolo Bonzini cur_progress = s->progress;
8043bd88451SPaolo Bonzini }
8053bd88451SPaolo Bonzini
8063bd88451SPaolo Bonzini remain = s->distance - cur_progress;
8073bd88451SPaolo Bonzini
8083bd88451SPaolo Bonzini if (!s->cnt_run) {
8093bd88451SPaolo Bonzini /* Both are stopped. */
8103bd88451SPaolo Bonzini tcnto = s->last_tcnto;
8113bd88451SPaolo Bonzini } else if (!s->int_run) {
8123bd88451SPaolo Bonzini /* INT counter is stopped, progress is by CNT timer */
8133bd88451SPaolo Bonzini tcnto = remain % s->tcntb;
8143bd88451SPaolo Bonzini } else {
8153bd88451SPaolo Bonzini /* Both are counting */
8163bd88451SPaolo Bonzini icnto = remain / s->tcntb;
8173bd88451SPaolo Bonzini if (icnto) {
818*c5d36da7SDmitry Frolov tcnto = remain % ((uint64_t)icnto * s->tcntb);
8193bd88451SPaolo Bonzini } else {
8203bd88451SPaolo Bonzini tcnto = remain % s->tcntb;
8213bd88451SPaolo Bonzini }
8223bd88451SPaolo Bonzini }
8233bd88451SPaolo Bonzini
8243bd88451SPaolo Bonzini return tcnto;
8253bd88451SPaolo Bonzini }
8263bd88451SPaolo Bonzini
8273bd88451SPaolo Bonzini /*
8283bd88451SPaolo Bonzini * Set new values of counters for CNT and INT timers
8296c27ee94SPeter Maydell * Must be called within exynos4210_ltick_tx_begin/commit block.
8303bd88451SPaolo Bonzini */
exynos4210_ltick_set_cntb(struct tick_timer * s,uint32_t new_cnt,uint32_t new_int)8313bd88451SPaolo Bonzini static void exynos4210_ltick_set_cntb(struct tick_timer *s, uint32_t new_cnt,
8323bd88451SPaolo Bonzini uint32_t new_int)
8333bd88451SPaolo Bonzini {
8343bd88451SPaolo Bonzini uint32_t cnt_stopped = 0;
8353bd88451SPaolo Bonzini uint32_t int_stopped = 0;
8363bd88451SPaolo Bonzini
8373bd88451SPaolo Bonzini if (s->cnt_run) {
8383bd88451SPaolo Bonzini exynos4210_ltick_cnt_stop(s);
8393bd88451SPaolo Bonzini cnt_stopped = 1;
8403bd88451SPaolo Bonzini }
8413bd88451SPaolo Bonzini
8423bd88451SPaolo Bonzini if (s->int_run) {
8433bd88451SPaolo Bonzini exynos4210_ltick_int_stop(s);
8443bd88451SPaolo Bonzini int_stopped = 1;
8453bd88451SPaolo Bonzini }
8463bd88451SPaolo Bonzini
8473bd88451SPaolo Bonzini s->tcntb = new_cnt + 1;
8483bd88451SPaolo Bonzini s->icntb = new_int + 1;
8493bd88451SPaolo Bonzini
8503bd88451SPaolo Bonzini if (cnt_stopped) {
8513bd88451SPaolo Bonzini exynos4210_ltick_cnt_start(s);
8523bd88451SPaolo Bonzini }
8533bd88451SPaolo Bonzini if (int_stopped) {
8543bd88451SPaolo Bonzini exynos4210_ltick_int_start(s);
8553bd88451SPaolo Bonzini }
8563bd88451SPaolo Bonzini
8573bd88451SPaolo Bonzini }
8583bd88451SPaolo Bonzini
8593bd88451SPaolo Bonzini /*
8603bd88451SPaolo Bonzini * Calculate new counter value for tick timer
8613bd88451SPaolo Bonzini */
exynos4210_ltick_recalc_count(struct tick_timer * s)8623bd88451SPaolo Bonzini static void exynos4210_ltick_recalc_count(struct tick_timer *s)
8633bd88451SPaolo Bonzini {
8643bd88451SPaolo Bonzini uint64_t to_count;
8653bd88451SPaolo Bonzini
8663bd88451SPaolo Bonzini if ((s->cnt_run && s->last_tcnto) || (s->int_run && s->last_icnto)) {
8673bd88451SPaolo Bonzini /*
8683bd88451SPaolo Bonzini * one or both timers run and not counted to the end;
8693bd88451SPaolo Bonzini * distance is not passed, recalculate with last_tcnto * last_icnto
8703bd88451SPaolo Bonzini */
8713bd88451SPaolo Bonzini
8723bd88451SPaolo Bonzini if (s->last_tcnto) {
873c3a699beSPeter Maydell to_count = (uint64_t)s->last_tcnto * s->last_icnto;
8743bd88451SPaolo Bonzini } else {
8753bd88451SPaolo Bonzini to_count = s->last_icnto;
8763bd88451SPaolo Bonzini }
8773bd88451SPaolo Bonzini } else {
8783bd88451SPaolo Bonzini /* distance is passed, recalculate with tcnto * icnto */
8793bd88451SPaolo Bonzini if (s->icntb) {
880c3a699beSPeter Maydell s->distance = (uint64_t)s->tcntb * s->icntb;
8813bd88451SPaolo Bonzini } else {
8823bd88451SPaolo Bonzini s->distance = s->tcntb;
8833bd88451SPaolo Bonzini }
8843bd88451SPaolo Bonzini
8853bd88451SPaolo Bonzini to_count = s->distance;
8863bd88451SPaolo Bonzini s->progress = 0;
8873bd88451SPaolo Bonzini }
8883bd88451SPaolo Bonzini
8893bd88451SPaolo Bonzini if (to_count > MCT_LT_COUNTER_STEP) {
8903bd88451SPaolo Bonzini /* count by step */
8913bd88451SPaolo Bonzini s->count = MCT_LT_COUNTER_STEP;
8923bd88451SPaolo Bonzini } else {
8933bd88451SPaolo Bonzini s->count = to_count;
8943bd88451SPaolo Bonzini }
8953bd88451SPaolo Bonzini }
8963bd88451SPaolo Bonzini
8973bd88451SPaolo Bonzini /*
8983bd88451SPaolo Bonzini * Initialize tick_timer
8993bd88451SPaolo Bonzini */
exynos4210_ltick_timer_init(struct tick_timer * s)9003bd88451SPaolo Bonzini static void exynos4210_ltick_timer_init(struct tick_timer *s)
9013bd88451SPaolo Bonzini {
9023bd88451SPaolo Bonzini exynos4210_ltick_int_stop(s);
9036c27ee94SPeter Maydell exynos4210_ltick_tx_begin(s);
9043bd88451SPaolo Bonzini exynos4210_ltick_cnt_stop(s);
9056c27ee94SPeter Maydell exynos4210_ltick_tx_commit(s);
9063bd88451SPaolo Bonzini
9073bd88451SPaolo Bonzini s->count = 0;
9083bd88451SPaolo Bonzini s->distance = 0;
9093bd88451SPaolo Bonzini s->progress = 0;
9103bd88451SPaolo Bonzini s->icntb = 0;
9113bd88451SPaolo Bonzini s->tcntb = 0;
9123bd88451SPaolo Bonzini }
9133bd88451SPaolo Bonzini
9143bd88451SPaolo Bonzini /*
9153bd88451SPaolo Bonzini * tick_timer event.
9163bd88451SPaolo Bonzini * Raises when abstract tick_timer expires.
9173bd88451SPaolo Bonzini */
exynos4210_ltick_timer_event(struct tick_timer * s)9183bd88451SPaolo Bonzini static void exynos4210_ltick_timer_event(struct tick_timer *s)
9193bd88451SPaolo Bonzini {
9203bd88451SPaolo Bonzini s->progress += s->count;
9213bd88451SPaolo Bonzini }
9223bd88451SPaolo Bonzini
9233bd88451SPaolo Bonzini /*
9243bd88451SPaolo Bonzini * Local timer tick counter handler.
9253bd88451SPaolo Bonzini * Don't use reloaded timers. If timer counter = zero
9263bd88451SPaolo Bonzini * then handler called but after handler finished no
9273bd88451SPaolo Bonzini * timer reload occurs.
9283bd88451SPaolo Bonzini */
exynos4210_ltick_event(void * opaque)9293bd88451SPaolo Bonzini static void exynos4210_ltick_event(void *opaque)
9303bd88451SPaolo Bonzini {
9313bd88451SPaolo Bonzini Exynos4210MCTLT * s = (Exynos4210MCTLT *)opaque;
9323bd88451SPaolo Bonzini uint32_t tcnto;
9333bd88451SPaolo Bonzini uint32_t icnto;
9343bd88451SPaolo Bonzini #ifdef DEBUG_MCT
9353bd88451SPaolo Bonzini static uint64_t time1[2] = {0};
9363bd88451SPaolo Bonzini static uint64_t time2[2] = {0};
9373bd88451SPaolo Bonzini #endif
9383bd88451SPaolo Bonzini
9393bd88451SPaolo Bonzini /* Call tick_timer event handler, it will update its tcntb and icntb. */
9403bd88451SPaolo Bonzini exynos4210_ltick_timer_event(&s->tick_timer);
9413bd88451SPaolo Bonzini
9423bd88451SPaolo Bonzini /* get tick_timer cnt */
9433bd88451SPaolo Bonzini tcnto = exynos4210_ltick_cnt_get_cnto(&s->tick_timer);
9443bd88451SPaolo Bonzini
9453bd88451SPaolo Bonzini /* get tick_timer int */
9463bd88451SPaolo Bonzini icnto = exynos4210_ltick_int_get_cnto(&s->tick_timer);
9473bd88451SPaolo Bonzini
9483bd88451SPaolo Bonzini /* raise IRQ if needed */
9493bd88451SPaolo Bonzini if (!icnto && s->reg.tcon & L_TCON_INT_START) {
9503bd88451SPaolo Bonzini /* INT counter enabled and expired */
9513bd88451SPaolo Bonzini
9523bd88451SPaolo Bonzini s->reg.int_cstat |= L_INT_CSTAT_INTCNT;
9533bd88451SPaolo Bonzini
9543bd88451SPaolo Bonzini /* raise interrupt if enabled */
9553bd88451SPaolo Bonzini if (s->reg.int_enb & L_INT_INTENB_ICNTEIE) {
9563bd88451SPaolo Bonzini #ifdef DEBUG_MCT
957bc72ad67SAlex Bligh time2[s->id] = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
9583bd88451SPaolo Bonzini DPRINTF("local timer[%d] IRQ: %llx\n", s->id,
9593bd88451SPaolo Bonzini time2[s->id] - time1[s->id]);
9603bd88451SPaolo Bonzini time1[s->id] = time2[s->id];
9613bd88451SPaolo Bonzini #endif
9623bd88451SPaolo Bonzini qemu_irq_raise(s->irq);
9633bd88451SPaolo Bonzini }
9643bd88451SPaolo Bonzini
9653bd88451SPaolo Bonzini /* reload ICNTB */
9663bd88451SPaolo Bonzini if (s->reg.tcon & L_TCON_INTERVAL_MODE) {
9673bd88451SPaolo Bonzini exynos4210_ltick_set_cntb(&s->tick_timer,
9683bd88451SPaolo Bonzini s->reg.cnt[L_REG_CNT_TCNTB],
9693bd88451SPaolo Bonzini s->reg.cnt[L_REG_CNT_ICNTB]);
9703bd88451SPaolo Bonzini }
9713bd88451SPaolo Bonzini } else {
9723bd88451SPaolo Bonzini /* reload TCNTB */
9733bd88451SPaolo Bonzini if (!tcnto) {
9743bd88451SPaolo Bonzini exynos4210_ltick_set_cntb(&s->tick_timer,
9753bd88451SPaolo Bonzini s->reg.cnt[L_REG_CNT_TCNTB],
9763bd88451SPaolo Bonzini icnto);
9773bd88451SPaolo Bonzini }
9783bd88451SPaolo Bonzini }
9793bd88451SPaolo Bonzini
9803bd88451SPaolo Bonzini /* start tick_timer cnt */
9813bd88451SPaolo Bonzini exynos4210_ltick_cnt_start(&s->tick_timer);
9823bd88451SPaolo Bonzini
9833bd88451SPaolo Bonzini /* start tick_timer int */
9843bd88451SPaolo Bonzini exynos4210_ltick_int_start(&s->tick_timer);
9853bd88451SPaolo Bonzini }
9863bd88451SPaolo Bonzini
tx_ptimer_set_freq(ptimer_state * s,uint32_t freq)9879ede4ec0SPeter Maydell static void tx_ptimer_set_freq(ptimer_state *s, uint32_t freq)
9889ede4ec0SPeter Maydell {
9899ede4ec0SPeter Maydell /*
9909ede4ec0SPeter Maydell * callers of exynos4210_mct_update_freq() never do anything
9919ede4ec0SPeter Maydell * else that needs to be in the same ptimer transaction, so
9929ede4ec0SPeter Maydell * to avoid a lot of repetition we have a convenience function
9939ede4ec0SPeter Maydell * for begin/set_freq/commit.
9949ede4ec0SPeter Maydell */
9959ede4ec0SPeter Maydell ptimer_transaction_begin(s);
9969ede4ec0SPeter Maydell ptimer_set_freq(s, freq);
9979ede4ec0SPeter Maydell ptimer_transaction_commit(s);
9989ede4ec0SPeter Maydell }
9999ede4ec0SPeter Maydell
10003bd88451SPaolo Bonzini /* update timer frequency */
exynos4210_mct_update_freq(Exynos4210MCTState * s)10013bd88451SPaolo Bonzini static void exynos4210_mct_update_freq(Exynos4210MCTState *s)
10023bd88451SPaolo Bonzini {
10033bd88451SPaolo Bonzini uint32_t freq = s->freq;
10043bd88451SPaolo Bonzini s->freq = 24000000 /
10053bd88451SPaolo Bonzini ((MCT_CFG_GET_PRESCALER(s->reg_mct_cfg) + 1) *
10063bd88451SPaolo Bonzini MCT_CFG_GET_DIVIDER(s->reg_mct_cfg));
10073bd88451SPaolo Bonzini
10083bd88451SPaolo Bonzini if (freq != s->freq) {
100963192565SAlex Chen DPRINTF("freq=%uHz\n", s->freq);
10103bd88451SPaolo Bonzini
10113bd88451SPaolo Bonzini /* global timer */
10129ede4ec0SPeter Maydell tx_ptimer_set_freq(s->g_timer.ptimer_frc, s->freq);
10133bd88451SPaolo Bonzini
10143bd88451SPaolo Bonzini /* local timer */
10156c27ee94SPeter Maydell tx_ptimer_set_freq(s->l_timer[0].tick_timer.ptimer_tick, s->freq);
101650f07d76SPeter Maydell tx_ptimer_set_freq(s->l_timer[0].ptimer_frc, s->freq);
10176c27ee94SPeter Maydell tx_ptimer_set_freq(s->l_timer[1].tick_timer.ptimer_tick, s->freq);
101850f07d76SPeter Maydell tx_ptimer_set_freq(s->l_timer[1].ptimer_frc, s->freq);
10193bd88451SPaolo Bonzini }
10203bd88451SPaolo Bonzini }
10213bd88451SPaolo Bonzini
10223bd88451SPaolo Bonzini /* set defaul_timer values for all fields */
exynos4210_mct_reset(DeviceState * d)10233bd88451SPaolo Bonzini static void exynos4210_mct_reset(DeviceState *d)
10243bd88451SPaolo Bonzini {
102581e1010dSAndreas Färber Exynos4210MCTState *s = EXYNOS4210_MCT(d);
10263bd88451SPaolo Bonzini uint32_t i;
10273bd88451SPaolo Bonzini
10283bd88451SPaolo Bonzini s->reg_mct_cfg = 0;
10293bd88451SPaolo Bonzini
10303bd88451SPaolo Bonzini /* global timer */
10313bd88451SPaolo Bonzini memset(&s->g_timer.reg, 0, sizeof(s->g_timer.reg));
10329ede4ec0SPeter Maydell exynos4210_gfrc_tx_begin(&s->g_timer);
10333bd88451SPaolo Bonzini exynos4210_gfrc_stop(&s->g_timer);
10349ede4ec0SPeter Maydell exynos4210_gfrc_tx_commit(&s->g_timer);
10353bd88451SPaolo Bonzini
10363bd88451SPaolo Bonzini /* local timer */
10373bd88451SPaolo Bonzini memset(s->l_timer[0].reg.cnt, 0, sizeof(s->l_timer[0].reg.cnt));
10383bd88451SPaolo Bonzini memset(s->l_timer[1].reg.cnt, 0, sizeof(s->l_timer[1].reg.cnt));
10393bd88451SPaolo Bonzini for (i = 0; i < 2; i++) {
10403bd88451SPaolo Bonzini s->l_timer[i].reg.int_cstat = 0;
10413bd88451SPaolo Bonzini s->l_timer[i].reg.int_enb = 0;
10423bd88451SPaolo Bonzini s->l_timer[i].reg.tcon = 0;
10433bd88451SPaolo Bonzini s->l_timer[i].reg.wstat = 0;
10443bd88451SPaolo Bonzini s->l_timer[i].tick_timer.count = 0;
10453bd88451SPaolo Bonzini s->l_timer[i].tick_timer.distance = 0;
10463bd88451SPaolo Bonzini s->l_timer[i].tick_timer.progress = 0;
104750f07d76SPeter Maydell exynos4210_lfrc_tx_begin(&s->l_timer[i]);
10483bd88451SPaolo Bonzini ptimer_stop(s->l_timer[i].ptimer_frc);
104950f07d76SPeter Maydell exynos4210_lfrc_tx_commit(&s->l_timer[i]);
10503bd88451SPaolo Bonzini
10513bd88451SPaolo Bonzini exynos4210_ltick_timer_init(&s->l_timer[i].tick_timer);
10523bd88451SPaolo Bonzini }
10533bd88451SPaolo Bonzini
10543bd88451SPaolo Bonzini exynos4210_mct_update_freq(s);
10553bd88451SPaolo Bonzini
10563bd88451SPaolo Bonzini }
10573bd88451SPaolo Bonzini
10583bd88451SPaolo Bonzini /* Multi Core Timer read */
exynos4210_mct_read(void * opaque,hwaddr offset,unsigned size)10593bd88451SPaolo Bonzini static uint64_t exynos4210_mct_read(void *opaque, hwaddr offset,
10603bd88451SPaolo Bonzini unsigned size)
10613bd88451SPaolo Bonzini {
10623bd88451SPaolo Bonzini Exynos4210MCTState *s = (Exynos4210MCTState *)opaque;
10633bd88451SPaolo Bonzini int index;
10643bd88451SPaolo Bonzini int shift;
10653bd88451SPaolo Bonzini uint64_t count;
1066a50fe668SPhilippe Mathieu-Daudé uint32_t value = 0;
10673bd88451SPaolo Bonzini int lt_i;
10683bd88451SPaolo Bonzini
10693bd88451SPaolo Bonzini switch (offset) {
10703bd88451SPaolo Bonzini
10713bd88451SPaolo Bonzini case MCT_CFG:
10723bd88451SPaolo Bonzini value = s->reg_mct_cfg;
10733bd88451SPaolo Bonzini break;
10743bd88451SPaolo Bonzini
10753bd88451SPaolo Bonzini case G_CNT_L: case G_CNT_U:
10763bd88451SPaolo Bonzini shift = 8 * (offset & 0x4);
10773bd88451SPaolo Bonzini count = exynos4210_gfrc_get_count(&s->g_timer);
10783bd88451SPaolo Bonzini value = UINT32_MAX & (count >> shift);
10793bd88451SPaolo Bonzini DPRINTF("read FRC=0x%llx\n", count);
10803bd88451SPaolo Bonzini break;
10813bd88451SPaolo Bonzini
10823bd88451SPaolo Bonzini case G_CNT_WSTAT:
10833bd88451SPaolo Bonzini value = s->g_timer.reg.cnt_wstat;
10843bd88451SPaolo Bonzini break;
10853bd88451SPaolo Bonzini
10863bd88451SPaolo Bonzini case G_COMP_L(0): case G_COMP_L(1): case G_COMP_L(2): case G_COMP_L(3):
10873bd88451SPaolo Bonzini case G_COMP_U(0): case G_COMP_U(1): case G_COMP_U(2): case G_COMP_U(3):
10883bd88451SPaolo Bonzini index = GET_G_COMP_IDX(offset);
10893bd88451SPaolo Bonzini shift = 8 * (offset & 0x4);
10903bd88451SPaolo Bonzini value = UINT32_MAX & (s->g_timer.reg.comp[index] >> shift);
10913bd88451SPaolo Bonzini break;
10923bd88451SPaolo Bonzini
10933bd88451SPaolo Bonzini case G_TCON:
10943bd88451SPaolo Bonzini value = s->g_timer.reg.tcon;
10953bd88451SPaolo Bonzini break;
10963bd88451SPaolo Bonzini
10973bd88451SPaolo Bonzini case G_INT_CSTAT:
10983bd88451SPaolo Bonzini value = s->g_timer.reg.int_cstat;
10993bd88451SPaolo Bonzini break;
11003bd88451SPaolo Bonzini
11013bd88451SPaolo Bonzini case G_INT_ENB:
11023bd88451SPaolo Bonzini value = s->g_timer.reg.int_enb;
11033bd88451SPaolo Bonzini break;
11043bd88451SPaolo Bonzini case G_WSTAT:
11053bd88451SPaolo Bonzini value = s->g_timer.reg.wstat;
11063bd88451SPaolo Bonzini break;
11073bd88451SPaolo Bonzini
11083bd88451SPaolo Bonzini case G_COMP0_ADD_INCR: case G_COMP1_ADD_INCR:
11093bd88451SPaolo Bonzini case G_COMP2_ADD_INCR: case G_COMP3_ADD_INCR:
11103bd88451SPaolo Bonzini value = s->g_timer.reg.comp_add_incr[GET_G_COMP_ADD_INCR_IDX(offset)];
11113bd88451SPaolo Bonzini break;
11123bd88451SPaolo Bonzini
11133bd88451SPaolo Bonzini /* Local timers */
11143bd88451SPaolo Bonzini case L0_TCNTB: case L0_ICNTB: case L0_FRCNTB:
11153bd88451SPaolo Bonzini case L1_TCNTB: case L1_ICNTB: case L1_FRCNTB:
11163bd88451SPaolo Bonzini lt_i = GET_L_TIMER_IDX(offset);
11173bd88451SPaolo Bonzini index = GET_L_TIMER_CNT_REG_IDX(offset, lt_i);
11183bd88451SPaolo Bonzini value = s->l_timer[lt_i].reg.cnt[index];
11193bd88451SPaolo Bonzini break;
11203bd88451SPaolo Bonzini
11213bd88451SPaolo Bonzini case L0_TCNTO: case L1_TCNTO:
11223bd88451SPaolo Bonzini lt_i = GET_L_TIMER_IDX(offset);
11233bd88451SPaolo Bonzini
11243bd88451SPaolo Bonzini value = exynos4210_ltick_cnt_get_cnto(&s->l_timer[lt_i].tick_timer);
11253bd88451SPaolo Bonzini DPRINTF("local timer[%d] read TCNTO %x\n", lt_i, value);
11263bd88451SPaolo Bonzini break;
11273bd88451SPaolo Bonzini
11283bd88451SPaolo Bonzini case L0_ICNTO: case L1_ICNTO:
11293bd88451SPaolo Bonzini lt_i = GET_L_TIMER_IDX(offset);
11303bd88451SPaolo Bonzini
11313bd88451SPaolo Bonzini value = exynos4210_ltick_int_get_cnto(&s->l_timer[lt_i].tick_timer);
11323bd88451SPaolo Bonzini DPRINTF("local timer[%d] read ICNTO %x\n", lt_i, value);
11333bd88451SPaolo Bonzini break;
11343bd88451SPaolo Bonzini
11353bd88451SPaolo Bonzini case L0_FRCNTO: case L1_FRCNTO:
11363bd88451SPaolo Bonzini lt_i = GET_L_TIMER_IDX(offset);
11373bd88451SPaolo Bonzini
11383bd88451SPaolo Bonzini value = exynos4210_lfrc_get_count(&s->l_timer[lt_i]);
11393bd88451SPaolo Bonzini break;
11403bd88451SPaolo Bonzini
11413bd88451SPaolo Bonzini case L0_TCON: case L1_TCON:
11423bd88451SPaolo Bonzini lt_i = ((offset & 0xF00) - L0_TCNTB) / 0x100;
11433bd88451SPaolo Bonzini value = s->l_timer[lt_i].reg.tcon;
11443bd88451SPaolo Bonzini break;
11453bd88451SPaolo Bonzini
11463bd88451SPaolo Bonzini case L0_INT_CSTAT: case L1_INT_CSTAT:
11473bd88451SPaolo Bonzini lt_i = ((offset & 0xF00) - L0_TCNTB) / 0x100;
11483bd88451SPaolo Bonzini value = s->l_timer[lt_i].reg.int_cstat;
11493bd88451SPaolo Bonzini break;
11503bd88451SPaolo Bonzini
11513bd88451SPaolo Bonzini case L0_INT_ENB: case L1_INT_ENB:
11523bd88451SPaolo Bonzini lt_i = ((offset & 0xF00) - L0_TCNTB) / 0x100;
11533bd88451SPaolo Bonzini value = s->l_timer[lt_i].reg.int_enb;
11543bd88451SPaolo Bonzini break;
11553bd88451SPaolo Bonzini
11563bd88451SPaolo Bonzini case L0_WSTAT: case L1_WSTAT:
11573bd88451SPaolo Bonzini lt_i = ((offset & 0xF00) - L0_TCNTB) / 0x100;
11583bd88451SPaolo Bonzini value = s->l_timer[lt_i].reg.wstat;
11593bd88451SPaolo Bonzini break;
11603bd88451SPaolo Bonzini
11613bd88451SPaolo Bonzini default:
1162a50fe668SPhilippe Mathieu-Daudé qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIX "\n",
1163a50fe668SPhilippe Mathieu-Daudé __func__, offset);
11643bd88451SPaolo Bonzini break;
11653bd88451SPaolo Bonzini }
11663bd88451SPaolo Bonzini return value;
11673bd88451SPaolo Bonzini }
11683bd88451SPaolo Bonzini
11693bd88451SPaolo Bonzini /* MCT write */
exynos4210_mct_write(void * opaque,hwaddr offset,uint64_t value,unsigned size)11703bd88451SPaolo Bonzini static void exynos4210_mct_write(void *opaque, hwaddr offset,
11713bd88451SPaolo Bonzini uint64_t value, unsigned size)
11723bd88451SPaolo Bonzini {
11733bd88451SPaolo Bonzini Exynos4210MCTState *s = (Exynos4210MCTState *)opaque;
11743bd88451SPaolo Bonzini int index; /* index in buffer which represents register set */
11753bd88451SPaolo Bonzini int shift;
11763bd88451SPaolo Bonzini int lt_i;
11773bd88451SPaolo Bonzini uint64_t new_frc;
11783bd88451SPaolo Bonzini uint32_t i;
11793bd88451SPaolo Bonzini uint32_t old_val;
11803bd88451SPaolo Bonzini #ifdef DEBUG_MCT
11813bd88451SPaolo Bonzini static uint32_t icntb_max[2] = {0};
11823bd88451SPaolo Bonzini static uint32_t icntb_min[2] = {UINT32_MAX, UINT32_MAX};
11833bd88451SPaolo Bonzini static uint32_t tcntb_max[2] = {0};
11843bd88451SPaolo Bonzini static uint32_t tcntb_min[2] = {UINT32_MAX, UINT32_MAX};
11853bd88451SPaolo Bonzini #endif
11863bd88451SPaolo Bonzini
11873bd88451SPaolo Bonzini new_frc = s->g_timer.reg.cnt;
11883bd88451SPaolo Bonzini
11893bd88451SPaolo Bonzini switch (offset) {
11903bd88451SPaolo Bonzini
11913bd88451SPaolo Bonzini case MCT_CFG:
11923bd88451SPaolo Bonzini s->reg_mct_cfg = value;
11933bd88451SPaolo Bonzini exynos4210_mct_update_freq(s);
11943bd88451SPaolo Bonzini break;
11953bd88451SPaolo Bonzini
11963bd88451SPaolo Bonzini case G_CNT_L:
11973bd88451SPaolo Bonzini case G_CNT_U:
11983bd88451SPaolo Bonzini if (offset == G_CNT_L) {
11993bd88451SPaolo Bonzini
12003bd88451SPaolo Bonzini DPRINTF("global timer write to reg.cntl %llx\n", value);
12013bd88451SPaolo Bonzini
12023bd88451SPaolo Bonzini new_frc = (s->g_timer.reg.cnt & (uint64_t)UINT32_MAX << 32) + value;
12033bd88451SPaolo Bonzini s->g_timer.reg.cnt_wstat |= G_CNT_WSTAT_L;
12043bd88451SPaolo Bonzini }
12053bd88451SPaolo Bonzini if (offset == G_CNT_U) {
12063bd88451SPaolo Bonzini
12073bd88451SPaolo Bonzini DPRINTF("global timer write to reg.cntu %llx\n", value);
12083bd88451SPaolo Bonzini
12093bd88451SPaolo Bonzini new_frc = (s->g_timer.reg.cnt & UINT32_MAX) +
12103bd88451SPaolo Bonzini ((uint64_t)value << 32);
12113bd88451SPaolo Bonzini s->g_timer.reg.cnt_wstat |= G_CNT_WSTAT_U;
12123bd88451SPaolo Bonzini }
12133bd88451SPaolo Bonzini
12143bd88451SPaolo Bonzini s->g_timer.reg.cnt = new_frc;
12159ede4ec0SPeter Maydell exynos4210_gfrc_tx_begin(&s->g_timer);
12163bd88451SPaolo Bonzini exynos4210_gfrc_restart(s);
12179ede4ec0SPeter Maydell exynos4210_gfrc_tx_commit(&s->g_timer);
12183bd88451SPaolo Bonzini break;
12193bd88451SPaolo Bonzini
12203bd88451SPaolo Bonzini case G_CNT_WSTAT:
12213bd88451SPaolo Bonzini s->g_timer.reg.cnt_wstat &= ~(value);
12223bd88451SPaolo Bonzini break;
12233bd88451SPaolo Bonzini
12243bd88451SPaolo Bonzini case G_COMP_L(0): case G_COMP_L(1): case G_COMP_L(2): case G_COMP_L(3):
12253bd88451SPaolo Bonzini case G_COMP_U(0): case G_COMP_U(1): case G_COMP_U(2): case G_COMP_U(3):
12263bd88451SPaolo Bonzini index = GET_G_COMP_IDX(offset);
12273bd88451SPaolo Bonzini shift = 8 * (offset & 0x4);
12283bd88451SPaolo Bonzini s->g_timer.reg.comp[index] =
12293bd88451SPaolo Bonzini (s->g_timer.reg.comp[index] &
12303bd88451SPaolo Bonzini (((uint64_t)UINT32_MAX << 32) >> shift)) +
12313bd88451SPaolo Bonzini (value << shift);
12323bd88451SPaolo Bonzini
12333bd88451SPaolo Bonzini DPRINTF("comparator %d write 0x%llx val << %d\n", index, value, shift);
12343bd88451SPaolo Bonzini
12353bd88451SPaolo Bonzini if (offset & 0x4) {
12363bd88451SPaolo Bonzini s->g_timer.reg.wstat |= G_WSTAT_COMP_U(index);
12373bd88451SPaolo Bonzini } else {
12383bd88451SPaolo Bonzini s->g_timer.reg.wstat |= G_WSTAT_COMP_L(index);
12393bd88451SPaolo Bonzini }
12403bd88451SPaolo Bonzini
12419ede4ec0SPeter Maydell exynos4210_gfrc_tx_begin(&s->g_timer);
12423bd88451SPaolo Bonzini exynos4210_gfrc_restart(s);
12439ede4ec0SPeter Maydell exynos4210_gfrc_tx_commit(&s->g_timer);
12443bd88451SPaolo Bonzini break;
12453bd88451SPaolo Bonzini
12463bd88451SPaolo Bonzini case G_TCON:
12473bd88451SPaolo Bonzini old_val = s->g_timer.reg.tcon;
12483bd88451SPaolo Bonzini s->g_timer.reg.tcon = value;
12493bd88451SPaolo Bonzini s->g_timer.reg.wstat |= G_WSTAT_TCON_WRITE;
12503bd88451SPaolo Bonzini
12513bd88451SPaolo Bonzini DPRINTF("global timer write to reg.g_tcon %llx\n", value);
12523bd88451SPaolo Bonzini
12539ede4ec0SPeter Maydell exynos4210_gfrc_tx_begin(&s->g_timer);
12549ede4ec0SPeter Maydell
12553bd88451SPaolo Bonzini /* Start FRC if transition from disabled to enabled */
12563bd88451SPaolo Bonzini if ((value & G_TCON_TIMER_ENABLE) > (old_val &
12573bd88451SPaolo Bonzini G_TCON_TIMER_ENABLE)) {
12581a391e20SGuenter Roeck exynos4210_gfrc_restart(s);
12593bd88451SPaolo Bonzini }
12603bd88451SPaolo Bonzini if ((value & G_TCON_TIMER_ENABLE) < (old_val &
12613bd88451SPaolo Bonzini G_TCON_TIMER_ENABLE)) {
12623bd88451SPaolo Bonzini exynos4210_gfrc_stop(&s->g_timer);
12633bd88451SPaolo Bonzini }
12643bd88451SPaolo Bonzini
12653bd88451SPaolo Bonzini /* Start CMP if transition from disabled to enabled */
12663bd88451SPaolo Bonzini for (i = 0; i < MCT_GT_CMP_NUM; i++) {
12673bd88451SPaolo Bonzini if ((value & G_TCON_COMP_ENABLE(i)) != (old_val &
12683bd88451SPaolo Bonzini G_TCON_COMP_ENABLE(i))) {
12693bd88451SPaolo Bonzini exynos4210_gfrc_restart(s);
12703bd88451SPaolo Bonzini }
12713bd88451SPaolo Bonzini }
12729ede4ec0SPeter Maydell
12739ede4ec0SPeter Maydell exynos4210_gfrc_tx_commit(&s->g_timer);
12743bd88451SPaolo Bonzini break;
12753bd88451SPaolo Bonzini
12763bd88451SPaolo Bonzini case G_INT_CSTAT:
12773bd88451SPaolo Bonzini s->g_timer.reg.int_cstat &= ~(value);
12783bd88451SPaolo Bonzini for (i = 0; i < MCT_GT_CMP_NUM; i++) {
12793bd88451SPaolo Bonzini if (value & G_INT_CSTAT_COMP(i)) {
12803bd88451SPaolo Bonzini exynos4210_gcomp_lower_irq(&s->g_timer, i);
12813bd88451SPaolo Bonzini }
12823bd88451SPaolo Bonzini }
12833bd88451SPaolo Bonzini break;
12843bd88451SPaolo Bonzini
12853bd88451SPaolo Bonzini case G_INT_ENB:
12863bd88451SPaolo Bonzini /* Raise IRQ if transition from disabled to enabled and CSTAT pending */
12873bd88451SPaolo Bonzini for (i = 0; i < MCT_GT_CMP_NUM; i++) {
12883bd88451SPaolo Bonzini if ((value & G_INT_ENABLE(i)) > (s->g_timer.reg.tcon &
12893bd88451SPaolo Bonzini G_INT_ENABLE(i))) {
12903bd88451SPaolo Bonzini if (s->g_timer.reg.int_cstat & G_INT_CSTAT_COMP(i)) {
12913bd88451SPaolo Bonzini exynos4210_gcomp_raise_irq(&s->g_timer, i);
12923bd88451SPaolo Bonzini }
12933bd88451SPaolo Bonzini }
12943bd88451SPaolo Bonzini
12953bd88451SPaolo Bonzini if ((value & G_INT_ENABLE(i)) < (s->g_timer.reg.tcon &
12963bd88451SPaolo Bonzini G_INT_ENABLE(i))) {
12973bd88451SPaolo Bonzini exynos4210_gcomp_lower_irq(&s->g_timer, i);
12983bd88451SPaolo Bonzini }
12993bd88451SPaolo Bonzini }
13003bd88451SPaolo Bonzini
13013bd88451SPaolo Bonzini DPRINTF("global timer INT enable %llx\n", value);
13023bd88451SPaolo Bonzini s->g_timer.reg.int_enb = value;
13033bd88451SPaolo Bonzini break;
13043bd88451SPaolo Bonzini
13053bd88451SPaolo Bonzini case G_WSTAT:
13063bd88451SPaolo Bonzini s->g_timer.reg.wstat &= ~(value);
13073bd88451SPaolo Bonzini break;
13083bd88451SPaolo Bonzini
13093bd88451SPaolo Bonzini case G_COMP0_ADD_INCR: case G_COMP1_ADD_INCR:
13103bd88451SPaolo Bonzini case G_COMP2_ADD_INCR: case G_COMP3_ADD_INCR:
13113bd88451SPaolo Bonzini index = GET_G_COMP_ADD_INCR_IDX(offset);
13123bd88451SPaolo Bonzini s->g_timer.reg.comp_add_incr[index] = value;
13133bd88451SPaolo Bonzini s->g_timer.reg.wstat |= G_WSTAT_COMP_ADDINCR(index);
13143bd88451SPaolo Bonzini break;
13153bd88451SPaolo Bonzini
13163bd88451SPaolo Bonzini /* Local timers */
13173bd88451SPaolo Bonzini case L0_TCON: case L1_TCON:
13183bd88451SPaolo Bonzini lt_i = GET_L_TIMER_IDX(offset);
13193bd88451SPaolo Bonzini old_val = s->l_timer[lt_i].reg.tcon;
13203bd88451SPaolo Bonzini
13213bd88451SPaolo Bonzini s->l_timer[lt_i].reg.wstat |= L_WSTAT_TCON_WRITE;
13223bd88451SPaolo Bonzini s->l_timer[lt_i].reg.tcon = value;
13233bd88451SPaolo Bonzini
13246c27ee94SPeter Maydell exynos4210_ltick_tx_begin(&s->l_timer[lt_i].tick_timer);
13253bd88451SPaolo Bonzini /* Stop local CNT */
13263bd88451SPaolo Bonzini if ((value & L_TCON_TICK_START) <
13273bd88451SPaolo Bonzini (old_val & L_TCON_TICK_START)) {
13283bd88451SPaolo Bonzini DPRINTF("local timer[%d] stop cnt\n", lt_i);
13293bd88451SPaolo Bonzini exynos4210_ltick_cnt_stop(&s->l_timer[lt_i].tick_timer);
13303bd88451SPaolo Bonzini }
13313bd88451SPaolo Bonzini
13323bd88451SPaolo Bonzini /* Stop local INT */
13333bd88451SPaolo Bonzini if ((value & L_TCON_INT_START) <
13343bd88451SPaolo Bonzini (old_val & L_TCON_INT_START)) {
13353bd88451SPaolo Bonzini DPRINTF("local timer[%d] stop int\n", lt_i);
13363bd88451SPaolo Bonzini exynos4210_ltick_int_stop(&s->l_timer[lt_i].tick_timer);
13373bd88451SPaolo Bonzini }
13383bd88451SPaolo Bonzini
13393bd88451SPaolo Bonzini /* Start local CNT */
13403bd88451SPaolo Bonzini if ((value & L_TCON_TICK_START) >
13413bd88451SPaolo Bonzini (old_val & L_TCON_TICK_START)) {
13423bd88451SPaolo Bonzini DPRINTF("local timer[%d] start cnt\n", lt_i);
13433bd88451SPaolo Bonzini exynos4210_ltick_cnt_start(&s->l_timer[lt_i].tick_timer);
13443bd88451SPaolo Bonzini }
13453bd88451SPaolo Bonzini
13463bd88451SPaolo Bonzini /* Start local INT */
13473bd88451SPaolo Bonzini if ((value & L_TCON_INT_START) >
13483bd88451SPaolo Bonzini (old_val & L_TCON_INT_START)) {
13493bd88451SPaolo Bonzini DPRINTF("local timer[%d] start int\n", lt_i);
13503bd88451SPaolo Bonzini exynos4210_ltick_int_start(&s->l_timer[lt_i].tick_timer);
13513bd88451SPaolo Bonzini }
13526c27ee94SPeter Maydell exynos4210_ltick_tx_commit(&s->l_timer[lt_i].tick_timer);
13533bd88451SPaolo Bonzini
13543bd88451SPaolo Bonzini /* Start or Stop local FRC if TCON changed */
135550f07d76SPeter Maydell exynos4210_lfrc_tx_begin(&s->l_timer[lt_i]);
13563bd88451SPaolo Bonzini if ((value & L_TCON_FRC_START) >
13573bd88451SPaolo Bonzini (s->l_timer[lt_i].reg.tcon & L_TCON_FRC_START)) {
13583bd88451SPaolo Bonzini DPRINTF("local timer[%d] start frc\n", lt_i);
13593bd88451SPaolo Bonzini exynos4210_lfrc_start(&s->l_timer[lt_i]);
13603bd88451SPaolo Bonzini }
13613bd88451SPaolo Bonzini if ((value & L_TCON_FRC_START) <
13623bd88451SPaolo Bonzini (s->l_timer[lt_i].reg.tcon & L_TCON_FRC_START)) {
13633bd88451SPaolo Bonzini DPRINTF("local timer[%d] stop frc\n", lt_i);
13643bd88451SPaolo Bonzini exynos4210_lfrc_stop(&s->l_timer[lt_i]);
13653bd88451SPaolo Bonzini }
136650f07d76SPeter Maydell exynos4210_lfrc_tx_commit(&s->l_timer[lt_i]);
13673bd88451SPaolo Bonzini break;
13683bd88451SPaolo Bonzini
13693bd88451SPaolo Bonzini case L0_TCNTB: case L1_TCNTB:
13703bd88451SPaolo Bonzini lt_i = GET_L_TIMER_IDX(offset);
13713bd88451SPaolo Bonzini
13723bd88451SPaolo Bonzini /*
13733bd88451SPaolo Bonzini * TCNTB is updated to internal register only after CNT expired.
13743bd88451SPaolo Bonzini * Due to this we should reload timer to nearest moment when CNT is
13753bd88451SPaolo Bonzini * expired and then in event handler update tcntb to new TCNTB value.
13763bd88451SPaolo Bonzini */
13776c27ee94SPeter Maydell exynos4210_ltick_tx_begin(&s->l_timer[lt_i].tick_timer);
13783bd88451SPaolo Bonzini exynos4210_ltick_set_cntb(&s->l_timer[lt_i].tick_timer, value,
13793bd88451SPaolo Bonzini s->l_timer[lt_i].tick_timer.icntb);
13806c27ee94SPeter Maydell exynos4210_ltick_tx_commit(&s->l_timer[lt_i].tick_timer);
13813bd88451SPaolo Bonzini
13823bd88451SPaolo Bonzini s->l_timer[lt_i].reg.wstat |= L_WSTAT_TCNTB_WRITE;
13833bd88451SPaolo Bonzini s->l_timer[lt_i].reg.cnt[L_REG_CNT_TCNTB] = value;
13843bd88451SPaolo Bonzini
13853bd88451SPaolo Bonzini #ifdef DEBUG_MCT
13863bd88451SPaolo Bonzini if (tcntb_min[lt_i] > value) {
13873bd88451SPaolo Bonzini tcntb_min[lt_i] = value;
13883bd88451SPaolo Bonzini }
13893bd88451SPaolo Bonzini if (tcntb_max[lt_i] < value) {
13903bd88451SPaolo Bonzini tcntb_max[lt_i] = value;
13913bd88451SPaolo Bonzini }
13923bd88451SPaolo Bonzini DPRINTF("local timer[%d] TCNTB write %llx; max=%x, min=%x\n",
13933bd88451SPaolo Bonzini lt_i, value, tcntb_max[lt_i], tcntb_min[lt_i]);
13943bd88451SPaolo Bonzini #endif
13953bd88451SPaolo Bonzini break;
13963bd88451SPaolo Bonzini
13973bd88451SPaolo Bonzini case L0_ICNTB: case L1_ICNTB:
13983bd88451SPaolo Bonzini lt_i = GET_L_TIMER_IDX(offset);
13993bd88451SPaolo Bonzini
14003bd88451SPaolo Bonzini s->l_timer[lt_i].reg.wstat |= L_WSTAT_ICNTB_WRITE;
14013bd88451SPaolo Bonzini s->l_timer[lt_i].reg.cnt[L_REG_CNT_ICNTB] = value &
14023bd88451SPaolo Bonzini ~L_ICNTB_MANUAL_UPDATE;
14033bd88451SPaolo Bonzini
14043bd88451SPaolo Bonzini /*
14053bd88451SPaolo Bonzini * We need to avoid too small values for TCNTB*ICNTB. If not, IRQ event
14063bd88451SPaolo Bonzini * could raise too fast disallowing QEMU to execute target code.
14073bd88451SPaolo Bonzini */
14083bd88451SPaolo Bonzini if (s->l_timer[lt_i].reg.cnt[L_REG_CNT_ICNTB] *
14093bd88451SPaolo Bonzini s->l_timer[lt_i].reg.cnt[L_REG_CNT_TCNTB] < MCT_LT_CNT_LOW_LIMIT) {
14103bd88451SPaolo Bonzini if (!s->l_timer[lt_i].reg.cnt[L_REG_CNT_TCNTB]) {
14113bd88451SPaolo Bonzini s->l_timer[lt_i].reg.cnt[L_REG_CNT_ICNTB] =
14123bd88451SPaolo Bonzini MCT_LT_CNT_LOW_LIMIT;
14133bd88451SPaolo Bonzini } else {
14143bd88451SPaolo Bonzini s->l_timer[lt_i].reg.cnt[L_REG_CNT_ICNTB] =
14153bd88451SPaolo Bonzini MCT_LT_CNT_LOW_LIMIT /
14163bd88451SPaolo Bonzini s->l_timer[lt_i].reg.cnt[L_REG_CNT_TCNTB];
14173bd88451SPaolo Bonzini }
14183bd88451SPaolo Bonzini }
14193bd88451SPaolo Bonzini
14203bd88451SPaolo Bonzini if (value & L_ICNTB_MANUAL_UPDATE) {
14213bd88451SPaolo Bonzini exynos4210_ltick_set_cntb(&s->l_timer[lt_i].tick_timer,
14223bd88451SPaolo Bonzini s->l_timer[lt_i].tick_timer.tcntb,
14233bd88451SPaolo Bonzini s->l_timer[lt_i].reg.cnt[L_REG_CNT_ICNTB]);
14243bd88451SPaolo Bonzini }
14253bd88451SPaolo Bonzini
14263bd88451SPaolo Bonzini #ifdef DEBUG_MCT
14273bd88451SPaolo Bonzini if (icntb_min[lt_i] > value) {
14283bd88451SPaolo Bonzini icntb_min[lt_i] = value;
14293bd88451SPaolo Bonzini }
14303bd88451SPaolo Bonzini if (icntb_max[lt_i] < value) {
14313bd88451SPaolo Bonzini icntb_max[lt_i] = value;
14323bd88451SPaolo Bonzini }
14333bd88451SPaolo Bonzini DPRINTF("local timer[%d] ICNTB write %llx; max=%x, min=%x\n\n",
14343bd88451SPaolo Bonzini lt_i, value, icntb_max[lt_i], icntb_min[lt_i]);
14353bd88451SPaolo Bonzini #endif
14363bd88451SPaolo Bonzini break;
14373bd88451SPaolo Bonzini
14383bd88451SPaolo Bonzini case L0_FRCNTB: case L1_FRCNTB:
14393bd88451SPaolo Bonzini lt_i = GET_L_TIMER_IDX(offset);
14403bd88451SPaolo Bonzini DPRINTF("local timer[%d] FRCNTB write %llx\n", lt_i, value);
14413bd88451SPaolo Bonzini
14423bd88451SPaolo Bonzini s->l_timer[lt_i].reg.wstat |= L_WSTAT_FRCCNTB_WRITE;
14433bd88451SPaolo Bonzini s->l_timer[lt_i].reg.cnt[L_REG_CNT_FRCCNTB] = value;
14443bd88451SPaolo Bonzini
14453bd88451SPaolo Bonzini break;
14463bd88451SPaolo Bonzini
14473bd88451SPaolo Bonzini case L0_TCNTO: case L1_TCNTO:
14483bd88451SPaolo Bonzini case L0_ICNTO: case L1_ICNTO:
14493bd88451SPaolo Bonzini case L0_FRCNTO: case L1_FRCNTO:
1450f2ad5140SKrzysztof Kozlowski qemu_log_mask(LOG_GUEST_ERROR,
1451883f2c59SPhilippe Mathieu-Daudé "exynos4210.mct: write to RO register " HWADDR_FMT_plx,
1452f2ad5140SKrzysztof Kozlowski offset);
14533bd88451SPaolo Bonzini break;
14543bd88451SPaolo Bonzini
14553bd88451SPaolo Bonzini case L0_INT_CSTAT: case L1_INT_CSTAT:
14563bd88451SPaolo Bonzini lt_i = GET_L_TIMER_IDX(offset);
14573bd88451SPaolo Bonzini
14583bd88451SPaolo Bonzini DPRINTF("local timer[%d] CSTAT write %llx\n", lt_i, value);
14593bd88451SPaolo Bonzini
14603bd88451SPaolo Bonzini s->l_timer[lt_i].reg.int_cstat &= ~value;
14613bd88451SPaolo Bonzini if (!s->l_timer[lt_i].reg.int_cstat) {
14623bd88451SPaolo Bonzini qemu_irq_lower(s->l_timer[lt_i].irq);
14633bd88451SPaolo Bonzini }
14643bd88451SPaolo Bonzini break;
14653bd88451SPaolo Bonzini
14663bd88451SPaolo Bonzini case L0_INT_ENB: case L1_INT_ENB:
14673bd88451SPaolo Bonzini lt_i = GET_L_TIMER_IDX(offset);
14683bd88451SPaolo Bonzini old_val = s->l_timer[lt_i].reg.int_enb;
14693bd88451SPaolo Bonzini
14703bd88451SPaolo Bonzini /* Raise Local timer IRQ if cstat is pending */
14713bd88451SPaolo Bonzini if ((value & L_INT_INTENB_ICNTEIE) > (old_val & L_INT_INTENB_ICNTEIE)) {
14723bd88451SPaolo Bonzini if (s->l_timer[lt_i].reg.int_cstat & L_INT_CSTAT_INTCNT) {
14733bd88451SPaolo Bonzini qemu_irq_raise(s->l_timer[lt_i].irq);
14743bd88451SPaolo Bonzini }
14753bd88451SPaolo Bonzini }
14763bd88451SPaolo Bonzini
14773bd88451SPaolo Bonzini s->l_timer[lt_i].reg.int_enb = value;
14783bd88451SPaolo Bonzini
14793bd88451SPaolo Bonzini break;
14803bd88451SPaolo Bonzini
14813bd88451SPaolo Bonzini case L0_WSTAT: case L1_WSTAT:
14823bd88451SPaolo Bonzini lt_i = GET_L_TIMER_IDX(offset);
14833bd88451SPaolo Bonzini
14843bd88451SPaolo Bonzini s->l_timer[lt_i].reg.wstat &= ~value;
14853bd88451SPaolo Bonzini break;
14863bd88451SPaolo Bonzini
14873bd88451SPaolo Bonzini default:
1488a50fe668SPhilippe Mathieu-Daudé qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIX "\n",
1489a50fe668SPhilippe Mathieu-Daudé __func__, offset);
14903bd88451SPaolo Bonzini break;
14913bd88451SPaolo Bonzini }
14923bd88451SPaolo Bonzini }
14933bd88451SPaolo Bonzini
14943bd88451SPaolo Bonzini static const MemoryRegionOps exynos4210_mct_ops = {
14953bd88451SPaolo Bonzini .read = exynos4210_mct_read,
14963bd88451SPaolo Bonzini .write = exynos4210_mct_write,
14973bd88451SPaolo Bonzini .endianness = DEVICE_NATIVE_ENDIAN,
14983bd88451SPaolo Bonzini };
14993bd88451SPaolo Bonzini
15003bd88451SPaolo Bonzini /* MCT init */
exynos4210_mct_init(Object * obj)15017a53a140Sxiaoqiang.zhao static void exynos4210_mct_init(Object *obj)
15023bd88451SPaolo Bonzini {
15033bd88451SPaolo Bonzini int i;
15047a53a140Sxiaoqiang.zhao Exynos4210MCTState *s = EXYNOS4210_MCT(obj);
15057a53a140Sxiaoqiang.zhao SysBusDevice *dev = SYS_BUS_DEVICE(obj);
15063bd88451SPaolo Bonzini
15073bd88451SPaolo Bonzini /* Global timer */
15089ede4ec0SPeter Maydell s->g_timer.ptimer_frc = ptimer_init(exynos4210_gfrc_event, s,
15099598c1bbSPeter Maydell PTIMER_POLICY_LEGACY);
15103bd88451SPaolo Bonzini memset(&s->g_timer.reg, 0, sizeof(struct gregs));
15113bd88451SPaolo Bonzini
15123bd88451SPaolo Bonzini /* Local timers */
15133bd88451SPaolo Bonzini for (i = 0; i < 2; i++) {
1514e7ea81c3SDmitry Osipenko s->l_timer[i].tick_timer.ptimer_tick =
15156c27ee94SPeter Maydell ptimer_init(exynos4210_ltick_event, &s->l_timer[i],
15169598c1bbSPeter Maydell PTIMER_POLICY_LEGACY);
1517b0142262SPeter Maydell s->l_timer[i].ptimer_frc =
151850f07d76SPeter Maydell ptimer_init(exynos4210_lfrc_event, &s->l_timer[i],
15199598c1bbSPeter Maydell PTIMER_POLICY_LEGACY);
15203bd88451SPaolo Bonzini s->l_timer[i].id = i;
15213bd88451SPaolo Bonzini }
15223bd88451SPaolo Bonzini
15233bd88451SPaolo Bonzini /* IRQs */
15243bd88451SPaolo Bonzini for (i = 0; i < MCT_GT_CMP_NUM; i++) {
15253bd88451SPaolo Bonzini sysbus_init_irq(dev, &s->g_timer.irq[i]);
15263bd88451SPaolo Bonzini }
15273bd88451SPaolo Bonzini for (i = 0; i < 2; i++) {
15283bd88451SPaolo Bonzini sysbus_init_irq(dev, &s->l_timer[i].irq);
15293bd88451SPaolo Bonzini }
15303bd88451SPaolo Bonzini
15317a53a140Sxiaoqiang.zhao memory_region_init_io(&s->iomem, obj, &exynos4210_mct_ops, s,
1532853dca12SPaolo Bonzini "exynos4210-mct", MCT_SFR_SIZE);
15333bd88451SPaolo Bonzini sysbus_init_mmio(dev, &s->iomem);
15343bd88451SPaolo Bonzini }
15353bd88451SPaolo Bonzini
exynos4210_mct_finalize(Object * obj)1536d97d9152SGan Qixin static void exynos4210_mct_finalize(Object *obj)
1537d97d9152SGan Qixin {
1538d97d9152SGan Qixin int i;
1539d97d9152SGan Qixin Exynos4210MCTState *s = EXYNOS4210_MCT(obj);
1540d97d9152SGan Qixin
1541d97d9152SGan Qixin ptimer_free(s->g_timer.ptimer_frc);
1542d97d9152SGan Qixin
1543d97d9152SGan Qixin for (i = 0; i < 2; i++) {
1544d97d9152SGan Qixin ptimer_free(s->l_timer[i].tick_timer.ptimer_tick);
1545d97d9152SGan Qixin ptimer_free(s->l_timer[i].ptimer_frc);
1546d97d9152SGan Qixin }
1547d97d9152SGan Qixin }
1548d97d9152SGan Qixin
exynos4210_mct_class_init(ObjectClass * klass,void * data)15493bd88451SPaolo Bonzini static void exynos4210_mct_class_init(ObjectClass *klass, void *data)
15503bd88451SPaolo Bonzini {
15513bd88451SPaolo Bonzini DeviceClass *dc = DEVICE_CLASS(klass);
15523bd88451SPaolo Bonzini
1553e3d08143SPeter Maydell device_class_set_legacy_reset(dc, exynos4210_mct_reset);
15543bd88451SPaolo Bonzini dc->vmsd = &vmstate_exynos4210_mct_state;
15553bd88451SPaolo Bonzini }
15563bd88451SPaolo Bonzini
15573bd88451SPaolo Bonzini static const TypeInfo exynos4210_mct_info = {
155881e1010dSAndreas Färber .name = TYPE_EXYNOS4210_MCT,
15593bd88451SPaolo Bonzini .parent = TYPE_SYS_BUS_DEVICE,
15603bd88451SPaolo Bonzini .instance_size = sizeof(Exynos4210MCTState),
15617a53a140Sxiaoqiang.zhao .instance_init = exynos4210_mct_init,
1562d97d9152SGan Qixin .instance_finalize = exynos4210_mct_finalize,
15633bd88451SPaolo Bonzini .class_init = exynos4210_mct_class_init,
15643bd88451SPaolo Bonzini };
15653bd88451SPaolo Bonzini
exynos4210_mct_register_types(void)15663bd88451SPaolo Bonzini static void exynos4210_mct_register_types(void)
15673bd88451SPaolo Bonzini {
15683bd88451SPaolo Bonzini type_register_static(&exynos4210_mct_info);
15693bd88451SPaolo Bonzini }
15703bd88451SPaolo Bonzini
15713bd88451SPaolo Bonzini type_init(exynos4210_mct_register_types)
1572