xref: /openbmc/qemu/hw/timer/imx_gpt.c (revision 6a1751b7aad6e38e9d1ae6bcea72fa28bf6cc5fb)
1a50c0d6fSJean-Christophe DUBOIS /*
2a50c0d6fSJean-Christophe DUBOIS  * IMX GPT Timer
3a50c0d6fSJean-Christophe DUBOIS  *
4a50c0d6fSJean-Christophe DUBOIS  * Copyright (c) 2008 OK Labs
5a50c0d6fSJean-Christophe DUBOIS  * Copyright (c) 2011 NICTA Pty Ltd
6a50c0d6fSJean-Christophe DUBOIS  * Originally written by Hans Jiang
7a50c0d6fSJean-Christophe DUBOIS  * Updated by Peter Chubb
85ec694b5SJean-Christophe DUBOIS  * Updated by Jean-Christophe Dubois
9a50c0d6fSJean-Christophe DUBOIS  *
10a50c0d6fSJean-Christophe DUBOIS  * This code is licensed under GPL version 2 or later.  See
11a50c0d6fSJean-Christophe DUBOIS  * the COPYING file in the top-level directory.
12a50c0d6fSJean-Christophe DUBOIS  *
13a50c0d6fSJean-Christophe DUBOIS  */
14a50c0d6fSJean-Christophe DUBOIS 
15a50c0d6fSJean-Christophe DUBOIS #include "hw/hw.h"
16a50c0d6fSJean-Christophe DUBOIS #include "qemu/bitops.h"
17a50c0d6fSJean-Christophe DUBOIS #include "qemu/timer.h"
18a50c0d6fSJean-Christophe DUBOIS #include "hw/ptimer.h"
19a50c0d6fSJean-Christophe DUBOIS #include "hw/sysbus.h"
20a50c0d6fSJean-Christophe DUBOIS #include "hw/arm/imx.h"
21*6a1751b7SAlex Bligh #include "qemu/main-loop.h"
22a50c0d6fSJean-Christophe DUBOIS 
235ec694b5SJean-Christophe DUBOIS #define TYPE_IMX_GPT "imx.gpt"
245ec694b5SJean-Christophe DUBOIS 
255ec694b5SJean-Christophe DUBOIS /*
265ec694b5SJean-Christophe DUBOIS  * Define to 1 for debug messages
275ec694b5SJean-Christophe DUBOIS  */
285ec694b5SJean-Christophe DUBOIS #define DEBUG_TIMER 0
295ec694b5SJean-Christophe DUBOIS #if DEBUG_TIMER
305ec694b5SJean-Christophe DUBOIS 
3167110c3eSJean-Christophe DUBOIS static char const *imx_gpt_reg_name(uint32_t reg)
325ec694b5SJean-Christophe DUBOIS {
335ec694b5SJean-Christophe DUBOIS     switch (reg) {
345ec694b5SJean-Christophe DUBOIS     case 0:
355ec694b5SJean-Christophe DUBOIS         return "CR";
365ec694b5SJean-Christophe DUBOIS     case 1:
375ec694b5SJean-Christophe DUBOIS         return "PR";
385ec694b5SJean-Christophe DUBOIS     case 2:
395ec694b5SJean-Christophe DUBOIS         return "SR";
405ec694b5SJean-Christophe DUBOIS     case 3:
415ec694b5SJean-Christophe DUBOIS         return "IR";
425ec694b5SJean-Christophe DUBOIS     case 4:
435ec694b5SJean-Christophe DUBOIS         return "OCR1";
445ec694b5SJean-Christophe DUBOIS     case 5:
455ec694b5SJean-Christophe DUBOIS         return "OCR2";
465ec694b5SJean-Christophe DUBOIS     case 6:
475ec694b5SJean-Christophe DUBOIS         return "OCR3";
485ec694b5SJean-Christophe DUBOIS     case 7:
495ec694b5SJean-Christophe DUBOIS         return "ICR1";
505ec694b5SJean-Christophe DUBOIS     case 8:
515ec694b5SJean-Christophe DUBOIS         return "ICR2";
525ec694b5SJean-Christophe DUBOIS     case 9:
535ec694b5SJean-Christophe DUBOIS         return "CNT";
545ec694b5SJean-Christophe DUBOIS     default:
555ec694b5SJean-Christophe DUBOIS         return "[?]";
565ec694b5SJean-Christophe DUBOIS     }
575ec694b5SJean-Christophe DUBOIS }
585ec694b5SJean-Christophe DUBOIS 
59a50c0d6fSJean-Christophe DUBOIS #  define DPRINTF(fmt, args...) \
605ec694b5SJean-Christophe DUBOIS           do { printf("%s: " fmt , __func__, ##args); } while (0)
61a50c0d6fSJean-Christophe DUBOIS #else
62a50c0d6fSJean-Christophe DUBOIS #  define DPRINTF(fmt, args...) do {} while (0)
63a50c0d6fSJean-Christophe DUBOIS #endif
64a50c0d6fSJean-Christophe DUBOIS 
65a50c0d6fSJean-Christophe DUBOIS /*
66a50c0d6fSJean-Christophe DUBOIS  * Define to 1 for messages about attempts to
67a50c0d6fSJean-Christophe DUBOIS  * access unimplemented registers or similar.
68a50c0d6fSJean-Christophe DUBOIS  */
69a50c0d6fSJean-Christophe DUBOIS #define DEBUG_IMPLEMENTATION 1
70a50c0d6fSJean-Christophe DUBOIS #if DEBUG_IMPLEMENTATION
71a50c0d6fSJean-Christophe DUBOIS #  define IPRINTF(fmt, args...) \
725ec694b5SJean-Christophe DUBOIS           do { fprintf(stderr, "%s: " fmt, __func__, ##args); } while (0)
73a50c0d6fSJean-Christophe DUBOIS #else
74a50c0d6fSJean-Christophe DUBOIS #  define IPRINTF(fmt, args...) do {} while (0)
75a50c0d6fSJean-Christophe DUBOIS #endif
76a50c0d6fSJean-Christophe DUBOIS 
7767110c3eSJean-Christophe DUBOIS #define IMX_GPT(obj) \
7867110c3eSJean-Christophe DUBOIS         OBJECT_CHECK(IMXGPTState, (obj), TYPE_IMX_GPT)
79a50c0d6fSJean-Christophe DUBOIS /*
80a50c0d6fSJean-Christophe DUBOIS  * GPT : General purpose timer
81a50c0d6fSJean-Christophe DUBOIS  *
82a50c0d6fSJean-Christophe DUBOIS  * This timer counts up continuously while it is enabled, resetting itself
83a50c0d6fSJean-Christophe DUBOIS  * to 0 when it reaches TIMER_MAX (in freerun mode) or when it
845ec694b5SJean-Christophe DUBOIS  * reaches the value of one of the ocrX (in periodic mode).
85a50c0d6fSJean-Christophe DUBOIS  */
86a50c0d6fSJean-Christophe DUBOIS 
87a50c0d6fSJean-Christophe DUBOIS #define TIMER_MAX  0XFFFFFFFFUL
88a50c0d6fSJean-Christophe DUBOIS 
89a50c0d6fSJean-Christophe DUBOIS /* Control register.  Not all of these bits have any effect (yet) */
90a50c0d6fSJean-Christophe DUBOIS #define GPT_CR_EN     (1 << 0)  /* GPT Enable */
91a50c0d6fSJean-Christophe DUBOIS #define GPT_CR_ENMOD  (1 << 1)  /* GPT Enable Mode */
92a50c0d6fSJean-Christophe DUBOIS #define GPT_CR_DBGEN  (1 << 2)  /* GPT Debug mode enable */
93a50c0d6fSJean-Christophe DUBOIS #define GPT_CR_WAITEN (1 << 3)  /* GPT Wait Mode Enable  */
94a50c0d6fSJean-Christophe DUBOIS #define GPT_CR_DOZEN  (1 << 4)  /* GPT Doze mode enable */
95a50c0d6fSJean-Christophe DUBOIS #define GPT_CR_STOPEN (1 << 5)  /* GPT Stop Mode Enable */
96a50c0d6fSJean-Christophe DUBOIS #define GPT_CR_CLKSRC_SHIFT (6)
97a50c0d6fSJean-Christophe DUBOIS #define GPT_CR_CLKSRC_MASK  (0x7)
98a50c0d6fSJean-Christophe DUBOIS 
99a50c0d6fSJean-Christophe DUBOIS #define GPT_CR_FRR    (1 << 9)  /* Freerun or Restart */
100a50c0d6fSJean-Christophe DUBOIS #define GPT_CR_SWR    (1 << 15) /* Software Reset */
101a50c0d6fSJean-Christophe DUBOIS #define GPT_CR_IM1    (3 << 16) /* Input capture channel 1 mode (2 bits) */
102a50c0d6fSJean-Christophe DUBOIS #define GPT_CR_IM2    (3 << 18) /* Input capture channel 2 mode (2 bits) */
103a50c0d6fSJean-Christophe DUBOIS #define GPT_CR_OM1    (7 << 20) /* Output Compare Channel 1 Mode (3 bits) */
104a50c0d6fSJean-Christophe DUBOIS #define GPT_CR_OM2    (7 << 23) /* Output Compare Channel 2 Mode (3 bits) */
105a50c0d6fSJean-Christophe DUBOIS #define GPT_CR_OM3    (7 << 26) /* Output Compare Channel 3 Mode (3 bits) */
106a50c0d6fSJean-Christophe DUBOIS #define GPT_CR_FO1    (1 << 29) /* Force Output Compare Channel 1 */
107a50c0d6fSJean-Christophe DUBOIS #define GPT_CR_FO2    (1 << 30) /* Force Output Compare Channel 2 */
108a50c0d6fSJean-Christophe DUBOIS #define GPT_CR_FO3    (1 << 31) /* Force Output Compare Channel 3 */
109a50c0d6fSJean-Christophe DUBOIS 
110a50c0d6fSJean-Christophe DUBOIS #define GPT_SR_OF1  (1 << 0)
1115ec694b5SJean-Christophe DUBOIS #define GPT_SR_OF2  (1 << 1)
1125ec694b5SJean-Christophe DUBOIS #define GPT_SR_OF3  (1 << 2)
113a50c0d6fSJean-Christophe DUBOIS #define GPT_SR_ROV  (1 << 5)
114a50c0d6fSJean-Christophe DUBOIS 
115a50c0d6fSJean-Christophe DUBOIS #define GPT_IR_OF1IE  (1 << 0)
1165ec694b5SJean-Christophe DUBOIS #define GPT_IR_OF2IE  (1 << 1)
1175ec694b5SJean-Christophe DUBOIS #define GPT_IR_OF3IE  (1 << 2)
118a50c0d6fSJean-Christophe DUBOIS #define GPT_IR_ROVIE  (1 << 5)
119a50c0d6fSJean-Christophe DUBOIS 
120a50c0d6fSJean-Christophe DUBOIS typedef struct {
121a50c0d6fSJean-Christophe DUBOIS     SysBusDevice busdev;
122a50c0d6fSJean-Christophe DUBOIS     ptimer_state *timer;
123a50c0d6fSJean-Christophe DUBOIS     MemoryRegion iomem;
124a50c0d6fSJean-Christophe DUBOIS     DeviceState *ccm;
125a50c0d6fSJean-Christophe DUBOIS 
126a50c0d6fSJean-Christophe DUBOIS     uint32_t cr;
127a50c0d6fSJean-Christophe DUBOIS     uint32_t pr;
128a50c0d6fSJean-Christophe DUBOIS     uint32_t sr;
129a50c0d6fSJean-Christophe DUBOIS     uint32_t ir;
130a50c0d6fSJean-Christophe DUBOIS     uint32_t ocr1;
131a50c0d6fSJean-Christophe DUBOIS     uint32_t ocr2;
132a50c0d6fSJean-Christophe DUBOIS     uint32_t ocr3;
133a50c0d6fSJean-Christophe DUBOIS     uint32_t icr1;
134a50c0d6fSJean-Christophe DUBOIS     uint32_t icr2;
135a50c0d6fSJean-Christophe DUBOIS     uint32_t cnt;
136a50c0d6fSJean-Christophe DUBOIS 
1375ec694b5SJean-Christophe DUBOIS     uint32_t next_timeout;
1385ec694b5SJean-Christophe DUBOIS     uint32_t next_int;
1395ec694b5SJean-Christophe DUBOIS 
1405ec694b5SJean-Christophe DUBOIS     uint32_t freq;
1415ec694b5SJean-Christophe DUBOIS 
142a50c0d6fSJean-Christophe DUBOIS     qemu_irq irq;
14367110c3eSJean-Christophe DUBOIS } IMXGPTState;
144a50c0d6fSJean-Christophe DUBOIS 
14567110c3eSJean-Christophe DUBOIS static const VMStateDescription vmstate_imx_timer_gpt = {
1466783ecf1SPeter Maydell     .name = "imx.gpt",
1475ec694b5SJean-Christophe DUBOIS     .version_id = 3,
1485ec694b5SJean-Christophe DUBOIS     .minimum_version_id = 3,
1495ec694b5SJean-Christophe DUBOIS     .minimum_version_id_old = 3,
150a50c0d6fSJean-Christophe DUBOIS     .fields      = (VMStateField[]) {
15167110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(cr, IMXGPTState),
15267110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(pr, IMXGPTState),
15367110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(sr, IMXGPTState),
15467110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(ir, IMXGPTState),
15567110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(ocr1, IMXGPTState),
15667110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(ocr2, IMXGPTState),
15767110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(ocr3, IMXGPTState),
15867110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(icr1, IMXGPTState),
15967110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(icr2, IMXGPTState),
16067110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(cnt, IMXGPTState),
16167110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(next_timeout, IMXGPTState),
16267110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(next_int, IMXGPTState),
16367110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(freq, IMXGPTState),
16467110c3eSJean-Christophe DUBOIS         VMSTATE_PTIMER(timer, IMXGPTState),
165a50c0d6fSJean-Christophe DUBOIS         VMSTATE_END_OF_LIST()
166a50c0d6fSJean-Christophe DUBOIS     }
167a50c0d6fSJean-Christophe DUBOIS };
168a50c0d6fSJean-Christophe DUBOIS 
16967110c3eSJean-Christophe DUBOIS static const IMXClk imx_gpt_clocks[] = {
170a50c0d6fSJean-Christophe DUBOIS     NOCLK,    /* 000 No clock source */
171a50c0d6fSJean-Christophe DUBOIS     IPG,      /* 001 ipg_clk, 532MHz*/
172a50c0d6fSJean-Christophe DUBOIS     IPG,      /* 010 ipg_clk_highfreq */
173a50c0d6fSJean-Christophe DUBOIS     NOCLK,    /* 011 not defined */
174a50c0d6fSJean-Christophe DUBOIS     CLK_32k,  /* 100 ipg_clk_32k */
175a50c0d6fSJean-Christophe DUBOIS     NOCLK,    /* 101 not defined */
176a50c0d6fSJean-Christophe DUBOIS     NOCLK,    /* 110 not defined */
177a50c0d6fSJean-Christophe DUBOIS     NOCLK,    /* 111 not defined */
178a50c0d6fSJean-Christophe DUBOIS };
179a50c0d6fSJean-Christophe DUBOIS 
18067110c3eSJean-Christophe DUBOIS static void imx_gpt_set_freq(IMXGPTState *s)
181a50c0d6fSJean-Christophe DUBOIS {
1825ec694b5SJean-Christophe DUBOIS     uint32_t clksrc = extract32(s->cr, GPT_CR_CLKSRC_SHIFT, 3);
18367110c3eSJean-Christophe DUBOIS     uint32_t freq = imx_clock_frequency(s->ccm, imx_gpt_clocks[clksrc])
1845ec694b5SJean-Christophe DUBOIS                                                 / (1 + s->pr);
1855ec694b5SJean-Christophe DUBOIS     s->freq = freq;
186a50c0d6fSJean-Christophe DUBOIS 
1875ec694b5SJean-Christophe DUBOIS     DPRINTF("Setting clksrc %d to frequency %d\n", clksrc, freq);
188a50c0d6fSJean-Christophe DUBOIS 
189a50c0d6fSJean-Christophe DUBOIS     if (freq) {
190a50c0d6fSJean-Christophe DUBOIS         ptimer_set_freq(s->timer, freq);
191a50c0d6fSJean-Christophe DUBOIS     }
192a50c0d6fSJean-Christophe DUBOIS }
193a50c0d6fSJean-Christophe DUBOIS 
19467110c3eSJean-Christophe DUBOIS static void imx_gpt_update_int(IMXGPTState *s)
195a50c0d6fSJean-Christophe DUBOIS {
1965ec694b5SJean-Christophe DUBOIS     if ((s->sr & s->ir) && (s->cr & GPT_CR_EN)) {
1975ec694b5SJean-Christophe DUBOIS         qemu_irq_raise(s->irq);
1985ec694b5SJean-Christophe DUBOIS     } else {
1995ec694b5SJean-Christophe DUBOIS         qemu_irq_lower(s->irq);
2005ec694b5SJean-Christophe DUBOIS     }
201a50c0d6fSJean-Christophe DUBOIS }
202a50c0d6fSJean-Christophe DUBOIS 
20367110c3eSJean-Christophe DUBOIS static uint32_t imx_gpt_update_count(IMXGPTState *s)
204a50c0d6fSJean-Christophe DUBOIS {
2055ec694b5SJean-Christophe DUBOIS     s->cnt = s->next_timeout - (uint32_t)ptimer_get_count(s->timer);
2065ec694b5SJean-Christophe DUBOIS 
207a50c0d6fSJean-Christophe DUBOIS     return s->cnt;
208a50c0d6fSJean-Christophe DUBOIS }
209a50c0d6fSJean-Christophe DUBOIS 
21067110c3eSJean-Christophe DUBOIS static inline uint32_t imx_gpt_find_limit(uint32_t count, uint32_t reg,
2115ec694b5SJean-Christophe DUBOIS                                              uint32_t timeout)
212a50c0d6fSJean-Christophe DUBOIS {
2135ec694b5SJean-Christophe DUBOIS     if ((count < reg) && (timeout > reg)) {
2145ec694b5SJean-Christophe DUBOIS         timeout = reg;
2155ec694b5SJean-Christophe DUBOIS     }
216a50c0d6fSJean-Christophe DUBOIS 
2175ec694b5SJean-Christophe DUBOIS     return timeout;
2185ec694b5SJean-Christophe DUBOIS }
2195ec694b5SJean-Christophe DUBOIS 
22067110c3eSJean-Christophe DUBOIS static void imx_gpt_compute_next_timeout(IMXGPTState *s, bool event)
2215ec694b5SJean-Christophe DUBOIS {
2225ec694b5SJean-Christophe DUBOIS     uint32_t timeout = TIMER_MAX;
2235ec694b5SJean-Christophe DUBOIS     uint32_t count = 0;
2245ec694b5SJean-Christophe DUBOIS     long long limit;
2255ec694b5SJean-Christophe DUBOIS 
2265ec694b5SJean-Christophe DUBOIS     if (!(s->cr & GPT_CR_EN)) {
2275ec694b5SJean-Christophe DUBOIS         /* if not enabled just return */
228a50c0d6fSJean-Christophe DUBOIS         return;
229a50c0d6fSJean-Christophe DUBOIS     }
230a50c0d6fSJean-Christophe DUBOIS 
2315ec694b5SJean-Christophe DUBOIS     if (event) {
2325ec694b5SJean-Christophe DUBOIS         /* This is a timer event  */
2335ec694b5SJean-Christophe DUBOIS 
2345ec694b5SJean-Christophe DUBOIS         if ((s->cr & GPT_CR_FRR)  && (s->next_timeout != TIMER_MAX)) {
235a50c0d6fSJean-Christophe DUBOIS             /*
2365ec694b5SJean-Christophe DUBOIS              * if we are in free running mode and we have not reached
2375ec694b5SJean-Christophe DUBOIS              * the TIMER_MAX limit, then update the count
238a50c0d6fSJean-Christophe DUBOIS              */
23967110c3eSJean-Christophe DUBOIS             count = imx_gpt_update_count(s);
240a50c0d6fSJean-Christophe DUBOIS         }
2415ec694b5SJean-Christophe DUBOIS     } else {
2425ec694b5SJean-Christophe DUBOIS         /* not a timer event, then just update the count */
2435ec694b5SJean-Christophe DUBOIS 
24467110c3eSJean-Christophe DUBOIS         count = imx_gpt_update_count(s);
2455ec694b5SJean-Christophe DUBOIS     }
2465ec694b5SJean-Christophe DUBOIS 
2475ec694b5SJean-Christophe DUBOIS     /* now, find the next timeout related to count */
2485ec694b5SJean-Christophe DUBOIS 
2495ec694b5SJean-Christophe DUBOIS     if (s->ir & GPT_IR_OF1IE) {
25067110c3eSJean-Christophe DUBOIS         timeout = imx_gpt_find_limit(count, s->ocr1, timeout);
2515ec694b5SJean-Christophe DUBOIS     }
2525ec694b5SJean-Christophe DUBOIS     if (s->ir & GPT_IR_OF2IE) {
25367110c3eSJean-Christophe DUBOIS         timeout = imx_gpt_find_limit(count, s->ocr2, timeout);
2545ec694b5SJean-Christophe DUBOIS     }
2555ec694b5SJean-Christophe DUBOIS     if (s->ir & GPT_IR_OF3IE) {
25667110c3eSJean-Christophe DUBOIS         timeout = imx_gpt_find_limit(count, s->ocr3, timeout);
2575ec694b5SJean-Christophe DUBOIS     }
2585ec694b5SJean-Christophe DUBOIS 
2595ec694b5SJean-Christophe DUBOIS     /* find the next set of interrupts to raise for next timer event */
2605ec694b5SJean-Christophe DUBOIS 
2615ec694b5SJean-Christophe DUBOIS     s->next_int = 0;
2625ec694b5SJean-Christophe DUBOIS     if ((s->ir & GPT_IR_OF1IE) && (timeout == s->ocr1)) {
2635ec694b5SJean-Christophe DUBOIS         s->next_int |= GPT_SR_OF1;
2645ec694b5SJean-Christophe DUBOIS     }
2655ec694b5SJean-Christophe DUBOIS     if ((s->ir & GPT_IR_OF2IE) && (timeout == s->ocr2)) {
2665ec694b5SJean-Christophe DUBOIS         s->next_int |= GPT_SR_OF2;
2675ec694b5SJean-Christophe DUBOIS     }
2685ec694b5SJean-Christophe DUBOIS     if ((s->ir & GPT_IR_OF3IE) && (timeout == s->ocr3)) {
2695ec694b5SJean-Christophe DUBOIS         s->next_int |= GPT_SR_OF3;
2705ec694b5SJean-Christophe DUBOIS     }
2715ec694b5SJean-Christophe DUBOIS     if ((s->ir & GPT_IR_ROVIE) && (timeout == TIMER_MAX)) {
2725ec694b5SJean-Christophe DUBOIS         s->next_int |= GPT_SR_ROV;
2735ec694b5SJean-Christophe DUBOIS     }
2745ec694b5SJean-Christophe DUBOIS 
2755ec694b5SJean-Christophe DUBOIS     /* the new range to count down from */
27667110c3eSJean-Christophe DUBOIS     limit = timeout - imx_gpt_update_count(s);
2775ec694b5SJean-Christophe DUBOIS 
2785ec694b5SJean-Christophe DUBOIS     if (limit < 0) {
2795ec694b5SJean-Christophe DUBOIS         /*
2805ec694b5SJean-Christophe DUBOIS          * if we reach here, then QEMU is running too slow and we pass the
2815ec694b5SJean-Christophe DUBOIS          * timeout limit while computing it. Let's deliver the interrupt
2825ec694b5SJean-Christophe DUBOIS          * and compute a new limit.
2835ec694b5SJean-Christophe DUBOIS          */
2845ec694b5SJean-Christophe DUBOIS         s->sr |= s->next_int;
2855ec694b5SJean-Christophe DUBOIS 
28667110c3eSJean-Christophe DUBOIS         imx_gpt_compute_next_timeout(s, event);
2875ec694b5SJean-Christophe DUBOIS 
28867110c3eSJean-Christophe DUBOIS         imx_gpt_update_int(s);
2895ec694b5SJean-Christophe DUBOIS     } else {
2905ec694b5SJean-Christophe DUBOIS         /* New timeout value */
2915ec694b5SJean-Christophe DUBOIS         s->next_timeout = timeout;
2925ec694b5SJean-Christophe DUBOIS 
2935ec694b5SJean-Christophe DUBOIS         /* reset the limit to the computed range */
2945ec694b5SJean-Christophe DUBOIS         ptimer_set_limit(s->timer, limit, 1);
2955ec694b5SJean-Christophe DUBOIS     }
296a50c0d6fSJean-Christophe DUBOIS }
297a50c0d6fSJean-Christophe DUBOIS 
29867110c3eSJean-Christophe DUBOIS static uint64_t imx_gpt_read(void *opaque, hwaddr offset, unsigned size)
299a50c0d6fSJean-Christophe DUBOIS {
30067110c3eSJean-Christophe DUBOIS     IMXGPTState *s = IMX_GPT(opaque);
3015ec694b5SJean-Christophe DUBOIS     uint32_t reg_value = 0;
3025ec694b5SJean-Christophe DUBOIS     uint32_t reg = offset >> 2;
303a50c0d6fSJean-Christophe DUBOIS 
3045ec694b5SJean-Christophe DUBOIS     switch (reg) {
305a50c0d6fSJean-Christophe DUBOIS     case 0: /* Control Register */
3065ec694b5SJean-Christophe DUBOIS         reg_value = s->cr;
3075ec694b5SJean-Christophe DUBOIS         break;
308a50c0d6fSJean-Christophe DUBOIS 
309a50c0d6fSJean-Christophe DUBOIS     case 1: /* prescaler */
3105ec694b5SJean-Christophe DUBOIS         reg_value = s->pr;
3115ec694b5SJean-Christophe DUBOIS         break;
312a50c0d6fSJean-Christophe DUBOIS 
313a50c0d6fSJean-Christophe DUBOIS     case 2: /* Status Register */
3145ec694b5SJean-Christophe DUBOIS         reg_value = s->sr;
3155ec694b5SJean-Christophe DUBOIS         break;
316a50c0d6fSJean-Christophe DUBOIS 
317a50c0d6fSJean-Christophe DUBOIS     case 3: /* Interrupt Register */
3185ec694b5SJean-Christophe DUBOIS         reg_value = s->ir;
3195ec694b5SJean-Christophe DUBOIS         break;
320a50c0d6fSJean-Christophe DUBOIS 
321a50c0d6fSJean-Christophe DUBOIS     case 4: /* Output Compare Register 1 */
3225ec694b5SJean-Christophe DUBOIS         reg_value = s->ocr1;
3235ec694b5SJean-Christophe DUBOIS         break;
324a50c0d6fSJean-Christophe DUBOIS 
325a50c0d6fSJean-Christophe DUBOIS     case 5: /* Output Compare Register 2 */
3265ec694b5SJean-Christophe DUBOIS         reg_value = s->ocr2;
3275ec694b5SJean-Christophe DUBOIS         break;
328a50c0d6fSJean-Christophe DUBOIS 
329a50c0d6fSJean-Christophe DUBOIS     case 6: /* Output Compare Register 3 */
3305ec694b5SJean-Christophe DUBOIS         reg_value = s->ocr3;
3315ec694b5SJean-Christophe DUBOIS         break;
332a50c0d6fSJean-Christophe DUBOIS 
333a50c0d6fSJean-Christophe DUBOIS     case 7: /* input Capture Register 1 */
3345ec694b5SJean-Christophe DUBOIS         qemu_log_mask(LOG_UNIMP, "icr1 feature is not implemented\n");
3355ec694b5SJean-Christophe DUBOIS         reg_value = s->icr1;
3365ec694b5SJean-Christophe DUBOIS         break;
337a50c0d6fSJean-Christophe DUBOIS 
338a50c0d6fSJean-Christophe DUBOIS     case 8: /* input Capture Register 2 */
3395ec694b5SJean-Christophe DUBOIS         qemu_log_mask(LOG_UNIMP, "icr2 feature is not implemented\n");
3405ec694b5SJean-Christophe DUBOIS         reg_value = s->icr2;
3415ec694b5SJean-Christophe DUBOIS         break;
342a50c0d6fSJean-Christophe DUBOIS 
343a50c0d6fSJean-Christophe DUBOIS     case 9: /* cnt */
34467110c3eSJean-Christophe DUBOIS         imx_gpt_update_count(s);
3455ec694b5SJean-Christophe DUBOIS         reg_value = s->cnt;
3465ec694b5SJean-Christophe DUBOIS         break;
3475ec694b5SJean-Christophe DUBOIS 
3485ec694b5SJean-Christophe DUBOIS     default:
3495ec694b5SJean-Christophe DUBOIS         IPRINTF("Bad offset %x\n", reg);
3505ec694b5SJean-Christophe DUBOIS         break;
351a50c0d6fSJean-Christophe DUBOIS     }
352a50c0d6fSJean-Christophe DUBOIS 
35367110c3eSJean-Christophe DUBOIS     DPRINTF("(%s) = 0x%08x\n", imx_gpt_reg_name(reg), reg_value);
354a50c0d6fSJean-Christophe DUBOIS 
3555ec694b5SJean-Christophe DUBOIS     return reg_value;
356a50c0d6fSJean-Christophe DUBOIS }
357a50c0d6fSJean-Christophe DUBOIS 
35867110c3eSJean-Christophe DUBOIS static void imx_gpt_reset(DeviceState *dev)
359a50c0d6fSJean-Christophe DUBOIS {
36067110c3eSJean-Christophe DUBOIS     IMXGPTState *s = IMX_GPT(dev);
361a50c0d6fSJean-Christophe DUBOIS 
3625ec694b5SJean-Christophe DUBOIS     /* stop timer */
3635ec694b5SJean-Christophe DUBOIS     ptimer_stop(s->timer);
3645ec694b5SJean-Christophe DUBOIS 
365a50c0d6fSJean-Christophe DUBOIS     /*
366a50c0d6fSJean-Christophe DUBOIS      * Soft reset doesn't touch some bits; hard reset clears them
367a50c0d6fSJean-Christophe DUBOIS      */
368a50c0d6fSJean-Christophe DUBOIS     s->cr &= ~(GPT_CR_EN|GPT_CR_ENMOD|GPT_CR_STOPEN|GPT_CR_DOZEN|
369a50c0d6fSJean-Christophe DUBOIS                GPT_CR_WAITEN|GPT_CR_DBGEN);
370a50c0d6fSJean-Christophe DUBOIS     s->sr = 0;
371a50c0d6fSJean-Christophe DUBOIS     s->pr = 0;
372a50c0d6fSJean-Christophe DUBOIS     s->ir = 0;
373a50c0d6fSJean-Christophe DUBOIS     s->cnt = 0;
374a50c0d6fSJean-Christophe DUBOIS     s->ocr1 = TIMER_MAX;
375a50c0d6fSJean-Christophe DUBOIS     s->ocr2 = TIMER_MAX;
376a50c0d6fSJean-Christophe DUBOIS     s->ocr3 = TIMER_MAX;
377a50c0d6fSJean-Christophe DUBOIS     s->icr1 = 0;
378a50c0d6fSJean-Christophe DUBOIS     s->icr2 = 0;
3795ec694b5SJean-Christophe DUBOIS 
3805ec694b5SJean-Christophe DUBOIS     s->next_timeout = TIMER_MAX;
3815ec694b5SJean-Christophe DUBOIS     s->next_int = 0;
3825ec694b5SJean-Christophe DUBOIS 
3835ec694b5SJean-Christophe DUBOIS     /* compute new freq */
38467110c3eSJean-Christophe DUBOIS     imx_gpt_set_freq(s);
3855ec694b5SJean-Christophe DUBOIS 
3865ec694b5SJean-Christophe DUBOIS     /* reset the limit to TIMER_MAX */
3875ec694b5SJean-Christophe DUBOIS     ptimer_set_limit(s->timer, TIMER_MAX, 1);
3885ec694b5SJean-Christophe DUBOIS 
3895ec694b5SJean-Christophe DUBOIS     /* if the timer is still enabled, restart it */
3905ec694b5SJean-Christophe DUBOIS     if (s->freq && (s->cr & GPT_CR_EN)) {
3915ec694b5SJean-Christophe DUBOIS         ptimer_run(s->timer, 1);
3925ec694b5SJean-Christophe DUBOIS     }
393a50c0d6fSJean-Christophe DUBOIS }
394a50c0d6fSJean-Christophe DUBOIS 
39567110c3eSJean-Christophe DUBOIS static void imx_gpt_write(void *opaque, hwaddr offset, uint64_t value,
39667110c3eSJean-Christophe DUBOIS                           unsigned size)
397a50c0d6fSJean-Christophe DUBOIS {
39867110c3eSJean-Christophe DUBOIS     IMXGPTState *s = IMX_GPT(opaque);
3995ec694b5SJean-Christophe DUBOIS     uint32_t oldreg;
4005ec694b5SJean-Christophe DUBOIS     uint32_t reg = offset >> 2;
401a50c0d6fSJean-Christophe DUBOIS 
40267110c3eSJean-Christophe DUBOIS     DPRINTF("(%s, value = 0x%08x)\n", imx_gpt_reg_name(reg),
4035ec694b5SJean-Christophe DUBOIS             (uint32_t)value);
404a50c0d6fSJean-Christophe DUBOIS 
4055ec694b5SJean-Christophe DUBOIS     switch (reg) {
4065ec694b5SJean-Christophe DUBOIS     case 0:
4075ec694b5SJean-Christophe DUBOIS         oldreg = s->cr;
4085ec694b5SJean-Christophe DUBOIS         s->cr = value & ~0x7c14;
4095ec694b5SJean-Christophe DUBOIS         if (s->cr & GPT_CR_SWR) { /* force reset */
4105ec694b5SJean-Christophe DUBOIS             /* handle the reset */
41167110c3eSJean-Christophe DUBOIS             imx_gpt_reset(DEVICE(s));
412a50c0d6fSJean-Christophe DUBOIS         } else {
4135ec694b5SJean-Christophe DUBOIS             /* set our freq, as the source might have changed */
41467110c3eSJean-Christophe DUBOIS             imx_gpt_set_freq(s);
4155ec694b5SJean-Christophe DUBOIS 
4165ec694b5SJean-Christophe DUBOIS             if ((oldreg ^ s->cr) & GPT_CR_EN) {
4175ec694b5SJean-Christophe DUBOIS                 if (s->cr & GPT_CR_EN) {
4185ec694b5SJean-Christophe DUBOIS                     if (s->cr & GPT_CR_ENMOD) {
4195ec694b5SJean-Christophe DUBOIS                         s->next_timeout = TIMER_MAX;
4205ec694b5SJean-Christophe DUBOIS                         ptimer_set_count(s->timer, TIMER_MAX);
42167110c3eSJean-Christophe DUBOIS                         imx_gpt_compute_next_timeout(s, false);
4225ec694b5SJean-Christophe DUBOIS                     }
4235ec694b5SJean-Christophe DUBOIS                     ptimer_run(s->timer, 1);
4245ec694b5SJean-Christophe DUBOIS                 } else {
4255ec694b5SJean-Christophe DUBOIS                     /* stop timer */
426a50c0d6fSJean-Christophe DUBOIS                     ptimer_stop(s->timer);
427a50c0d6fSJean-Christophe DUBOIS                 }
428a50c0d6fSJean-Christophe DUBOIS             }
4295ec694b5SJean-Christophe DUBOIS         }
4305ec694b5SJean-Christophe DUBOIS         break;
431a50c0d6fSJean-Christophe DUBOIS 
432a50c0d6fSJean-Christophe DUBOIS     case 1: /* Prescaler */
433a50c0d6fSJean-Christophe DUBOIS         s->pr = value & 0xfff;
43467110c3eSJean-Christophe DUBOIS         imx_gpt_set_freq(s);
4355ec694b5SJean-Christophe DUBOIS         break;
436a50c0d6fSJean-Christophe DUBOIS 
437a50c0d6fSJean-Christophe DUBOIS     case 2: /* SR */
4385ec694b5SJean-Christophe DUBOIS         s->sr &= ~(value & 0x3f);
43967110c3eSJean-Christophe DUBOIS         imx_gpt_update_int(s);
4405ec694b5SJean-Christophe DUBOIS         break;
441a50c0d6fSJean-Christophe DUBOIS 
442a50c0d6fSJean-Christophe DUBOIS     case 3: /* IR -- interrupt register */
443a50c0d6fSJean-Christophe DUBOIS         s->ir = value & 0x3f;
44467110c3eSJean-Christophe DUBOIS         imx_gpt_update_int(s);
4455ec694b5SJean-Christophe DUBOIS 
44667110c3eSJean-Christophe DUBOIS         imx_gpt_compute_next_timeout(s, false);
4475ec694b5SJean-Christophe DUBOIS 
4485ec694b5SJean-Christophe DUBOIS         break;
449a50c0d6fSJean-Christophe DUBOIS 
450a50c0d6fSJean-Christophe DUBOIS     case 4: /* OCR1 -- output compare register */
4515ec694b5SJean-Christophe DUBOIS         s->ocr1 = value;
4525ec694b5SJean-Christophe DUBOIS 
453a50c0d6fSJean-Christophe DUBOIS         /* In non-freerun mode, reset count when this register is written */
454a50c0d6fSJean-Christophe DUBOIS         if (!(s->cr & GPT_CR_FRR)) {
4555ec694b5SJean-Christophe DUBOIS             s->next_timeout = TIMER_MAX;
4565ec694b5SJean-Christophe DUBOIS             ptimer_set_limit(s->timer, TIMER_MAX, 1);
457a50c0d6fSJean-Christophe DUBOIS         }
4585ec694b5SJean-Christophe DUBOIS 
4595ec694b5SJean-Christophe DUBOIS         /* compute the new timeout */
46067110c3eSJean-Christophe DUBOIS         imx_gpt_compute_next_timeout(s, false);
4615ec694b5SJean-Christophe DUBOIS 
4625ec694b5SJean-Christophe DUBOIS         break;
463a50c0d6fSJean-Christophe DUBOIS 
464a50c0d6fSJean-Christophe DUBOIS     case 5: /* OCR2 -- output compare register */
4655ec694b5SJean-Christophe DUBOIS         s->ocr2 = value;
4665ec694b5SJean-Christophe DUBOIS 
4675ec694b5SJean-Christophe DUBOIS         /* compute the new timeout */
46867110c3eSJean-Christophe DUBOIS         imx_gpt_compute_next_timeout(s, false);
4695ec694b5SJean-Christophe DUBOIS 
4705ec694b5SJean-Christophe DUBOIS         break;
4715ec694b5SJean-Christophe DUBOIS 
472a50c0d6fSJean-Christophe DUBOIS     case 6: /* OCR3 -- output compare register */
4735ec694b5SJean-Christophe DUBOIS         s->ocr3 = value;
4745ec694b5SJean-Christophe DUBOIS 
4755ec694b5SJean-Christophe DUBOIS         /* compute the new timeout */
47667110c3eSJean-Christophe DUBOIS         imx_gpt_compute_next_timeout(s, false);
4775ec694b5SJean-Christophe DUBOIS 
4785ec694b5SJean-Christophe DUBOIS         break;
4795ec694b5SJean-Christophe DUBOIS 
480a50c0d6fSJean-Christophe DUBOIS     default:
4815ec694b5SJean-Christophe DUBOIS         IPRINTF("Bad offset %x\n", reg);
4825ec694b5SJean-Christophe DUBOIS         break;
483a50c0d6fSJean-Christophe DUBOIS     }
484a50c0d6fSJean-Christophe DUBOIS }
485a50c0d6fSJean-Christophe DUBOIS 
48667110c3eSJean-Christophe DUBOIS static void imx_gpt_timeout(void *opaque)
487a50c0d6fSJean-Christophe DUBOIS {
48867110c3eSJean-Christophe DUBOIS     IMXGPTState *s = IMX_GPT(opaque);
489a50c0d6fSJean-Christophe DUBOIS 
4905ec694b5SJean-Christophe DUBOIS     DPRINTF("\n");
491a50c0d6fSJean-Christophe DUBOIS 
4925ec694b5SJean-Christophe DUBOIS     s->sr |= s->next_int;
4935ec694b5SJean-Christophe DUBOIS     s->next_int = 0;
494a50c0d6fSJean-Christophe DUBOIS 
49567110c3eSJean-Christophe DUBOIS     imx_gpt_compute_next_timeout(s, true);
4965ec694b5SJean-Christophe DUBOIS 
49767110c3eSJean-Christophe DUBOIS     imx_gpt_update_int(s);
4985ec694b5SJean-Christophe DUBOIS 
4995ec694b5SJean-Christophe DUBOIS     if (s->freq && (s->cr & GPT_CR_EN)) {
5005ec694b5SJean-Christophe DUBOIS         ptimer_run(s->timer, 1);
5015ec694b5SJean-Christophe DUBOIS     }
502a50c0d6fSJean-Christophe DUBOIS }
503a50c0d6fSJean-Christophe DUBOIS 
50467110c3eSJean-Christophe DUBOIS static const MemoryRegionOps imx_gpt_ops = {
50567110c3eSJean-Christophe DUBOIS     .read = imx_gpt_read,
50667110c3eSJean-Christophe DUBOIS     .write = imx_gpt_write,
507a50c0d6fSJean-Christophe DUBOIS     .endianness = DEVICE_NATIVE_ENDIAN,
508a50c0d6fSJean-Christophe DUBOIS };
509a50c0d6fSJean-Christophe DUBOIS 
510a50c0d6fSJean-Christophe DUBOIS 
51167110c3eSJean-Christophe DUBOIS static void imx_gpt_realize(DeviceState *dev, Error **errp)
512a50c0d6fSJean-Christophe DUBOIS {
51367110c3eSJean-Christophe DUBOIS     IMXGPTState *s = IMX_GPT(dev);
51467110c3eSJean-Christophe DUBOIS     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
515a50c0d6fSJean-Christophe DUBOIS     QEMUBH *bh;
516a50c0d6fSJean-Christophe DUBOIS 
51767110c3eSJean-Christophe DUBOIS     sysbus_init_irq(sbd, &s->irq);
518853dca12SPaolo Bonzini     memory_region_init_io(&s->iomem, OBJECT(s), &imx_gpt_ops, s, TYPE_IMX_GPT,
519a50c0d6fSJean-Christophe DUBOIS                           0x00001000);
52067110c3eSJean-Christophe DUBOIS     sysbus_init_mmio(sbd, &s->iomem);
521a50c0d6fSJean-Christophe DUBOIS 
52267110c3eSJean-Christophe DUBOIS     bh = qemu_bh_new(imx_gpt_timeout, s);
523a50c0d6fSJean-Christophe DUBOIS     s->timer = ptimer_init(bh);
524a50c0d6fSJean-Christophe DUBOIS }
525a50c0d6fSJean-Christophe DUBOIS 
5265ec694b5SJean-Christophe DUBOIS void imx_timerg_create(const hwaddr addr, qemu_irq irq, DeviceState *ccm)
527a50c0d6fSJean-Christophe DUBOIS {
52867110c3eSJean-Christophe DUBOIS     IMXGPTState *pp;
529a50c0d6fSJean-Christophe DUBOIS     DeviceState *dev;
530a50c0d6fSJean-Christophe DUBOIS 
5315ec694b5SJean-Christophe DUBOIS     dev = sysbus_create_simple(TYPE_IMX_GPT, addr, irq);
53267110c3eSJean-Christophe DUBOIS     pp = IMX_GPT(dev);
533a50c0d6fSJean-Christophe DUBOIS     pp->ccm = ccm;
534a50c0d6fSJean-Christophe DUBOIS }
535a50c0d6fSJean-Christophe DUBOIS 
53667110c3eSJean-Christophe DUBOIS static void imx_gpt_class_init(ObjectClass *klass, void *data)
537a50c0d6fSJean-Christophe DUBOIS {
538a50c0d6fSJean-Christophe DUBOIS     DeviceClass *dc = DEVICE_CLASS(klass);
53967110c3eSJean-Christophe DUBOIS 
54067110c3eSJean-Christophe DUBOIS     dc->realize = imx_gpt_realize;
54167110c3eSJean-Christophe DUBOIS     dc->reset = imx_gpt_reset;
54267110c3eSJean-Christophe DUBOIS     dc->vmsd = &vmstate_imx_timer_gpt;
543a50c0d6fSJean-Christophe DUBOIS     dc->desc = "i.MX general timer";
544a50c0d6fSJean-Christophe DUBOIS }
545a50c0d6fSJean-Christophe DUBOIS 
54667110c3eSJean-Christophe DUBOIS static const TypeInfo imx_gpt_info = {
5475ec694b5SJean-Christophe DUBOIS     .name = TYPE_IMX_GPT,
548a50c0d6fSJean-Christophe DUBOIS     .parent = TYPE_SYS_BUS_DEVICE,
54967110c3eSJean-Christophe DUBOIS     .instance_size = sizeof(IMXGPTState),
55067110c3eSJean-Christophe DUBOIS     .class_init = imx_gpt_class_init,
551a50c0d6fSJean-Christophe DUBOIS };
552a50c0d6fSJean-Christophe DUBOIS 
55367110c3eSJean-Christophe DUBOIS static void imx_gpt_register_types(void)
554a50c0d6fSJean-Christophe DUBOIS {
55567110c3eSJean-Christophe DUBOIS     type_register_static(&imx_gpt_info);
556a50c0d6fSJean-Christophe DUBOIS }
557a50c0d6fSJean-Christophe DUBOIS 
55867110c3eSJean-Christophe DUBOIS type_init(imx_gpt_register_types)
559