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" 176a1751b7SAlex Bligh #include "qemu/main-loop.h" 18*0b8fa32fSMarkus Armbruster #include "qemu/module.h" 1903dd024fSPaolo 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 33d675765aSPeter Maydell static const char *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 8466542f63SJean-Christophe Dubois static const IMXClk imx25_gpt_clocks[] = { 8566542f63SJean-Christophe Dubois CLK_NONE, /* 000 No clock source */ 8666542f63SJean-Christophe Dubois CLK_IPG, /* 001 ipg_clk, 532MHz*/ 8766542f63SJean-Christophe Dubois CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */ 8866542f63SJean-Christophe Dubois CLK_NONE, /* 011 not defined */ 8966542f63SJean-Christophe Dubois CLK_32k, /* 100 ipg_clk_32k */ 9066542f63SJean-Christophe Dubois CLK_32k, /* 101 ipg_clk_32k */ 9166542f63SJean-Christophe Dubois CLK_32k, /* 110 ipg_clk_32k */ 9266542f63SJean-Christophe Dubois CLK_32k, /* 111 ipg_clk_32k */ 9366542f63SJean-Christophe Dubois }; 9466542f63SJean-Christophe Dubois 9566542f63SJean-Christophe Dubois static const IMXClk imx31_gpt_clocks[] = { 96c91a5883SJean-Christophe Dubois CLK_NONE, /* 000 No clock source */ 97aaa9ec3bSJean-Christophe Dubois CLK_IPG, /* 001 ipg_clk, 532MHz*/ 98d552f675SJean-Christophe Dubois CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */ 99c91a5883SJean-Christophe Dubois CLK_NONE, /* 011 not defined */ 100a50c0d6fSJean-Christophe DUBOIS CLK_32k, /* 100 ipg_clk_32k */ 101c91a5883SJean-Christophe Dubois CLK_NONE, /* 101 not defined */ 102c91a5883SJean-Christophe Dubois CLK_NONE, /* 110 not defined */ 103c91a5883SJean-Christophe Dubois CLK_NONE, /* 111 not defined */ 104a50c0d6fSJean-Christophe DUBOIS }; 105a50c0d6fSJean-Christophe DUBOIS 10666542f63SJean-Christophe Dubois static const IMXClk imx6_gpt_clocks[] = { 10766542f63SJean-Christophe Dubois CLK_NONE, /* 000 No clock source */ 10866542f63SJean-Christophe Dubois CLK_IPG, /* 001 ipg_clk, 532MHz*/ 10966542f63SJean-Christophe Dubois CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */ 11066542f63SJean-Christophe Dubois CLK_EXT, /* 011 External clock */ 11166542f63SJean-Christophe Dubois CLK_32k, /* 100 ipg_clk_32k */ 11266542f63SJean-Christophe Dubois CLK_HIGH_DIV, /* 101 reference clock / 8 */ 11366542f63SJean-Christophe Dubois CLK_NONE, /* 110 not defined */ 11466542f63SJean-Christophe Dubois CLK_HIGH, /* 111 reference clock */ 11566542f63SJean-Christophe Dubois }; 11666542f63SJean-Christophe Dubois 117a62bf59fSAndrey Smirnov static const IMXClk imx7_gpt_clocks[] = { 118a62bf59fSAndrey Smirnov CLK_NONE, /* 000 No clock source */ 119a62bf59fSAndrey Smirnov CLK_IPG, /* 001 ipg_clk, 532MHz*/ 120a62bf59fSAndrey Smirnov CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */ 121a62bf59fSAndrey Smirnov CLK_EXT, /* 011 External clock */ 122a62bf59fSAndrey Smirnov CLK_32k, /* 100 ipg_clk_32k */ 123a62bf59fSAndrey Smirnov CLK_HIGH, /* 101 reference clock */ 124a62bf59fSAndrey Smirnov CLK_NONE, /* 110 not defined */ 125a62bf59fSAndrey Smirnov CLK_NONE, /* 111 not defined */ 126a62bf59fSAndrey Smirnov }; 127a62bf59fSAndrey Smirnov 12867110c3eSJean-Christophe DUBOIS static void imx_gpt_set_freq(IMXGPTState *s) 129a50c0d6fSJean-Christophe DUBOIS { 1305ec694b5SJean-Christophe DUBOIS uint32_t clksrc = extract32(s->cr, GPT_CR_CLKSRC_SHIFT, 3); 131a50c0d6fSJean-Christophe DUBOIS 132aaa9ec3bSJean-Christophe Dubois s->freq = imx_ccm_get_clock_frequency(s->ccm, 13366542f63SJean-Christophe Dubois s->clocks[clksrc]) / (1 + s->pr); 134a50c0d6fSJean-Christophe DUBOIS 135aaa9ec3bSJean-Christophe Dubois DPRINTF("Setting clksrc %d to frequency %d\n", clksrc, s->freq); 136aaa9ec3bSJean-Christophe Dubois 137aaa9ec3bSJean-Christophe Dubois if (s->freq) { 138aaa9ec3bSJean-Christophe Dubois ptimer_set_freq(s->timer, s->freq); 139a50c0d6fSJean-Christophe DUBOIS } 140a50c0d6fSJean-Christophe DUBOIS } 141a50c0d6fSJean-Christophe DUBOIS 14267110c3eSJean-Christophe DUBOIS static void imx_gpt_update_int(IMXGPTState *s) 143a50c0d6fSJean-Christophe DUBOIS { 1445ec694b5SJean-Christophe DUBOIS if ((s->sr & s->ir) && (s->cr & GPT_CR_EN)) { 1455ec694b5SJean-Christophe DUBOIS qemu_irq_raise(s->irq); 1465ec694b5SJean-Christophe DUBOIS } else { 1475ec694b5SJean-Christophe DUBOIS qemu_irq_lower(s->irq); 1485ec694b5SJean-Christophe DUBOIS } 149a50c0d6fSJean-Christophe DUBOIS } 150a50c0d6fSJean-Christophe DUBOIS 15167110c3eSJean-Christophe DUBOIS static uint32_t imx_gpt_update_count(IMXGPTState *s) 152a50c0d6fSJean-Christophe DUBOIS { 1535ec694b5SJean-Christophe DUBOIS s->cnt = s->next_timeout - (uint32_t)ptimer_get_count(s->timer); 1545ec694b5SJean-Christophe DUBOIS 155a50c0d6fSJean-Christophe DUBOIS return s->cnt; 156a50c0d6fSJean-Christophe DUBOIS } 157a50c0d6fSJean-Christophe DUBOIS 15867110c3eSJean-Christophe DUBOIS static inline uint32_t imx_gpt_find_limit(uint32_t count, uint32_t reg, 1595ec694b5SJean-Christophe DUBOIS uint32_t timeout) 160a50c0d6fSJean-Christophe DUBOIS { 1615ec694b5SJean-Christophe DUBOIS if ((count < reg) && (timeout > reg)) { 1625ec694b5SJean-Christophe DUBOIS timeout = reg; 1635ec694b5SJean-Christophe DUBOIS } 164a50c0d6fSJean-Christophe DUBOIS 1655ec694b5SJean-Christophe DUBOIS return timeout; 1665ec694b5SJean-Christophe DUBOIS } 1675ec694b5SJean-Christophe DUBOIS 16867110c3eSJean-Christophe DUBOIS static void imx_gpt_compute_next_timeout(IMXGPTState *s, bool event) 1695ec694b5SJean-Christophe DUBOIS { 170203d65a4SMichael Tokarev uint32_t timeout = GPT_TIMER_MAX; 1714833e15fSJean-Christophe Dubois uint32_t count; 1725ec694b5SJean-Christophe DUBOIS long long limit; 1735ec694b5SJean-Christophe DUBOIS 1745ec694b5SJean-Christophe DUBOIS if (!(s->cr & GPT_CR_EN)) { 1755ec694b5SJean-Christophe DUBOIS /* if not enabled just return */ 176a50c0d6fSJean-Christophe DUBOIS return; 177a50c0d6fSJean-Christophe DUBOIS } 178a50c0d6fSJean-Christophe DUBOIS 1794833e15fSJean-Christophe Dubois /* update the count */ 1804833e15fSJean-Christophe Dubois count = imx_gpt_update_count(s); 1814833e15fSJean-Christophe Dubois 1825ec694b5SJean-Christophe DUBOIS if (event) { 183a50c0d6fSJean-Christophe DUBOIS /* 1844833e15fSJean-Christophe Dubois * This is an event (the ptimer reached 0 and stopped), and the 1854833e15fSJean-Christophe Dubois * timer counter is now equal to s->next_timeout. 186a50c0d6fSJean-Christophe DUBOIS */ 1874833e15fSJean-Christophe Dubois if (!(s->cr & GPT_CR_FRR) && (count == s->ocr1)) { 1884833e15fSJean-Christophe Dubois /* We are in restart mode and we crossed the compare channel 1 1894833e15fSJean-Christophe Dubois * value. We need to reset the counter to 0. 1904833e15fSJean-Christophe Dubois */ 1914833e15fSJean-Christophe Dubois count = s->cnt = s->next_timeout = 0; 1924833e15fSJean-Christophe Dubois } else if (count == GPT_TIMER_MAX) { 1934833e15fSJean-Christophe Dubois /* We reached GPT_TIMER_MAX so we need to rollover */ 1944833e15fSJean-Christophe Dubois count = s->cnt = s->next_timeout = 0; 195a50c0d6fSJean-Christophe DUBOIS } 1965ec694b5SJean-Christophe DUBOIS } 1975ec694b5SJean-Christophe DUBOIS 1985ec694b5SJean-Christophe DUBOIS /* now, find the next timeout related to count */ 1995ec694b5SJean-Christophe DUBOIS 2005ec694b5SJean-Christophe DUBOIS if (s->ir & GPT_IR_OF1IE) { 20167110c3eSJean-Christophe DUBOIS timeout = imx_gpt_find_limit(count, s->ocr1, timeout); 2025ec694b5SJean-Christophe DUBOIS } 2035ec694b5SJean-Christophe DUBOIS if (s->ir & GPT_IR_OF2IE) { 20467110c3eSJean-Christophe DUBOIS timeout = imx_gpt_find_limit(count, s->ocr2, timeout); 2055ec694b5SJean-Christophe DUBOIS } 2065ec694b5SJean-Christophe DUBOIS if (s->ir & GPT_IR_OF3IE) { 20767110c3eSJean-Christophe DUBOIS timeout = imx_gpt_find_limit(count, s->ocr3, timeout); 2085ec694b5SJean-Christophe DUBOIS } 2095ec694b5SJean-Christophe DUBOIS 2105ec694b5SJean-Christophe DUBOIS /* find the next set of interrupts to raise for next timer event */ 2115ec694b5SJean-Christophe DUBOIS 2125ec694b5SJean-Christophe DUBOIS s->next_int = 0; 2135ec694b5SJean-Christophe DUBOIS if ((s->ir & GPT_IR_OF1IE) && (timeout == s->ocr1)) { 2145ec694b5SJean-Christophe DUBOIS s->next_int |= GPT_SR_OF1; 2155ec694b5SJean-Christophe DUBOIS } 2165ec694b5SJean-Christophe DUBOIS if ((s->ir & GPT_IR_OF2IE) && (timeout == s->ocr2)) { 2175ec694b5SJean-Christophe DUBOIS s->next_int |= GPT_SR_OF2; 2185ec694b5SJean-Christophe DUBOIS } 2195ec694b5SJean-Christophe DUBOIS if ((s->ir & GPT_IR_OF3IE) && (timeout == s->ocr3)) { 2205ec694b5SJean-Christophe DUBOIS s->next_int |= GPT_SR_OF3; 2215ec694b5SJean-Christophe DUBOIS } 222203d65a4SMichael Tokarev if ((s->ir & GPT_IR_ROVIE) && (timeout == GPT_TIMER_MAX)) { 2235ec694b5SJean-Christophe DUBOIS s->next_int |= GPT_SR_ROV; 2245ec694b5SJean-Christophe DUBOIS } 2255ec694b5SJean-Christophe DUBOIS 2265ec694b5SJean-Christophe DUBOIS /* the new range to count down from */ 22767110c3eSJean-Christophe DUBOIS limit = timeout - imx_gpt_update_count(s); 2285ec694b5SJean-Christophe DUBOIS 2295ec694b5SJean-Christophe DUBOIS if (limit < 0) { 2305ec694b5SJean-Christophe DUBOIS /* 2315ec694b5SJean-Christophe DUBOIS * if we reach here, then QEMU is running too slow and we pass the 2325ec694b5SJean-Christophe DUBOIS * timeout limit while computing it. Let's deliver the interrupt 2335ec694b5SJean-Christophe DUBOIS * and compute a new limit. 2345ec694b5SJean-Christophe DUBOIS */ 2355ec694b5SJean-Christophe DUBOIS s->sr |= s->next_int; 2365ec694b5SJean-Christophe DUBOIS 23767110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, event); 2385ec694b5SJean-Christophe DUBOIS 23967110c3eSJean-Christophe DUBOIS imx_gpt_update_int(s); 2405ec694b5SJean-Christophe DUBOIS } else { 2415ec694b5SJean-Christophe DUBOIS /* New timeout value */ 2425ec694b5SJean-Christophe DUBOIS s->next_timeout = timeout; 2435ec694b5SJean-Christophe DUBOIS 2445ec694b5SJean-Christophe DUBOIS /* reset the limit to the computed range */ 2455ec694b5SJean-Christophe DUBOIS ptimer_set_limit(s->timer, limit, 1); 2465ec694b5SJean-Christophe DUBOIS } 247a50c0d6fSJean-Christophe DUBOIS } 248a50c0d6fSJean-Christophe DUBOIS 24967110c3eSJean-Christophe DUBOIS static uint64_t imx_gpt_read(void *opaque, hwaddr offset, unsigned size) 250a50c0d6fSJean-Christophe DUBOIS { 25167110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(opaque); 2525ec694b5SJean-Christophe DUBOIS uint32_t reg_value = 0; 253a50c0d6fSJean-Christophe DUBOIS 25405453526SJean-Christophe Dubois switch (offset >> 2) { 255a50c0d6fSJean-Christophe DUBOIS case 0: /* Control Register */ 2565ec694b5SJean-Christophe DUBOIS reg_value = s->cr; 2575ec694b5SJean-Christophe DUBOIS break; 258a50c0d6fSJean-Christophe DUBOIS 259a50c0d6fSJean-Christophe DUBOIS case 1: /* prescaler */ 2605ec694b5SJean-Christophe DUBOIS reg_value = s->pr; 2615ec694b5SJean-Christophe DUBOIS break; 262a50c0d6fSJean-Christophe DUBOIS 263a50c0d6fSJean-Christophe DUBOIS case 2: /* Status Register */ 2645ec694b5SJean-Christophe DUBOIS reg_value = s->sr; 2655ec694b5SJean-Christophe DUBOIS break; 266a50c0d6fSJean-Christophe DUBOIS 267a50c0d6fSJean-Christophe DUBOIS case 3: /* Interrupt Register */ 2685ec694b5SJean-Christophe DUBOIS reg_value = s->ir; 2695ec694b5SJean-Christophe DUBOIS break; 270a50c0d6fSJean-Christophe DUBOIS 271a50c0d6fSJean-Christophe DUBOIS case 4: /* Output Compare Register 1 */ 2725ec694b5SJean-Christophe DUBOIS reg_value = s->ocr1; 2735ec694b5SJean-Christophe DUBOIS break; 274a50c0d6fSJean-Christophe DUBOIS 275a50c0d6fSJean-Christophe DUBOIS case 5: /* Output Compare Register 2 */ 2765ec694b5SJean-Christophe DUBOIS reg_value = s->ocr2; 2775ec694b5SJean-Christophe DUBOIS break; 278a50c0d6fSJean-Christophe DUBOIS 279a50c0d6fSJean-Christophe DUBOIS case 6: /* Output Compare Register 3 */ 2805ec694b5SJean-Christophe DUBOIS reg_value = s->ocr3; 2815ec694b5SJean-Christophe DUBOIS break; 282a50c0d6fSJean-Christophe DUBOIS 283a50c0d6fSJean-Christophe DUBOIS case 7: /* input Capture Register 1 */ 28405453526SJean-Christophe Dubois qemu_log_mask(LOG_UNIMP, "[%s]%s: icr1 feature is not implemented\n", 28505453526SJean-Christophe Dubois TYPE_IMX_GPT, __func__); 2865ec694b5SJean-Christophe DUBOIS reg_value = s->icr1; 2875ec694b5SJean-Christophe DUBOIS break; 288a50c0d6fSJean-Christophe DUBOIS 289a50c0d6fSJean-Christophe DUBOIS case 8: /* input Capture Register 2 */ 29005453526SJean-Christophe Dubois qemu_log_mask(LOG_UNIMP, "[%s]%s: icr2 feature is not implemented\n", 29105453526SJean-Christophe Dubois TYPE_IMX_GPT, __func__); 2925ec694b5SJean-Christophe DUBOIS reg_value = s->icr2; 2935ec694b5SJean-Christophe DUBOIS break; 294a50c0d6fSJean-Christophe DUBOIS 295a50c0d6fSJean-Christophe DUBOIS case 9: /* cnt */ 29667110c3eSJean-Christophe DUBOIS imx_gpt_update_count(s); 2975ec694b5SJean-Christophe DUBOIS reg_value = s->cnt; 2985ec694b5SJean-Christophe DUBOIS break; 2995ec694b5SJean-Christophe DUBOIS 3005ec694b5SJean-Christophe DUBOIS default: 30105453526SJean-Christophe Dubois qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%" 30205453526SJean-Christophe Dubois HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset); 3035ec694b5SJean-Christophe DUBOIS break; 304a50c0d6fSJean-Christophe DUBOIS } 305a50c0d6fSJean-Christophe DUBOIS 30605453526SJean-Christophe Dubois DPRINTF("(%s) = 0x%08x\n", imx_gpt_reg_name(offset >> 2), reg_value); 307a50c0d6fSJean-Christophe DUBOIS 3085ec694b5SJean-Christophe DUBOIS return reg_value; 309a50c0d6fSJean-Christophe DUBOIS } 310a50c0d6fSJean-Christophe DUBOIS 311a50c0d6fSJean-Christophe DUBOIS 312c98c9ebaSKurban Mallachiev static void imx_gpt_reset_common(IMXGPTState *s, bool is_soft_reset) 313c98c9ebaSKurban Mallachiev { 3145ec694b5SJean-Christophe DUBOIS /* stop timer */ 3155ec694b5SJean-Christophe DUBOIS ptimer_stop(s->timer); 3165ec694b5SJean-Christophe DUBOIS 317c98c9ebaSKurban Mallachiev /* Soft reset and hard reset differ only in their handling of the CR 318c98c9ebaSKurban Mallachiev * register -- soft reset preserves the values of some bits there. 319a50c0d6fSJean-Christophe DUBOIS */ 320c98c9ebaSKurban Mallachiev if (is_soft_reset) { 321c98c9ebaSKurban Mallachiev /* Clear all CR bits except those that are preserved by soft reset. */ 322c98c9ebaSKurban Mallachiev s->cr &= GPT_CR_EN | GPT_CR_ENMOD | GPT_CR_STOPEN | GPT_CR_DOZEN | 323c98c9ebaSKurban Mallachiev GPT_CR_WAITEN | GPT_CR_DBGEN | 324c98c9ebaSKurban Mallachiev (GPT_CR_CLKSRC_MASK << GPT_CR_CLKSRC_SHIFT); 325c98c9ebaSKurban Mallachiev } else { 326c98c9ebaSKurban Mallachiev s->cr = 0; 327c98c9ebaSKurban Mallachiev } 328a50c0d6fSJean-Christophe DUBOIS s->sr = 0; 329a50c0d6fSJean-Christophe DUBOIS s->pr = 0; 330a50c0d6fSJean-Christophe DUBOIS s->ir = 0; 331a50c0d6fSJean-Christophe DUBOIS s->cnt = 0; 332203d65a4SMichael Tokarev s->ocr1 = GPT_TIMER_MAX; 333203d65a4SMichael Tokarev s->ocr2 = GPT_TIMER_MAX; 334203d65a4SMichael Tokarev s->ocr3 = GPT_TIMER_MAX; 335a50c0d6fSJean-Christophe DUBOIS s->icr1 = 0; 336a50c0d6fSJean-Christophe DUBOIS s->icr2 = 0; 3375ec694b5SJean-Christophe DUBOIS 338203d65a4SMichael Tokarev s->next_timeout = GPT_TIMER_MAX; 3395ec694b5SJean-Christophe DUBOIS s->next_int = 0; 3405ec694b5SJean-Christophe DUBOIS 3415ec694b5SJean-Christophe DUBOIS /* compute new freq */ 34267110c3eSJean-Christophe DUBOIS imx_gpt_set_freq(s); 3435ec694b5SJean-Christophe DUBOIS 344203d65a4SMichael Tokarev /* reset the limit to GPT_TIMER_MAX */ 345203d65a4SMichael Tokarev ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1); 3465ec694b5SJean-Christophe DUBOIS 3475ec694b5SJean-Christophe DUBOIS /* if the timer is still enabled, restart it */ 3485ec694b5SJean-Christophe DUBOIS if (s->freq && (s->cr & GPT_CR_EN)) { 3495ec694b5SJean-Christophe DUBOIS ptimer_run(s->timer, 1); 3505ec694b5SJean-Christophe DUBOIS } 351a50c0d6fSJean-Christophe DUBOIS } 352a50c0d6fSJean-Christophe DUBOIS 353c98c9ebaSKurban Mallachiev static void imx_gpt_soft_reset(DeviceState *dev) 354c98c9ebaSKurban Mallachiev { 355c98c9ebaSKurban Mallachiev IMXGPTState *s = IMX_GPT(dev); 356c98c9ebaSKurban Mallachiev imx_gpt_reset_common(s, true); 357c98c9ebaSKurban Mallachiev } 358c98c9ebaSKurban Mallachiev 359c98c9ebaSKurban Mallachiev static void imx_gpt_reset(DeviceState *dev) 360c98c9ebaSKurban Mallachiev { 361c98c9ebaSKurban Mallachiev IMXGPTState *s = IMX_GPT(dev); 362c98c9ebaSKurban Mallachiev imx_gpt_reset_common(s, false); 363c98c9ebaSKurban Mallachiev } 364c98c9ebaSKurban Mallachiev 36567110c3eSJean-Christophe DUBOIS static void imx_gpt_write(void *opaque, hwaddr offset, uint64_t value, 36667110c3eSJean-Christophe DUBOIS unsigned size) 367a50c0d6fSJean-Christophe DUBOIS { 36867110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(opaque); 3695ec694b5SJean-Christophe DUBOIS uint32_t oldreg; 370a50c0d6fSJean-Christophe DUBOIS 37105453526SJean-Christophe Dubois DPRINTF("(%s, value = 0x%08x)\n", imx_gpt_reg_name(offset >> 2), 3725ec694b5SJean-Christophe DUBOIS (uint32_t)value); 373a50c0d6fSJean-Christophe DUBOIS 37405453526SJean-Christophe Dubois switch (offset >> 2) { 3755ec694b5SJean-Christophe DUBOIS case 0: 3765ec694b5SJean-Christophe DUBOIS oldreg = s->cr; 3775ec694b5SJean-Christophe DUBOIS s->cr = value & ~0x7c14; 3785ec694b5SJean-Christophe DUBOIS if (s->cr & GPT_CR_SWR) { /* force reset */ 3795ec694b5SJean-Christophe DUBOIS /* handle the reset */ 380c98c9ebaSKurban Mallachiev imx_gpt_soft_reset(DEVICE(s)); 381a50c0d6fSJean-Christophe DUBOIS } else { 3825ec694b5SJean-Christophe DUBOIS /* set our freq, as the source might have changed */ 38367110c3eSJean-Christophe DUBOIS imx_gpt_set_freq(s); 3845ec694b5SJean-Christophe DUBOIS 3855ec694b5SJean-Christophe DUBOIS if ((oldreg ^ s->cr) & GPT_CR_EN) { 3865ec694b5SJean-Christophe DUBOIS if (s->cr & GPT_CR_EN) { 3875ec694b5SJean-Christophe DUBOIS if (s->cr & GPT_CR_ENMOD) { 388203d65a4SMichael Tokarev s->next_timeout = GPT_TIMER_MAX; 389203d65a4SMichael Tokarev ptimer_set_count(s->timer, GPT_TIMER_MAX); 39067110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 3915ec694b5SJean-Christophe DUBOIS } 3925ec694b5SJean-Christophe DUBOIS ptimer_run(s->timer, 1); 3935ec694b5SJean-Christophe DUBOIS } else { 3945ec694b5SJean-Christophe DUBOIS /* stop timer */ 395a50c0d6fSJean-Christophe DUBOIS ptimer_stop(s->timer); 396a50c0d6fSJean-Christophe DUBOIS } 397a50c0d6fSJean-Christophe DUBOIS } 3985ec694b5SJean-Christophe DUBOIS } 3995ec694b5SJean-Christophe DUBOIS break; 400a50c0d6fSJean-Christophe DUBOIS 401a50c0d6fSJean-Christophe DUBOIS case 1: /* Prescaler */ 402a50c0d6fSJean-Christophe DUBOIS s->pr = value & 0xfff; 40367110c3eSJean-Christophe DUBOIS imx_gpt_set_freq(s); 4045ec694b5SJean-Christophe DUBOIS break; 405a50c0d6fSJean-Christophe DUBOIS 406a50c0d6fSJean-Christophe DUBOIS case 2: /* SR */ 4075ec694b5SJean-Christophe DUBOIS s->sr &= ~(value & 0x3f); 40867110c3eSJean-Christophe DUBOIS imx_gpt_update_int(s); 4095ec694b5SJean-Christophe DUBOIS break; 410a50c0d6fSJean-Christophe DUBOIS 411a50c0d6fSJean-Christophe DUBOIS case 3: /* IR -- interrupt register */ 412a50c0d6fSJean-Christophe DUBOIS s->ir = value & 0x3f; 41367110c3eSJean-Christophe DUBOIS imx_gpt_update_int(s); 4145ec694b5SJean-Christophe DUBOIS 41567110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 4165ec694b5SJean-Christophe DUBOIS 4175ec694b5SJean-Christophe DUBOIS break; 418a50c0d6fSJean-Christophe DUBOIS 419a50c0d6fSJean-Christophe DUBOIS case 4: /* OCR1 -- output compare register */ 4205ec694b5SJean-Christophe DUBOIS s->ocr1 = value; 4215ec694b5SJean-Christophe DUBOIS 422a50c0d6fSJean-Christophe DUBOIS /* In non-freerun mode, reset count when this register is written */ 423a50c0d6fSJean-Christophe DUBOIS if (!(s->cr & GPT_CR_FRR)) { 424203d65a4SMichael Tokarev s->next_timeout = GPT_TIMER_MAX; 425203d65a4SMichael Tokarev ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1); 426a50c0d6fSJean-Christophe DUBOIS } 4275ec694b5SJean-Christophe DUBOIS 4285ec694b5SJean-Christophe DUBOIS /* compute the new timeout */ 42967110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 4305ec694b5SJean-Christophe DUBOIS 4315ec694b5SJean-Christophe DUBOIS break; 432a50c0d6fSJean-Christophe DUBOIS 433a50c0d6fSJean-Christophe DUBOIS case 5: /* OCR2 -- output compare register */ 4345ec694b5SJean-Christophe DUBOIS s->ocr2 = value; 4355ec694b5SJean-Christophe DUBOIS 4365ec694b5SJean-Christophe DUBOIS /* compute the new timeout */ 43767110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 4385ec694b5SJean-Christophe DUBOIS 4395ec694b5SJean-Christophe DUBOIS break; 4405ec694b5SJean-Christophe DUBOIS 441a50c0d6fSJean-Christophe DUBOIS case 6: /* OCR3 -- output compare register */ 4425ec694b5SJean-Christophe DUBOIS s->ocr3 = value; 4435ec694b5SJean-Christophe DUBOIS 4445ec694b5SJean-Christophe DUBOIS /* compute the new timeout */ 44567110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 4465ec694b5SJean-Christophe DUBOIS 4475ec694b5SJean-Christophe DUBOIS break; 4485ec694b5SJean-Christophe DUBOIS 449a50c0d6fSJean-Christophe DUBOIS default: 45005453526SJean-Christophe Dubois qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%" 45105453526SJean-Christophe Dubois HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset); 4525ec694b5SJean-Christophe DUBOIS break; 453a50c0d6fSJean-Christophe DUBOIS } 454a50c0d6fSJean-Christophe DUBOIS } 455a50c0d6fSJean-Christophe DUBOIS 45667110c3eSJean-Christophe DUBOIS static void imx_gpt_timeout(void *opaque) 457a50c0d6fSJean-Christophe DUBOIS { 45867110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(opaque); 459a50c0d6fSJean-Christophe DUBOIS 4605ec694b5SJean-Christophe DUBOIS DPRINTF("\n"); 461a50c0d6fSJean-Christophe DUBOIS 4625ec694b5SJean-Christophe DUBOIS s->sr |= s->next_int; 4635ec694b5SJean-Christophe DUBOIS s->next_int = 0; 464a50c0d6fSJean-Christophe DUBOIS 46567110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, true); 4665ec694b5SJean-Christophe DUBOIS 46767110c3eSJean-Christophe DUBOIS imx_gpt_update_int(s); 4685ec694b5SJean-Christophe DUBOIS 4695ec694b5SJean-Christophe DUBOIS if (s->freq && (s->cr & GPT_CR_EN)) { 4705ec694b5SJean-Christophe DUBOIS ptimer_run(s->timer, 1); 4715ec694b5SJean-Christophe DUBOIS } 472a50c0d6fSJean-Christophe DUBOIS } 473a50c0d6fSJean-Christophe DUBOIS 47467110c3eSJean-Christophe DUBOIS static const MemoryRegionOps imx_gpt_ops = { 47567110c3eSJean-Christophe DUBOIS .read = imx_gpt_read, 47667110c3eSJean-Christophe DUBOIS .write = imx_gpt_write, 477a50c0d6fSJean-Christophe DUBOIS .endianness = DEVICE_NATIVE_ENDIAN, 478a50c0d6fSJean-Christophe DUBOIS }; 479a50c0d6fSJean-Christophe DUBOIS 480a50c0d6fSJean-Christophe DUBOIS 48167110c3eSJean-Christophe DUBOIS static void imx_gpt_realize(DeviceState *dev, Error **errp) 482a50c0d6fSJean-Christophe DUBOIS { 48367110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(dev); 48467110c3eSJean-Christophe DUBOIS SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 485a50c0d6fSJean-Christophe DUBOIS QEMUBH *bh; 486a50c0d6fSJean-Christophe DUBOIS 48767110c3eSJean-Christophe DUBOIS sysbus_init_irq(sbd, &s->irq); 488853dca12SPaolo Bonzini memory_region_init_io(&s->iomem, OBJECT(s), &imx_gpt_ops, s, TYPE_IMX_GPT, 489a50c0d6fSJean-Christophe DUBOIS 0x00001000); 49067110c3eSJean-Christophe DUBOIS sysbus_init_mmio(sbd, &s->iomem); 491a50c0d6fSJean-Christophe DUBOIS 49267110c3eSJean-Christophe DUBOIS bh = qemu_bh_new(imx_gpt_timeout, s); 493e7ea81c3SDmitry Osipenko s->timer = ptimer_init(bh, PTIMER_POLICY_DEFAULT); 494a50c0d6fSJean-Christophe DUBOIS } 495a50c0d6fSJean-Christophe DUBOIS 49667110c3eSJean-Christophe DUBOIS static void imx_gpt_class_init(ObjectClass *klass, void *data) 497a50c0d6fSJean-Christophe DUBOIS { 498a50c0d6fSJean-Christophe DUBOIS DeviceClass *dc = DEVICE_CLASS(klass); 49967110c3eSJean-Christophe DUBOIS 50067110c3eSJean-Christophe DUBOIS dc->realize = imx_gpt_realize; 50167110c3eSJean-Christophe DUBOIS dc->reset = imx_gpt_reset; 50267110c3eSJean-Christophe DUBOIS dc->vmsd = &vmstate_imx_timer_gpt; 503a50c0d6fSJean-Christophe DUBOIS dc->desc = "i.MX general timer"; 504a50c0d6fSJean-Christophe DUBOIS } 505a50c0d6fSJean-Christophe DUBOIS 50666542f63SJean-Christophe Dubois static void imx25_gpt_init(Object *obj) 50766542f63SJean-Christophe Dubois { 50866542f63SJean-Christophe Dubois IMXGPTState *s = IMX_GPT(obj); 50966542f63SJean-Christophe Dubois 51066542f63SJean-Christophe Dubois s->clocks = imx25_gpt_clocks; 51166542f63SJean-Christophe Dubois } 51266542f63SJean-Christophe Dubois 51366542f63SJean-Christophe Dubois static void imx31_gpt_init(Object *obj) 51466542f63SJean-Christophe Dubois { 51566542f63SJean-Christophe Dubois IMXGPTState *s = IMX_GPT(obj); 51666542f63SJean-Christophe Dubois 51766542f63SJean-Christophe Dubois s->clocks = imx31_gpt_clocks; 51866542f63SJean-Christophe Dubois } 51966542f63SJean-Christophe Dubois 52066542f63SJean-Christophe Dubois static void imx6_gpt_init(Object *obj) 52166542f63SJean-Christophe Dubois { 52266542f63SJean-Christophe Dubois IMXGPTState *s = IMX_GPT(obj); 52366542f63SJean-Christophe Dubois 52466542f63SJean-Christophe Dubois s->clocks = imx6_gpt_clocks; 52566542f63SJean-Christophe Dubois } 52666542f63SJean-Christophe Dubois 527a62bf59fSAndrey Smirnov static void imx7_gpt_init(Object *obj) 528a62bf59fSAndrey Smirnov { 529a62bf59fSAndrey Smirnov IMXGPTState *s = IMX_GPT(obj); 530a62bf59fSAndrey Smirnov 531a62bf59fSAndrey Smirnov s->clocks = imx7_gpt_clocks; 532a62bf59fSAndrey Smirnov } 533a62bf59fSAndrey Smirnov 53466542f63SJean-Christophe Dubois static const TypeInfo imx25_gpt_info = { 53566542f63SJean-Christophe Dubois .name = TYPE_IMX25_GPT, 536a50c0d6fSJean-Christophe DUBOIS .parent = TYPE_SYS_BUS_DEVICE, 53767110c3eSJean-Christophe DUBOIS .instance_size = sizeof(IMXGPTState), 53866542f63SJean-Christophe Dubois .instance_init = imx25_gpt_init, 53967110c3eSJean-Christophe DUBOIS .class_init = imx_gpt_class_init, 540a50c0d6fSJean-Christophe DUBOIS }; 541a50c0d6fSJean-Christophe DUBOIS 54266542f63SJean-Christophe Dubois static const TypeInfo imx31_gpt_info = { 54366542f63SJean-Christophe Dubois .name = TYPE_IMX31_GPT, 54466542f63SJean-Christophe Dubois .parent = TYPE_IMX25_GPT, 54566542f63SJean-Christophe Dubois .instance_init = imx31_gpt_init, 54666542f63SJean-Christophe Dubois }; 54766542f63SJean-Christophe Dubois 54866542f63SJean-Christophe Dubois static const TypeInfo imx6_gpt_info = { 54966542f63SJean-Christophe Dubois .name = TYPE_IMX6_GPT, 55066542f63SJean-Christophe Dubois .parent = TYPE_IMX25_GPT, 55166542f63SJean-Christophe Dubois .instance_init = imx6_gpt_init, 55266542f63SJean-Christophe Dubois }; 55366542f63SJean-Christophe Dubois 554a62bf59fSAndrey Smirnov static const TypeInfo imx7_gpt_info = { 555a62bf59fSAndrey Smirnov .name = TYPE_IMX7_GPT, 556a62bf59fSAndrey Smirnov .parent = TYPE_IMX25_GPT, 557a62bf59fSAndrey Smirnov .instance_init = imx7_gpt_init, 558a62bf59fSAndrey Smirnov }; 559a62bf59fSAndrey Smirnov 56067110c3eSJean-Christophe DUBOIS static void imx_gpt_register_types(void) 561a50c0d6fSJean-Christophe DUBOIS { 56266542f63SJean-Christophe Dubois type_register_static(&imx25_gpt_info); 56366542f63SJean-Christophe Dubois type_register_static(&imx31_gpt_info); 56466542f63SJean-Christophe Dubois type_register_static(&imx6_gpt_info); 565a62bf59fSAndrey Smirnov type_register_static(&imx7_gpt_info); 566a50c0d6fSJean-Christophe DUBOIS } 567a50c0d6fSJean-Christophe DUBOIS 56867110c3eSJean-Christophe DUBOIS type_init(imx_gpt_register_types) 569