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" 1803dd024fSPaolo Bonzini #include "qemu/log.h" 19a50c0d6fSJean-Christophe DUBOIS 2005453526SJean-Christophe Dubois #ifndef DEBUG_IMX_GPT 2105453526SJean-Christophe Dubois #define DEBUG_IMX_GPT 0 2205453526SJean-Christophe Dubois #endif 2305453526SJean-Christophe Dubois 2405453526SJean-Christophe Dubois #define DPRINTF(fmt, args...) \ 2505453526SJean-Christophe Dubois do { \ 2605453526SJean-Christophe Dubois if (DEBUG_IMX_GPT) { \ 2705453526SJean-Christophe Dubois fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_GPT, \ 2805453526SJean-Christophe Dubois __func__, ##args); \ 2905453526SJean-Christophe Dubois } \ 3005453526SJean-Christophe Dubois } while (0) 315ec694b5SJean-Christophe DUBOIS 32d675765aSPeter Maydell static const char *imx_gpt_reg_name(uint32_t reg) 335ec694b5SJean-Christophe DUBOIS { 345ec694b5SJean-Christophe DUBOIS switch (reg) { 355ec694b5SJean-Christophe DUBOIS case 0: 365ec694b5SJean-Christophe DUBOIS return "CR"; 375ec694b5SJean-Christophe DUBOIS case 1: 385ec694b5SJean-Christophe DUBOIS return "PR"; 395ec694b5SJean-Christophe DUBOIS case 2: 405ec694b5SJean-Christophe DUBOIS return "SR"; 415ec694b5SJean-Christophe DUBOIS case 3: 425ec694b5SJean-Christophe DUBOIS return "IR"; 435ec694b5SJean-Christophe DUBOIS case 4: 445ec694b5SJean-Christophe DUBOIS return "OCR1"; 455ec694b5SJean-Christophe DUBOIS case 5: 465ec694b5SJean-Christophe DUBOIS return "OCR2"; 475ec694b5SJean-Christophe DUBOIS case 6: 485ec694b5SJean-Christophe DUBOIS return "OCR3"; 495ec694b5SJean-Christophe DUBOIS case 7: 505ec694b5SJean-Christophe DUBOIS return "ICR1"; 515ec694b5SJean-Christophe DUBOIS case 8: 525ec694b5SJean-Christophe DUBOIS return "ICR2"; 535ec694b5SJean-Christophe DUBOIS case 9: 545ec694b5SJean-Christophe DUBOIS return "CNT"; 555ec694b5SJean-Christophe DUBOIS default: 565ec694b5SJean-Christophe DUBOIS return "[?]"; 575ec694b5SJean-Christophe DUBOIS } 585ec694b5SJean-Christophe DUBOIS } 595ec694b5SJean-Christophe DUBOIS 6067110c3eSJean-Christophe DUBOIS static const VMStateDescription vmstate_imx_timer_gpt = { 6168b85290SJean-Christophe Dubois .name = TYPE_IMX_GPT, 625ec694b5SJean-Christophe DUBOIS .version_id = 3, 635ec694b5SJean-Christophe DUBOIS .minimum_version_id = 3, 64a50c0d6fSJean-Christophe DUBOIS .fields = (VMStateField[]) { 6567110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(cr, IMXGPTState), 6667110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(pr, IMXGPTState), 6767110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(sr, IMXGPTState), 6867110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(ir, IMXGPTState), 6967110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(ocr1, IMXGPTState), 7067110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(ocr2, IMXGPTState), 7167110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(ocr3, IMXGPTState), 7267110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(icr1, IMXGPTState), 7367110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(icr2, IMXGPTState), 7467110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(cnt, IMXGPTState), 7567110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(next_timeout, IMXGPTState), 7667110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(next_int, IMXGPTState), 7767110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(freq, IMXGPTState), 7867110c3eSJean-Christophe DUBOIS VMSTATE_PTIMER(timer, IMXGPTState), 79a50c0d6fSJean-Christophe DUBOIS VMSTATE_END_OF_LIST() 80a50c0d6fSJean-Christophe DUBOIS } 81a50c0d6fSJean-Christophe DUBOIS }; 82a50c0d6fSJean-Christophe DUBOIS 8366542f63SJean-Christophe Dubois static const IMXClk imx25_gpt_clocks[] = { 8466542f63SJean-Christophe Dubois CLK_NONE, /* 000 No clock source */ 8566542f63SJean-Christophe Dubois CLK_IPG, /* 001 ipg_clk, 532MHz*/ 8666542f63SJean-Christophe Dubois CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */ 8766542f63SJean-Christophe Dubois CLK_NONE, /* 011 not defined */ 8866542f63SJean-Christophe Dubois CLK_32k, /* 100 ipg_clk_32k */ 8966542f63SJean-Christophe Dubois CLK_32k, /* 101 ipg_clk_32k */ 9066542f63SJean-Christophe Dubois CLK_32k, /* 110 ipg_clk_32k */ 9166542f63SJean-Christophe Dubois CLK_32k, /* 111 ipg_clk_32k */ 9266542f63SJean-Christophe Dubois }; 9366542f63SJean-Christophe Dubois 9466542f63SJean-Christophe Dubois static const IMXClk imx31_gpt_clocks[] = { 95c91a5883SJean-Christophe Dubois CLK_NONE, /* 000 No clock source */ 96aaa9ec3bSJean-Christophe Dubois CLK_IPG, /* 001 ipg_clk, 532MHz*/ 97d552f675SJean-Christophe Dubois CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */ 98c91a5883SJean-Christophe Dubois CLK_NONE, /* 011 not defined */ 99a50c0d6fSJean-Christophe DUBOIS CLK_32k, /* 100 ipg_clk_32k */ 100c91a5883SJean-Christophe Dubois CLK_NONE, /* 101 not defined */ 101c91a5883SJean-Christophe Dubois CLK_NONE, /* 110 not defined */ 102c91a5883SJean-Christophe Dubois CLK_NONE, /* 111 not defined */ 103a50c0d6fSJean-Christophe DUBOIS }; 104a50c0d6fSJean-Christophe DUBOIS 10566542f63SJean-Christophe Dubois static const IMXClk imx6_gpt_clocks[] = { 10666542f63SJean-Christophe Dubois CLK_NONE, /* 000 No clock source */ 10766542f63SJean-Christophe Dubois CLK_IPG, /* 001 ipg_clk, 532MHz*/ 10866542f63SJean-Christophe Dubois CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */ 10966542f63SJean-Christophe Dubois CLK_EXT, /* 011 External clock */ 11066542f63SJean-Christophe Dubois CLK_32k, /* 100 ipg_clk_32k */ 11166542f63SJean-Christophe Dubois CLK_HIGH_DIV, /* 101 reference clock / 8 */ 11266542f63SJean-Christophe Dubois CLK_NONE, /* 110 not defined */ 11366542f63SJean-Christophe Dubois CLK_HIGH, /* 111 reference clock */ 11466542f63SJean-Christophe Dubois }; 11566542f63SJean-Christophe Dubois 116*a62bf59fSAndrey Smirnov static const IMXClk imx7_gpt_clocks[] = { 117*a62bf59fSAndrey Smirnov CLK_NONE, /* 000 No clock source */ 118*a62bf59fSAndrey Smirnov CLK_IPG, /* 001 ipg_clk, 532MHz*/ 119*a62bf59fSAndrey Smirnov CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */ 120*a62bf59fSAndrey Smirnov CLK_EXT, /* 011 External clock */ 121*a62bf59fSAndrey Smirnov CLK_32k, /* 100 ipg_clk_32k */ 122*a62bf59fSAndrey Smirnov CLK_HIGH, /* 101 reference clock */ 123*a62bf59fSAndrey Smirnov CLK_NONE, /* 110 not defined */ 124*a62bf59fSAndrey Smirnov CLK_NONE, /* 111 not defined */ 125*a62bf59fSAndrey Smirnov }; 126*a62bf59fSAndrey Smirnov 12767110c3eSJean-Christophe DUBOIS static void imx_gpt_set_freq(IMXGPTState *s) 128a50c0d6fSJean-Christophe DUBOIS { 1295ec694b5SJean-Christophe DUBOIS uint32_t clksrc = extract32(s->cr, GPT_CR_CLKSRC_SHIFT, 3); 130a50c0d6fSJean-Christophe DUBOIS 131aaa9ec3bSJean-Christophe Dubois s->freq = imx_ccm_get_clock_frequency(s->ccm, 13266542f63SJean-Christophe Dubois s->clocks[clksrc]) / (1 + s->pr); 133a50c0d6fSJean-Christophe DUBOIS 134aaa9ec3bSJean-Christophe Dubois DPRINTF("Setting clksrc %d to frequency %d\n", clksrc, s->freq); 135aaa9ec3bSJean-Christophe Dubois 136aaa9ec3bSJean-Christophe Dubois if (s->freq) { 137aaa9ec3bSJean-Christophe Dubois ptimer_set_freq(s->timer, s->freq); 138a50c0d6fSJean-Christophe DUBOIS } 139a50c0d6fSJean-Christophe DUBOIS } 140a50c0d6fSJean-Christophe DUBOIS 14167110c3eSJean-Christophe DUBOIS static void imx_gpt_update_int(IMXGPTState *s) 142a50c0d6fSJean-Christophe DUBOIS { 1435ec694b5SJean-Christophe DUBOIS if ((s->sr & s->ir) && (s->cr & GPT_CR_EN)) { 1445ec694b5SJean-Christophe DUBOIS qemu_irq_raise(s->irq); 1455ec694b5SJean-Christophe DUBOIS } else { 1465ec694b5SJean-Christophe DUBOIS qemu_irq_lower(s->irq); 1475ec694b5SJean-Christophe DUBOIS } 148a50c0d6fSJean-Christophe DUBOIS } 149a50c0d6fSJean-Christophe DUBOIS 15067110c3eSJean-Christophe DUBOIS static uint32_t imx_gpt_update_count(IMXGPTState *s) 151a50c0d6fSJean-Christophe DUBOIS { 1525ec694b5SJean-Christophe DUBOIS s->cnt = s->next_timeout - (uint32_t)ptimer_get_count(s->timer); 1535ec694b5SJean-Christophe DUBOIS 154a50c0d6fSJean-Christophe DUBOIS return s->cnt; 155a50c0d6fSJean-Christophe DUBOIS } 156a50c0d6fSJean-Christophe DUBOIS 15767110c3eSJean-Christophe DUBOIS static inline uint32_t imx_gpt_find_limit(uint32_t count, uint32_t reg, 1585ec694b5SJean-Christophe DUBOIS uint32_t timeout) 159a50c0d6fSJean-Christophe DUBOIS { 1605ec694b5SJean-Christophe DUBOIS if ((count < reg) && (timeout > reg)) { 1615ec694b5SJean-Christophe DUBOIS timeout = reg; 1625ec694b5SJean-Christophe DUBOIS } 163a50c0d6fSJean-Christophe DUBOIS 1645ec694b5SJean-Christophe DUBOIS return timeout; 1655ec694b5SJean-Christophe DUBOIS } 1665ec694b5SJean-Christophe DUBOIS 16767110c3eSJean-Christophe DUBOIS static void imx_gpt_compute_next_timeout(IMXGPTState *s, bool event) 1685ec694b5SJean-Christophe DUBOIS { 169203d65a4SMichael Tokarev uint32_t timeout = GPT_TIMER_MAX; 1704833e15fSJean-Christophe Dubois uint32_t count; 1715ec694b5SJean-Christophe DUBOIS long long limit; 1725ec694b5SJean-Christophe DUBOIS 1735ec694b5SJean-Christophe DUBOIS if (!(s->cr & GPT_CR_EN)) { 1745ec694b5SJean-Christophe DUBOIS /* if not enabled just return */ 175a50c0d6fSJean-Christophe DUBOIS return; 176a50c0d6fSJean-Christophe DUBOIS } 177a50c0d6fSJean-Christophe DUBOIS 1784833e15fSJean-Christophe Dubois /* update the count */ 1794833e15fSJean-Christophe Dubois count = imx_gpt_update_count(s); 1804833e15fSJean-Christophe Dubois 1815ec694b5SJean-Christophe DUBOIS if (event) { 182a50c0d6fSJean-Christophe DUBOIS /* 1834833e15fSJean-Christophe Dubois * This is an event (the ptimer reached 0 and stopped), and the 1844833e15fSJean-Christophe Dubois * timer counter is now equal to s->next_timeout. 185a50c0d6fSJean-Christophe DUBOIS */ 1864833e15fSJean-Christophe Dubois if (!(s->cr & GPT_CR_FRR) && (count == s->ocr1)) { 1874833e15fSJean-Christophe Dubois /* We are in restart mode and we crossed the compare channel 1 1884833e15fSJean-Christophe Dubois * value. We need to reset the counter to 0. 1894833e15fSJean-Christophe Dubois */ 1904833e15fSJean-Christophe Dubois count = s->cnt = s->next_timeout = 0; 1914833e15fSJean-Christophe Dubois } else if (count == GPT_TIMER_MAX) { 1924833e15fSJean-Christophe Dubois /* We reached GPT_TIMER_MAX so we need to rollover */ 1934833e15fSJean-Christophe Dubois count = s->cnt = s->next_timeout = 0; 194a50c0d6fSJean-Christophe DUBOIS } 1955ec694b5SJean-Christophe DUBOIS } 1965ec694b5SJean-Christophe DUBOIS 1975ec694b5SJean-Christophe DUBOIS /* now, find the next timeout related to count */ 1985ec694b5SJean-Christophe DUBOIS 1995ec694b5SJean-Christophe DUBOIS if (s->ir & GPT_IR_OF1IE) { 20067110c3eSJean-Christophe DUBOIS timeout = imx_gpt_find_limit(count, s->ocr1, timeout); 2015ec694b5SJean-Christophe DUBOIS } 2025ec694b5SJean-Christophe DUBOIS if (s->ir & GPT_IR_OF2IE) { 20367110c3eSJean-Christophe DUBOIS timeout = imx_gpt_find_limit(count, s->ocr2, timeout); 2045ec694b5SJean-Christophe DUBOIS } 2055ec694b5SJean-Christophe DUBOIS if (s->ir & GPT_IR_OF3IE) { 20667110c3eSJean-Christophe DUBOIS timeout = imx_gpt_find_limit(count, s->ocr3, timeout); 2075ec694b5SJean-Christophe DUBOIS } 2085ec694b5SJean-Christophe DUBOIS 2095ec694b5SJean-Christophe DUBOIS /* find the next set of interrupts to raise for next timer event */ 2105ec694b5SJean-Christophe DUBOIS 2115ec694b5SJean-Christophe DUBOIS s->next_int = 0; 2125ec694b5SJean-Christophe DUBOIS if ((s->ir & GPT_IR_OF1IE) && (timeout == s->ocr1)) { 2135ec694b5SJean-Christophe DUBOIS s->next_int |= GPT_SR_OF1; 2145ec694b5SJean-Christophe DUBOIS } 2155ec694b5SJean-Christophe DUBOIS if ((s->ir & GPT_IR_OF2IE) && (timeout == s->ocr2)) { 2165ec694b5SJean-Christophe DUBOIS s->next_int |= GPT_SR_OF2; 2175ec694b5SJean-Christophe DUBOIS } 2185ec694b5SJean-Christophe DUBOIS if ((s->ir & GPT_IR_OF3IE) && (timeout == s->ocr3)) { 2195ec694b5SJean-Christophe DUBOIS s->next_int |= GPT_SR_OF3; 2205ec694b5SJean-Christophe DUBOIS } 221203d65a4SMichael Tokarev if ((s->ir & GPT_IR_ROVIE) && (timeout == GPT_TIMER_MAX)) { 2225ec694b5SJean-Christophe DUBOIS s->next_int |= GPT_SR_ROV; 2235ec694b5SJean-Christophe DUBOIS } 2245ec694b5SJean-Christophe DUBOIS 2255ec694b5SJean-Christophe DUBOIS /* the new range to count down from */ 22667110c3eSJean-Christophe DUBOIS limit = timeout - imx_gpt_update_count(s); 2275ec694b5SJean-Christophe DUBOIS 2285ec694b5SJean-Christophe DUBOIS if (limit < 0) { 2295ec694b5SJean-Christophe DUBOIS /* 2305ec694b5SJean-Christophe DUBOIS * if we reach here, then QEMU is running too slow and we pass the 2315ec694b5SJean-Christophe DUBOIS * timeout limit while computing it. Let's deliver the interrupt 2325ec694b5SJean-Christophe DUBOIS * and compute a new limit. 2335ec694b5SJean-Christophe DUBOIS */ 2345ec694b5SJean-Christophe DUBOIS s->sr |= s->next_int; 2355ec694b5SJean-Christophe DUBOIS 23667110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, event); 2375ec694b5SJean-Christophe DUBOIS 23867110c3eSJean-Christophe DUBOIS imx_gpt_update_int(s); 2395ec694b5SJean-Christophe DUBOIS } else { 2405ec694b5SJean-Christophe DUBOIS /* New timeout value */ 2415ec694b5SJean-Christophe DUBOIS s->next_timeout = timeout; 2425ec694b5SJean-Christophe DUBOIS 2435ec694b5SJean-Christophe DUBOIS /* reset the limit to the computed range */ 2445ec694b5SJean-Christophe DUBOIS ptimer_set_limit(s->timer, limit, 1); 2455ec694b5SJean-Christophe DUBOIS } 246a50c0d6fSJean-Christophe DUBOIS } 247a50c0d6fSJean-Christophe DUBOIS 24867110c3eSJean-Christophe DUBOIS static uint64_t imx_gpt_read(void *opaque, hwaddr offset, unsigned size) 249a50c0d6fSJean-Christophe DUBOIS { 25067110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(opaque); 2515ec694b5SJean-Christophe DUBOIS uint32_t reg_value = 0; 252a50c0d6fSJean-Christophe DUBOIS 25305453526SJean-Christophe Dubois switch (offset >> 2) { 254a50c0d6fSJean-Christophe DUBOIS case 0: /* Control Register */ 2555ec694b5SJean-Christophe DUBOIS reg_value = s->cr; 2565ec694b5SJean-Christophe DUBOIS break; 257a50c0d6fSJean-Christophe DUBOIS 258a50c0d6fSJean-Christophe DUBOIS case 1: /* prescaler */ 2595ec694b5SJean-Christophe DUBOIS reg_value = s->pr; 2605ec694b5SJean-Christophe DUBOIS break; 261a50c0d6fSJean-Christophe DUBOIS 262a50c0d6fSJean-Christophe DUBOIS case 2: /* Status Register */ 2635ec694b5SJean-Christophe DUBOIS reg_value = s->sr; 2645ec694b5SJean-Christophe DUBOIS break; 265a50c0d6fSJean-Christophe DUBOIS 266a50c0d6fSJean-Christophe DUBOIS case 3: /* Interrupt Register */ 2675ec694b5SJean-Christophe DUBOIS reg_value = s->ir; 2685ec694b5SJean-Christophe DUBOIS break; 269a50c0d6fSJean-Christophe DUBOIS 270a50c0d6fSJean-Christophe DUBOIS case 4: /* Output Compare Register 1 */ 2715ec694b5SJean-Christophe DUBOIS reg_value = s->ocr1; 2725ec694b5SJean-Christophe DUBOIS break; 273a50c0d6fSJean-Christophe DUBOIS 274a50c0d6fSJean-Christophe DUBOIS case 5: /* Output Compare Register 2 */ 2755ec694b5SJean-Christophe DUBOIS reg_value = s->ocr2; 2765ec694b5SJean-Christophe DUBOIS break; 277a50c0d6fSJean-Christophe DUBOIS 278a50c0d6fSJean-Christophe DUBOIS case 6: /* Output Compare Register 3 */ 2795ec694b5SJean-Christophe DUBOIS reg_value = s->ocr3; 2805ec694b5SJean-Christophe DUBOIS break; 281a50c0d6fSJean-Christophe DUBOIS 282a50c0d6fSJean-Christophe DUBOIS case 7: /* input Capture Register 1 */ 28305453526SJean-Christophe Dubois qemu_log_mask(LOG_UNIMP, "[%s]%s: icr1 feature is not implemented\n", 28405453526SJean-Christophe Dubois TYPE_IMX_GPT, __func__); 2855ec694b5SJean-Christophe DUBOIS reg_value = s->icr1; 2865ec694b5SJean-Christophe DUBOIS break; 287a50c0d6fSJean-Christophe DUBOIS 288a50c0d6fSJean-Christophe DUBOIS case 8: /* input Capture Register 2 */ 28905453526SJean-Christophe Dubois qemu_log_mask(LOG_UNIMP, "[%s]%s: icr2 feature is not implemented\n", 29005453526SJean-Christophe Dubois TYPE_IMX_GPT, __func__); 2915ec694b5SJean-Christophe DUBOIS reg_value = s->icr2; 2925ec694b5SJean-Christophe DUBOIS break; 293a50c0d6fSJean-Christophe DUBOIS 294a50c0d6fSJean-Christophe DUBOIS case 9: /* cnt */ 29567110c3eSJean-Christophe DUBOIS imx_gpt_update_count(s); 2965ec694b5SJean-Christophe DUBOIS reg_value = s->cnt; 2975ec694b5SJean-Christophe DUBOIS break; 2985ec694b5SJean-Christophe DUBOIS 2995ec694b5SJean-Christophe DUBOIS default: 30005453526SJean-Christophe Dubois qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%" 30105453526SJean-Christophe Dubois HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset); 3025ec694b5SJean-Christophe DUBOIS break; 303a50c0d6fSJean-Christophe DUBOIS } 304a50c0d6fSJean-Christophe DUBOIS 30505453526SJean-Christophe Dubois DPRINTF("(%s) = 0x%08x\n", imx_gpt_reg_name(offset >> 2), reg_value); 306a50c0d6fSJean-Christophe DUBOIS 3075ec694b5SJean-Christophe DUBOIS return reg_value; 308a50c0d6fSJean-Christophe DUBOIS } 309a50c0d6fSJean-Christophe DUBOIS 310a50c0d6fSJean-Christophe DUBOIS 311c98c9ebaSKurban Mallachiev static void imx_gpt_reset_common(IMXGPTState *s, bool is_soft_reset) 312c98c9ebaSKurban Mallachiev { 3135ec694b5SJean-Christophe DUBOIS /* stop timer */ 3145ec694b5SJean-Christophe DUBOIS ptimer_stop(s->timer); 3155ec694b5SJean-Christophe DUBOIS 316c98c9ebaSKurban Mallachiev /* Soft reset and hard reset differ only in their handling of the CR 317c98c9ebaSKurban Mallachiev * register -- soft reset preserves the values of some bits there. 318a50c0d6fSJean-Christophe DUBOIS */ 319c98c9ebaSKurban Mallachiev if (is_soft_reset) { 320c98c9ebaSKurban Mallachiev /* Clear all CR bits except those that are preserved by soft reset. */ 321c98c9ebaSKurban Mallachiev s->cr &= GPT_CR_EN | GPT_CR_ENMOD | GPT_CR_STOPEN | GPT_CR_DOZEN | 322c98c9ebaSKurban Mallachiev GPT_CR_WAITEN | GPT_CR_DBGEN | 323c98c9ebaSKurban Mallachiev (GPT_CR_CLKSRC_MASK << GPT_CR_CLKSRC_SHIFT); 324c98c9ebaSKurban Mallachiev } else { 325c98c9ebaSKurban Mallachiev s->cr = 0; 326c98c9ebaSKurban Mallachiev } 327a50c0d6fSJean-Christophe DUBOIS s->sr = 0; 328a50c0d6fSJean-Christophe DUBOIS s->pr = 0; 329a50c0d6fSJean-Christophe DUBOIS s->ir = 0; 330a50c0d6fSJean-Christophe DUBOIS s->cnt = 0; 331203d65a4SMichael Tokarev s->ocr1 = GPT_TIMER_MAX; 332203d65a4SMichael Tokarev s->ocr2 = GPT_TIMER_MAX; 333203d65a4SMichael Tokarev s->ocr3 = GPT_TIMER_MAX; 334a50c0d6fSJean-Christophe DUBOIS s->icr1 = 0; 335a50c0d6fSJean-Christophe DUBOIS s->icr2 = 0; 3365ec694b5SJean-Christophe DUBOIS 337203d65a4SMichael Tokarev s->next_timeout = GPT_TIMER_MAX; 3385ec694b5SJean-Christophe DUBOIS s->next_int = 0; 3395ec694b5SJean-Christophe DUBOIS 3405ec694b5SJean-Christophe DUBOIS /* compute new freq */ 34167110c3eSJean-Christophe DUBOIS imx_gpt_set_freq(s); 3425ec694b5SJean-Christophe DUBOIS 343203d65a4SMichael Tokarev /* reset the limit to GPT_TIMER_MAX */ 344203d65a4SMichael Tokarev ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1); 3455ec694b5SJean-Christophe DUBOIS 3465ec694b5SJean-Christophe DUBOIS /* if the timer is still enabled, restart it */ 3475ec694b5SJean-Christophe DUBOIS if (s->freq && (s->cr & GPT_CR_EN)) { 3485ec694b5SJean-Christophe DUBOIS ptimer_run(s->timer, 1); 3495ec694b5SJean-Christophe DUBOIS } 350a50c0d6fSJean-Christophe DUBOIS } 351a50c0d6fSJean-Christophe DUBOIS 352c98c9ebaSKurban Mallachiev static void imx_gpt_soft_reset(DeviceState *dev) 353c98c9ebaSKurban Mallachiev { 354c98c9ebaSKurban Mallachiev IMXGPTState *s = IMX_GPT(dev); 355c98c9ebaSKurban Mallachiev imx_gpt_reset_common(s, true); 356c98c9ebaSKurban Mallachiev } 357c98c9ebaSKurban Mallachiev 358c98c9ebaSKurban Mallachiev static void imx_gpt_reset(DeviceState *dev) 359c98c9ebaSKurban Mallachiev { 360c98c9ebaSKurban Mallachiev IMXGPTState *s = IMX_GPT(dev); 361c98c9ebaSKurban Mallachiev imx_gpt_reset_common(s, false); 362c98c9ebaSKurban Mallachiev } 363c98c9ebaSKurban Mallachiev 36467110c3eSJean-Christophe DUBOIS static void imx_gpt_write(void *opaque, hwaddr offset, uint64_t value, 36567110c3eSJean-Christophe DUBOIS unsigned size) 366a50c0d6fSJean-Christophe DUBOIS { 36767110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(opaque); 3685ec694b5SJean-Christophe DUBOIS uint32_t oldreg; 369a50c0d6fSJean-Christophe DUBOIS 37005453526SJean-Christophe Dubois DPRINTF("(%s, value = 0x%08x)\n", imx_gpt_reg_name(offset >> 2), 3715ec694b5SJean-Christophe DUBOIS (uint32_t)value); 372a50c0d6fSJean-Christophe DUBOIS 37305453526SJean-Christophe Dubois switch (offset >> 2) { 3745ec694b5SJean-Christophe DUBOIS case 0: 3755ec694b5SJean-Christophe DUBOIS oldreg = s->cr; 3765ec694b5SJean-Christophe DUBOIS s->cr = value & ~0x7c14; 3775ec694b5SJean-Christophe DUBOIS if (s->cr & GPT_CR_SWR) { /* force reset */ 3785ec694b5SJean-Christophe DUBOIS /* handle the reset */ 379c98c9ebaSKurban Mallachiev imx_gpt_soft_reset(DEVICE(s)); 380a50c0d6fSJean-Christophe DUBOIS } else { 3815ec694b5SJean-Christophe DUBOIS /* set our freq, as the source might have changed */ 38267110c3eSJean-Christophe DUBOIS imx_gpt_set_freq(s); 3835ec694b5SJean-Christophe DUBOIS 3845ec694b5SJean-Christophe DUBOIS if ((oldreg ^ s->cr) & GPT_CR_EN) { 3855ec694b5SJean-Christophe DUBOIS if (s->cr & GPT_CR_EN) { 3865ec694b5SJean-Christophe DUBOIS if (s->cr & GPT_CR_ENMOD) { 387203d65a4SMichael Tokarev s->next_timeout = GPT_TIMER_MAX; 388203d65a4SMichael Tokarev ptimer_set_count(s->timer, GPT_TIMER_MAX); 38967110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 3905ec694b5SJean-Christophe DUBOIS } 3915ec694b5SJean-Christophe DUBOIS ptimer_run(s->timer, 1); 3925ec694b5SJean-Christophe DUBOIS } else { 3935ec694b5SJean-Christophe DUBOIS /* stop timer */ 394a50c0d6fSJean-Christophe DUBOIS ptimer_stop(s->timer); 395a50c0d6fSJean-Christophe DUBOIS } 396a50c0d6fSJean-Christophe DUBOIS } 3975ec694b5SJean-Christophe DUBOIS } 3985ec694b5SJean-Christophe DUBOIS break; 399a50c0d6fSJean-Christophe DUBOIS 400a50c0d6fSJean-Christophe DUBOIS case 1: /* Prescaler */ 401a50c0d6fSJean-Christophe DUBOIS s->pr = value & 0xfff; 40267110c3eSJean-Christophe DUBOIS imx_gpt_set_freq(s); 4035ec694b5SJean-Christophe DUBOIS break; 404a50c0d6fSJean-Christophe DUBOIS 405a50c0d6fSJean-Christophe DUBOIS case 2: /* SR */ 4065ec694b5SJean-Christophe DUBOIS s->sr &= ~(value & 0x3f); 40767110c3eSJean-Christophe DUBOIS imx_gpt_update_int(s); 4085ec694b5SJean-Christophe DUBOIS break; 409a50c0d6fSJean-Christophe DUBOIS 410a50c0d6fSJean-Christophe DUBOIS case 3: /* IR -- interrupt register */ 411a50c0d6fSJean-Christophe DUBOIS s->ir = value & 0x3f; 41267110c3eSJean-Christophe DUBOIS imx_gpt_update_int(s); 4135ec694b5SJean-Christophe DUBOIS 41467110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 4155ec694b5SJean-Christophe DUBOIS 4165ec694b5SJean-Christophe DUBOIS break; 417a50c0d6fSJean-Christophe DUBOIS 418a50c0d6fSJean-Christophe DUBOIS case 4: /* OCR1 -- output compare register */ 4195ec694b5SJean-Christophe DUBOIS s->ocr1 = value; 4205ec694b5SJean-Christophe DUBOIS 421a50c0d6fSJean-Christophe DUBOIS /* In non-freerun mode, reset count when this register is written */ 422a50c0d6fSJean-Christophe DUBOIS if (!(s->cr & GPT_CR_FRR)) { 423203d65a4SMichael Tokarev s->next_timeout = GPT_TIMER_MAX; 424203d65a4SMichael Tokarev ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1); 425a50c0d6fSJean-Christophe DUBOIS } 4265ec694b5SJean-Christophe DUBOIS 4275ec694b5SJean-Christophe DUBOIS /* compute the new timeout */ 42867110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 4295ec694b5SJean-Christophe DUBOIS 4305ec694b5SJean-Christophe DUBOIS break; 431a50c0d6fSJean-Christophe DUBOIS 432a50c0d6fSJean-Christophe DUBOIS case 5: /* OCR2 -- output compare register */ 4335ec694b5SJean-Christophe DUBOIS s->ocr2 = value; 4345ec694b5SJean-Christophe DUBOIS 4355ec694b5SJean-Christophe DUBOIS /* compute the new timeout */ 43667110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 4375ec694b5SJean-Christophe DUBOIS 4385ec694b5SJean-Christophe DUBOIS break; 4395ec694b5SJean-Christophe DUBOIS 440a50c0d6fSJean-Christophe DUBOIS case 6: /* OCR3 -- output compare register */ 4415ec694b5SJean-Christophe DUBOIS s->ocr3 = value; 4425ec694b5SJean-Christophe DUBOIS 4435ec694b5SJean-Christophe DUBOIS /* compute the new timeout */ 44467110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 4455ec694b5SJean-Christophe DUBOIS 4465ec694b5SJean-Christophe DUBOIS break; 4475ec694b5SJean-Christophe DUBOIS 448a50c0d6fSJean-Christophe DUBOIS default: 44905453526SJean-Christophe Dubois qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%" 45005453526SJean-Christophe Dubois HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset); 4515ec694b5SJean-Christophe DUBOIS break; 452a50c0d6fSJean-Christophe DUBOIS } 453a50c0d6fSJean-Christophe DUBOIS } 454a50c0d6fSJean-Christophe DUBOIS 45567110c3eSJean-Christophe DUBOIS static void imx_gpt_timeout(void *opaque) 456a50c0d6fSJean-Christophe DUBOIS { 45767110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(opaque); 458a50c0d6fSJean-Christophe DUBOIS 4595ec694b5SJean-Christophe DUBOIS DPRINTF("\n"); 460a50c0d6fSJean-Christophe DUBOIS 4615ec694b5SJean-Christophe DUBOIS s->sr |= s->next_int; 4625ec694b5SJean-Christophe DUBOIS s->next_int = 0; 463a50c0d6fSJean-Christophe DUBOIS 46467110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, true); 4655ec694b5SJean-Christophe DUBOIS 46667110c3eSJean-Christophe DUBOIS imx_gpt_update_int(s); 4675ec694b5SJean-Christophe DUBOIS 4685ec694b5SJean-Christophe DUBOIS if (s->freq && (s->cr & GPT_CR_EN)) { 4695ec694b5SJean-Christophe DUBOIS ptimer_run(s->timer, 1); 4705ec694b5SJean-Christophe DUBOIS } 471a50c0d6fSJean-Christophe DUBOIS } 472a50c0d6fSJean-Christophe DUBOIS 47367110c3eSJean-Christophe DUBOIS static const MemoryRegionOps imx_gpt_ops = { 47467110c3eSJean-Christophe DUBOIS .read = imx_gpt_read, 47567110c3eSJean-Christophe DUBOIS .write = imx_gpt_write, 476a50c0d6fSJean-Christophe DUBOIS .endianness = DEVICE_NATIVE_ENDIAN, 477a50c0d6fSJean-Christophe DUBOIS }; 478a50c0d6fSJean-Christophe DUBOIS 479a50c0d6fSJean-Christophe DUBOIS 48067110c3eSJean-Christophe DUBOIS static void imx_gpt_realize(DeviceState *dev, Error **errp) 481a50c0d6fSJean-Christophe DUBOIS { 48267110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(dev); 48367110c3eSJean-Christophe DUBOIS SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 484a50c0d6fSJean-Christophe DUBOIS QEMUBH *bh; 485a50c0d6fSJean-Christophe DUBOIS 48667110c3eSJean-Christophe DUBOIS sysbus_init_irq(sbd, &s->irq); 487853dca12SPaolo Bonzini memory_region_init_io(&s->iomem, OBJECT(s), &imx_gpt_ops, s, TYPE_IMX_GPT, 488a50c0d6fSJean-Christophe DUBOIS 0x00001000); 48967110c3eSJean-Christophe DUBOIS sysbus_init_mmio(sbd, &s->iomem); 490a50c0d6fSJean-Christophe DUBOIS 49167110c3eSJean-Christophe DUBOIS bh = qemu_bh_new(imx_gpt_timeout, s); 492e7ea81c3SDmitry Osipenko s->timer = ptimer_init(bh, PTIMER_POLICY_DEFAULT); 493a50c0d6fSJean-Christophe DUBOIS } 494a50c0d6fSJean-Christophe DUBOIS 49567110c3eSJean-Christophe DUBOIS static void imx_gpt_class_init(ObjectClass *klass, void *data) 496a50c0d6fSJean-Christophe DUBOIS { 497a50c0d6fSJean-Christophe DUBOIS DeviceClass *dc = DEVICE_CLASS(klass); 49867110c3eSJean-Christophe DUBOIS 49967110c3eSJean-Christophe DUBOIS dc->realize = imx_gpt_realize; 50067110c3eSJean-Christophe DUBOIS dc->reset = imx_gpt_reset; 50167110c3eSJean-Christophe DUBOIS dc->vmsd = &vmstate_imx_timer_gpt; 502a50c0d6fSJean-Christophe DUBOIS dc->desc = "i.MX general timer"; 503a50c0d6fSJean-Christophe DUBOIS } 504a50c0d6fSJean-Christophe DUBOIS 50566542f63SJean-Christophe Dubois static void imx25_gpt_init(Object *obj) 50666542f63SJean-Christophe Dubois { 50766542f63SJean-Christophe Dubois IMXGPTState *s = IMX_GPT(obj); 50866542f63SJean-Christophe Dubois 50966542f63SJean-Christophe Dubois s->clocks = imx25_gpt_clocks; 51066542f63SJean-Christophe Dubois } 51166542f63SJean-Christophe Dubois 51266542f63SJean-Christophe Dubois static void imx31_gpt_init(Object *obj) 51366542f63SJean-Christophe Dubois { 51466542f63SJean-Christophe Dubois IMXGPTState *s = IMX_GPT(obj); 51566542f63SJean-Christophe Dubois 51666542f63SJean-Christophe Dubois s->clocks = imx31_gpt_clocks; 51766542f63SJean-Christophe Dubois } 51866542f63SJean-Christophe Dubois 51966542f63SJean-Christophe Dubois static void imx6_gpt_init(Object *obj) 52066542f63SJean-Christophe Dubois { 52166542f63SJean-Christophe Dubois IMXGPTState *s = IMX_GPT(obj); 52266542f63SJean-Christophe Dubois 52366542f63SJean-Christophe Dubois s->clocks = imx6_gpt_clocks; 52466542f63SJean-Christophe Dubois } 52566542f63SJean-Christophe Dubois 526*a62bf59fSAndrey Smirnov static void imx7_gpt_init(Object *obj) 527*a62bf59fSAndrey Smirnov { 528*a62bf59fSAndrey Smirnov IMXGPTState *s = IMX_GPT(obj); 529*a62bf59fSAndrey Smirnov 530*a62bf59fSAndrey Smirnov s->clocks = imx7_gpt_clocks; 531*a62bf59fSAndrey Smirnov } 532*a62bf59fSAndrey Smirnov 53366542f63SJean-Christophe Dubois static const TypeInfo imx25_gpt_info = { 53466542f63SJean-Christophe Dubois .name = TYPE_IMX25_GPT, 535a50c0d6fSJean-Christophe DUBOIS .parent = TYPE_SYS_BUS_DEVICE, 53667110c3eSJean-Christophe DUBOIS .instance_size = sizeof(IMXGPTState), 53766542f63SJean-Christophe Dubois .instance_init = imx25_gpt_init, 53867110c3eSJean-Christophe DUBOIS .class_init = imx_gpt_class_init, 539a50c0d6fSJean-Christophe DUBOIS }; 540a50c0d6fSJean-Christophe DUBOIS 54166542f63SJean-Christophe Dubois static const TypeInfo imx31_gpt_info = { 54266542f63SJean-Christophe Dubois .name = TYPE_IMX31_GPT, 54366542f63SJean-Christophe Dubois .parent = TYPE_IMX25_GPT, 54466542f63SJean-Christophe Dubois .instance_init = imx31_gpt_init, 54566542f63SJean-Christophe Dubois }; 54666542f63SJean-Christophe Dubois 54766542f63SJean-Christophe Dubois static const TypeInfo imx6_gpt_info = { 54866542f63SJean-Christophe Dubois .name = TYPE_IMX6_GPT, 54966542f63SJean-Christophe Dubois .parent = TYPE_IMX25_GPT, 55066542f63SJean-Christophe Dubois .instance_init = imx6_gpt_init, 55166542f63SJean-Christophe Dubois }; 55266542f63SJean-Christophe Dubois 553*a62bf59fSAndrey Smirnov static const TypeInfo imx7_gpt_info = { 554*a62bf59fSAndrey Smirnov .name = TYPE_IMX7_GPT, 555*a62bf59fSAndrey Smirnov .parent = TYPE_IMX25_GPT, 556*a62bf59fSAndrey Smirnov .instance_init = imx7_gpt_init, 557*a62bf59fSAndrey Smirnov }; 558*a62bf59fSAndrey Smirnov 55967110c3eSJean-Christophe DUBOIS static void imx_gpt_register_types(void) 560a50c0d6fSJean-Christophe DUBOIS { 56166542f63SJean-Christophe Dubois type_register_static(&imx25_gpt_info); 56266542f63SJean-Christophe Dubois type_register_static(&imx31_gpt_info); 56366542f63SJean-Christophe Dubois type_register_static(&imx6_gpt_info); 564*a62bf59fSAndrey Smirnov type_register_static(&imx7_gpt_info); 565a50c0d6fSJean-Christophe DUBOIS } 566a50c0d6fSJean-Christophe DUBOIS 56767110c3eSJean-Christophe DUBOIS type_init(imx_gpt_register_types) 568