xref: /openbmc/qemu/hw/timer/imx_gpt.c (revision e3d0814368d00e7985c31edf5d0cfce45972d4be)
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
8d647b26dSJean-Christophe Dubois  * Updated by Jean-Christophe Dubois <jcd@tribudubois.net>
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 
158ef94f0bSPeter Maydell #include "qemu/osdep.h"
1664552b6bSMarkus Armbruster #include "hw/irq.h"
17d647b26dSJean-Christophe Dubois #include "hw/timer/imx_gpt.h"
18d6454270SMarkus Armbruster #include "migration/vmstate.h"
190b8fa32fSMarkus Armbruster #include "qemu/module.h"
2003dd024fSPaolo Bonzini #include "qemu/log.h"
21a50c0d6fSJean-Christophe DUBOIS 
2205453526SJean-Christophe Dubois #ifndef DEBUG_IMX_GPT
2305453526SJean-Christophe Dubois #define DEBUG_IMX_GPT 0
2405453526SJean-Christophe Dubois #endif
2505453526SJean-Christophe Dubois 
2605453526SJean-Christophe Dubois #define DPRINTF(fmt, args...) \
2705453526SJean-Christophe Dubois     do { \
2805453526SJean-Christophe Dubois         if (DEBUG_IMX_GPT) { \
2905453526SJean-Christophe Dubois             fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_GPT, \
3005453526SJean-Christophe Dubois                                              __func__, ##args); \
3105453526SJean-Christophe Dubois         } \
3205453526SJean-Christophe Dubois     } while (0)
335ec694b5SJean-Christophe DUBOIS 
34d675765aSPeter Maydell static const char *imx_gpt_reg_name(uint32_t reg)
355ec694b5SJean-Christophe DUBOIS {
365ec694b5SJean-Christophe DUBOIS     switch (reg) {
375ec694b5SJean-Christophe DUBOIS     case 0:
385ec694b5SJean-Christophe DUBOIS         return "CR";
395ec694b5SJean-Christophe DUBOIS     case 1:
405ec694b5SJean-Christophe DUBOIS         return "PR";
415ec694b5SJean-Christophe DUBOIS     case 2:
425ec694b5SJean-Christophe DUBOIS         return "SR";
435ec694b5SJean-Christophe DUBOIS     case 3:
445ec694b5SJean-Christophe DUBOIS         return "IR";
455ec694b5SJean-Christophe DUBOIS     case 4:
465ec694b5SJean-Christophe DUBOIS         return "OCR1";
475ec694b5SJean-Christophe DUBOIS     case 5:
485ec694b5SJean-Christophe DUBOIS         return "OCR2";
495ec694b5SJean-Christophe DUBOIS     case 6:
505ec694b5SJean-Christophe DUBOIS         return "OCR3";
515ec694b5SJean-Christophe DUBOIS     case 7:
525ec694b5SJean-Christophe DUBOIS         return "ICR1";
535ec694b5SJean-Christophe DUBOIS     case 8:
545ec694b5SJean-Christophe DUBOIS         return "ICR2";
555ec694b5SJean-Christophe DUBOIS     case 9:
565ec694b5SJean-Christophe DUBOIS         return "CNT";
575ec694b5SJean-Christophe DUBOIS     default:
585ec694b5SJean-Christophe DUBOIS         return "[?]";
595ec694b5SJean-Christophe DUBOIS     }
605ec694b5SJean-Christophe DUBOIS }
615ec694b5SJean-Christophe DUBOIS 
6267110c3eSJean-Christophe DUBOIS static const VMStateDescription vmstate_imx_timer_gpt = {
6368b85290SJean-Christophe Dubois     .name = TYPE_IMX_GPT,
645ec694b5SJean-Christophe DUBOIS     .version_id = 3,
655ec694b5SJean-Christophe DUBOIS     .minimum_version_id = 3,
66ba324b3fSRichard Henderson     .fields = (const VMStateField[]) {
6767110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(cr, IMXGPTState),
6867110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(pr, IMXGPTState),
6967110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(sr, IMXGPTState),
7067110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(ir, IMXGPTState),
7167110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(ocr1, IMXGPTState),
7267110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(ocr2, IMXGPTState),
7367110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(ocr3, IMXGPTState),
7467110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(icr1, IMXGPTState),
7567110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(icr2, IMXGPTState),
7667110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(cnt, IMXGPTState),
7767110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(next_timeout, IMXGPTState),
7867110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(next_int, IMXGPTState),
7967110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(freq, IMXGPTState),
8067110c3eSJean-Christophe DUBOIS         VMSTATE_PTIMER(timer, IMXGPTState),
81a50c0d6fSJean-Christophe DUBOIS         VMSTATE_END_OF_LIST()
82a50c0d6fSJean-Christophe DUBOIS     }
83a50c0d6fSJean-Christophe DUBOIS };
84a50c0d6fSJean-Christophe DUBOIS 
8566542f63SJean-Christophe Dubois static const IMXClk imx25_gpt_clocks[] = {
8666542f63SJean-Christophe Dubois     CLK_NONE,      /* 000 No clock source */
8766542f63SJean-Christophe Dubois     CLK_IPG,       /* 001 ipg_clk, 532MHz*/
8866542f63SJean-Christophe Dubois     CLK_IPG_HIGH,  /* 010 ipg_clk_highfreq */
8966542f63SJean-Christophe Dubois     CLK_NONE,      /* 011 not defined */
9066542f63SJean-Christophe Dubois     CLK_32k,       /* 100 ipg_clk_32k */
9166542f63SJean-Christophe Dubois     CLK_32k,       /* 101 ipg_clk_32k */
9266542f63SJean-Christophe Dubois     CLK_32k,       /* 110 ipg_clk_32k */
9366542f63SJean-Christophe Dubois     CLK_32k,       /* 111 ipg_clk_32k */
9466542f63SJean-Christophe Dubois };
9566542f63SJean-Christophe Dubois 
9666542f63SJean-Christophe Dubois static const IMXClk imx31_gpt_clocks[] = {
97c91a5883SJean-Christophe Dubois     CLK_NONE,      /* 000 No clock source */
98aaa9ec3bSJean-Christophe Dubois     CLK_IPG,       /* 001 ipg_clk, 532MHz*/
99d552f675SJean-Christophe Dubois     CLK_IPG_HIGH,  /* 010 ipg_clk_highfreq */
100c91a5883SJean-Christophe Dubois     CLK_NONE,      /* 011 not defined */
101a50c0d6fSJean-Christophe DUBOIS     CLK_32k,       /* 100 ipg_clk_32k */
102c91a5883SJean-Christophe Dubois     CLK_NONE,      /* 101 not defined */
103c91a5883SJean-Christophe Dubois     CLK_NONE,      /* 110 not defined */
104c91a5883SJean-Christophe Dubois     CLK_NONE,      /* 111 not defined */
105a50c0d6fSJean-Christophe DUBOIS };
106a50c0d6fSJean-Christophe DUBOIS 
10766542f63SJean-Christophe Dubois static const IMXClk imx6_gpt_clocks[] = {
10866542f63SJean-Christophe Dubois     CLK_NONE,      /* 000 No clock source */
10966542f63SJean-Christophe Dubois     CLK_IPG,       /* 001 ipg_clk, 532MHz*/
11066542f63SJean-Christophe Dubois     CLK_IPG_HIGH,  /* 010 ipg_clk_highfreq */
11166542f63SJean-Christophe Dubois     CLK_EXT,       /* 011 External clock */
11266542f63SJean-Christophe Dubois     CLK_32k,       /* 100 ipg_clk_32k */
11366542f63SJean-Christophe Dubois     CLK_HIGH_DIV,  /* 101 reference clock / 8 */
11466542f63SJean-Christophe Dubois     CLK_NONE,      /* 110 not defined */
11566542f63SJean-Christophe Dubois     CLK_HIGH,      /* 111 reference clock */
11666542f63SJean-Christophe Dubois };
11766542f63SJean-Christophe Dubois 
118a1e03956SJean-Christophe Dubois static const IMXClk imx6ul_gpt_clocks[] = {
119a1e03956SJean-Christophe Dubois     CLK_NONE,      /* 000 No clock source */
120a1e03956SJean-Christophe Dubois     CLK_IPG,       /* 001 ipg_clk, 532MHz*/
121a1e03956SJean-Christophe Dubois     CLK_IPG_HIGH,  /* 010 ipg_clk_highfreq */
122a1e03956SJean-Christophe Dubois     CLK_EXT,       /* 011 External clock */
123a1e03956SJean-Christophe Dubois     CLK_32k,       /* 100 ipg_clk_32k */
124a1e03956SJean-Christophe Dubois     CLK_NONE,      /* 101 not defined */
125a1e03956SJean-Christophe Dubois     CLK_NONE,      /* 110 not defined */
126a1e03956SJean-Christophe Dubois     CLK_NONE,      /* 111 not defined */
127a1e03956SJean-Christophe Dubois };
128a1e03956SJean-Christophe Dubois 
129a62bf59fSAndrey Smirnov static const IMXClk imx7_gpt_clocks[] = {
130a62bf59fSAndrey Smirnov     CLK_NONE,      /* 000 No clock source */
131a62bf59fSAndrey Smirnov     CLK_IPG,       /* 001 ipg_clk, 532MHz*/
132a62bf59fSAndrey Smirnov     CLK_IPG_HIGH,  /* 010 ipg_clk_highfreq */
133a62bf59fSAndrey Smirnov     CLK_EXT,       /* 011 External clock */
134a62bf59fSAndrey Smirnov     CLK_32k,       /* 100 ipg_clk_32k */
135a62bf59fSAndrey Smirnov     CLK_HIGH,      /* 101 reference clock */
136a62bf59fSAndrey Smirnov     CLK_NONE,      /* 110 not defined */
137a62bf59fSAndrey Smirnov     CLK_NONE,      /* 111 not defined */
138a62bf59fSAndrey Smirnov };
139a62bf59fSAndrey Smirnov 
1401b914994SPeter Maydell /* Must be called from within ptimer_transaction_begin/commit block */
14167110c3eSJean-Christophe DUBOIS static void imx_gpt_set_freq(IMXGPTState *s)
142a50c0d6fSJean-Christophe DUBOIS {
1435ec694b5SJean-Christophe DUBOIS     uint32_t clksrc = extract32(s->cr, GPT_CR_CLKSRC_SHIFT, 3);
144a50c0d6fSJean-Christophe DUBOIS 
145aaa9ec3bSJean-Christophe Dubois     s->freq = imx_ccm_get_clock_frequency(s->ccm,
14666542f63SJean-Christophe Dubois                                           s->clocks[clksrc]) / (1 + s->pr);
147a50c0d6fSJean-Christophe DUBOIS 
148aaa9ec3bSJean-Christophe Dubois     DPRINTF("Setting clksrc %d to frequency %d\n", clksrc, s->freq);
149aaa9ec3bSJean-Christophe Dubois 
150aaa9ec3bSJean-Christophe Dubois     if (s->freq) {
151aaa9ec3bSJean-Christophe Dubois         ptimer_set_freq(s->timer, s->freq);
152a50c0d6fSJean-Christophe DUBOIS     }
153a50c0d6fSJean-Christophe DUBOIS }
154a50c0d6fSJean-Christophe DUBOIS 
15567110c3eSJean-Christophe DUBOIS static void imx_gpt_update_int(IMXGPTState *s)
156a50c0d6fSJean-Christophe DUBOIS {
1575ec694b5SJean-Christophe DUBOIS     if ((s->sr & s->ir) && (s->cr & GPT_CR_EN)) {
1585ec694b5SJean-Christophe DUBOIS         qemu_irq_raise(s->irq);
1595ec694b5SJean-Christophe DUBOIS     } else {
1605ec694b5SJean-Christophe DUBOIS         qemu_irq_lower(s->irq);
1615ec694b5SJean-Christophe DUBOIS     }
162a50c0d6fSJean-Christophe DUBOIS }
163a50c0d6fSJean-Christophe DUBOIS 
16467110c3eSJean-Christophe DUBOIS static uint32_t imx_gpt_update_count(IMXGPTState *s)
165a50c0d6fSJean-Christophe DUBOIS {
1665ec694b5SJean-Christophe DUBOIS     s->cnt = s->next_timeout - (uint32_t)ptimer_get_count(s->timer);
1675ec694b5SJean-Christophe DUBOIS 
168a50c0d6fSJean-Christophe DUBOIS     return s->cnt;
169a50c0d6fSJean-Christophe DUBOIS }
170a50c0d6fSJean-Christophe DUBOIS 
17167110c3eSJean-Christophe DUBOIS static inline uint32_t imx_gpt_find_limit(uint32_t count, uint32_t reg,
1725ec694b5SJean-Christophe DUBOIS                                           uint32_t timeout)
173a50c0d6fSJean-Christophe DUBOIS {
1745ec694b5SJean-Christophe DUBOIS     if ((count < reg) && (timeout > reg)) {
1755ec694b5SJean-Christophe DUBOIS         timeout = reg;
1765ec694b5SJean-Christophe DUBOIS     }
177a50c0d6fSJean-Christophe DUBOIS 
1785ec694b5SJean-Christophe DUBOIS     return timeout;
1795ec694b5SJean-Christophe DUBOIS }
1805ec694b5SJean-Christophe DUBOIS 
1811b914994SPeter Maydell /* Must be called from within ptimer_transaction_begin/commit block */
18267110c3eSJean-Christophe DUBOIS static void imx_gpt_compute_next_timeout(IMXGPTState *s, bool event)
1835ec694b5SJean-Christophe DUBOIS {
184203d65a4SMichael Tokarev     uint32_t timeout = GPT_TIMER_MAX;
1854833e15fSJean-Christophe Dubois     uint32_t count;
1865ec694b5SJean-Christophe DUBOIS     long long limit;
1875ec694b5SJean-Christophe DUBOIS 
1885ec694b5SJean-Christophe DUBOIS     if (!(s->cr & GPT_CR_EN)) {
1895ec694b5SJean-Christophe DUBOIS         /* if not enabled just return */
190a50c0d6fSJean-Christophe DUBOIS         return;
191a50c0d6fSJean-Christophe DUBOIS     }
192a50c0d6fSJean-Christophe DUBOIS 
1934833e15fSJean-Christophe Dubois     /* update the count */
1944833e15fSJean-Christophe Dubois     count = imx_gpt_update_count(s);
1954833e15fSJean-Christophe Dubois 
1965ec694b5SJean-Christophe DUBOIS     if (event) {
197a50c0d6fSJean-Christophe DUBOIS         /*
1984833e15fSJean-Christophe Dubois          * This is an event (the ptimer reached 0 and stopped), and the
1994833e15fSJean-Christophe Dubois          * timer counter is now equal to s->next_timeout.
200a50c0d6fSJean-Christophe DUBOIS          */
2014833e15fSJean-Christophe Dubois         if (!(s->cr & GPT_CR_FRR) && (count == s->ocr1)) {
2024833e15fSJean-Christophe Dubois             /* We are in restart mode and we crossed the compare channel 1
2034833e15fSJean-Christophe Dubois              * value. We need to reset the counter to 0.
2044833e15fSJean-Christophe Dubois              */
2054833e15fSJean-Christophe Dubois             count = s->cnt = s->next_timeout = 0;
2064833e15fSJean-Christophe Dubois         } else if (count == GPT_TIMER_MAX) {
2074833e15fSJean-Christophe Dubois             /* We reached GPT_TIMER_MAX so we need to rollover */
2084833e15fSJean-Christophe Dubois             count = s->cnt = s->next_timeout = 0;
209a50c0d6fSJean-Christophe DUBOIS         }
2105ec694b5SJean-Christophe DUBOIS     }
2115ec694b5SJean-Christophe DUBOIS 
2125ec694b5SJean-Christophe DUBOIS     /* now, find the next timeout related to count */
2135ec694b5SJean-Christophe DUBOIS 
2145ec694b5SJean-Christophe DUBOIS     if (s->ir & GPT_IR_OF1IE) {
21567110c3eSJean-Christophe DUBOIS         timeout = imx_gpt_find_limit(count, s->ocr1, timeout);
2165ec694b5SJean-Christophe DUBOIS     }
2175ec694b5SJean-Christophe DUBOIS     if (s->ir & GPT_IR_OF2IE) {
21867110c3eSJean-Christophe DUBOIS         timeout = imx_gpt_find_limit(count, s->ocr2, timeout);
2195ec694b5SJean-Christophe DUBOIS     }
2205ec694b5SJean-Christophe DUBOIS     if (s->ir & GPT_IR_OF3IE) {
22167110c3eSJean-Christophe DUBOIS         timeout = imx_gpt_find_limit(count, s->ocr3, timeout);
2225ec694b5SJean-Christophe DUBOIS     }
2235ec694b5SJean-Christophe DUBOIS 
2245ec694b5SJean-Christophe DUBOIS     /* find the next set of interrupts to raise for next timer event */
2255ec694b5SJean-Christophe DUBOIS 
2265ec694b5SJean-Christophe DUBOIS     s->next_int = 0;
2275ec694b5SJean-Christophe DUBOIS     if ((s->ir & GPT_IR_OF1IE) && (timeout == s->ocr1)) {
2285ec694b5SJean-Christophe DUBOIS         s->next_int |= GPT_SR_OF1;
2295ec694b5SJean-Christophe DUBOIS     }
2305ec694b5SJean-Christophe DUBOIS     if ((s->ir & GPT_IR_OF2IE) && (timeout == s->ocr2)) {
2315ec694b5SJean-Christophe DUBOIS         s->next_int |= GPT_SR_OF2;
2325ec694b5SJean-Christophe DUBOIS     }
2335ec694b5SJean-Christophe DUBOIS     if ((s->ir & GPT_IR_OF3IE) && (timeout == s->ocr3)) {
2345ec694b5SJean-Christophe DUBOIS         s->next_int |= GPT_SR_OF3;
2355ec694b5SJean-Christophe DUBOIS     }
236203d65a4SMichael Tokarev     if ((s->ir & GPT_IR_ROVIE) && (timeout == GPT_TIMER_MAX)) {
2375ec694b5SJean-Christophe DUBOIS         s->next_int |= GPT_SR_ROV;
2385ec694b5SJean-Christophe DUBOIS     }
2395ec694b5SJean-Christophe DUBOIS 
2405ec694b5SJean-Christophe DUBOIS     /* the new range to count down from */
24167110c3eSJean-Christophe DUBOIS     limit = timeout - imx_gpt_update_count(s);
2425ec694b5SJean-Christophe DUBOIS 
2435ec694b5SJean-Christophe DUBOIS     if (limit < 0) {
2445ec694b5SJean-Christophe DUBOIS         /*
2455ec694b5SJean-Christophe DUBOIS          * if we reach here, then QEMU is running too slow and we pass the
2465ec694b5SJean-Christophe DUBOIS          * timeout limit while computing it. Let's deliver the interrupt
2475ec694b5SJean-Christophe DUBOIS          * and compute a new limit.
2485ec694b5SJean-Christophe DUBOIS          */
2495ec694b5SJean-Christophe DUBOIS         s->sr |= s->next_int;
2505ec694b5SJean-Christophe DUBOIS 
25167110c3eSJean-Christophe DUBOIS         imx_gpt_compute_next_timeout(s, event);
2525ec694b5SJean-Christophe DUBOIS 
25367110c3eSJean-Christophe DUBOIS         imx_gpt_update_int(s);
2545ec694b5SJean-Christophe DUBOIS     } else {
2555ec694b5SJean-Christophe DUBOIS         /* New timeout value */
2565ec694b5SJean-Christophe DUBOIS         s->next_timeout = timeout;
2575ec694b5SJean-Christophe DUBOIS 
2585ec694b5SJean-Christophe DUBOIS         /* reset the limit to the computed range */
2595ec694b5SJean-Christophe DUBOIS         ptimer_set_limit(s->timer, limit, 1);
2605ec694b5SJean-Christophe DUBOIS     }
261a50c0d6fSJean-Christophe DUBOIS }
262a50c0d6fSJean-Christophe DUBOIS 
26367110c3eSJean-Christophe DUBOIS static uint64_t imx_gpt_read(void *opaque, hwaddr offset, unsigned size)
264a50c0d6fSJean-Christophe DUBOIS {
26567110c3eSJean-Christophe DUBOIS     IMXGPTState *s = IMX_GPT(opaque);
2665ec694b5SJean-Christophe DUBOIS     uint32_t reg_value = 0;
267a50c0d6fSJean-Christophe DUBOIS 
26805453526SJean-Christophe Dubois     switch (offset >> 2) {
269a50c0d6fSJean-Christophe DUBOIS     case 0: /* Control Register */
2705ec694b5SJean-Christophe DUBOIS         reg_value = s->cr;
2715ec694b5SJean-Christophe DUBOIS         break;
272a50c0d6fSJean-Christophe DUBOIS 
273a50c0d6fSJean-Christophe DUBOIS     case 1: /* prescaler */
2745ec694b5SJean-Christophe DUBOIS         reg_value = s->pr;
2755ec694b5SJean-Christophe DUBOIS         break;
276a50c0d6fSJean-Christophe DUBOIS 
277a50c0d6fSJean-Christophe DUBOIS     case 2: /* Status Register */
2785ec694b5SJean-Christophe DUBOIS         reg_value = s->sr;
2795ec694b5SJean-Christophe DUBOIS         break;
280a50c0d6fSJean-Christophe DUBOIS 
281a50c0d6fSJean-Christophe DUBOIS     case 3: /* Interrupt Register */
2825ec694b5SJean-Christophe DUBOIS         reg_value = s->ir;
2835ec694b5SJean-Christophe DUBOIS         break;
284a50c0d6fSJean-Christophe DUBOIS 
285a50c0d6fSJean-Christophe DUBOIS     case 4: /* Output Compare Register 1 */
2865ec694b5SJean-Christophe DUBOIS         reg_value = s->ocr1;
2875ec694b5SJean-Christophe DUBOIS         break;
288a50c0d6fSJean-Christophe DUBOIS 
289a50c0d6fSJean-Christophe DUBOIS     case 5: /* Output Compare Register 2 */
2905ec694b5SJean-Christophe DUBOIS         reg_value = s->ocr2;
2915ec694b5SJean-Christophe DUBOIS         break;
292a50c0d6fSJean-Christophe DUBOIS 
293a50c0d6fSJean-Christophe DUBOIS     case 6: /* Output Compare Register 3 */
2945ec694b5SJean-Christophe DUBOIS         reg_value = s->ocr3;
2955ec694b5SJean-Christophe DUBOIS         break;
296a50c0d6fSJean-Christophe DUBOIS 
297a50c0d6fSJean-Christophe DUBOIS     case 7: /* input Capture Register 1 */
29805453526SJean-Christophe Dubois         qemu_log_mask(LOG_UNIMP, "[%s]%s: icr1 feature is not implemented\n",
29905453526SJean-Christophe Dubois                       TYPE_IMX_GPT, __func__);
3005ec694b5SJean-Christophe DUBOIS         reg_value = s->icr1;
3015ec694b5SJean-Christophe DUBOIS         break;
302a50c0d6fSJean-Christophe DUBOIS 
303a50c0d6fSJean-Christophe DUBOIS     case 8: /* input Capture Register 2 */
30405453526SJean-Christophe Dubois         qemu_log_mask(LOG_UNIMP, "[%s]%s: icr2 feature is not implemented\n",
30505453526SJean-Christophe Dubois                       TYPE_IMX_GPT, __func__);
3065ec694b5SJean-Christophe DUBOIS         reg_value = s->icr2;
3075ec694b5SJean-Christophe DUBOIS         break;
308a50c0d6fSJean-Christophe DUBOIS 
309a50c0d6fSJean-Christophe DUBOIS     case 9: /* cnt */
31067110c3eSJean-Christophe DUBOIS         imx_gpt_update_count(s);
3115ec694b5SJean-Christophe DUBOIS         reg_value = s->cnt;
3125ec694b5SJean-Christophe DUBOIS         break;
3135ec694b5SJean-Christophe DUBOIS 
3145ec694b5SJean-Christophe DUBOIS     default:
31505453526SJean-Christophe Dubois         qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
31605453526SJean-Christophe Dubois                       HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset);
3175ec694b5SJean-Christophe DUBOIS         break;
318a50c0d6fSJean-Christophe DUBOIS     }
319a50c0d6fSJean-Christophe DUBOIS 
32005453526SJean-Christophe Dubois     DPRINTF("(%s) = 0x%08x\n", imx_gpt_reg_name(offset >> 2), reg_value);
321a50c0d6fSJean-Christophe DUBOIS 
3225ec694b5SJean-Christophe DUBOIS     return reg_value;
323a50c0d6fSJean-Christophe DUBOIS }
324a50c0d6fSJean-Christophe DUBOIS 
325a50c0d6fSJean-Christophe DUBOIS 
326c98c9ebaSKurban Mallachiev static void imx_gpt_reset_common(IMXGPTState *s, bool is_soft_reset)
327c98c9ebaSKurban Mallachiev {
3281b914994SPeter Maydell     ptimer_transaction_begin(s->timer);
3295ec694b5SJean-Christophe DUBOIS     /* stop timer */
3305ec694b5SJean-Christophe DUBOIS     ptimer_stop(s->timer);
3315ec694b5SJean-Christophe DUBOIS 
332c98c9ebaSKurban Mallachiev     /* Soft reset and hard reset differ only in their handling of the CR
333c98c9ebaSKurban Mallachiev      * register -- soft reset preserves the values of some bits there.
334a50c0d6fSJean-Christophe DUBOIS      */
335c98c9ebaSKurban Mallachiev     if (is_soft_reset) {
336c98c9ebaSKurban Mallachiev         /* Clear all CR bits except those that are preserved by soft reset. */
337c98c9ebaSKurban Mallachiev         s->cr &= GPT_CR_EN | GPT_CR_ENMOD | GPT_CR_STOPEN | GPT_CR_DOZEN |
338c98c9ebaSKurban Mallachiev             GPT_CR_WAITEN | GPT_CR_DBGEN |
339c98c9ebaSKurban Mallachiev             (GPT_CR_CLKSRC_MASK << GPT_CR_CLKSRC_SHIFT);
340c98c9ebaSKurban Mallachiev     } else {
341c98c9ebaSKurban Mallachiev         s->cr = 0;
342c98c9ebaSKurban Mallachiev     }
343a50c0d6fSJean-Christophe DUBOIS     s->sr = 0;
344a50c0d6fSJean-Christophe DUBOIS     s->pr = 0;
345a50c0d6fSJean-Christophe DUBOIS     s->ir = 0;
346a50c0d6fSJean-Christophe DUBOIS     s->cnt = 0;
347203d65a4SMichael Tokarev     s->ocr1 = GPT_TIMER_MAX;
348203d65a4SMichael Tokarev     s->ocr2 = GPT_TIMER_MAX;
349203d65a4SMichael Tokarev     s->ocr3 = GPT_TIMER_MAX;
350a50c0d6fSJean-Christophe DUBOIS     s->icr1 = 0;
351a50c0d6fSJean-Christophe DUBOIS     s->icr2 = 0;
3525ec694b5SJean-Christophe DUBOIS 
353203d65a4SMichael Tokarev     s->next_timeout = GPT_TIMER_MAX;
3545ec694b5SJean-Christophe DUBOIS     s->next_int = 0;
3555ec694b5SJean-Christophe DUBOIS 
3565ec694b5SJean-Christophe DUBOIS     /* compute new freq */
35767110c3eSJean-Christophe DUBOIS     imx_gpt_set_freq(s);
3585ec694b5SJean-Christophe DUBOIS 
359203d65a4SMichael Tokarev     /* reset the limit to GPT_TIMER_MAX */
360203d65a4SMichael Tokarev     ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1);
3615ec694b5SJean-Christophe DUBOIS 
3625ec694b5SJean-Christophe DUBOIS     /* if the timer is still enabled, restart it */
3635ec694b5SJean-Christophe DUBOIS     if (s->freq && (s->cr & GPT_CR_EN)) {
3645ec694b5SJean-Christophe DUBOIS         ptimer_run(s->timer, 1);
3655ec694b5SJean-Christophe DUBOIS     }
3661b914994SPeter Maydell     ptimer_transaction_commit(s->timer);
367a50c0d6fSJean-Christophe DUBOIS }
368a50c0d6fSJean-Christophe DUBOIS 
369c98c9ebaSKurban Mallachiev static void imx_gpt_soft_reset(DeviceState *dev)
370c98c9ebaSKurban Mallachiev {
371c98c9ebaSKurban Mallachiev     IMXGPTState *s = IMX_GPT(dev);
372c98c9ebaSKurban Mallachiev     imx_gpt_reset_common(s, true);
373c98c9ebaSKurban Mallachiev }
374c98c9ebaSKurban Mallachiev 
375c98c9ebaSKurban Mallachiev static void imx_gpt_reset(DeviceState *dev)
376c98c9ebaSKurban Mallachiev {
377c98c9ebaSKurban Mallachiev     IMXGPTState *s = IMX_GPT(dev);
378c98c9ebaSKurban Mallachiev     imx_gpt_reset_common(s, false);
379c98c9ebaSKurban Mallachiev }
380c98c9ebaSKurban Mallachiev 
38167110c3eSJean-Christophe DUBOIS static void imx_gpt_write(void *opaque, hwaddr offset, uint64_t value,
38267110c3eSJean-Christophe DUBOIS                           unsigned size)
383a50c0d6fSJean-Christophe DUBOIS {
38467110c3eSJean-Christophe DUBOIS     IMXGPTState *s = IMX_GPT(opaque);
3855ec694b5SJean-Christophe DUBOIS     uint32_t oldreg;
386a50c0d6fSJean-Christophe DUBOIS 
38705453526SJean-Christophe Dubois     DPRINTF("(%s, value = 0x%08x)\n", imx_gpt_reg_name(offset >> 2),
3885ec694b5SJean-Christophe DUBOIS             (uint32_t)value);
389a50c0d6fSJean-Christophe DUBOIS 
39005453526SJean-Christophe Dubois     switch (offset >> 2) {
3915ec694b5SJean-Christophe DUBOIS     case 0:
3925ec694b5SJean-Christophe DUBOIS         oldreg = s->cr;
3935ec694b5SJean-Christophe DUBOIS         s->cr = value & ~0x7c14;
3945ec694b5SJean-Christophe DUBOIS         if (s->cr & GPT_CR_SWR) { /* force reset */
3955ec694b5SJean-Christophe DUBOIS             /* handle the reset */
396c98c9ebaSKurban Mallachiev             imx_gpt_soft_reset(DEVICE(s));
397a50c0d6fSJean-Christophe DUBOIS         } else {
3985ec694b5SJean-Christophe DUBOIS             /* set our freq, as the source might have changed */
3991b914994SPeter Maydell             ptimer_transaction_begin(s->timer);
40067110c3eSJean-Christophe DUBOIS             imx_gpt_set_freq(s);
4015ec694b5SJean-Christophe DUBOIS 
4025ec694b5SJean-Christophe DUBOIS             if ((oldreg ^ s->cr) & GPT_CR_EN) {
4035ec694b5SJean-Christophe DUBOIS                 if (s->cr & GPT_CR_EN) {
4045ec694b5SJean-Christophe DUBOIS                     if (s->cr & GPT_CR_ENMOD) {
405203d65a4SMichael Tokarev                         s->next_timeout = GPT_TIMER_MAX;
406203d65a4SMichael Tokarev                         ptimer_set_count(s->timer, GPT_TIMER_MAX);
40767110c3eSJean-Christophe DUBOIS                         imx_gpt_compute_next_timeout(s, false);
4085ec694b5SJean-Christophe DUBOIS                     }
4095ec694b5SJean-Christophe DUBOIS                     ptimer_run(s->timer, 1);
4105ec694b5SJean-Christophe DUBOIS                 } else {
4115ec694b5SJean-Christophe DUBOIS                     /* stop timer */
412a50c0d6fSJean-Christophe DUBOIS                     ptimer_stop(s->timer);
413a50c0d6fSJean-Christophe DUBOIS                 }
414a50c0d6fSJean-Christophe DUBOIS             }
4151b914994SPeter Maydell             ptimer_transaction_commit(s->timer);
4165ec694b5SJean-Christophe DUBOIS         }
4175ec694b5SJean-Christophe DUBOIS         break;
418a50c0d6fSJean-Christophe DUBOIS 
419a50c0d6fSJean-Christophe DUBOIS     case 1: /* Prescaler */
420a50c0d6fSJean-Christophe DUBOIS         s->pr = value & 0xfff;
4211b914994SPeter Maydell         ptimer_transaction_begin(s->timer);
42267110c3eSJean-Christophe DUBOIS         imx_gpt_set_freq(s);
4231b914994SPeter Maydell         ptimer_transaction_commit(s->timer);
4245ec694b5SJean-Christophe DUBOIS         break;
425a50c0d6fSJean-Christophe DUBOIS 
426a50c0d6fSJean-Christophe DUBOIS     case 2: /* SR */
4275ec694b5SJean-Christophe DUBOIS         s->sr &= ~(value & 0x3f);
42867110c3eSJean-Christophe DUBOIS         imx_gpt_update_int(s);
4295ec694b5SJean-Christophe DUBOIS         break;
430a50c0d6fSJean-Christophe DUBOIS 
431a50c0d6fSJean-Christophe DUBOIS     case 3: /* IR -- interrupt register */
432a50c0d6fSJean-Christophe DUBOIS         s->ir = value & 0x3f;
43367110c3eSJean-Christophe DUBOIS         imx_gpt_update_int(s);
4345ec694b5SJean-Christophe DUBOIS 
4351b914994SPeter Maydell         ptimer_transaction_begin(s->timer);
43667110c3eSJean-Christophe DUBOIS         imx_gpt_compute_next_timeout(s, false);
4371b914994SPeter Maydell         ptimer_transaction_commit(s->timer);
4385ec694b5SJean-Christophe DUBOIS 
4395ec694b5SJean-Christophe DUBOIS         break;
440a50c0d6fSJean-Christophe DUBOIS 
441a50c0d6fSJean-Christophe DUBOIS     case 4: /* OCR1 -- output compare register */
4425ec694b5SJean-Christophe DUBOIS         s->ocr1 = value;
4435ec694b5SJean-Christophe DUBOIS 
4441b914994SPeter Maydell         ptimer_transaction_begin(s->timer);
445a50c0d6fSJean-Christophe DUBOIS         /* In non-freerun mode, reset count when this register is written */
446a50c0d6fSJean-Christophe DUBOIS         if (!(s->cr & GPT_CR_FRR)) {
447203d65a4SMichael Tokarev             s->next_timeout = GPT_TIMER_MAX;
448203d65a4SMichael Tokarev             ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1);
449a50c0d6fSJean-Christophe DUBOIS         }
4505ec694b5SJean-Christophe DUBOIS 
4515ec694b5SJean-Christophe DUBOIS         /* compute the new timeout */
45267110c3eSJean-Christophe DUBOIS         imx_gpt_compute_next_timeout(s, false);
4531b914994SPeter Maydell         ptimer_transaction_commit(s->timer);
4545ec694b5SJean-Christophe DUBOIS 
4555ec694b5SJean-Christophe DUBOIS         break;
456a50c0d6fSJean-Christophe DUBOIS 
457a50c0d6fSJean-Christophe DUBOIS     case 5: /* OCR2 -- output compare register */
4585ec694b5SJean-Christophe DUBOIS         s->ocr2 = value;
4595ec694b5SJean-Christophe DUBOIS 
4605ec694b5SJean-Christophe DUBOIS         /* compute the new timeout */
4611b914994SPeter Maydell         ptimer_transaction_begin(s->timer);
46267110c3eSJean-Christophe DUBOIS         imx_gpt_compute_next_timeout(s, false);
4631b914994SPeter Maydell         ptimer_transaction_commit(s->timer);
4645ec694b5SJean-Christophe DUBOIS 
4655ec694b5SJean-Christophe DUBOIS         break;
4665ec694b5SJean-Christophe DUBOIS 
467a50c0d6fSJean-Christophe DUBOIS     case 6: /* OCR3 -- output compare register */
4685ec694b5SJean-Christophe DUBOIS         s->ocr3 = value;
4695ec694b5SJean-Christophe DUBOIS 
4705ec694b5SJean-Christophe DUBOIS         /* compute the new timeout */
4711b914994SPeter Maydell         ptimer_transaction_begin(s->timer);
47267110c3eSJean-Christophe DUBOIS         imx_gpt_compute_next_timeout(s, false);
4731b914994SPeter Maydell         ptimer_transaction_commit(s->timer);
4745ec694b5SJean-Christophe DUBOIS 
4755ec694b5SJean-Christophe DUBOIS         break;
4765ec694b5SJean-Christophe DUBOIS 
477a50c0d6fSJean-Christophe DUBOIS     default:
47805453526SJean-Christophe Dubois         qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
47905453526SJean-Christophe Dubois                       HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset);
4805ec694b5SJean-Christophe DUBOIS         break;
481a50c0d6fSJean-Christophe DUBOIS     }
482a50c0d6fSJean-Christophe DUBOIS }
483a50c0d6fSJean-Christophe DUBOIS 
48467110c3eSJean-Christophe DUBOIS static void imx_gpt_timeout(void *opaque)
485a50c0d6fSJean-Christophe DUBOIS {
48667110c3eSJean-Christophe DUBOIS     IMXGPTState *s = IMX_GPT(opaque);
487a50c0d6fSJean-Christophe DUBOIS 
4885ec694b5SJean-Christophe DUBOIS     DPRINTF("\n");
489a50c0d6fSJean-Christophe DUBOIS 
4905ec694b5SJean-Christophe DUBOIS     s->sr |= s->next_int;
4915ec694b5SJean-Christophe DUBOIS     s->next_int = 0;
492a50c0d6fSJean-Christophe DUBOIS 
49367110c3eSJean-Christophe DUBOIS     imx_gpt_compute_next_timeout(s, true);
4945ec694b5SJean-Christophe DUBOIS 
49567110c3eSJean-Christophe DUBOIS     imx_gpt_update_int(s);
4965ec694b5SJean-Christophe DUBOIS 
4975ec694b5SJean-Christophe DUBOIS     if (s->freq && (s->cr & GPT_CR_EN)) {
4985ec694b5SJean-Christophe DUBOIS         ptimer_run(s->timer, 1);
4995ec694b5SJean-Christophe DUBOIS     }
500a50c0d6fSJean-Christophe DUBOIS }
501a50c0d6fSJean-Christophe DUBOIS 
50267110c3eSJean-Christophe DUBOIS static const MemoryRegionOps imx_gpt_ops = {
50367110c3eSJean-Christophe DUBOIS     .read = imx_gpt_read,
50467110c3eSJean-Christophe DUBOIS     .write = imx_gpt_write,
505a50c0d6fSJean-Christophe DUBOIS     .endianness = DEVICE_NATIVE_ENDIAN,
506a50c0d6fSJean-Christophe DUBOIS };
507a50c0d6fSJean-Christophe DUBOIS 
508a50c0d6fSJean-Christophe DUBOIS 
50967110c3eSJean-Christophe DUBOIS static void imx_gpt_realize(DeviceState *dev, Error **errp)
510a50c0d6fSJean-Christophe DUBOIS {
51167110c3eSJean-Christophe DUBOIS     IMXGPTState *s = IMX_GPT(dev);
51267110c3eSJean-Christophe DUBOIS     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
513a50c0d6fSJean-Christophe DUBOIS 
51467110c3eSJean-Christophe DUBOIS     sysbus_init_irq(sbd, &s->irq);
515853dca12SPaolo Bonzini     memory_region_init_io(&s->iomem, OBJECT(s), &imx_gpt_ops, s, TYPE_IMX_GPT,
516a50c0d6fSJean-Christophe DUBOIS                           0x00001000);
51767110c3eSJean-Christophe DUBOIS     sysbus_init_mmio(sbd, &s->iomem);
518a50c0d6fSJean-Christophe DUBOIS 
5199598c1bbSPeter Maydell     s->timer = ptimer_init(imx_gpt_timeout, s, PTIMER_POLICY_LEGACY);
520a50c0d6fSJean-Christophe DUBOIS }
521a50c0d6fSJean-Christophe DUBOIS 
52267110c3eSJean-Christophe DUBOIS static void imx_gpt_class_init(ObjectClass *klass, void *data)
523a50c0d6fSJean-Christophe DUBOIS {
524a50c0d6fSJean-Christophe DUBOIS     DeviceClass *dc = DEVICE_CLASS(klass);
52567110c3eSJean-Christophe DUBOIS 
52667110c3eSJean-Christophe DUBOIS     dc->realize = imx_gpt_realize;
527*e3d08143SPeter Maydell     device_class_set_legacy_reset(dc, imx_gpt_reset);
52867110c3eSJean-Christophe DUBOIS     dc->vmsd = &vmstate_imx_timer_gpt;
529a50c0d6fSJean-Christophe DUBOIS     dc->desc = "i.MX general timer";
530a50c0d6fSJean-Christophe DUBOIS }
531a50c0d6fSJean-Christophe DUBOIS 
53266542f63SJean-Christophe Dubois static void imx25_gpt_init(Object *obj)
53366542f63SJean-Christophe Dubois {
53466542f63SJean-Christophe Dubois     IMXGPTState *s = IMX_GPT(obj);
53566542f63SJean-Christophe Dubois 
53666542f63SJean-Christophe Dubois     s->clocks = imx25_gpt_clocks;
53766542f63SJean-Christophe Dubois }
53866542f63SJean-Christophe Dubois 
53966542f63SJean-Christophe Dubois static void imx31_gpt_init(Object *obj)
54066542f63SJean-Christophe Dubois {
54166542f63SJean-Christophe Dubois     IMXGPTState *s = IMX_GPT(obj);
54266542f63SJean-Christophe Dubois 
54366542f63SJean-Christophe Dubois     s->clocks = imx31_gpt_clocks;
54466542f63SJean-Christophe Dubois }
54566542f63SJean-Christophe Dubois 
54666542f63SJean-Christophe Dubois static void imx6_gpt_init(Object *obj)
54766542f63SJean-Christophe Dubois {
54866542f63SJean-Christophe Dubois     IMXGPTState *s = IMX_GPT(obj);
54966542f63SJean-Christophe Dubois 
55066542f63SJean-Christophe Dubois     s->clocks = imx6_gpt_clocks;
55166542f63SJean-Christophe Dubois }
55266542f63SJean-Christophe Dubois 
553a1e03956SJean-Christophe Dubois static void imx6ul_gpt_init(Object *obj)
554a1e03956SJean-Christophe Dubois {
555a1e03956SJean-Christophe Dubois     IMXGPTState *s = IMX_GPT(obj);
556a1e03956SJean-Christophe Dubois 
557a1e03956SJean-Christophe Dubois     s->clocks = imx6ul_gpt_clocks;
558a1e03956SJean-Christophe Dubois }
559a1e03956SJean-Christophe Dubois 
560a62bf59fSAndrey Smirnov static void imx7_gpt_init(Object *obj)
561a62bf59fSAndrey Smirnov {
562a62bf59fSAndrey Smirnov     IMXGPTState *s = IMX_GPT(obj);
563a62bf59fSAndrey Smirnov 
564a62bf59fSAndrey Smirnov     s->clocks = imx7_gpt_clocks;
565a62bf59fSAndrey Smirnov }
566a62bf59fSAndrey Smirnov 
56766542f63SJean-Christophe Dubois static const TypeInfo imx25_gpt_info = {
56866542f63SJean-Christophe Dubois     .name = TYPE_IMX25_GPT,
569a50c0d6fSJean-Christophe DUBOIS     .parent = TYPE_SYS_BUS_DEVICE,
57067110c3eSJean-Christophe DUBOIS     .instance_size = sizeof(IMXGPTState),
57166542f63SJean-Christophe Dubois     .instance_init = imx25_gpt_init,
57267110c3eSJean-Christophe DUBOIS     .class_init = imx_gpt_class_init,
573a50c0d6fSJean-Christophe DUBOIS };
574a50c0d6fSJean-Christophe DUBOIS 
57566542f63SJean-Christophe Dubois static const TypeInfo imx31_gpt_info = {
57666542f63SJean-Christophe Dubois     .name = TYPE_IMX31_GPT,
57766542f63SJean-Christophe Dubois     .parent = TYPE_IMX25_GPT,
57866542f63SJean-Christophe Dubois     .instance_init = imx31_gpt_init,
57966542f63SJean-Christophe Dubois };
58066542f63SJean-Christophe Dubois 
58166542f63SJean-Christophe Dubois static const TypeInfo imx6_gpt_info = {
58266542f63SJean-Christophe Dubois     .name = TYPE_IMX6_GPT,
58366542f63SJean-Christophe Dubois     .parent = TYPE_IMX25_GPT,
58466542f63SJean-Christophe Dubois     .instance_init = imx6_gpt_init,
58566542f63SJean-Christophe Dubois };
58666542f63SJean-Christophe Dubois 
587a1e03956SJean-Christophe Dubois static const TypeInfo imx6ul_gpt_info = {
588a1e03956SJean-Christophe Dubois     .name = TYPE_IMX6UL_GPT,
589a1e03956SJean-Christophe Dubois     .parent = TYPE_IMX25_GPT,
590a1e03956SJean-Christophe Dubois     .instance_init = imx6ul_gpt_init,
591a1e03956SJean-Christophe Dubois };
592a1e03956SJean-Christophe Dubois 
593a62bf59fSAndrey Smirnov static const TypeInfo imx7_gpt_info = {
594a62bf59fSAndrey Smirnov     .name = TYPE_IMX7_GPT,
595a62bf59fSAndrey Smirnov     .parent = TYPE_IMX25_GPT,
596a62bf59fSAndrey Smirnov     .instance_init = imx7_gpt_init,
597a62bf59fSAndrey Smirnov };
598a62bf59fSAndrey Smirnov 
59967110c3eSJean-Christophe DUBOIS static void imx_gpt_register_types(void)
600a50c0d6fSJean-Christophe DUBOIS {
60166542f63SJean-Christophe Dubois     type_register_static(&imx25_gpt_info);
60266542f63SJean-Christophe Dubois     type_register_static(&imx31_gpt_info);
60366542f63SJean-Christophe Dubois     type_register_static(&imx6_gpt_info);
604a1e03956SJean-Christophe Dubois     type_register_static(&imx6ul_gpt_info);
605a62bf59fSAndrey Smirnov     type_register_static(&imx7_gpt_info);
606a50c0d6fSJean-Christophe DUBOIS }
607a50c0d6fSJean-Christophe DUBOIS 
60867110c3eSJean-Christophe DUBOIS type_init(imx_gpt_register_types)
609