xref: /openbmc/qemu/hw/timer/imx_gpt.c (revision 03dd024ff57733a55cd2e455f361d053c81b1b29)
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"
16d647b26dSJean-Christophe Dubois #include "hw/timer/imx_gpt.h"
17d647b26dSJean-Christophe Dubois #include "hw/misc/imx_ccm.h"
186a1751b7SAlex Bligh #include "qemu/main-loop.h"
19*03dd024fSPaolo Bonzini #include "qemu/log.h"
20a50c0d6fSJean-Christophe DUBOIS 
2105453526SJean-Christophe Dubois #ifndef DEBUG_IMX_GPT
2205453526SJean-Christophe Dubois #define DEBUG_IMX_GPT 0
2305453526SJean-Christophe Dubois #endif
2405453526SJean-Christophe Dubois 
2505453526SJean-Christophe Dubois #define DPRINTF(fmt, args...) \
2605453526SJean-Christophe Dubois     do { \
2705453526SJean-Christophe Dubois         if (DEBUG_IMX_GPT) { \
2805453526SJean-Christophe Dubois             fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_GPT, \
2905453526SJean-Christophe Dubois                                              __func__, ##args); \
3005453526SJean-Christophe Dubois         } \
3105453526SJean-Christophe Dubois     } while (0)
325ec694b5SJean-Christophe DUBOIS 
3367110c3eSJean-Christophe DUBOIS static char const *imx_gpt_reg_name(uint32_t reg)
345ec694b5SJean-Christophe DUBOIS {
355ec694b5SJean-Christophe DUBOIS     switch (reg) {
365ec694b5SJean-Christophe DUBOIS     case 0:
375ec694b5SJean-Christophe DUBOIS         return "CR";
385ec694b5SJean-Christophe DUBOIS     case 1:
395ec694b5SJean-Christophe DUBOIS         return "PR";
405ec694b5SJean-Christophe DUBOIS     case 2:
415ec694b5SJean-Christophe DUBOIS         return "SR";
425ec694b5SJean-Christophe DUBOIS     case 3:
435ec694b5SJean-Christophe DUBOIS         return "IR";
445ec694b5SJean-Christophe DUBOIS     case 4:
455ec694b5SJean-Christophe DUBOIS         return "OCR1";
465ec694b5SJean-Christophe DUBOIS     case 5:
475ec694b5SJean-Christophe DUBOIS         return "OCR2";
485ec694b5SJean-Christophe DUBOIS     case 6:
495ec694b5SJean-Christophe DUBOIS         return "OCR3";
505ec694b5SJean-Christophe DUBOIS     case 7:
515ec694b5SJean-Christophe DUBOIS         return "ICR1";
525ec694b5SJean-Christophe DUBOIS     case 8:
535ec694b5SJean-Christophe DUBOIS         return "ICR2";
545ec694b5SJean-Christophe DUBOIS     case 9:
555ec694b5SJean-Christophe DUBOIS         return "CNT";
565ec694b5SJean-Christophe DUBOIS     default:
575ec694b5SJean-Christophe DUBOIS         return "[?]";
585ec694b5SJean-Christophe DUBOIS     }
595ec694b5SJean-Christophe DUBOIS }
605ec694b5SJean-Christophe DUBOIS 
6167110c3eSJean-Christophe DUBOIS static const VMStateDescription vmstate_imx_timer_gpt = {
6268b85290SJean-Christophe Dubois     .name = TYPE_IMX_GPT,
635ec694b5SJean-Christophe DUBOIS     .version_id = 3,
645ec694b5SJean-Christophe DUBOIS     .minimum_version_id = 3,
65a50c0d6fSJean-Christophe DUBOIS     .fields = (VMStateField[]) {
6667110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(cr, IMXGPTState),
6767110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(pr, IMXGPTState),
6867110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(sr, IMXGPTState),
6967110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(ir, IMXGPTState),
7067110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(ocr1, IMXGPTState),
7167110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(ocr2, IMXGPTState),
7267110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(ocr3, IMXGPTState),
7367110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(icr1, IMXGPTState),
7467110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(icr2, IMXGPTState),
7567110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(cnt, IMXGPTState),
7667110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(next_timeout, IMXGPTState),
7767110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(next_int, IMXGPTState),
7867110c3eSJean-Christophe DUBOIS         VMSTATE_UINT32(freq, IMXGPTState),
7967110c3eSJean-Christophe DUBOIS         VMSTATE_PTIMER(timer, IMXGPTState),
80a50c0d6fSJean-Christophe DUBOIS         VMSTATE_END_OF_LIST()
81a50c0d6fSJean-Christophe DUBOIS     }
82a50c0d6fSJean-Christophe DUBOIS };
83a50c0d6fSJean-Christophe DUBOIS 
8467110c3eSJean-Christophe DUBOIS static const IMXClk imx_gpt_clocks[] = {
85c91a5883SJean-Christophe Dubois     CLK_NONE,      /* 000 No clock source */
86aaa9ec3bSJean-Christophe Dubois     CLK_IPG,       /* 001 ipg_clk, 532MHz*/
87d552f675SJean-Christophe Dubois     CLK_IPG_HIGH,  /* 010 ipg_clk_highfreq */
88c91a5883SJean-Christophe Dubois     CLK_NONE,      /* 011 not defined */
89a50c0d6fSJean-Christophe DUBOIS     CLK_32k,       /* 100 ipg_clk_32k */
90c91a5883SJean-Christophe Dubois     CLK_NONE,      /* 101 not defined */
91c91a5883SJean-Christophe Dubois     CLK_NONE,      /* 110 not defined */
92c91a5883SJean-Christophe Dubois     CLK_NONE,      /* 111 not defined */
93a50c0d6fSJean-Christophe DUBOIS };
94a50c0d6fSJean-Christophe DUBOIS 
9567110c3eSJean-Christophe DUBOIS static void imx_gpt_set_freq(IMXGPTState *s)
96a50c0d6fSJean-Christophe DUBOIS {
975ec694b5SJean-Christophe DUBOIS     uint32_t clksrc = extract32(s->cr, GPT_CR_CLKSRC_SHIFT, 3);
98a50c0d6fSJean-Christophe DUBOIS 
99aaa9ec3bSJean-Christophe Dubois     s->freq = imx_ccm_get_clock_frequency(s->ccm,
100aaa9ec3bSJean-Christophe Dubois                                 imx_gpt_clocks[clksrc]) / (1 + s->pr);
101a50c0d6fSJean-Christophe DUBOIS 
102aaa9ec3bSJean-Christophe Dubois     DPRINTF("Setting clksrc %d to frequency %d\n", clksrc, s->freq);
103aaa9ec3bSJean-Christophe Dubois 
104aaa9ec3bSJean-Christophe Dubois     if (s->freq) {
105aaa9ec3bSJean-Christophe Dubois         ptimer_set_freq(s->timer, s->freq);
106a50c0d6fSJean-Christophe DUBOIS     }
107a50c0d6fSJean-Christophe DUBOIS }
108a50c0d6fSJean-Christophe DUBOIS 
10967110c3eSJean-Christophe DUBOIS static void imx_gpt_update_int(IMXGPTState *s)
110a50c0d6fSJean-Christophe DUBOIS {
1115ec694b5SJean-Christophe DUBOIS     if ((s->sr & s->ir) && (s->cr & GPT_CR_EN)) {
1125ec694b5SJean-Christophe DUBOIS         qemu_irq_raise(s->irq);
1135ec694b5SJean-Christophe DUBOIS     } else {
1145ec694b5SJean-Christophe DUBOIS         qemu_irq_lower(s->irq);
1155ec694b5SJean-Christophe DUBOIS     }
116a50c0d6fSJean-Christophe DUBOIS }
117a50c0d6fSJean-Christophe DUBOIS 
11867110c3eSJean-Christophe DUBOIS static uint32_t imx_gpt_update_count(IMXGPTState *s)
119a50c0d6fSJean-Christophe DUBOIS {
1205ec694b5SJean-Christophe DUBOIS     s->cnt = s->next_timeout - (uint32_t)ptimer_get_count(s->timer);
1215ec694b5SJean-Christophe DUBOIS 
122a50c0d6fSJean-Christophe DUBOIS     return s->cnt;
123a50c0d6fSJean-Christophe DUBOIS }
124a50c0d6fSJean-Christophe DUBOIS 
12567110c3eSJean-Christophe DUBOIS static inline uint32_t imx_gpt_find_limit(uint32_t count, uint32_t reg,
1265ec694b5SJean-Christophe DUBOIS                                           uint32_t timeout)
127a50c0d6fSJean-Christophe DUBOIS {
1285ec694b5SJean-Christophe DUBOIS     if ((count < reg) && (timeout > reg)) {
1295ec694b5SJean-Christophe DUBOIS         timeout = reg;
1305ec694b5SJean-Christophe DUBOIS     }
131a50c0d6fSJean-Christophe DUBOIS 
1325ec694b5SJean-Christophe DUBOIS     return timeout;
1335ec694b5SJean-Christophe DUBOIS }
1345ec694b5SJean-Christophe DUBOIS 
13567110c3eSJean-Christophe DUBOIS static void imx_gpt_compute_next_timeout(IMXGPTState *s, bool event)
1365ec694b5SJean-Christophe DUBOIS {
137203d65a4SMichael Tokarev     uint32_t timeout = GPT_TIMER_MAX;
1384833e15fSJean-Christophe Dubois     uint32_t count;
1395ec694b5SJean-Christophe DUBOIS     long long limit;
1405ec694b5SJean-Christophe DUBOIS 
1415ec694b5SJean-Christophe DUBOIS     if (!(s->cr & GPT_CR_EN)) {
1425ec694b5SJean-Christophe DUBOIS         /* if not enabled just return */
143a50c0d6fSJean-Christophe DUBOIS         return;
144a50c0d6fSJean-Christophe DUBOIS     }
145a50c0d6fSJean-Christophe DUBOIS 
1464833e15fSJean-Christophe Dubois     /* update the count */
1474833e15fSJean-Christophe Dubois     count = imx_gpt_update_count(s);
1484833e15fSJean-Christophe Dubois 
1495ec694b5SJean-Christophe DUBOIS     if (event) {
150a50c0d6fSJean-Christophe DUBOIS         /*
1514833e15fSJean-Christophe Dubois          * This is an event (the ptimer reached 0 and stopped), and the
1524833e15fSJean-Christophe Dubois          * timer counter is now equal to s->next_timeout.
153a50c0d6fSJean-Christophe DUBOIS          */
1544833e15fSJean-Christophe Dubois         if (!(s->cr & GPT_CR_FRR) && (count == s->ocr1)) {
1554833e15fSJean-Christophe Dubois             /* We are in restart mode and we crossed the compare channel 1
1564833e15fSJean-Christophe Dubois              * value. We need to reset the counter to 0.
1574833e15fSJean-Christophe Dubois              */
1584833e15fSJean-Christophe Dubois             count = s->cnt = s->next_timeout = 0;
1594833e15fSJean-Christophe Dubois         } else if (count == GPT_TIMER_MAX) {
1604833e15fSJean-Christophe Dubois             /* We reached GPT_TIMER_MAX so we need to rollover */
1614833e15fSJean-Christophe Dubois             count = s->cnt = s->next_timeout = 0;
162a50c0d6fSJean-Christophe DUBOIS         }
1635ec694b5SJean-Christophe DUBOIS     }
1645ec694b5SJean-Christophe DUBOIS 
1655ec694b5SJean-Christophe DUBOIS     /* now, find the next timeout related to count */
1665ec694b5SJean-Christophe DUBOIS 
1675ec694b5SJean-Christophe DUBOIS     if (s->ir & GPT_IR_OF1IE) {
16867110c3eSJean-Christophe DUBOIS         timeout = imx_gpt_find_limit(count, s->ocr1, timeout);
1695ec694b5SJean-Christophe DUBOIS     }
1705ec694b5SJean-Christophe DUBOIS     if (s->ir & GPT_IR_OF2IE) {
17167110c3eSJean-Christophe DUBOIS         timeout = imx_gpt_find_limit(count, s->ocr2, timeout);
1725ec694b5SJean-Christophe DUBOIS     }
1735ec694b5SJean-Christophe DUBOIS     if (s->ir & GPT_IR_OF3IE) {
17467110c3eSJean-Christophe DUBOIS         timeout = imx_gpt_find_limit(count, s->ocr3, timeout);
1755ec694b5SJean-Christophe DUBOIS     }
1765ec694b5SJean-Christophe DUBOIS 
1775ec694b5SJean-Christophe DUBOIS     /* find the next set of interrupts to raise for next timer event */
1785ec694b5SJean-Christophe DUBOIS 
1795ec694b5SJean-Christophe DUBOIS     s->next_int = 0;
1805ec694b5SJean-Christophe DUBOIS     if ((s->ir & GPT_IR_OF1IE) && (timeout == s->ocr1)) {
1815ec694b5SJean-Christophe DUBOIS         s->next_int |= GPT_SR_OF1;
1825ec694b5SJean-Christophe DUBOIS     }
1835ec694b5SJean-Christophe DUBOIS     if ((s->ir & GPT_IR_OF2IE) && (timeout == s->ocr2)) {
1845ec694b5SJean-Christophe DUBOIS         s->next_int |= GPT_SR_OF2;
1855ec694b5SJean-Christophe DUBOIS     }
1865ec694b5SJean-Christophe DUBOIS     if ((s->ir & GPT_IR_OF3IE) && (timeout == s->ocr3)) {
1875ec694b5SJean-Christophe DUBOIS         s->next_int |= GPT_SR_OF3;
1885ec694b5SJean-Christophe DUBOIS     }
189203d65a4SMichael Tokarev     if ((s->ir & GPT_IR_ROVIE) && (timeout == GPT_TIMER_MAX)) {
1905ec694b5SJean-Christophe DUBOIS         s->next_int |= GPT_SR_ROV;
1915ec694b5SJean-Christophe DUBOIS     }
1925ec694b5SJean-Christophe DUBOIS 
1935ec694b5SJean-Christophe DUBOIS     /* the new range to count down from */
19467110c3eSJean-Christophe DUBOIS     limit = timeout - imx_gpt_update_count(s);
1955ec694b5SJean-Christophe DUBOIS 
1965ec694b5SJean-Christophe DUBOIS     if (limit < 0) {
1975ec694b5SJean-Christophe DUBOIS         /*
1985ec694b5SJean-Christophe DUBOIS          * if we reach here, then QEMU is running too slow and we pass the
1995ec694b5SJean-Christophe DUBOIS          * timeout limit while computing it. Let's deliver the interrupt
2005ec694b5SJean-Christophe DUBOIS          * and compute a new limit.
2015ec694b5SJean-Christophe DUBOIS          */
2025ec694b5SJean-Christophe DUBOIS         s->sr |= s->next_int;
2035ec694b5SJean-Christophe DUBOIS 
20467110c3eSJean-Christophe DUBOIS         imx_gpt_compute_next_timeout(s, event);
2055ec694b5SJean-Christophe DUBOIS 
20667110c3eSJean-Christophe DUBOIS         imx_gpt_update_int(s);
2075ec694b5SJean-Christophe DUBOIS     } else {
2085ec694b5SJean-Christophe DUBOIS         /* New timeout value */
2095ec694b5SJean-Christophe DUBOIS         s->next_timeout = timeout;
2105ec694b5SJean-Christophe DUBOIS 
2115ec694b5SJean-Christophe DUBOIS         /* reset the limit to the computed range */
2125ec694b5SJean-Christophe DUBOIS         ptimer_set_limit(s->timer, limit, 1);
2135ec694b5SJean-Christophe DUBOIS     }
214a50c0d6fSJean-Christophe DUBOIS }
215a50c0d6fSJean-Christophe DUBOIS 
21667110c3eSJean-Christophe DUBOIS static uint64_t imx_gpt_read(void *opaque, hwaddr offset, unsigned size)
217a50c0d6fSJean-Christophe DUBOIS {
21867110c3eSJean-Christophe DUBOIS     IMXGPTState *s = IMX_GPT(opaque);
2195ec694b5SJean-Christophe DUBOIS     uint32_t reg_value = 0;
220a50c0d6fSJean-Christophe DUBOIS 
22105453526SJean-Christophe Dubois     switch (offset >> 2) {
222a50c0d6fSJean-Christophe DUBOIS     case 0: /* Control Register */
2235ec694b5SJean-Christophe DUBOIS         reg_value = s->cr;
2245ec694b5SJean-Christophe DUBOIS         break;
225a50c0d6fSJean-Christophe DUBOIS 
226a50c0d6fSJean-Christophe DUBOIS     case 1: /* prescaler */
2275ec694b5SJean-Christophe DUBOIS         reg_value = s->pr;
2285ec694b5SJean-Christophe DUBOIS         break;
229a50c0d6fSJean-Christophe DUBOIS 
230a50c0d6fSJean-Christophe DUBOIS     case 2: /* Status Register */
2315ec694b5SJean-Christophe DUBOIS         reg_value = s->sr;
2325ec694b5SJean-Christophe DUBOIS         break;
233a50c0d6fSJean-Christophe DUBOIS 
234a50c0d6fSJean-Christophe DUBOIS     case 3: /* Interrupt Register */
2355ec694b5SJean-Christophe DUBOIS         reg_value = s->ir;
2365ec694b5SJean-Christophe DUBOIS         break;
237a50c0d6fSJean-Christophe DUBOIS 
238a50c0d6fSJean-Christophe DUBOIS     case 4: /* Output Compare Register 1 */
2395ec694b5SJean-Christophe DUBOIS         reg_value = s->ocr1;
2405ec694b5SJean-Christophe DUBOIS         break;
241a50c0d6fSJean-Christophe DUBOIS 
242a50c0d6fSJean-Christophe DUBOIS     case 5: /* Output Compare Register 2 */
2435ec694b5SJean-Christophe DUBOIS         reg_value = s->ocr2;
2445ec694b5SJean-Christophe DUBOIS         break;
245a50c0d6fSJean-Christophe DUBOIS 
246a50c0d6fSJean-Christophe DUBOIS     case 6: /* Output Compare Register 3 */
2475ec694b5SJean-Christophe DUBOIS         reg_value = s->ocr3;
2485ec694b5SJean-Christophe DUBOIS         break;
249a50c0d6fSJean-Christophe DUBOIS 
250a50c0d6fSJean-Christophe DUBOIS     case 7: /* input Capture Register 1 */
25105453526SJean-Christophe Dubois         qemu_log_mask(LOG_UNIMP, "[%s]%s: icr1 feature is not implemented\n",
25205453526SJean-Christophe Dubois                       TYPE_IMX_GPT, __func__);
2535ec694b5SJean-Christophe DUBOIS         reg_value = s->icr1;
2545ec694b5SJean-Christophe DUBOIS         break;
255a50c0d6fSJean-Christophe DUBOIS 
256a50c0d6fSJean-Christophe DUBOIS     case 8: /* input Capture Register 2 */
25705453526SJean-Christophe Dubois         qemu_log_mask(LOG_UNIMP, "[%s]%s: icr2 feature is not implemented\n",
25805453526SJean-Christophe Dubois                       TYPE_IMX_GPT, __func__);
2595ec694b5SJean-Christophe DUBOIS         reg_value = s->icr2;
2605ec694b5SJean-Christophe DUBOIS         break;
261a50c0d6fSJean-Christophe DUBOIS 
262a50c0d6fSJean-Christophe DUBOIS     case 9: /* cnt */
26367110c3eSJean-Christophe DUBOIS         imx_gpt_update_count(s);
2645ec694b5SJean-Christophe DUBOIS         reg_value = s->cnt;
2655ec694b5SJean-Christophe DUBOIS         break;
2665ec694b5SJean-Christophe DUBOIS 
2675ec694b5SJean-Christophe DUBOIS     default:
26805453526SJean-Christophe Dubois         qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
26905453526SJean-Christophe Dubois                       HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset);
2705ec694b5SJean-Christophe DUBOIS         break;
271a50c0d6fSJean-Christophe DUBOIS     }
272a50c0d6fSJean-Christophe DUBOIS 
27305453526SJean-Christophe Dubois     DPRINTF("(%s) = 0x%08x\n", imx_gpt_reg_name(offset >> 2), reg_value);
274a50c0d6fSJean-Christophe DUBOIS 
2755ec694b5SJean-Christophe DUBOIS     return reg_value;
276a50c0d6fSJean-Christophe DUBOIS }
277a50c0d6fSJean-Christophe DUBOIS 
27867110c3eSJean-Christophe DUBOIS static void imx_gpt_reset(DeviceState *dev)
279a50c0d6fSJean-Christophe DUBOIS {
28067110c3eSJean-Christophe DUBOIS     IMXGPTState *s = IMX_GPT(dev);
281a50c0d6fSJean-Christophe DUBOIS 
2825ec694b5SJean-Christophe DUBOIS     /* stop timer */
2835ec694b5SJean-Christophe DUBOIS     ptimer_stop(s->timer);
2845ec694b5SJean-Christophe DUBOIS 
285a50c0d6fSJean-Christophe DUBOIS     /*
286a50c0d6fSJean-Christophe DUBOIS      * Soft reset doesn't touch some bits; hard reset clears them
287a50c0d6fSJean-Christophe DUBOIS      */
288a50c0d6fSJean-Christophe DUBOIS     s->cr &= ~(GPT_CR_EN|GPT_CR_ENMOD|GPT_CR_STOPEN|GPT_CR_DOZEN|
289a50c0d6fSJean-Christophe DUBOIS                GPT_CR_WAITEN|GPT_CR_DBGEN);
290a50c0d6fSJean-Christophe DUBOIS     s->sr = 0;
291a50c0d6fSJean-Christophe DUBOIS     s->pr = 0;
292a50c0d6fSJean-Christophe DUBOIS     s->ir = 0;
293a50c0d6fSJean-Christophe DUBOIS     s->cnt = 0;
294203d65a4SMichael Tokarev     s->ocr1 = GPT_TIMER_MAX;
295203d65a4SMichael Tokarev     s->ocr2 = GPT_TIMER_MAX;
296203d65a4SMichael Tokarev     s->ocr3 = GPT_TIMER_MAX;
297a50c0d6fSJean-Christophe DUBOIS     s->icr1 = 0;
298a50c0d6fSJean-Christophe DUBOIS     s->icr2 = 0;
2995ec694b5SJean-Christophe DUBOIS 
300203d65a4SMichael Tokarev     s->next_timeout = GPT_TIMER_MAX;
3015ec694b5SJean-Christophe DUBOIS     s->next_int = 0;
3025ec694b5SJean-Christophe DUBOIS 
3035ec694b5SJean-Christophe DUBOIS     /* compute new freq */
30467110c3eSJean-Christophe DUBOIS     imx_gpt_set_freq(s);
3055ec694b5SJean-Christophe DUBOIS 
306203d65a4SMichael Tokarev     /* reset the limit to GPT_TIMER_MAX */
307203d65a4SMichael Tokarev     ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1);
3085ec694b5SJean-Christophe DUBOIS 
3095ec694b5SJean-Christophe DUBOIS     /* if the timer is still enabled, restart it */
3105ec694b5SJean-Christophe DUBOIS     if (s->freq && (s->cr & GPT_CR_EN)) {
3115ec694b5SJean-Christophe DUBOIS         ptimer_run(s->timer, 1);
3125ec694b5SJean-Christophe DUBOIS     }
313a50c0d6fSJean-Christophe DUBOIS }
314a50c0d6fSJean-Christophe DUBOIS 
31567110c3eSJean-Christophe DUBOIS static void imx_gpt_write(void *opaque, hwaddr offset, uint64_t value,
31667110c3eSJean-Christophe DUBOIS                           unsigned size)
317a50c0d6fSJean-Christophe DUBOIS {
31867110c3eSJean-Christophe DUBOIS     IMXGPTState *s = IMX_GPT(opaque);
3195ec694b5SJean-Christophe DUBOIS     uint32_t oldreg;
320a50c0d6fSJean-Christophe DUBOIS 
32105453526SJean-Christophe Dubois     DPRINTF("(%s, value = 0x%08x)\n", imx_gpt_reg_name(offset >> 2),
3225ec694b5SJean-Christophe DUBOIS             (uint32_t)value);
323a50c0d6fSJean-Christophe DUBOIS 
32405453526SJean-Christophe Dubois     switch (offset >> 2) {
3255ec694b5SJean-Christophe DUBOIS     case 0:
3265ec694b5SJean-Christophe DUBOIS         oldreg = s->cr;
3275ec694b5SJean-Christophe DUBOIS         s->cr = value & ~0x7c14;
3285ec694b5SJean-Christophe DUBOIS         if (s->cr & GPT_CR_SWR) { /* force reset */
3295ec694b5SJean-Christophe DUBOIS             /* handle the reset */
33067110c3eSJean-Christophe DUBOIS             imx_gpt_reset(DEVICE(s));
331a50c0d6fSJean-Christophe DUBOIS         } else {
3325ec694b5SJean-Christophe DUBOIS             /* set our freq, as the source might have changed */
33367110c3eSJean-Christophe DUBOIS             imx_gpt_set_freq(s);
3345ec694b5SJean-Christophe DUBOIS 
3355ec694b5SJean-Christophe DUBOIS             if ((oldreg ^ s->cr) & GPT_CR_EN) {
3365ec694b5SJean-Christophe DUBOIS                 if (s->cr & GPT_CR_EN) {
3375ec694b5SJean-Christophe DUBOIS                     if (s->cr & GPT_CR_ENMOD) {
338203d65a4SMichael Tokarev                         s->next_timeout = GPT_TIMER_MAX;
339203d65a4SMichael Tokarev                         ptimer_set_count(s->timer, GPT_TIMER_MAX);
34067110c3eSJean-Christophe DUBOIS                         imx_gpt_compute_next_timeout(s, false);
3415ec694b5SJean-Christophe DUBOIS                     }
3425ec694b5SJean-Christophe DUBOIS                     ptimer_run(s->timer, 1);
3435ec694b5SJean-Christophe DUBOIS                 } else {
3445ec694b5SJean-Christophe DUBOIS                     /* stop timer */
345a50c0d6fSJean-Christophe DUBOIS                     ptimer_stop(s->timer);
346a50c0d6fSJean-Christophe DUBOIS                 }
347a50c0d6fSJean-Christophe DUBOIS             }
3485ec694b5SJean-Christophe DUBOIS         }
3495ec694b5SJean-Christophe DUBOIS         break;
350a50c0d6fSJean-Christophe DUBOIS 
351a50c0d6fSJean-Christophe DUBOIS     case 1: /* Prescaler */
352a50c0d6fSJean-Christophe DUBOIS         s->pr = value & 0xfff;
35367110c3eSJean-Christophe DUBOIS         imx_gpt_set_freq(s);
3545ec694b5SJean-Christophe DUBOIS         break;
355a50c0d6fSJean-Christophe DUBOIS 
356a50c0d6fSJean-Christophe DUBOIS     case 2: /* SR */
3575ec694b5SJean-Christophe DUBOIS         s->sr &= ~(value & 0x3f);
35867110c3eSJean-Christophe DUBOIS         imx_gpt_update_int(s);
3595ec694b5SJean-Christophe DUBOIS         break;
360a50c0d6fSJean-Christophe DUBOIS 
361a50c0d6fSJean-Christophe DUBOIS     case 3: /* IR -- interrupt register */
362a50c0d6fSJean-Christophe DUBOIS         s->ir = value & 0x3f;
36367110c3eSJean-Christophe DUBOIS         imx_gpt_update_int(s);
3645ec694b5SJean-Christophe DUBOIS 
36567110c3eSJean-Christophe DUBOIS         imx_gpt_compute_next_timeout(s, false);
3665ec694b5SJean-Christophe DUBOIS 
3675ec694b5SJean-Christophe DUBOIS         break;
368a50c0d6fSJean-Christophe DUBOIS 
369a50c0d6fSJean-Christophe DUBOIS     case 4: /* OCR1 -- output compare register */
3705ec694b5SJean-Christophe DUBOIS         s->ocr1 = value;
3715ec694b5SJean-Christophe DUBOIS 
372a50c0d6fSJean-Christophe DUBOIS         /* In non-freerun mode, reset count when this register is written */
373a50c0d6fSJean-Christophe DUBOIS         if (!(s->cr & GPT_CR_FRR)) {
374203d65a4SMichael Tokarev             s->next_timeout = GPT_TIMER_MAX;
375203d65a4SMichael Tokarev             ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1);
376a50c0d6fSJean-Christophe DUBOIS         }
3775ec694b5SJean-Christophe DUBOIS 
3785ec694b5SJean-Christophe DUBOIS         /* compute the new timeout */
37967110c3eSJean-Christophe DUBOIS         imx_gpt_compute_next_timeout(s, false);
3805ec694b5SJean-Christophe DUBOIS 
3815ec694b5SJean-Christophe DUBOIS         break;
382a50c0d6fSJean-Christophe DUBOIS 
383a50c0d6fSJean-Christophe DUBOIS     case 5: /* OCR2 -- output compare register */
3845ec694b5SJean-Christophe DUBOIS         s->ocr2 = value;
3855ec694b5SJean-Christophe DUBOIS 
3865ec694b5SJean-Christophe DUBOIS         /* compute the new timeout */
38767110c3eSJean-Christophe DUBOIS         imx_gpt_compute_next_timeout(s, false);
3885ec694b5SJean-Christophe DUBOIS 
3895ec694b5SJean-Christophe DUBOIS         break;
3905ec694b5SJean-Christophe DUBOIS 
391a50c0d6fSJean-Christophe DUBOIS     case 6: /* OCR3 -- output compare register */
3925ec694b5SJean-Christophe DUBOIS         s->ocr3 = value;
3935ec694b5SJean-Christophe DUBOIS 
3945ec694b5SJean-Christophe DUBOIS         /* compute the new timeout */
39567110c3eSJean-Christophe DUBOIS         imx_gpt_compute_next_timeout(s, false);
3965ec694b5SJean-Christophe DUBOIS 
3975ec694b5SJean-Christophe DUBOIS         break;
3985ec694b5SJean-Christophe DUBOIS 
399a50c0d6fSJean-Christophe DUBOIS     default:
40005453526SJean-Christophe Dubois         qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
40105453526SJean-Christophe Dubois                       HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset);
4025ec694b5SJean-Christophe DUBOIS         break;
403a50c0d6fSJean-Christophe DUBOIS     }
404a50c0d6fSJean-Christophe DUBOIS }
405a50c0d6fSJean-Christophe DUBOIS 
40667110c3eSJean-Christophe DUBOIS static void imx_gpt_timeout(void *opaque)
407a50c0d6fSJean-Christophe DUBOIS {
40867110c3eSJean-Christophe DUBOIS     IMXGPTState *s = IMX_GPT(opaque);
409a50c0d6fSJean-Christophe DUBOIS 
4105ec694b5SJean-Christophe DUBOIS     DPRINTF("\n");
411a50c0d6fSJean-Christophe DUBOIS 
4125ec694b5SJean-Christophe DUBOIS     s->sr |= s->next_int;
4135ec694b5SJean-Christophe DUBOIS     s->next_int = 0;
414a50c0d6fSJean-Christophe DUBOIS 
41567110c3eSJean-Christophe DUBOIS     imx_gpt_compute_next_timeout(s, true);
4165ec694b5SJean-Christophe DUBOIS 
41767110c3eSJean-Christophe DUBOIS     imx_gpt_update_int(s);
4185ec694b5SJean-Christophe DUBOIS 
4195ec694b5SJean-Christophe DUBOIS     if (s->freq && (s->cr & GPT_CR_EN)) {
4205ec694b5SJean-Christophe DUBOIS         ptimer_run(s->timer, 1);
4215ec694b5SJean-Christophe DUBOIS     }
422a50c0d6fSJean-Christophe DUBOIS }
423a50c0d6fSJean-Christophe DUBOIS 
42467110c3eSJean-Christophe DUBOIS static const MemoryRegionOps imx_gpt_ops = {
42567110c3eSJean-Christophe DUBOIS     .read = imx_gpt_read,
42667110c3eSJean-Christophe DUBOIS     .write = imx_gpt_write,
427a50c0d6fSJean-Christophe DUBOIS     .endianness = DEVICE_NATIVE_ENDIAN,
428a50c0d6fSJean-Christophe DUBOIS };
429a50c0d6fSJean-Christophe DUBOIS 
430a50c0d6fSJean-Christophe DUBOIS 
43167110c3eSJean-Christophe DUBOIS static void imx_gpt_realize(DeviceState *dev, Error **errp)
432a50c0d6fSJean-Christophe DUBOIS {
43367110c3eSJean-Christophe DUBOIS     IMXGPTState *s = IMX_GPT(dev);
43467110c3eSJean-Christophe DUBOIS     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
435a50c0d6fSJean-Christophe DUBOIS     QEMUBH *bh;
436a50c0d6fSJean-Christophe DUBOIS 
43767110c3eSJean-Christophe DUBOIS     sysbus_init_irq(sbd, &s->irq);
438853dca12SPaolo Bonzini     memory_region_init_io(&s->iomem, OBJECT(s), &imx_gpt_ops, s, TYPE_IMX_GPT,
439a50c0d6fSJean-Christophe DUBOIS                           0x00001000);
44067110c3eSJean-Christophe DUBOIS     sysbus_init_mmio(sbd, &s->iomem);
441a50c0d6fSJean-Christophe DUBOIS 
44267110c3eSJean-Christophe DUBOIS     bh = qemu_bh_new(imx_gpt_timeout, s);
443a50c0d6fSJean-Christophe DUBOIS     s->timer = ptimer_init(bh);
444a50c0d6fSJean-Christophe DUBOIS }
445a50c0d6fSJean-Christophe DUBOIS 
44667110c3eSJean-Christophe DUBOIS static void imx_gpt_class_init(ObjectClass *klass, void *data)
447a50c0d6fSJean-Christophe DUBOIS {
448a50c0d6fSJean-Christophe DUBOIS     DeviceClass *dc = DEVICE_CLASS(klass);
44967110c3eSJean-Christophe DUBOIS 
45067110c3eSJean-Christophe DUBOIS     dc->realize = imx_gpt_realize;
45167110c3eSJean-Christophe DUBOIS     dc->reset = imx_gpt_reset;
45267110c3eSJean-Christophe DUBOIS     dc->vmsd = &vmstate_imx_timer_gpt;
453a50c0d6fSJean-Christophe DUBOIS     dc->desc = "i.MX general timer";
454a50c0d6fSJean-Christophe DUBOIS }
455a50c0d6fSJean-Christophe DUBOIS 
45667110c3eSJean-Christophe DUBOIS static const TypeInfo imx_gpt_info = {
4575ec694b5SJean-Christophe DUBOIS     .name = TYPE_IMX_GPT,
458a50c0d6fSJean-Christophe DUBOIS     .parent = TYPE_SYS_BUS_DEVICE,
45967110c3eSJean-Christophe DUBOIS     .instance_size = sizeof(IMXGPTState),
46067110c3eSJean-Christophe DUBOIS     .class_init = imx_gpt_class_init,
461a50c0d6fSJean-Christophe DUBOIS };
462a50c0d6fSJean-Christophe DUBOIS 
46367110c3eSJean-Christophe DUBOIS static void imx_gpt_register_types(void)
464a50c0d6fSJean-Christophe DUBOIS {
46567110c3eSJean-Christophe DUBOIS     type_register_static(&imx_gpt_info);
466a50c0d6fSJean-Christophe DUBOIS }
467a50c0d6fSJean-Christophe DUBOIS 
46867110c3eSJean-Christophe DUBOIS type_init(imx_gpt_register_types)
469