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" 16*64552b6bSMarkus Armbruster #include "hw/irq.h" 17d647b26dSJean-Christophe Dubois #include "hw/timer/imx_gpt.h" 186a1751b7SAlex Bligh #include "qemu/main-loop.h" 190b8fa32fSMarkus Armbruster #include "qemu/module.h" 2003dd024fSPaolo Bonzini #include "qemu/log.h" 21a50c0d6fSJean-Christophe DUBOIS 2205453526SJean-Christophe Dubois #ifndef DEBUG_IMX_GPT 2305453526SJean-Christophe Dubois #define DEBUG_IMX_GPT 0 2405453526SJean-Christophe Dubois #endif 2505453526SJean-Christophe Dubois 2605453526SJean-Christophe Dubois #define DPRINTF(fmt, args...) \ 2705453526SJean-Christophe Dubois do { \ 2805453526SJean-Christophe Dubois if (DEBUG_IMX_GPT) { \ 2905453526SJean-Christophe Dubois fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_GPT, \ 3005453526SJean-Christophe Dubois __func__, ##args); \ 3105453526SJean-Christophe Dubois } \ 3205453526SJean-Christophe Dubois } while (0) 335ec694b5SJean-Christophe DUBOIS 34d675765aSPeter Maydell static const char *imx_gpt_reg_name(uint32_t reg) 355ec694b5SJean-Christophe DUBOIS { 365ec694b5SJean-Christophe DUBOIS switch (reg) { 375ec694b5SJean-Christophe DUBOIS case 0: 385ec694b5SJean-Christophe DUBOIS return "CR"; 395ec694b5SJean-Christophe DUBOIS case 1: 405ec694b5SJean-Christophe DUBOIS return "PR"; 415ec694b5SJean-Christophe DUBOIS case 2: 425ec694b5SJean-Christophe DUBOIS return "SR"; 435ec694b5SJean-Christophe DUBOIS case 3: 445ec694b5SJean-Christophe DUBOIS return "IR"; 455ec694b5SJean-Christophe DUBOIS case 4: 465ec694b5SJean-Christophe DUBOIS return "OCR1"; 475ec694b5SJean-Christophe DUBOIS case 5: 485ec694b5SJean-Christophe DUBOIS return "OCR2"; 495ec694b5SJean-Christophe DUBOIS case 6: 505ec694b5SJean-Christophe DUBOIS return "OCR3"; 515ec694b5SJean-Christophe DUBOIS case 7: 525ec694b5SJean-Christophe DUBOIS return "ICR1"; 535ec694b5SJean-Christophe DUBOIS case 8: 545ec694b5SJean-Christophe DUBOIS return "ICR2"; 555ec694b5SJean-Christophe DUBOIS case 9: 565ec694b5SJean-Christophe DUBOIS return "CNT"; 575ec694b5SJean-Christophe DUBOIS default: 585ec694b5SJean-Christophe DUBOIS return "[?]"; 595ec694b5SJean-Christophe DUBOIS } 605ec694b5SJean-Christophe DUBOIS } 615ec694b5SJean-Christophe DUBOIS 6267110c3eSJean-Christophe DUBOIS static const VMStateDescription vmstate_imx_timer_gpt = { 6368b85290SJean-Christophe Dubois .name = TYPE_IMX_GPT, 645ec694b5SJean-Christophe DUBOIS .version_id = 3, 655ec694b5SJean-Christophe DUBOIS .minimum_version_id = 3, 66a50c0d6fSJean-Christophe DUBOIS .fields = (VMStateField[]) { 6767110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(cr, IMXGPTState), 6867110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(pr, IMXGPTState), 6967110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(sr, IMXGPTState), 7067110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(ir, IMXGPTState), 7167110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(ocr1, IMXGPTState), 7267110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(ocr2, IMXGPTState), 7367110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(ocr3, IMXGPTState), 7467110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(icr1, IMXGPTState), 7567110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(icr2, IMXGPTState), 7667110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(cnt, IMXGPTState), 7767110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(next_timeout, IMXGPTState), 7867110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(next_int, IMXGPTState), 7967110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(freq, IMXGPTState), 8067110c3eSJean-Christophe DUBOIS VMSTATE_PTIMER(timer, IMXGPTState), 81a50c0d6fSJean-Christophe DUBOIS VMSTATE_END_OF_LIST() 82a50c0d6fSJean-Christophe DUBOIS } 83a50c0d6fSJean-Christophe DUBOIS }; 84a50c0d6fSJean-Christophe DUBOIS 8566542f63SJean-Christophe Dubois static const IMXClk imx25_gpt_clocks[] = { 8666542f63SJean-Christophe Dubois CLK_NONE, /* 000 No clock source */ 8766542f63SJean-Christophe Dubois CLK_IPG, /* 001 ipg_clk, 532MHz*/ 8866542f63SJean-Christophe Dubois CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */ 8966542f63SJean-Christophe Dubois CLK_NONE, /* 011 not defined */ 9066542f63SJean-Christophe Dubois CLK_32k, /* 100 ipg_clk_32k */ 9166542f63SJean-Christophe Dubois CLK_32k, /* 101 ipg_clk_32k */ 9266542f63SJean-Christophe Dubois CLK_32k, /* 110 ipg_clk_32k */ 9366542f63SJean-Christophe Dubois CLK_32k, /* 111 ipg_clk_32k */ 9466542f63SJean-Christophe Dubois }; 9566542f63SJean-Christophe Dubois 9666542f63SJean-Christophe Dubois static const IMXClk imx31_gpt_clocks[] = { 97c91a5883SJean-Christophe Dubois CLK_NONE, /* 000 No clock source */ 98aaa9ec3bSJean-Christophe Dubois CLK_IPG, /* 001 ipg_clk, 532MHz*/ 99d552f675SJean-Christophe Dubois CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */ 100c91a5883SJean-Christophe Dubois CLK_NONE, /* 011 not defined */ 101a50c0d6fSJean-Christophe DUBOIS CLK_32k, /* 100 ipg_clk_32k */ 102c91a5883SJean-Christophe Dubois CLK_NONE, /* 101 not defined */ 103c91a5883SJean-Christophe Dubois CLK_NONE, /* 110 not defined */ 104c91a5883SJean-Christophe Dubois CLK_NONE, /* 111 not defined */ 105a50c0d6fSJean-Christophe DUBOIS }; 106a50c0d6fSJean-Christophe DUBOIS 10766542f63SJean-Christophe Dubois static const IMXClk imx6_gpt_clocks[] = { 10866542f63SJean-Christophe Dubois CLK_NONE, /* 000 No clock source */ 10966542f63SJean-Christophe Dubois CLK_IPG, /* 001 ipg_clk, 532MHz*/ 11066542f63SJean-Christophe Dubois CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */ 11166542f63SJean-Christophe Dubois CLK_EXT, /* 011 External clock */ 11266542f63SJean-Christophe Dubois CLK_32k, /* 100 ipg_clk_32k */ 11366542f63SJean-Christophe Dubois CLK_HIGH_DIV, /* 101 reference clock / 8 */ 11466542f63SJean-Christophe Dubois CLK_NONE, /* 110 not defined */ 11566542f63SJean-Christophe Dubois CLK_HIGH, /* 111 reference clock */ 11666542f63SJean-Christophe Dubois }; 11766542f63SJean-Christophe Dubois 118a62bf59fSAndrey Smirnov static const IMXClk imx7_gpt_clocks[] = { 119a62bf59fSAndrey Smirnov CLK_NONE, /* 000 No clock source */ 120a62bf59fSAndrey Smirnov CLK_IPG, /* 001 ipg_clk, 532MHz*/ 121a62bf59fSAndrey Smirnov CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */ 122a62bf59fSAndrey Smirnov CLK_EXT, /* 011 External clock */ 123a62bf59fSAndrey Smirnov CLK_32k, /* 100 ipg_clk_32k */ 124a62bf59fSAndrey Smirnov CLK_HIGH, /* 101 reference clock */ 125a62bf59fSAndrey Smirnov CLK_NONE, /* 110 not defined */ 126a62bf59fSAndrey Smirnov CLK_NONE, /* 111 not defined */ 127a62bf59fSAndrey Smirnov }; 128a62bf59fSAndrey Smirnov 12967110c3eSJean-Christophe DUBOIS static void imx_gpt_set_freq(IMXGPTState *s) 130a50c0d6fSJean-Christophe DUBOIS { 1315ec694b5SJean-Christophe DUBOIS uint32_t clksrc = extract32(s->cr, GPT_CR_CLKSRC_SHIFT, 3); 132a50c0d6fSJean-Christophe DUBOIS 133aaa9ec3bSJean-Christophe Dubois s->freq = imx_ccm_get_clock_frequency(s->ccm, 13466542f63SJean-Christophe Dubois s->clocks[clksrc]) / (1 + s->pr); 135a50c0d6fSJean-Christophe DUBOIS 136aaa9ec3bSJean-Christophe Dubois DPRINTF("Setting clksrc %d to frequency %d\n", clksrc, s->freq); 137aaa9ec3bSJean-Christophe Dubois 138aaa9ec3bSJean-Christophe Dubois if (s->freq) { 139aaa9ec3bSJean-Christophe Dubois ptimer_set_freq(s->timer, s->freq); 140a50c0d6fSJean-Christophe DUBOIS } 141a50c0d6fSJean-Christophe DUBOIS } 142a50c0d6fSJean-Christophe DUBOIS 14367110c3eSJean-Christophe DUBOIS static void imx_gpt_update_int(IMXGPTState *s) 144a50c0d6fSJean-Christophe DUBOIS { 1455ec694b5SJean-Christophe DUBOIS if ((s->sr & s->ir) && (s->cr & GPT_CR_EN)) { 1465ec694b5SJean-Christophe DUBOIS qemu_irq_raise(s->irq); 1475ec694b5SJean-Christophe DUBOIS } else { 1485ec694b5SJean-Christophe DUBOIS qemu_irq_lower(s->irq); 1495ec694b5SJean-Christophe DUBOIS } 150a50c0d6fSJean-Christophe DUBOIS } 151a50c0d6fSJean-Christophe DUBOIS 15267110c3eSJean-Christophe DUBOIS static uint32_t imx_gpt_update_count(IMXGPTState *s) 153a50c0d6fSJean-Christophe DUBOIS { 1545ec694b5SJean-Christophe DUBOIS s->cnt = s->next_timeout - (uint32_t)ptimer_get_count(s->timer); 1555ec694b5SJean-Christophe DUBOIS 156a50c0d6fSJean-Christophe DUBOIS return s->cnt; 157a50c0d6fSJean-Christophe DUBOIS } 158a50c0d6fSJean-Christophe DUBOIS 15967110c3eSJean-Christophe DUBOIS static inline uint32_t imx_gpt_find_limit(uint32_t count, uint32_t reg, 1605ec694b5SJean-Christophe DUBOIS uint32_t timeout) 161a50c0d6fSJean-Christophe DUBOIS { 1625ec694b5SJean-Christophe DUBOIS if ((count < reg) && (timeout > reg)) { 1635ec694b5SJean-Christophe DUBOIS timeout = reg; 1645ec694b5SJean-Christophe DUBOIS } 165a50c0d6fSJean-Christophe DUBOIS 1665ec694b5SJean-Christophe DUBOIS return timeout; 1675ec694b5SJean-Christophe DUBOIS } 1685ec694b5SJean-Christophe DUBOIS 16967110c3eSJean-Christophe DUBOIS static void imx_gpt_compute_next_timeout(IMXGPTState *s, bool event) 1705ec694b5SJean-Christophe DUBOIS { 171203d65a4SMichael Tokarev uint32_t timeout = GPT_TIMER_MAX; 1724833e15fSJean-Christophe Dubois uint32_t count; 1735ec694b5SJean-Christophe DUBOIS long long limit; 1745ec694b5SJean-Christophe DUBOIS 1755ec694b5SJean-Christophe DUBOIS if (!(s->cr & GPT_CR_EN)) { 1765ec694b5SJean-Christophe DUBOIS /* if not enabled just return */ 177a50c0d6fSJean-Christophe DUBOIS return; 178a50c0d6fSJean-Christophe DUBOIS } 179a50c0d6fSJean-Christophe DUBOIS 1804833e15fSJean-Christophe Dubois /* update the count */ 1814833e15fSJean-Christophe Dubois count = imx_gpt_update_count(s); 1824833e15fSJean-Christophe Dubois 1835ec694b5SJean-Christophe DUBOIS if (event) { 184a50c0d6fSJean-Christophe DUBOIS /* 1854833e15fSJean-Christophe Dubois * This is an event (the ptimer reached 0 and stopped), and the 1864833e15fSJean-Christophe Dubois * timer counter is now equal to s->next_timeout. 187a50c0d6fSJean-Christophe DUBOIS */ 1884833e15fSJean-Christophe Dubois if (!(s->cr & GPT_CR_FRR) && (count == s->ocr1)) { 1894833e15fSJean-Christophe Dubois /* We are in restart mode and we crossed the compare channel 1 1904833e15fSJean-Christophe Dubois * value. We need to reset the counter to 0. 1914833e15fSJean-Christophe Dubois */ 1924833e15fSJean-Christophe Dubois count = s->cnt = s->next_timeout = 0; 1934833e15fSJean-Christophe Dubois } else if (count == GPT_TIMER_MAX) { 1944833e15fSJean-Christophe Dubois /* We reached GPT_TIMER_MAX so we need to rollover */ 1954833e15fSJean-Christophe Dubois count = s->cnt = s->next_timeout = 0; 196a50c0d6fSJean-Christophe DUBOIS } 1975ec694b5SJean-Christophe DUBOIS } 1985ec694b5SJean-Christophe DUBOIS 1995ec694b5SJean-Christophe DUBOIS /* now, find the next timeout related to count */ 2005ec694b5SJean-Christophe DUBOIS 2015ec694b5SJean-Christophe DUBOIS if (s->ir & GPT_IR_OF1IE) { 20267110c3eSJean-Christophe DUBOIS timeout = imx_gpt_find_limit(count, s->ocr1, timeout); 2035ec694b5SJean-Christophe DUBOIS } 2045ec694b5SJean-Christophe DUBOIS if (s->ir & GPT_IR_OF2IE) { 20567110c3eSJean-Christophe DUBOIS timeout = imx_gpt_find_limit(count, s->ocr2, timeout); 2065ec694b5SJean-Christophe DUBOIS } 2075ec694b5SJean-Christophe DUBOIS if (s->ir & GPT_IR_OF3IE) { 20867110c3eSJean-Christophe DUBOIS timeout = imx_gpt_find_limit(count, s->ocr3, timeout); 2095ec694b5SJean-Christophe DUBOIS } 2105ec694b5SJean-Christophe DUBOIS 2115ec694b5SJean-Christophe DUBOIS /* find the next set of interrupts to raise for next timer event */ 2125ec694b5SJean-Christophe DUBOIS 2135ec694b5SJean-Christophe DUBOIS s->next_int = 0; 2145ec694b5SJean-Christophe DUBOIS if ((s->ir & GPT_IR_OF1IE) && (timeout == s->ocr1)) { 2155ec694b5SJean-Christophe DUBOIS s->next_int |= GPT_SR_OF1; 2165ec694b5SJean-Christophe DUBOIS } 2175ec694b5SJean-Christophe DUBOIS if ((s->ir & GPT_IR_OF2IE) && (timeout == s->ocr2)) { 2185ec694b5SJean-Christophe DUBOIS s->next_int |= GPT_SR_OF2; 2195ec694b5SJean-Christophe DUBOIS } 2205ec694b5SJean-Christophe DUBOIS if ((s->ir & GPT_IR_OF3IE) && (timeout == s->ocr3)) { 2215ec694b5SJean-Christophe DUBOIS s->next_int |= GPT_SR_OF3; 2225ec694b5SJean-Christophe DUBOIS } 223203d65a4SMichael Tokarev if ((s->ir & GPT_IR_ROVIE) && (timeout == GPT_TIMER_MAX)) { 2245ec694b5SJean-Christophe DUBOIS s->next_int |= GPT_SR_ROV; 2255ec694b5SJean-Christophe DUBOIS } 2265ec694b5SJean-Christophe DUBOIS 2275ec694b5SJean-Christophe DUBOIS /* the new range to count down from */ 22867110c3eSJean-Christophe DUBOIS limit = timeout - imx_gpt_update_count(s); 2295ec694b5SJean-Christophe DUBOIS 2305ec694b5SJean-Christophe DUBOIS if (limit < 0) { 2315ec694b5SJean-Christophe DUBOIS /* 2325ec694b5SJean-Christophe DUBOIS * if we reach here, then QEMU is running too slow and we pass the 2335ec694b5SJean-Christophe DUBOIS * timeout limit while computing it. Let's deliver the interrupt 2345ec694b5SJean-Christophe DUBOIS * and compute a new limit. 2355ec694b5SJean-Christophe DUBOIS */ 2365ec694b5SJean-Christophe DUBOIS s->sr |= s->next_int; 2375ec694b5SJean-Christophe DUBOIS 23867110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, event); 2395ec694b5SJean-Christophe DUBOIS 24067110c3eSJean-Christophe DUBOIS imx_gpt_update_int(s); 2415ec694b5SJean-Christophe DUBOIS } else { 2425ec694b5SJean-Christophe DUBOIS /* New timeout value */ 2435ec694b5SJean-Christophe DUBOIS s->next_timeout = timeout; 2445ec694b5SJean-Christophe DUBOIS 2455ec694b5SJean-Christophe DUBOIS /* reset the limit to the computed range */ 2465ec694b5SJean-Christophe DUBOIS ptimer_set_limit(s->timer, limit, 1); 2475ec694b5SJean-Christophe DUBOIS } 248a50c0d6fSJean-Christophe DUBOIS } 249a50c0d6fSJean-Christophe DUBOIS 25067110c3eSJean-Christophe DUBOIS static uint64_t imx_gpt_read(void *opaque, hwaddr offset, unsigned size) 251a50c0d6fSJean-Christophe DUBOIS { 25267110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(opaque); 2535ec694b5SJean-Christophe DUBOIS uint32_t reg_value = 0; 254a50c0d6fSJean-Christophe DUBOIS 25505453526SJean-Christophe Dubois switch (offset >> 2) { 256a50c0d6fSJean-Christophe DUBOIS case 0: /* Control Register */ 2575ec694b5SJean-Christophe DUBOIS reg_value = s->cr; 2585ec694b5SJean-Christophe DUBOIS break; 259a50c0d6fSJean-Christophe DUBOIS 260a50c0d6fSJean-Christophe DUBOIS case 1: /* prescaler */ 2615ec694b5SJean-Christophe DUBOIS reg_value = s->pr; 2625ec694b5SJean-Christophe DUBOIS break; 263a50c0d6fSJean-Christophe DUBOIS 264a50c0d6fSJean-Christophe DUBOIS case 2: /* Status Register */ 2655ec694b5SJean-Christophe DUBOIS reg_value = s->sr; 2665ec694b5SJean-Christophe DUBOIS break; 267a50c0d6fSJean-Christophe DUBOIS 268a50c0d6fSJean-Christophe DUBOIS case 3: /* Interrupt Register */ 2695ec694b5SJean-Christophe DUBOIS reg_value = s->ir; 2705ec694b5SJean-Christophe DUBOIS break; 271a50c0d6fSJean-Christophe DUBOIS 272a50c0d6fSJean-Christophe DUBOIS case 4: /* Output Compare Register 1 */ 2735ec694b5SJean-Christophe DUBOIS reg_value = s->ocr1; 2745ec694b5SJean-Christophe DUBOIS break; 275a50c0d6fSJean-Christophe DUBOIS 276a50c0d6fSJean-Christophe DUBOIS case 5: /* Output Compare Register 2 */ 2775ec694b5SJean-Christophe DUBOIS reg_value = s->ocr2; 2785ec694b5SJean-Christophe DUBOIS break; 279a50c0d6fSJean-Christophe DUBOIS 280a50c0d6fSJean-Christophe DUBOIS case 6: /* Output Compare Register 3 */ 2815ec694b5SJean-Christophe DUBOIS reg_value = s->ocr3; 2825ec694b5SJean-Christophe DUBOIS break; 283a50c0d6fSJean-Christophe DUBOIS 284a50c0d6fSJean-Christophe DUBOIS case 7: /* input Capture Register 1 */ 28505453526SJean-Christophe Dubois qemu_log_mask(LOG_UNIMP, "[%s]%s: icr1 feature is not implemented\n", 28605453526SJean-Christophe Dubois TYPE_IMX_GPT, __func__); 2875ec694b5SJean-Christophe DUBOIS reg_value = s->icr1; 2885ec694b5SJean-Christophe DUBOIS break; 289a50c0d6fSJean-Christophe DUBOIS 290a50c0d6fSJean-Christophe DUBOIS case 8: /* input Capture Register 2 */ 29105453526SJean-Christophe Dubois qemu_log_mask(LOG_UNIMP, "[%s]%s: icr2 feature is not implemented\n", 29205453526SJean-Christophe Dubois TYPE_IMX_GPT, __func__); 2935ec694b5SJean-Christophe DUBOIS reg_value = s->icr2; 2945ec694b5SJean-Christophe DUBOIS break; 295a50c0d6fSJean-Christophe DUBOIS 296a50c0d6fSJean-Christophe DUBOIS case 9: /* cnt */ 29767110c3eSJean-Christophe DUBOIS imx_gpt_update_count(s); 2985ec694b5SJean-Christophe DUBOIS reg_value = s->cnt; 2995ec694b5SJean-Christophe DUBOIS break; 3005ec694b5SJean-Christophe DUBOIS 3015ec694b5SJean-Christophe DUBOIS default: 30205453526SJean-Christophe Dubois qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%" 30305453526SJean-Christophe Dubois HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset); 3045ec694b5SJean-Christophe DUBOIS break; 305a50c0d6fSJean-Christophe DUBOIS } 306a50c0d6fSJean-Christophe DUBOIS 30705453526SJean-Christophe Dubois DPRINTF("(%s) = 0x%08x\n", imx_gpt_reg_name(offset >> 2), reg_value); 308a50c0d6fSJean-Christophe DUBOIS 3095ec694b5SJean-Christophe DUBOIS return reg_value; 310a50c0d6fSJean-Christophe DUBOIS } 311a50c0d6fSJean-Christophe DUBOIS 312a50c0d6fSJean-Christophe DUBOIS 313c98c9ebaSKurban Mallachiev static void imx_gpt_reset_common(IMXGPTState *s, bool is_soft_reset) 314c98c9ebaSKurban Mallachiev { 3155ec694b5SJean-Christophe DUBOIS /* stop timer */ 3165ec694b5SJean-Christophe DUBOIS ptimer_stop(s->timer); 3175ec694b5SJean-Christophe DUBOIS 318c98c9ebaSKurban Mallachiev /* Soft reset and hard reset differ only in their handling of the CR 319c98c9ebaSKurban Mallachiev * register -- soft reset preserves the values of some bits there. 320a50c0d6fSJean-Christophe DUBOIS */ 321c98c9ebaSKurban Mallachiev if (is_soft_reset) { 322c98c9ebaSKurban Mallachiev /* Clear all CR bits except those that are preserved by soft reset. */ 323c98c9ebaSKurban Mallachiev s->cr &= GPT_CR_EN | GPT_CR_ENMOD | GPT_CR_STOPEN | GPT_CR_DOZEN | 324c98c9ebaSKurban Mallachiev GPT_CR_WAITEN | GPT_CR_DBGEN | 325c98c9ebaSKurban Mallachiev (GPT_CR_CLKSRC_MASK << GPT_CR_CLKSRC_SHIFT); 326c98c9ebaSKurban Mallachiev } else { 327c98c9ebaSKurban Mallachiev s->cr = 0; 328c98c9ebaSKurban Mallachiev } 329a50c0d6fSJean-Christophe DUBOIS s->sr = 0; 330a50c0d6fSJean-Christophe DUBOIS s->pr = 0; 331a50c0d6fSJean-Christophe DUBOIS s->ir = 0; 332a50c0d6fSJean-Christophe DUBOIS s->cnt = 0; 333203d65a4SMichael Tokarev s->ocr1 = GPT_TIMER_MAX; 334203d65a4SMichael Tokarev s->ocr2 = GPT_TIMER_MAX; 335203d65a4SMichael Tokarev s->ocr3 = GPT_TIMER_MAX; 336a50c0d6fSJean-Christophe DUBOIS s->icr1 = 0; 337a50c0d6fSJean-Christophe DUBOIS s->icr2 = 0; 3385ec694b5SJean-Christophe DUBOIS 339203d65a4SMichael Tokarev s->next_timeout = GPT_TIMER_MAX; 3405ec694b5SJean-Christophe DUBOIS s->next_int = 0; 3415ec694b5SJean-Christophe DUBOIS 3425ec694b5SJean-Christophe DUBOIS /* compute new freq */ 34367110c3eSJean-Christophe DUBOIS imx_gpt_set_freq(s); 3445ec694b5SJean-Christophe DUBOIS 345203d65a4SMichael Tokarev /* reset the limit to GPT_TIMER_MAX */ 346203d65a4SMichael Tokarev ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1); 3475ec694b5SJean-Christophe DUBOIS 3485ec694b5SJean-Christophe DUBOIS /* if the timer is still enabled, restart it */ 3495ec694b5SJean-Christophe DUBOIS if (s->freq && (s->cr & GPT_CR_EN)) { 3505ec694b5SJean-Christophe DUBOIS ptimer_run(s->timer, 1); 3515ec694b5SJean-Christophe DUBOIS } 352a50c0d6fSJean-Christophe DUBOIS } 353a50c0d6fSJean-Christophe DUBOIS 354c98c9ebaSKurban Mallachiev static void imx_gpt_soft_reset(DeviceState *dev) 355c98c9ebaSKurban Mallachiev { 356c98c9ebaSKurban Mallachiev IMXGPTState *s = IMX_GPT(dev); 357c98c9ebaSKurban Mallachiev imx_gpt_reset_common(s, true); 358c98c9ebaSKurban Mallachiev } 359c98c9ebaSKurban Mallachiev 360c98c9ebaSKurban Mallachiev static void imx_gpt_reset(DeviceState *dev) 361c98c9ebaSKurban Mallachiev { 362c98c9ebaSKurban Mallachiev IMXGPTState *s = IMX_GPT(dev); 363c98c9ebaSKurban Mallachiev imx_gpt_reset_common(s, false); 364c98c9ebaSKurban Mallachiev } 365c98c9ebaSKurban Mallachiev 36667110c3eSJean-Christophe DUBOIS static void imx_gpt_write(void *opaque, hwaddr offset, uint64_t value, 36767110c3eSJean-Christophe DUBOIS unsigned size) 368a50c0d6fSJean-Christophe DUBOIS { 36967110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(opaque); 3705ec694b5SJean-Christophe DUBOIS uint32_t oldreg; 371a50c0d6fSJean-Christophe DUBOIS 37205453526SJean-Christophe Dubois DPRINTF("(%s, value = 0x%08x)\n", imx_gpt_reg_name(offset >> 2), 3735ec694b5SJean-Christophe DUBOIS (uint32_t)value); 374a50c0d6fSJean-Christophe DUBOIS 37505453526SJean-Christophe Dubois switch (offset >> 2) { 3765ec694b5SJean-Christophe DUBOIS case 0: 3775ec694b5SJean-Christophe DUBOIS oldreg = s->cr; 3785ec694b5SJean-Christophe DUBOIS s->cr = value & ~0x7c14; 3795ec694b5SJean-Christophe DUBOIS if (s->cr & GPT_CR_SWR) { /* force reset */ 3805ec694b5SJean-Christophe DUBOIS /* handle the reset */ 381c98c9ebaSKurban Mallachiev imx_gpt_soft_reset(DEVICE(s)); 382a50c0d6fSJean-Christophe DUBOIS } else { 3835ec694b5SJean-Christophe DUBOIS /* set our freq, as the source might have changed */ 38467110c3eSJean-Christophe DUBOIS imx_gpt_set_freq(s); 3855ec694b5SJean-Christophe DUBOIS 3865ec694b5SJean-Christophe DUBOIS if ((oldreg ^ s->cr) & GPT_CR_EN) { 3875ec694b5SJean-Christophe DUBOIS if (s->cr & GPT_CR_EN) { 3885ec694b5SJean-Christophe DUBOIS if (s->cr & GPT_CR_ENMOD) { 389203d65a4SMichael Tokarev s->next_timeout = GPT_TIMER_MAX; 390203d65a4SMichael Tokarev ptimer_set_count(s->timer, GPT_TIMER_MAX); 39167110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 3925ec694b5SJean-Christophe DUBOIS } 3935ec694b5SJean-Christophe DUBOIS ptimer_run(s->timer, 1); 3945ec694b5SJean-Christophe DUBOIS } else { 3955ec694b5SJean-Christophe DUBOIS /* stop timer */ 396a50c0d6fSJean-Christophe DUBOIS ptimer_stop(s->timer); 397a50c0d6fSJean-Christophe DUBOIS } 398a50c0d6fSJean-Christophe DUBOIS } 3995ec694b5SJean-Christophe DUBOIS } 4005ec694b5SJean-Christophe DUBOIS break; 401a50c0d6fSJean-Christophe DUBOIS 402a50c0d6fSJean-Christophe DUBOIS case 1: /* Prescaler */ 403a50c0d6fSJean-Christophe DUBOIS s->pr = value & 0xfff; 40467110c3eSJean-Christophe DUBOIS imx_gpt_set_freq(s); 4055ec694b5SJean-Christophe DUBOIS break; 406a50c0d6fSJean-Christophe DUBOIS 407a50c0d6fSJean-Christophe DUBOIS case 2: /* SR */ 4085ec694b5SJean-Christophe DUBOIS s->sr &= ~(value & 0x3f); 40967110c3eSJean-Christophe DUBOIS imx_gpt_update_int(s); 4105ec694b5SJean-Christophe DUBOIS break; 411a50c0d6fSJean-Christophe DUBOIS 412a50c0d6fSJean-Christophe DUBOIS case 3: /* IR -- interrupt register */ 413a50c0d6fSJean-Christophe DUBOIS s->ir = value & 0x3f; 41467110c3eSJean-Christophe DUBOIS imx_gpt_update_int(s); 4155ec694b5SJean-Christophe DUBOIS 41667110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 4175ec694b5SJean-Christophe DUBOIS 4185ec694b5SJean-Christophe DUBOIS break; 419a50c0d6fSJean-Christophe DUBOIS 420a50c0d6fSJean-Christophe DUBOIS case 4: /* OCR1 -- output compare register */ 4215ec694b5SJean-Christophe DUBOIS s->ocr1 = value; 4225ec694b5SJean-Christophe DUBOIS 423a50c0d6fSJean-Christophe DUBOIS /* In non-freerun mode, reset count when this register is written */ 424a50c0d6fSJean-Christophe DUBOIS if (!(s->cr & GPT_CR_FRR)) { 425203d65a4SMichael Tokarev s->next_timeout = GPT_TIMER_MAX; 426203d65a4SMichael Tokarev ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1); 427a50c0d6fSJean-Christophe DUBOIS } 4285ec694b5SJean-Christophe DUBOIS 4295ec694b5SJean-Christophe DUBOIS /* compute the new timeout */ 43067110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 4315ec694b5SJean-Christophe DUBOIS 4325ec694b5SJean-Christophe DUBOIS break; 433a50c0d6fSJean-Christophe DUBOIS 434a50c0d6fSJean-Christophe DUBOIS case 5: /* OCR2 -- output compare register */ 4355ec694b5SJean-Christophe DUBOIS s->ocr2 = value; 4365ec694b5SJean-Christophe DUBOIS 4375ec694b5SJean-Christophe DUBOIS /* compute the new timeout */ 43867110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 4395ec694b5SJean-Christophe DUBOIS 4405ec694b5SJean-Christophe DUBOIS break; 4415ec694b5SJean-Christophe DUBOIS 442a50c0d6fSJean-Christophe DUBOIS case 6: /* OCR3 -- output compare register */ 4435ec694b5SJean-Christophe DUBOIS s->ocr3 = value; 4445ec694b5SJean-Christophe DUBOIS 4455ec694b5SJean-Christophe DUBOIS /* compute the new timeout */ 44667110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 4475ec694b5SJean-Christophe DUBOIS 4485ec694b5SJean-Christophe DUBOIS break; 4495ec694b5SJean-Christophe DUBOIS 450a50c0d6fSJean-Christophe DUBOIS default: 45105453526SJean-Christophe Dubois qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%" 45205453526SJean-Christophe Dubois HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset); 4535ec694b5SJean-Christophe DUBOIS break; 454a50c0d6fSJean-Christophe DUBOIS } 455a50c0d6fSJean-Christophe DUBOIS } 456a50c0d6fSJean-Christophe DUBOIS 45767110c3eSJean-Christophe DUBOIS static void imx_gpt_timeout(void *opaque) 458a50c0d6fSJean-Christophe DUBOIS { 45967110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(opaque); 460a50c0d6fSJean-Christophe DUBOIS 4615ec694b5SJean-Christophe DUBOIS DPRINTF("\n"); 462a50c0d6fSJean-Christophe DUBOIS 4635ec694b5SJean-Christophe DUBOIS s->sr |= s->next_int; 4645ec694b5SJean-Christophe DUBOIS s->next_int = 0; 465a50c0d6fSJean-Christophe DUBOIS 46667110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, true); 4675ec694b5SJean-Christophe DUBOIS 46867110c3eSJean-Christophe DUBOIS imx_gpt_update_int(s); 4695ec694b5SJean-Christophe DUBOIS 4705ec694b5SJean-Christophe DUBOIS if (s->freq && (s->cr & GPT_CR_EN)) { 4715ec694b5SJean-Christophe DUBOIS ptimer_run(s->timer, 1); 4725ec694b5SJean-Christophe DUBOIS } 473a50c0d6fSJean-Christophe DUBOIS } 474a50c0d6fSJean-Christophe DUBOIS 47567110c3eSJean-Christophe DUBOIS static const MemoryRegionOps imx_gpt_ops = { 47667110c3eSJean-Christophe DUBOIS .read = imx_gpt_read, 47767110c3eSJean-Christophe DUBOIS .write = imx_gpt_write, 478a50c0d6fSJean-Christophe DUBOIS .endianness = DEVICE_NATIVE_ENDIAN, 479a50c0d6fSJean-Christophe DUBOIS }; 480a50c0d6fSJean-Christophe DUBOIS 481a50c0d6fSJean-Christophe DUBOIS 48267110c3eSJean-Christophe DUBOIS static void imx_gpt_realize(DeviceState *dev, Error **errp) 483a50c0d6fSJean-Christophe DUBOIS { 48467110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(dev); 48567110c3eSJean-Christophe DUBOIS SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 486a50c0d6fSJean-Christophe DUBOIS QEMUBH *bh; 487a50c0d6fSJean-Christophe DUBOIS 48867110c3eSJean-Christophe DUBOIS sysbus_init_irq(sbd, &s->irq); 489853dca12SPaolo Bonzini memory_region_init_io(&s->iomem, OBJECT(s), &imx_gpt_ops, s, TYPE_IMX_GPT, 490a50c0d6fSJean-Christophe DUBOIS 0x00001000); 49167110c3eSJean-Christophe DUBOIS sysbus_init_mmio(sbd, &s->iomem); 492a50c0d6fSJean-Christophe DUBOIS 49367110c3eSJean-Christophe DUBOIS bh = qemu_bh_new(imx_gpt_timeout, s); 494e7ea81c3SDmitry Osipenko s->timer = ptimer_init(bh, PTIMER_POLICY_DEFAULT); 495a50c0d6fSJean-Christophe DUBOIS } 496a50c0d6fSJean-Christophe DUBOIS 49767110c3eSJean-Christophe DUBOIS static void imx_gpt_class_init(ObjectClass *klass, void *data) 498a50c0d6fSJean-Christophe DUBOIS { 499a50c0d6fSJean-Christophe DUBOIS DeviceClass *dc = DEVICE_CLASS(klass); 50067110c3eSJean-Christophe DUBOIS 50167110c3eSJean-Christophe DUBOIS dc->realize = imx_gpt_realize; 50267110c3eSJean-Christophe DUBOIS dc->reset = imx_gpt_reset; 50367110c3eSJean-Christophe DUBOIS dc->vmsd = &vmstate_imx_timer_gpt; 504a50c0d6fSJean-Christophe DUBOIS dc->desc = "i.MX general timer"; 505a50c0d6fSJean-Christophe DUBOIS } 506a50c0d6fSJean-Christophe DUBOIS 50766542f63SJean-Christophe Dubois static void imx25_gpt_init(Object *obj) 50866542f63SJean-Christophe Dubois { 50966542f63SJean-Christophe Dubois IMXGPTState *s = IMX_GPT(obj); 51066542f63SJean-Christophe Dubois 51166542f63SJean-Christophe Dubois s->clocks = imx25_gpt_clocks; 51266542f63SJean-Christophe Dubois } 51366542f63SJean-Christophe Dubois 51466542f63SJean-Christophe Dubois static void imx31_gpt_init(Object *obj) 51566542f63SJean-Christophe Dubois { 51666542f63SJean-Christophe Dubois IMXGPTState *s = IMX_GPT(obj); 51766542f63SJean-Christophe Dubois 51866542f63SJean-Christophe Dubois s->clocks = imx31_gpt_clocks; 51966542f63SJean-Christophe Dubois } 52066542f63SJean-Christophe Dubois 52166542f63SJean-Christophe Dubois static void imx6_gpt_init(Object *obj) 52266542f63SJean-Christophe Dubois { 52366542f63SJean-Christophe Dubois IMXGPTState *s = IMX_GPT(obj); 52466542f63SJean-Christophe Dubois 52566542f63SJean-Christophe Dubois s->clocks = imx6_gpt_clocks; 52666542f63SJean-Christophe Dubois } 52766542f63SJean-Christophe Dubois 528a62bf59fSAndrey Smirnov static void imx7_gpt_init(Object *obj) 529a62bf59fSAndrey Smirnov { 530a62bf59fSAndrey Smirnov IMXGPTState *s = IMX_GPT(obj); 531a62bf59fSAndrey Smirnov 532a62bf59fSAndrey Smirnov s->clocks = imx7_gpt_clocks; 533a62bf59fSAndrey Smirnov } 534a62bf59fSAndrey Smirnov 53566542f63SJean-Christophe Dubois static const TypeInfo imx25_gpt_info = { 53666542f63SJean-Christophe Dubois .name = TYPE_IMX25_GPT, 537a50c0d6fSJean-Christophe DUBOIS .parent = TYPE_SYS_BUS_DEVICE, 53867110c3eSJean-Christophe DUBOIS .instance_size = sizeof(IMXGPTState), 53966542f63SJean-Christophe Dubois .instance_init = imx25_gpt_init, 54067110c3eSJean-Christophe DUBOIS .class_init = imx_gpt_class_init, 541a50c0d6fSJean-Christophe DUBOIS }; 542a50c0d6fSJean-Christophe DUBOIS 54366542f63SJean-Christophe Dubois static const TypeInfo imx31_gpt_info = { 54466542f63SJean-Christophe Dubois .name = TYPE_IMX31_GPT, 54566542f63SJean-Christophe Dubois .parent = TYPE_IMX25_GPT, 54666542f63SJean-Christophe Dubois .instance_init = imx31_gpt_init, 54766542f63SJean-Christophe Dubois }; 54866542f63SJean-Christophe Dubois 54966542f63SJean-Christophe Dubois static const TypeInfo imx6_gpt_info = { 55066542f63SJean-Christophe Dubois .name = TYPE_IMX6_GPT, 55166542f63SJean-Christophe Dubois .parent = TYPE_IMX25_GPT, 55266542f63SJean-Christophe Dubois .instance_init = imx6_gpt_init, 55366542f63SJean-Christophe Dubois }; 55466542f63SJean-Christophe Dubois 555a62bf59fSAndrey Smirnov static const TypeInfo imx7_gpt_info = { 556a62bf59fSAndrey Smirnov .name = TYPE_IMX7_GPT, 557a62bf59fSAndrey Smirnov .parent = TYPE_IMX25_GPT, 558a62bf59fSAndrey Smirnov .instance_init = imx7_gpt_init, 559a62bf59fSAndrey Smirnov }; 560a62bf59fSAndrey Smirnov 56167110c3eSJean-Christophe DUBOIS static void imx_gpt_register_types(void) 562a50c0d6fSJean-Christophe DUBOIS { 56366542f63SJean-Christophe Dubois type_register_static(&imx25_gpt_info); 56466542f63SJean-Christophe Dubois type_register_static(&imx31_gpt_info); 56566542f63SJean-Christophe Dubois type_register_static(&imx6_gpt_info); 566a62bf59fSAndrey Smirnov type_register_static(&imx7_gpt_info); 567a50c0d6fSJean-Christophe DUBOIS } 568a50c0d6fSJean-Christophe DUBOIS 56967110c3eSJean-Christophe DUBOIS type_init(imx_gpt_register_types) 570