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 15*8ef94f0bSPeter 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" 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 3267110c3eSJean-Christophe DUBOIS static char const *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 8367110c3eSJean-Christophe DUBOIS static const IMXClk imx_gpt_clocks[] = { 84a50c0d6fSJean-Christophe DUBOIS NOCLK, /* 000 No clock source */ 85aaa9ec3bSJean-Christophe Dubois CLK_IPG, /* 001 ipg_clk, 532MHz*/ 86aaa9ec3bSJean-Christophe Dubois CLK_IPG, /* 010 ipg_clk_highfreq */ 87a50c0d6fSJean-Christophe DUBOIS NOCLK, /* 011 not defined */ 88a50c0d6fSJean-Christophe DUBOIS CLK_32k, /* 100 ipg_clk_32k */ 89a50c0d6fSJean-Christophe DUBOIS NOCLK, /* 101 not defined */ 90a50c0d6fSJean-Christophe DUBOIS NOCLK, /* 110 not defined */ 91a50c0d6fSJean-Christophe DUBOIS NOCLK, /* 111 not defined */ 92a50c0d6fSJean-Christophe DUBOIS }; 93a50c0d6fSJean-Christophe DUBOIS 9467110c3eSJean-Christophe DUBOIS static void imx_gpt_set_freq(IMXGPTState *s) 95a50c0d6fSJean-Christophe DUBOIS { 965ec694b5SJean-Christophe DUBOIS uint32_t clksrc = extract32(s->cr, GPT_CR_CLKSRC_SHIFT, 3); 97a50c0d6fSJean-Christophe DUBOIS 98aaa9ec3bSJean-Christophe Dubois s->freq = imx_ccm_get_clock_frequency(s->ccm, 99aaa9ec3bSJean-Christophe Dubois imx_gpt_clocks[clksrc]) / (1 + s->pr); 100a50c0d6fSJean-Christophe DUBOIS 101aaa9ec3bSJean-Christophe Dubois DPRINTF("Setting clksrc %d to frequency %d\n", clksrc, s->freq); 102aaa9ec3bSJean-Christophe Dubois 103aaa9ec3bSJean-Christophe Dubois if (s->freq) { 104aaa9ec3bSJean-Christophe Dubois ptimer_set_freq(s->timer, s->freq); 105a50c0d6fSJean-Christophe DUBOIS } 106a50c0d6fSJean-Christophe DUBOIS } 107a50c0d6fSJean-Christophe DUBOIS 10867110c3eSJean-Christophe DUBOIS static void imx_gpt_update_int(IMXGPTState *s) 109a50c0d6fSJean-Christophe DUBOIS { 1105ec694b5SJean-Christophe DUBOIS if ((s->sr & s->ir) && (s->cr & GPT_CR_EN)) { 1115ec694b5SJean-Christophe DUBOIS qemu_irq_raise(s->irq); 1125ec694b5SJean-Christophe DUBOIS } else { 1135ec694b5SJean-Christophe DUBOIS qemu_irq_lower(s->irq); 1145ec694b5SJean-Christophe DUBOIS } 115a50c0d6fSJean-Christophe DUBOIS } 116a50c0d6fSJean-Christophe DUBOIS 11767110c3eSJean-Christophe DUBOIS static uint32_t imx_gpt_update_count(IMXGPTState *s) 118a50c0d6fSJean-Christophe DUBOIS { 1195ec694b5SJean-Christophe DUBOIS s->cnt = s->next_timeout - (uint32_t)ptimer_get_count(s->timer); 1205ec694b5SJean-Christophe DUBOIS 121a50c0d6fSJean-Christophe DUBOIS return s->cnt; 122a50c0d6fSJean-Christophe DUBOIS } 123a50c0d6fSJean-Christophe DUBOIS 12467110c3eSJean-Christophe DUBOIS static inline uint32_t imx_gpt_find_limit(uint32_t count, uint32_t reg, 1255ec694b5SJean-Christophe DUBOIS uint32_t timeout) 126a50c0d6fSJean-Christophe DUBOIS { 1275ec694b5SJean-Christophe DUBOIS if ((count < reg) && (timeout > reg)) { 1285ec694b5SJean-Christophe DUBOIS timeout = reg; 1295ec694b5SJean-Christophe DUBOIS } 130a50c0d6fSJean-Christophe DUBOIS 1315ec694b5SJean-Christophe DUBOIS return timeout; 1325ec694b5SJean-Christophe DUBOIS } 1335ec694b5SJean-Christophe DUBOIS 13467110c3eSJean-Christophe DUBOIS static void imx_gpt_compute_next_timeout(IMXGPTState *s, bool event) 1355ec694b5SJean-Christophe DUBOIS { 136203d65a4SMichael Tokarev uint32_t timeout = GPT_TIMER_MAX; 1375ec694b5SJean-Christophe DUBOIS uint32_t count = 0; 1385ec694b5SJean-Christophe DUBOIS long long limit; 1395ec694b5SJean-Christophe DUBOIS 1405ec694b5SJean-Christophe DUBOIS if (!(s->cr & GPT_CR_EN)) { 1415ec694b5SJean-Christophe DUBOIS /* if not enabled just return */ 142a50c0d6fSJean-Christophe DUBOIS return; 143a50c0d6fSJean-Christophe DUBOIS } 144a50c0d6fSJean-Christophe DUBOIS 1455ec694b5SJean-Christophe DUBOIS if (event) { 1465ec694b5SJean-Christophe DUBOIS /* This is a timer event */ 1475ec694b5SJean-Christophe DUBOIS 148203d65a4SMichael Tokarev if ((s->cr & GPT_CR_FRR) && (s->next_timeout != GPT_TIMER_MAX)) { 149a50c0d6fSJean-Christophe DUBOIS /* 1505ec694b5SJean-Christophe DUBOIS * if we are in free running mode and we have not reached 151203d65a4SMichael Tokarev * the GPT_TIMER_MAX limit, then update the count 152a50c0d6fSJean-Christophe DUBOIS */ 15367110c3eSJean-Christophe DUBOIS count = imx_gpt_update_count(s); 154a50c0d6fSJean-Christophe DUBOIS } 1555ec694b5SJean-Christophe DUBOIS } else { 1565ec694b5SJean-Christophe DUBOIS /* not a timer event, then just update the count */ 1575ec694b5SJean-Christophe DUBOIS 15867110c3eSJean-Christophe DUBOIS count = imx_gpt_update_count(s); 1595ec694b5SJean-Christophe DUBOIS } 1605ec694b5SJean-Christophe DUBOIS 1615ec694b5SJean-Christophe DUBOIS /* now, find the next timeout related to count */ 1625ec694b5SJean-Christophe DUBOIS 1635ec694b5SJean-Christophe DUBOIS if (s->ir & GPT_IR_OF1IE) { 16467110c3eSJean-Christophe DUBOIS timeout = imx_gpt_find_limit(count, s->ocr1, timeout); 1655ec694b5SJean-Christophe DUBOIS } 1665ec694b5SJean-Christophe DUBOIS if (s->ir & GPT_IR_OF2IE) { 16767110c3eSJean-Christophe DUBOIS timeout = imx_gpt_find_limit(count, s->ocr2, timeout); 1685ec694b5SJean-Christophe DUBOIS } 1695ec694b5SJean-Christophe DUBOIS if (s->ir & GPT_IR_OF3IE) { 17067110c3eSJean-Christophe DUBOIS timeout = imx_gpt_find_limit(count, s->ocr3, timeout); 1715ec694b5SJean-Christophe DUBOIS } 1725ec694b5SJean-Christophe DUBOIS 1735ec694b5SJean-Christophe DUBOIS /* find the next set of interrupts to raise for next timer event */ 1745ec694b5SJean-Christophe DUBOIS 1755ec694b5SJean-Christophe DUBOIS s->next_int = 0; 1765ec694b5SJean-Christophe DUBOIS if ((s->ir & GPT_IR_OF1IE) && (timeout == s->ocr1)) { 1775ec694b5SJean-Christophe DUBOIS s->next_int |= GPT_SR_OF1; 1785ec694b5SJean-Christophe DUBOIS } 1795ec694b5SJean-Christophe DUBOIS if ((s->ir & GPT_IR_OF2IE) && (timeout == s->ocr2)) { 1805ec694b5SJean-Christophe DUBOIS s->next_int |= GPT_SR_OF2; 1815ec694b5SJean-Christophe DUBOIS } 1825ec694b5SJean-Christophe DUBOIS if ((s->ir & GPT_IR_OF3IE) && (timeout == s->ocr3)) { 1835ec694b5SJean-Christophe DUBOIS s->next_int |= GPT_SR_OF3; 1845ec694b5SJean-Christophe DUBOIS } 185203d65a4SMichael Tokarev if ((s->ir & GPT_IR_ROVIE) && (timeout == GPT_TIMER_MAX)) { 1865ec694b5SJean-Christophe DUBOIS s->next_int |= GPT_SR_ROV; 1875ec694b5SJean-Christophe DUBOIS } 1885ec694b5SJean-Christophe DUBOIS 1895ec694b5SJean-Christophe DUBOIS /* the new range to count down from */ 19067110c3eSJean-Christophe DUBOIS limit = timeout - imx_gpt_update_count(s); 1915ec694b5SJean-Christophe DUBOIS 1925ec694b5SJean-Christophe DUBOIS if (limit < 0) { 1935ec694b5SJean-Christophe DUBOIS /* 1945ec694b5SJean-Christophe DUBOIS * if we reach here, then QEMU is running too slow and we pass the 1955ec694b5SJean-Christophe DUBOIS * timeout limit while computing it. Let's deliver the interrupt 1965ec694b5SJean-Christophe DUBOIS * and compute a new limit. 1975ec694b5SJean-Christophe DUBOIS */ 1985ec694b5SJean-Christophe DUBOIS s->sr |= s->next_int; 1995ec694b5SJean-Christophe DUBOIS 20067110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, event); 2015ec694b5SJean-Christophe DUBOIS 20267110c3eSJean-Christophe DUBOIS imx_gpt_update_int(s); 2035ec694b5SJean-Christophe DUBOIS } else { 2045ec694b5SJean-Christophe DUBOIS /* New timeout value */ 2055ec694b5SJean-Christophe DUBOIS s->next_timeout = timeout; 2065ec694b5SJean-Christophe DUBOIS 2075ec694b5SJean-Christophe DUBOIS /* reset the limit to the computed range */ 2085ec694b5SJean-Christophe DUBOIS ptimer_set_limit(s->timer, limit, 1); 2095ec694b5SJean-Christophe DUBOIS } 210a50c0d6fSJean-Christophe DUBOIS } 211a50c0d6fSJean-Christophe DUBOIS 21267110c3eSJean-Christophe DUBOIS static uint64_t imx_gpt_read(void *opaque, hwaddr offset, unsigned size) 213a50c0d6fSJean-Christophe DUBOIS { 21467110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(opaque); 2155ec694b5SJean-Christophe DUBOIS uint32_t reg_value = 0; 216a50c0d6fSJean-Christophe DUBOIS 21705453526SJean-Christophe Dubois switch (offset >> 2) { 218a50c0d6fSJean-Christophe DUBOIS case 0: /* Control Register */ 2195ec694b5SJean-Christophe DUBOIS reg_value = s->cr; 2205ec694b5SJean-Christophe DUBOIS break; 221a50c0d6fSJean-Christophe DUBOIS 222a50c0d6fSJean-Christophe DUBOIS case 1: /* prescaler */ 2235ec694b5SJean-Christophe DUBOIS reg_value = s->pr; 2245ec694b5SJean-Christophe DUBOIS break; 225a50c0d6fSJean-Christophe DUBOIS 226a50c0d6fSJean-Christophe DUBOIS case 2: /* Status Register */ 2275ec694b5SJean-Christophe DUBOIS reg_value = s->sr; 2285ec694b5SJean-Christophe DUBOIS break; 229a50c0d6fSJean-Christophe DUBOIS 230a50c0d6fSJean-Christophe DUBOIS case 3: /* Interrupt Register */ 2315ec694b5SJean-Christophe DUBOIS reg_value = s->ir; 2325ec694b5SJean-Christophe DUBOIS break; 233a50c0d6fSJean-Christophe DUBOIS 234a50c0d6fSJean-Christophe DUBOIS case 4: /* Output Compare Register 1 */ 2355ec694b5SJean-Christophe DUBOIS reg_value = s->ocr1; 2365ec694b5SJean-Christophe DUBOIS break; 237a50c0d6fSJean-Christophe DUBOIS 238a50c0d6fSJean-Christophe DUBOIS case 5: /* Output Compare Register 2 */ 2395ec694b5SJean-Christophe DUBOIS reg_value = s->ocr2; 2405ec694b5SJean-Christophe DUBOIS break; 241a50c0d6fSJean-Christophe DUBOIS 242a50c0d6fSJean-Christophe DUBOIS case 6: /* Output Compare Register 3 */ 2435ec694b5SJean-Christophe DUBOIS reg_value = s->ocr3; 2445ec694b5SJean-Christophe DUBOIS break; 245a50c0d6fSJean-Christophe DUBOIS 246a50c0d6fSJean-Christophe DUBOIS case 7: /* input Capture Register 1 */ 24705453526SJean-Christophe Dubois qemu_log_mask(LOG_UNIMP, "[%s]%s: icr1 feature is not implemented\n", 24805453526SJean-Christophe Dubois TYPE_IMX_GPT, __func__); 2495ec694b5SJean-Christophe DUBOIS reg_value = s->icr1; 2505ec694b5SJean-Christophe DUBOIS break; 251a50c0d6fSJean-Christophe DUBOIS 252a50c0d6fSJean-Christophe DUBOIS case 8: /* input Capture Register 2 */ 25305453526SJean-Christophe Dubois qemu_log_mask(LOG_UNIMP, "[%s]%s: icr2 feature is not implemented\n", 25405453526SJean-Christophe Dubois TYPE_IMX_GPT, __func__); 2555ec694b5SJean-Christophe DUBOIS reg_value = s->icr2; 2565ec694b5SJean-Christophe DUBOIS break; 257a50c0d6fSJean-Christophe DUBOIS 258a50c0d6fSJean-Christophe DUBOIS case 9: /* cnt */ 25967110c3eSJean-Christophe DUBOIS imx_gpt_update_count(s); 2605ec694b5SJean-Christophe DUBOIS reg_value = s->cnt; 2615ec694b5SJean-Christophe DUBOIS break; 2625ec694b5SJean-Christophe DUBOIS 2635ec694b5SJean-Christophe DUBOIS default: 26405453526SJean-Christophe Dubois qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%" 26505453526SJean-Christophe Dubois HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset); 2665ec694b5SJean-Christophe DUBOIS break; 267a50c0d6fSJean-Christophe DUBOIS } 268a50c0d6fSJean-Christophe DUBOIS 26905453526SJean-Christophe Dubois DPRINTF("(%s) = 0x%08x\n", imx_gpt_reg_name(offset >> 2), reg_value); 270a50c0d6fSJean-Christophe DUBOIS 2715ec694b5SJean-Christophe DUBOIS return reg_value; 272a50c0d6fSJean-Christophe DUBOIS } 273a50c0d6fSJean-Christophe DUBOIS 27467110c3eSJean-Christophe DUBOIS static void imx_gpt_reset(DeviceState *dev) 275a50c0d6fSJean-Christophe DUBOIS { 27667110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(dev); 277a50c0d6fSJean-Christophe DUBOIS 2785ec694b5SJean-Christophe DUBOIS /* stop timer */ 2795ec694b5SJean-Christophe DUBOIS ptimer_stop(s->timer); 2805ec694b5SJean-Christophe DUBOIS 281a50c0d6fSJean-Christophe DUBOIS /* 282a50c0d6fSJean-Christophe DUBOIS * Soft reset doesn't touch some bits; hard reset clears them 283a50c0d6fSJean-Christophe DUBOIS */ 284a50c0d6fSJean-Christophe DUBOIS s->cr &= ~(GPT_CR_EN|GPT_CR_ENMOD|GPT_CR_STOPEN|GPT_CR_DOZEN| 285a50c0d6fSJean-Christophe DUBOIS GPT_CR_WAITEN|GPT_CR_DBGEN); 286a50c0d6fSJean-Christophe DUBOIS s->sr = 0; 287a50c0d6fSJean-Christophe DUBOIS s->pr = 0; 288a50c0d6fSJean-Christophe DUBOIS s->ir = 0; 289a50c0d6fSJean-Christophe DUBOIS s->cnt = 0; 290203d65a4SMichael Tokarev s->ocr1 = GPT_TIMER_MAX; 291203d65a4SMichael Tokarev s->ocr2 = GPT_TIMER_MAX; 292203d65a4SMichael Tokarev s->ocr3 = GPT_TIMER_MAX; 293a50c0d6fSJean-Christophe DUBOIS s->icr1 = 0; 294a50c0d6fSJean-Christophe DUBOIS s->icr2 = 0; 2955ec694b5SJean-Christophe DUBOIS 296203d65a4SMichael Tokarev s->next_timeout = GPT_TIMER_MAX; 2975ec694b5SJean-Christophe DUBOIS s->next_int = 0; 2985ec694b5SJean-Christophe DUBOIS 2995ec694b5SJean-Christophe DUBOIS /* compute new freq */ 30067110c3eSJean-Christophe DUBOIS imx_gpt_set_freq(s); 3015ec694b5SJean-Christophe DUBOIS 302203d65a4SMichael Tokarev /* reset the limit to GPT_TIMER_MAX */ 303203d65a4SMichael Tokarev ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1); 3045ec694b5SJean-Christophe DUBOIS 3055ec694b5SJean-Christophe DUBOIS /* if the timer is still enabled, restart it */ 3065ec694b5SJean-Christophe DUBOIS if (s->freq && (s->cr & GPT_CR_EN)) { 3075ec694b5SJean-Christophe DUBOIS ptimer_run(s->timer, 1); 3085ec694b5SJean-Christophe DUBOIS } 309a50c0d6fSJean-Christophe DUBOIS } 310a50c0d6fSJean-Christophe DUBOIS 31167110c3eSJean-Christophe DUBOIS static void imx_gpt_write(void *opaque, hwaddr offset, uint64_t value, 31267110c3eSJean-Christophe DUBOIS unsigned size) 313a50c0d6fSJean-Christophe DUBOIS { 31467110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(opaque); 3155ec694b5SJean-Christophe DUBOIS uint32_t oldreg; 316a50c0d6fSJean-Christophe DUBOIS 31705453526SJean-Christophe Dubois DPRINTF("(%s, value = 0x%08x)\n", imx_gpt_reg_name(offset >> 2), 3185ec694b5SJean-Christophe DUBOIS (uint32_t)value); 319a50c0d6fSJean-Christophe DUBOIS 32005453526SJean-Christophe Dubois switch (offset >> 2) { 3215ec694b5SJean-Christophe DUBOIS case 0: 3225ec694b5SJean-Christophe DUBOIS oldreg = s->cr; 3235ec694b5SJean-Christophe DUBOIS s->cr = value & ~0x7c14; 3245ec694b5SJean-Christophe DUBOIS if (s->cr & GPT_CR_SWR) { /* force reset */ 3255ec694b5SJean-Christophe DUBOIS /* handle the reset */ 32667110c3eSJean-Christophe DUBOIS imx_gpt_reset(DEVICE(s)); 327a50c0d6fSJean-Christophe DUBOIS } else { 3285ec694b5SJean-Christophe DUBOIS /* set our freq, as the source might have changed */ 32967110c3eSJean-Christophe DUBOIS imx_gpt_set_freq(s); 3305ec694b5SJean-Christophe DUBOIS 3315ec694b5SJean-Christophe DUBOIS if ((oldreg ^ s->cr) & GPT_CR_EN) { 3325ec694b5SJean-Christophe DUBOIS if (s->cr & GPT_CR_EN) { 3335ec694b5SJean-Christophe DUBOIS if (s->cr & GPT_CR_ENMOD) { 334203d65a4SMichael Tokarev s->next_timeout = GPT_TIMER_MAX; 335203d65a4SMichael Tokarev ptimer_set_count(s->timer, GPT_TIMER_MAX); 33667110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 3375ec694b5SJean-Christophe DUBOIS } 3385ec694b5SJean-Christophe DUBOIS ptimer_run(s->timer, 1); 3395ec694b5SJean-Christophe DUBOIS } else { 3405ec694b5SJean-Christophe DUBOIS /* stop timer */ 341a50c0d6fSJean-Christophe DUBOIS ptimer_stop(s->timer); 342a50c0d6fSJean-Christophe DUBOIS } 343a50c0d6fSJean-Christophe DUBOIS } 3445ec694b5SJean-Christophe DUBOIS } 3455ec694b5SJean-Christophe DUBOIS break; 346a50c0d6fSJean-Christophe DUBOIS 347a50c0d6fSJean-Christophe DUBOIS case 1: /* Prescaler */ 348a50c0d6fSJean-Christophe DUBOIS s->pr = value & 0xfff; 34967110c3eSJean-Christophe DUBOIS imx_gpt_set_freq(s); 3505ec694b5SJean-Christophe DUBOIS break; 351a50c0d6fSJean-Christophe DUBOIS 352a50c0d6fSJean-Christophe DUBOIS case 2: /* SR */ 3535ec694b5SJean-Christophe DUBOIS s->sr &= ~(value & 0x3f); 35467110c3eSJean-Christophe DUBOIS imx_gpt_update_int(s); 3555ec694b5SJean-Christophe DUBOIS break; 356a50c0d6fSJean-Christophe DUBOIS 357a50c0d6fSJean-Christophe DUBOIS case 3: /* IR -- interrupt register */ 358a50c0d6fSJean-Christophe DUBOIS s->ir = value & 0x3f; 35967110c3eSJean-Christophe DUBOIS imx_gpt_update_int(s); 3605ec694b5SJean-Christophe DUBOIS 36167110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 3625ec694b5SJean-Christophe DUBOIS 3635ec694b5SJean-Christophe DUBOIS break; 364a50c0d6fSJean-Christophe DUBOIS 365a50c0d6fSJean-Christophe DUBOIS case 4: /* OCR1 -- output compare register */ 3665ec694b5SJean-Christophe DUBOIS s->ocr1 = value; 3675ec694b5SJean-Christophe DUBOIS 368a50c0d6fSJean-Christophe DUBOIS /* In non-freerun mode, reset count when this register is written */ 369a50c0d6fSJean-Christophe DUBOIS if (!(s->cr & GPT_CR_FRR)) { 370203d65a4SMichael Tokarev s->next_timeout = GPT_TIMER_MAX; 371203d65a4SMichael Tokarev ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1); 372a50c0d6fSJean-Christophe DUBOIS } 3735ec694b5SJean-Christophe DUBOIS 3745ec694b5SJean-Christophe DUBOIS /* compute the new timeout */ 37567110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 3765ec694b5SJean-Christophe DUBOIS 3775ec694b5SJean-Christophe DUBOIS break; 378a50c0d6fSJean-Christophe DUBOIS 379a50c0d6fSJean-Christophe DUBOIS case 5: /* OCR2 -- output compare register */ 3805ec694b5SJean-Christophe DUBOIS s->ocr2 = value; 3815ec694b5SJean-Christophe DUBOIS 3825ec694b5SJean-Christophe DUBOIS /* compute the new timeout */ 38367110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 3845ec694b5SJean-Christophe DUBOIS 3855ec694b5SJean-Christophe DUBOIS break; 3865ec694b5SJean-Christophe DUBOIS 387a50c0d6fSJean-Christophe DUBOIS case 6: /* OCR3 -- output compare register */ 3885ec694b5SJean-Christophe DUBOIS s->ocr3 = value; 3895ec694b5SJean-Christophe DUBOIS 3905ec694b5SJean-Christophe DUBOIS /* compute the new timeout */ 39167110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 3925ec694b5SJean-Christophe DUBOIS 3935ec694b5SJean-Christophe DUBOIS break; 3945ec694b5SJean-Christophe DUBOIS 395a50c0d6fSJean-Christophe DUBOIS default: 39605453526SJean-Christophe Dubois qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%" 39705453526SJean-Christophe Dubois HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset); 3985ec694b5SJean-Christophe DUBOIS break; 399a50c0d6fSJean-Christophe DUBOIS } 400a50c0d6fSJean-Christophe DUBOIS } 401a50c0d6fSJean-Christophe DUBOIS 40267110c3eSJean-Christophe DUBOIS static void imx_gpt_timeout(void *opaque) 403a50c0d6fSJean-Christophe DUBOIS { 40467110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(opaque); 405a50c0d6fSJean-Christophe DUBOIS 4065ec694b5SJean-Christophe DUBOIS DPRINTF("\n"); 407a50c0d6fSJean-Christophe DUBOIS 4085ec694b5SJean-Christophe DUBOIS s->sr |= s->next_int; 4095ec694b5SJean-Christophe DUBOIS s->next_int = 0; 410a50c0d6fSJean-Christophe DUBOIS 41167110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, true); 4125ec694b5SJean-Christophe DUBOIS 41367110c3eSJean-Christophe DUBOIS imx_gpt_update_int(s); 4145ec694b5SJean-Christophe DUBOIS 4155ec694b5SJean-Christophe DUBOIS if (s->freq && (s->cr & GPT_CR_EN)) { 4165ec694b5SJean-Christophe DUBOIS ptimer_run(s->timer, 1); 4175ec694b5SJean-Christophe DUBOIS } 418a50c0d6fSJean-Christophe DUBOIS } 419a50c0d6fSJean-Christophe DUBOIS 42067110c3eSJean-Christophe DUBOIS static const MemoryRegionOps imx_gpt_ops = { 42167110c3eSJean-Christophe DUBOIS .read = imx_gpt_read, 42267110c3eSJean-Christophe DUBOIS .write = imx_gpt_write, 423a50c0d6fSJean-Christophe DUBOIS .endianness = DEVICE_NATIVE_ENDIAN, 424a50c0d6fSJean-Christophe DUBOIS }; 425a50c0d6fSJean-Christophe DUBOIS 426a50c0d6fSJean-Christophe DUBOIS 42767110c3eSJean-Christophe DUBOIS static void imx_gpt_realize(DeviceState *dev, Error **errp) 428a50c0d6fSJean-Christophe DUBOIS { 42967110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(dev); 43067110c3eSJean-Christophe DUBOIS SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 431a50c0d6fSJean-Christophe DUBOIS QEMUBH *bh; 432a50c0d6fSJean-Christophe DUBOIS 43367110c3eSJean-Christophe DUBOIS sysbus_init_irq(sbd, &s->irq); 434853dca12SPaolo Bonzini memory_region_init_io(&s->iomem, OBJECT(s), &imx_gpt_ops, s, TYPE_IMX_GPT, 435a50c0d6fSJean-Christophe DUBOIS 0x00001000); 43667110c3eSJean-Christophe DUBOIS sysbus_init_mmio(sbd, &s->iomem); 437a50c0d6fSJean-Christophe DUBOIS 43867110c3eSJean-Christophe DUBOIS bh = qemu_bh_new(imx_gpt_timeout, s); 439a50c0d6fSJean-Christophe DUBOIS s->timer = ptimer_init(bh); 440a50c0d6fSJean-Christophe DUBOIS } 441a50c0d6fSJean-Christophe DUBOIS 44267110c3eSJean-Christophe DUBOIS static void imx_gpt_class_init(ObjectClass *klass, void *data) 443a50c0d6fSJean-Christophe DUBOIS { 444a50c0d6fSJean-Christophe DUBOIS DeviceClass *dc = DEVICE_CLASS(klass); 44567110c3eSJean-Christophe DUBOIS 44667110c3eSJean-Christophe DUBOIS dc->realize = imx_gpt_realize; 44767110c3eSJean-Christophe DUBOIS dc->reset = imx_gpt_reset; 44867110c3eSJean-Christophe DUBOIS dc->vmsd = &vmstate_imx_timer_gpt; 449a50c0d6fSJean-Christophe DUBOIS dc->desc = "i.MX general timer"; 450a50c0d6fSJean-Christophe DUBOIS } 451a50c0d6fSJean-Christophe DUBOIS 45267110c3eSJean-Christophe DUBOIS static const TypeInfo imx_gpt_info = { 4535ec694b5SJean-Christophe DUBOIS .name = TYPE_IMX_GPT, 454a50c0d6fSJean-Christophe DUBOIS .parent = TYPE_SYS_BUS_DEVICE, 45567110c3eSJean-Christophe DUBOIS .instance_size = sizeof(IMXGPTState), 45667110c3eSJean-Christophe DUBOIS .class_init = imx_gpt_class_init, 457a50c0d6fSJean-Christophe DUBOIS }; 458a50c0d6fSJean-Christophe DUBOIS 45967110c3eSJean-Christophe DUBOIS static void imx_gpt_register_types(void) 460a50c0d6fSJean-Christophe DUBOIS { 46167110c3eSJean-Christophe DUBOIS type_register_static(&imx_gpt_info); 462a50c0d6fSJean-Christophe DUBOIS } 463a50c0d6fSJean-Christophe DUBOIS 46467110c3eSJean-Christophe DUBOIS type_init(imx_gpt_register_types) 465