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" 1664552b6bSMarkus Armbruster #include "hw/irq.h" 17d647b26dSJean-Christophe Dubois #include "hw/timer/imx_gpt.h" 18d6454270SMarkus Armbruster #include "migration/vmstate.h" 190b8fa32fSMarkus Armbruster #include "qemu/module.h" 2003dd024fSPaolo Bonzini #include "qemu/log.h" 21*afd431e4SBernhard Beschow #include "trace.h" 22a50c0d6fSJean-Christophe DUBOIS 2305453526SJean-Christophe Dubois #ifndef DEBUG_IMX_GPT 2405453526SJean-Christophe Dubois #define DEBUG_IMX_GPT 0 2505453526SJean-Christophe Dubois #endif 2605453526SJean-Christophe Dubois 27d675765aSPeter Maydell static const char *imx_gpt_reg_name(uint32_t reg) 285ec694b5SJean-Christophe DUBOIS { 295ec694b5SJean-Christophe DUBOIS switch (reg) { 305ec694b5SJean-Christophe DUBOIS case 0: 315ec694b5SJean-Christophe DUBOIS return "CR"; 325ec694b5SJean-Christophe DUBOIS case 1: 335ec694b5SJean-Christophe DUBOIS return "PR"; 345ec694b5SJean-Christophe DUBOIS case 2: 355ec694b5SJean-Christophe DUBOIS return "SR"; 365ec694b5SJean-Christophe DUBOIS case 3: 375ec694b5SJean-Christophe DUBOIS return "IR"; 385ec694b5SJean-Christophe DUBOIS case 4: 395ec694b5SJean-Christophe DUBOIS return "OCR1"; 405ec694b5SJean-Christophe DUBOIS case 5: 415ec694b5SJean-Christophe DUBOIS return "OCR2"; 425ec694b5SJean-Christophe DUBOIS case 6: 435ec694b5SJean-Christophe DUBOIS return "OCR3"; 445ec694b5SJean-Christophe DUBOIS case 7: 455ec694b5SJean-Christophe DUBOIS return "ICR1"; 465ec694b5SJean-Christophe DUBOIS case 8: 475ec694b5SJean-Christophe DUBOIS return "ICR2"; 485ec694b5SJean-Christophe DUBOIS case 9: 495ec694b5SJean-Christophe DUBOIS return "CNT"; 505ec694b5SJean-Christophe DUBOIS default: 515ec694b5SJean-Christophe DUBOIS return "[?]"; 525ec694b5SJean-Christophe DUBOIS } 535ec694b5SJean-Christophe DUBOIS } 545ec694b5SJean-Christophe DUBOIS 5567110c3eSJean-Christophe DUBOIS static const VMStateDescription vmstate_imx_timer_gpt = { 5668b85290SJean-Christophe Dubois .name = TYPE_IMX_GPT, 575ec694b5SJean-Christophe DUBOIS .version_id = 3, 585ec694b5SJean-Christophe DUBOIS .minimum_version_id = 3, 59ba324b3fSRichard Henderson .fields = (const VMStateField[]) { 6067110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(cr, IMXGPTState), 6167110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(pr, IMXGPTState), 6267110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(sr, IMXGPTState), 6367110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(ir, IMXGPTState), 6467110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(ocr1, IMXGPTState), 6567110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(ocr2, IMXGPTState), 6667110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(ocr3, IMXGPTState), 6767110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(icr1, IMXGPTState), 6867110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(icr2, IMXGPTState), 6967110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(cnt, IMXGPTState), 7067110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(next_timeout, IMXGPTState), 7167110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(next_int, IMXGPTState), 7267110c3eSJean-Christophe DUBOIS VMSTATE_UINT32(freq, IMXGPTState), 7367110c3eSJean-Christophe DUBOIS VMSTATE_PTIMER(timer, IMXGPTState), 74a50c0d6fSJean-Christophe DUBOIS VMSTATE_END_OF_LIST() 75a50c0d6fSJean-Christophe DUBOIS } 76a50c0d6fSJean-Christophe DUBOIS }; 77a50c0d6fSJean-Christophe DUBOIS 7866542f63SJean-Christophe Dubois static const IMXClk imx25_gpt_clocks[] = { 7966542f63SJean-Christophe Dubois CLK_NONE, /* 000 No clock source */ 8066542f63SJean-Christophe Dubois CLK_IPG, /* 001 ipg_clk, 532MHz*/ 8166542f63SJean-Christophe Dubois CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */ 8266542f63SJean-Christophe Dubois CLK_NONE, /* 011 not defined */ 8366542f63SJean-Christophe Dubois CLK_32k, /* 100 ipg_clk_32k */ 8466542f63SJean-Christophe Dubois CLK_32k, /* 101 ipg_clk_32k */ 8566542f63SJean-Christophe Dubois CLK_32k, /* 110 ipg_clk_32k */ 8666542f63SJean-Christophe Dubois CLK_32k, /* 111 ipg_clk_32k */ 8766542f63SJean-Christophe Dubois }; 8866542f63SJean-Christophe Dubois 8966542f63SJean-Christophe Dubois static const IMXClk imx31_gpt_clocks[] = { 90c91a5883SJean-Christophe Dubois CLK_NONE, /* 000 No clock source */ 91aaa9ec3bSJean-Christophe Dubois CLK_IPG, /* 001 ipg_clk, 532MHz*/ 92d552f675SJean-Christophe Dubois CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */ 93c91a5883SJean-Christophe Dubois CLK_NONE, /* 011 not defined */ 94a50c0d6fSJean-Christophe DUBOIS CLK_32k, /* 100 ipg_clk_32k */ 95c91a5883SJean-Christophe Dubois CLK_NONE, /* 101 not defined */ 96c91a5883SJean-Christophe Dubois CLK_NONE, /* 110 not defined */ 97c91a5883SJean-Christophe Dubois CLK_NONE, /* 111 not defined */ 98a50c0d6fSJean-Christophe DUBOIS }; 99a50c0d6fSJean-Christophe DUBOIS 10066542f63SJean-Christophe Dubois static const IMXClk imx6_gpt_clocks[] = { 10166542f63SJean-Christophe Dubois CLK_NONE, /* 000 No clock source */ 10266542f63SJean-Christophe Dubois CLK_IPG, /* 001 ipg_clk, 532MHz*/ 10366542f63SJean-Christophe Dubois CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */ 10466542f63SJean-Christophe Dubois CLK_EXT, /* 011 External clock */ 10566542f63SJean-Christophe Dubois CLK_32k, /* 100 ipg_clk_32k */ 10666542f63SJean-Christophe Dubois CLK_HIGH_DIV, /* 101 reference clock / 8 */ 10766542f63SJean-Christophe Dubois CLK_NONE, /* 110 not defined */ 10866542f63SJean-Christophe Dubois CLK_HIGH, /* 111 reference clock */ 10966542f63SJean-Christophe Dubois }; 11066542f63SJean-Christophe Dubois 111a1e03956SJean-Christophe Dubois static const IMXClk imx6ul_gpt_clocks[] = { 112a1e03956SJean-Christophe Dubois CLK_NONE, /* 000 No clock source */ 113a1e03956SJean-Christophe Dubois CLK_IPG, /* 001 ipg_clk, 532MHz*/ 114a1e03956SJean-Christophe Dubois CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */ 115a1e03956SJean-Christophe Dubois CLK_EXT, /* 011 External clock */ 116a1e03956SJean-Christophe Dubois CLK_32k, /* 100 ipg_clk_32k */ 117a1e03956SJean-Christophe Dubois CLK_NONE, /* 101 not defined */ 118a1e03956SJean-Christophe Dubois CLK_NONE, /* 110 not defined */ 119a1e03956SJean-Christophe Dubois CLK_NONE, /* 111 not defined */ 120a1e03956SJean-Christophe Dubois }; 121a1e03956SJean-Christophe Dubois 122a62bf59fSAndrey Smirnov static const IMXClk imx7_gpt_clocks[] = { 123a62bf59fSAndrey Smirnov CLK_NONE, /* 000 No clock source */ 124a62bf59fSAndrey Smirnov CLK_IPG, /* 001 ipg_clk, 532MHz*/ 125a62bf59fSAndrey Smirnov CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */ 126a62bf59fSAndrey Smirnov CLK_EXT, /* 011 External clock */ 127a62bf59fSAndrey Smirnov CLK_32k, /* 100 ipg_clk_32k */ 128a62bf59fSAndrey Smirnov CLK_HIGH, /* 101 reference clock */ 129a62bf59fSAndrey Smirnov CLK_NONE, /* 110 not defined */ 130a62bf59fSAndrey Smirnov CLK_NONE, /* 111 not defined */ 131a62bf59fSAndrey Smirnov }; 132a62bf59fSAndrey Smirnov 1331b914994SPeter Maydell /* Must be called from within ptimer_transaction_begin/commit block */ 13467110c3eSJean-Christophe DUBOIS static void imx_gpt_set_freq(IMXGPTState *s) 135a50c0d6fSJean-Christophe DUBOIS { 1365ec694b5SJean-Christophe DUBOIS uint32_t clksrc = extract32(s->cr, GPT_CR_CLKSRC_SHIFT, 3); 137a50c0d6fSJean-Christophe DUBOIS 138aaa9ec3bSJean-Christophe Dubois s->freq = imx_ccm_get_clock_frequency(s->ccm, 13966542f63SJean-Christophe Dubois s->clocks[clksrc]) / (1 + s->pr); 140a50c0d6fSJean-Christophe DUBOIS 141*afd431e4SBernhard Beschow trace_imx_gpt_set_freq(clksrc, s->freq); 142aaa9ec3bSJean-Christophe Dubois 143aaa9ec3bSJean-Christophe Dubois if (s->freq) { 144aaa9ec3bSJean-Christophe Dubois ptimer_set_freq(s->timer, s->freq); 145a50c0d6fSJean-Christophe DUBOIS } 146a50c0d6fSJean-Christophe DUBOIS } 147a50c0d6fSJean-Christophe DUBOIS 14867110c3eSJean-Christophe DUBOIS static void imx_gpt_update_int(IMXGPTState *s) 149a50c0d6fSJean-Christophe DUBOIS { 1505ec694b5SJean-Christophe DUBOIS if ((s->sr & s->ir) && (s->cr & GPT_CR_EN)) { 1515ec694b5SJean-Christophe DUBOIS qemu_irq_raise(s->irq); 1525ec694b5SJean-Christophe DUBOIS } else { 1535ec694b5SJean-Christophe DUBOIS qemu_irq_lower(s->irq); 1545ec694b5SJean-Christophe DUBOIS } 155a50c0d6fSJean-Christophe DUBOIS } 156a50c0d6fSJean-Christophe DUBOIS 15767110c3eSJean-Christophe DUBOIS static uint32_t imx_gpt_update_count(IMXGPTState *s) 158a50c0d6fSJean-Christophe DUBOIS { 1595ec694b5SJean-Christophe DUBOIS s->cnt = s->next_timeout - (uint32_t)ptimer_get_count(s->timer); 1605ec694b5SJean-Christophe DUBOIS 161a50c0d6fSJean-Christophe DUBOIS return s->cnt; 162a50c0d6fSJean-Christophe DUBOIS } 163a50c0d6fSJean-Christophe DUBOIS 16467110c3eSJean-Christophe DUBOIS static inline uint32_t imx_gpt_find_limit(uint32_t count, uint32_t reg, 1655ec694b5SJean-Christophe DUBOIS uint32_t timeout) 166a50c0d6fSJean-Christophe DUBOIS { 1675ec694b5SJean-Christophe DUBOIS if ((count < reg) && (timeout > reg)) { 1685ec694b5SJean-Christophe DUBOIS timeout = reg; 1695ec694b5SJean-Christophe DUBOIS } 170a50c0d6fSJean-Christophe DUBOIS 1715ec694b5SJean-Christophe DUBOIS return timeout; 1725ec694b5SJean-Christophe DUBOIS } 1735ec694b5SJean-Christophe DUBOIS 1741b914994SPeter Maydell /* Must be called from within ptimer_transaction_begin/commit block */ 17567110c3eSJean-Christophe DUBOIS static void imx_gpt_compute_next_timeout(IMXGPTState *s, bool event) 1765ec694b5SJean-Christophe DUBOIS { 177203d65a4SMichael Tokarev uint32_t timeout = GPT_TIMER_MAX; 1784833e15fSJean-Christophe Dubois uint32_t count; 1795ec694b5SJean-Christophe DUBOIS long long limit; 1805ec694b5SJean-Christophe DUBOIS 1815ec694b5SJean-Christophe DUBOIS if (!(s->cr & GPT_CR_EN)) { 1825ec694b5SJean-Christophe DUBOIS /* if not enabled just return */ 183a50c0d6fSJean-Christophe DUBOIS return; 184a50c0d6fSJean-Christophe DUBOIS } 185a50c0d6fSJean-Christophe DUBOIS 1864833e15fSJean-Christophe Dubois /* update the count */ 1874833e15fSJean-Christophe Dubois count = imx_gpt_update_count(s); 1884833e15fSJean-Christophe Dubois 1895ec694b5SJean-Christophe DUBOIS if (event) { 190a50c0d6fSJean-Christophe DUBOIS /* 1914833e15fSJean-Christophe Dubois * This is an event (the ptimer reached 0 and stopped), and the 1924833e15fSJean-Christophe Dubois * timer counter is now equal to s->next_timeout. 193a50c0d6fSJean-Christophe DUBOIS */ 1944833e15fSJean-Christophe Dubois if (!(s->cr & GPT_CR_FRR) && (count == s->ocr1)) { 1954833e15fSJean-Christophe Dubois /* We are in restart mode and we crossed the compare channel 1 1964833e15fSJean-Christophe Dubois * value. We need to reset the counter to 0. 1974833e15fSJean-Christophe Dubois */ 1984833e15fSJean-Christophe Dubois count = s->cnt = s->next_timeout = 0; 1994833e15fSJean-Christophe Dubois } else if (count == GPT_TIMER_MAX) { 2004833e15fSJean-Christophe Dubois /* We reached GPT_TIMER_MAX so we need to rollover */ 2014833e15fSJean-Christophe Dubois count = s->cnt = s->next_timeout = 0; 202a50c0d6fSJean-Christophe DUBOIS } 2035ec694b5SJean-Christophe DUBOIS } 2045ec694b5SJean-Christophe DUBOIS 2055ec694b5SJean-Christophe DUBOIS /* now, find the next timeout related to count */ 2065ec694b5SJean-Christophe DUBOIS 2075ec694b5SJean-Christophe DUBOIS if (s->ir & GPT_IR_OF1IE) { 20867110c3eSJean-Christophe DUBOIS timeout = imx_gpt_find_limit(count, s->ocr1, timeout); 2095ec694b5SJean-Christophe DUBOIS } 2105ec694b5SJean-Christophe DUBOIS if (s->ir & GPT_IR_OF2IE) { 21167110c3eSJean-Christophe DUBOIS timeout = imx_gpt_find_limit(count, s->ocr2, timeout); 2125ec694b5SJean-Christophe DUBOIS } 2135ec694b5SJean-Christophe DUBOIS if (s->ir & GPT_IR_OF3IE) { 21467110c3eSJean-Christophe DUBOIS timeout = imx_gpt_find_limit(count, s->ocr3, timeout); 2155ec694b5SJean-Christophe DUBOIS } 2165ec694b5SJean-Christophe DUBOIS 2175ec694b5SJean-Christophe DUBOIS /* find the next set of interrupts to raise for next timer event */ 2185ec694b5SJean-Christophe DUBOIS 2195ec694b5SJean-Christophe DUBOIS s->next_int = 0; 2205ec694b5SJean-Christophe DUBOIS if ((s->ir & GPT_IR_OF1IE) && (timeout == s->ocr1)) { 2215ec694b5SJean-Christophe DUBOIS s->next_int |= GPT_SR_OF1; 2225ec694b5SJean-Christophe DUBOIS } 2235ec694b5SJean-Christophe DUBOIS if ((s->ir & GPT_IR_OF2IE) && (timeout == s->ocr2)) { 2245ec694b5SJean-Christophe DUBOIS s->next_int |= GPT_SR_OF2; 2255ec694b5SJean-Christophe DUBOIS } 2265ec694b5SJean-Christophe DUBOIS if ((s->ir & GPT_IR_OF3IE) && (timeout == s->ocr3)) { 2275ec694b5SJean-Christophe DUBOIS s->next_int |= GPT_SR_OF3; 2285ec694b5SJean-Christophe DUBOIS } 229203d65a4SMichael Tokarev if ((s->ir & GPT_IR_ROVIE) && (timeout == GPT_TIMER_MAX)) { 2305ec694b5SJean-Christophe DUBOIS s->next_int |= GPT_SR_ROV; 2315ec694b5SJean-Christophe DUBOIS } 2325ec694b5SJean-Christophe DUBOIS 2335ec694b5SJean-Christophe DUBOIS /* the new range to count down from */ 23467110c3eSJean-Christophe DUBOIS limit = timeout - imx_gpt_update_count(s); 2355ec694b5SJean-Christophe DUBOIS 2365ec694b5SJean-Christophe DUBOIS if (limit < 0) { 2375ec694b5SJean-Christophe DUBOIS /* 2385ec694b5SJean-Christophe DUBOIS * if we reach here, then QEMU is running too slow and we pass the 2395ec694b5SJean-Christophe DUBOIS * timeout limit while computing it. Let's deliver the interrupt 2405ec694b5SJean-Christophe DUBOIS * and compute a new limit. 2415ec694b5SJean-Christophe DUBOIS */ 2425ec694b5SJean-Christophe DUBOIS s->sr |= s->next_int; 2435ec694b5SJean-Christophe DUBOIS 24467110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, event); 2455ec694b5SJean-Christophe DUBOIS 24667110c3eSJean-Christophe DUBOIS imx_gpt_update_int(s); 2475ec694b5SJean-Christophe DUBOIS } else { 2485ec694b5SJean-Christophe DUBOIS /* New timeout value */ 2495ec694b5SJean-Christophe DUBOIS s->next_timeout = timeout; 2505ec694b5SJean-Christophe DUBOIS 2515ec694b5SJean-Christophe DUBOIS /* reset the limit to the computed range */ 2525ec694b5SJean-Christophe DUBOIS ptimer_set_limit(s->timer, limit, 1); 2535ec694b5SJean-Christophe DUBOIS } 254a50c0d6fSJean-Christophe DUBOIS } 255a50c0d6fSJean-Christophe DUBOIS 25667110c3eSJean-Christophe DUBOIS static uint64_t imx_gpt_read(void *opaque, hwaddr offset, unsigned size) 257a50c0d6fSJean-Christophe DUBOIS { 25867110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(opaque); 2595ec694b5SJean-Christophe DUBOIS uint32_t reg_value = 0; 260a50c0d6fSJean-Christophe DUBOIS 26105453526SJean-Christophe Dubois switch (offset >> 2) { 262a50c0d6fSJean-Christophe DUBOIS case 0: /* Control Register */ 2635ec694b5SJean-Christophe DUBOIS reg_value = s->cr; 2645ec694b5SJean-Christophe DUBOIS break; 265a50c0d6fSJean-Christophe DUBOIS 266a50c0d6fSJean-Christophe DUBOIS case 1: /* prescaler */ 2675ec694b5SJean-Christophe DUBOIS reg_value = s->pr; 2685ec694b5SJean-Christophe DUBOIS break; 269a50c0d6fSJean-Christophe DUBOIS 270a50c0d6fSJean-Christophe DUBOIS case 2: /* Status Register */ 2715ec694b5SJean-Christophe DUBOIS reg_value = s->sr; 2725ec694b5SJean-Christophe DUBOIS break; 273a50c0d6fSJean-Christophe DUBOIS 274a50c0d6fSJean-Christophe DUBOIS case 3: /* Interrupt Register */ 2755ec694b5SJean-Christophe DUBOIS reg_value = s->ir; 2765ec694b5SJean-Christophe DUBOIS break; 277a50c0d6fSJean-Christophe DUBOIS 278a50c0d6fSJean-Christophe DUBOIS case 4: /* Output Compare Register 1 */ 2795ec694b5SJean-Christophe DUBOIS reg_value = s->ocr1; 2805ec694b5SJean-Christophe DUBOIS break; 281a50c0d6fSJean-Christophe DUBOIS 282a50c0d6fSJean-Christophe DUBOIS case 5: /* Output Compare Register 2 */ 2835ec694b5SJean-Christophe DUBOIS reg_value = s->ocr2; 2845ec694b5SJean-Christophe DUBOIS break; 285a50c0d6fSJean-Christophe DUBOIS 286a50c0d6fSJean-Christophe DUBOIS case 6: /* Output Compare Register 3 */ 2875ec694b5SJean-Christophe DUBOIS reg_value = s->ocr3; 2885ec694b5SJean-Christophe DUBOIS break; 289a50c0d6fSJean-Christophe DUBOIS 290a50c0d6fSJean-Christophe DUBOIS case 7: /* input Capture Register 1 */ 29105453526SJean-Christophe Dubois qemu_log_mask(LOG_UNIMP, "[%s]%s: icr1 feature is not implemented\n", 29205453526SJean-Christophe Dubois TYPE_IMX_GPT, __func__); 2935ec694b5SJean-Christophe DUBOIS reg_value = s->icr1; 2945ec694b5SJean-Christophe DUBOIS break; 295a50c0d6fSJean-Christophe DUBOIS 296a50c0d6fSJean-Christophe DUBOIS case 8: /* input Capture Register 2 */ 29705453526SJean-Christophe Dubois qemu_log_mask(LOG_UNIMP, "[%s]%s: icr2 feature is not implemented\n", 29805453526SJean-Christophe Dubois TYPE_IMX_GPT, __func__); 2995ec694b5SJean-Christophe DUBOIS reg_value = s->icr2; 3005ec694b5SJean-Christophe DUBOIS break; 301a50c0d6fSJean-Christophe DUBOIS 302a50c0d6fSJean-Christophe DUBOIS case 9: /* cnt */ 30367110c3eSJean-Christophe DUBOIS imx_gpt_update_count(s); 3045ec694b5SJean-Christophe DUBOIS reg_value = s->cnt; 3055ec694b5SJean-Christophe DUBOIS break; 3065ec694b5SJean-Christophe DUBOIS 3075ec694b5SJean-Christophe DUBOIS default: 30805453526SJean-Christophe Dubois qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%" 30905453526SJean-Christophe Dubois HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset); 3105ec694b5SJean-Christophe DUBOIS break; 311a50c0d6fSJean-Christophe DUBOIS } 312a50c0d6fSJean-Christophe DUBOIS 313*afd431e4SBernhard Beschow trace_imx_gpt_read(imx_gpt_reg_name(offset >> 2), reg_value); 314a50c0d6fSJean-Christophe DUBOIS 3155ec694b5SJean-Christophe DUBOIS return reg_value; 316a50c0d6fSJean-Christophe DUBOIS } 317a50c0d6fSJean-Christophe DUBOIS 318a50c0d6fSJean-Christophe DUBOIS 319c98c9ebaSKurban Mallachiev static void imx_gpt_reset_common(IMXGPTState *s, bool is_soft_reset) 320c98c9ebaSKurban Mallachiev { 3211b914994SPeter Maydell ptimer_transaction_begin(s->timer); 3225ec694b5SJean-Christophe DUBOIS /* stop timer */ 3235ec694b5SJean-Christophe DUBOIS ptimer_stop(s->timer); 3245ec694b5SJean-Christophe DUBOIS 325c98c9ebaSKurban Mallachiev /* Soft reset and hard reset differ only in their handling of the CR 326c98c9ebaSKurban Mallachiev * register -- soft reset preserves the values of some bits there. 327a50c0d6fSJean-Christophe DUBOIS */ 328c98c9ebaSKurban Mallachiev if (is_soft_reset) { 329c98c9ebaSKurban Mallachiev /* Clear all CR bits except those that are preserved by soft reset. */ 330c98c9ebaSKurban Mallachiev s->cr &= GPT_CR_EN | GPT_CR_ENMOD | GPT_CR_STOPEN | GPT_CR_DOZEN | 331c98c9ebaSKurban Mallachiev GPT_CR_WAITEN | GPT_CR_DBGEN | 332c98c9ebaSKurban Mallachiev (GPT_CR_CLKSRC_MASK << GPT_CR_CLKSRC_SHIFT); 333c98c9ebaSKurban Mallachiev } else { 334c98c9ebaSKurban Mallachiev s->cr = 0; 335c98c9ebaSKurban Mallachiev } 336a50c0d6fSJean-Christophe DUBOIS s->sr = 0; 337a50c0d6fSJean-Christophe DUBOIS s->pr = 0; 338a50c0d6fSJean-Christophe DUBOIS s->ir = 0; 339a50c0d6fSJean-Christophe DUBOIS s->cnt = 0; 340203d65a4SMichael Tokarev s->ocr1 = GPT_TIMER_MAX; 341203d65a4SMichael Tokarev s->ocr2 = GPT_TIMER_MAX; 342203d65a4SMichael Tokarev s->ocr3 = GPT_TIMER_MAX; 343a50c0d6fSJean-Christophe DUBOIS s->icr1 = 0; 344a50c0d6fSJean-Christophe DUBOIS s->icr2 = 0; 3455ec694b5SJean-Christophe DUBOIS 346203d65a4SMichael Tokarev s->next_timeout = GPT_TIMER_MAX; 3475ec694b5SJean-Christophe DUBOIS s->next_int = 0; 3485ec694b5SJean-Christophe DUBOIS 3495ec694b5SJean-Christophe DUBOIS /* compute new freq */ 35067110c3eSJean-Christophe DUBOIS imx_gpt_set_freq(s); 3515ec694b5SJean-Christophe DUBOIS 352203d65a4SMichael Tokarev /* reset the limit to GPT_TIMER_MAX */ 353203d65a4SMichael Tokarev ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1); 3545ec694b5SJean-Christophe DUBOIS 3555ec694b5SJean-Christophe DUBOIS /* if the timer is still enabled, restart it */ 3565ec694b5SJean-Christophe DUBOIS if (s->freq && (s->cr & GPT_CR_EN)) { 3575ec694b5SJean-Christophe DUBOIS ptimer_run(s->timer, 1); 3585ec694b5SJean-Christophe DUBOIS } 3591b914994SPeter Maydell ptimer_transaction_commit(s->timer); 360a50c0d6fSJean-Christophe DUBOIS } 361a50c0d6fSJean-Christophe DUBOIS 362c98c9ebaSKurban Mallachiev static void imx_gpt_soft_reset(DeviceState *dev) 363c98c9ebaSKurban Mallachiev { 364c98c9ebaSKurban Mallachiev IMXGPTState *s = IMX_GPT(dev); 365c98c9ebaSKurban Mallachiev imx_gpt_reset_common(s, true); 366c98c9ebaSKurban Mallachiev } 367c98c9ebaSKurban Mallachiev 368c98c9ebaSKurban Mallachiev static void imx_gpt_reset(DeviceState *dev) 369c98c9ebaSKurban Mallachiev { 370c98c9ebaSKurban Mallachiev IMXGPTState *s = IMX_GPT(dev); 371c98c9ebaSKurban Mallachiev imx_gpt_reset_common(s, false); 372c98c9ebaSKurban Mallachiev } 373c98c9ebaSKurban Mallachiev 37467110c3eSJean-Christophe DUBOIS static void imx_gpt_write(void *opaque, hwaddr offset, uint64_t value, 37567110c3eSJean-Christophe DUBOIS unsigned size) 376a50c0d6fSJean-Christophe DUBOIS { 37767110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(opaque); 3785ec694b5SJean-Christophe DUBOIS uint32_t oldreg; 379a50c0d6fSJean-Christophe DUBOIS 380*afd431e4SBernhard Beschow trace_imx_gpt_write(imx_gpt_reg_name(offset >> 2), (uint32_t)value); 381a50c0d6fSJean-Christophe DUBOIS 38205453526SJean-Christophe Dubois switch (offset >> 2) { 3835ec694b5SJean-Christophe DUBOIS case 0: 3845ec694b5SJean-Christophe DUBOIS oldreg = s->cr; 3855ec694b5SJean-Christophe DUBOIS s->cr = value & ~0x7c14; 3865ec694b5SJean-Christophe DUBOIS if (s->cr & GPT_CR_SWR) { /* force reset */ 3875ec694b5SJean-Christophe DUBOIS /* handle the reset */ 388c98c9ebaSKurban Mallachiev imx_gpt_soft_reset(DEVICE(s)); 389a50c0d6fSJean-Christophe DUBOIS } else { 3905ec694b5SJean-Christophe DUBOIS /* set our freq, as the source might have changed */ 3911b914994SPeter Maydell ptimer_transaction_begin(s->timer); 39267110c3eSJean-Christophe DUBOIS imx_gpt_set_freq(s); 3935ec694b5SJean-Christophe DUBOIS 3945ec694b5SJean-Christophe DUBOIS if ((oldreg ^ s->cr) & GPT_CR_EN) { 3955ec694b5SJean-Christophe DUBOIS if (s->cr & GPT_CR_EN) { 3965ec694b5SJean-Christophe DUBOIS if (s->cr & GPT_CR_ENMOD) { 397203d65a4SMichael Tokarev s->next_timeout = GPT_TIMER_MAX; 398203d65a4SMichael Tokarev ptimer_set_count(s->timer, GPT_TIMER_MAX); 39967110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 4005ec694b5SJean-Christophe DUBOIS } 4015ec694b5SJean-Christophe DUBOIS ptimer_run(s->timer, 1); 4025ec694b5SJean-Christophe DUBOIS } else { 4035ec694b5SJean-Christophe DUBOIS /* stop timer */ 404a50c0d6fSJean-Christophe DUBOIS ptimer_stop(s->timer); 405a50c0d6fSJean-Christophe DUBOIS } 406a50c0d6fSJean-Christophe DUBOIS } 4071b914994SPeter Maydell ptimer_transaction_commit(s->timer); 4085ec694b5SJean-Christophe DUBOIS } 4095ec694b5SJean-Christophe DUBOIS break; 410a50c0d6fSJean-Christophe DUBOIS 411a50c0d6fSJean-Christophe DUBOIS case 1: /* Prescaler */ 412a50c0d6fSJean-Christophe DUBOIS s->pr = value & 0xfff; 4131b914994SPeter Maydell ptimer_transaction_begin(s->timer); 41467110c3eSJean-Christophe DUBOIS imx_gpt_set_freq(s); 4151b914994SPeter Maydell ptimer_transaction_commit(s->timer); 4165ec694b5SJean-Christophe DUBOIS break; 417a50c0d6fSJean-Christophe DUBOIS 418a50c0d6fSJean-Christophe DUBOIS case 2: /* SR */ 4195ec694b5SJean-Christophe DUBOIS s->sr &= ~(value & 0x3f); 42067110c3eSJean-Christophe DUBOIS imx_gpt_update_int(s); 4215ec694b5SJean-Christophe DUBOIS break; 422a50c0d6fSJean-Christophe DUBOIS 423a50c0d6fSJean-Christophe DUBOIS case 3: /* IR -- interrupt register */ 424a50c0d6fSJean-Christophe DUBOIS s->ir = value & 0x3f; 42567110c3eSJean-Christophe DUBOIS imx_gpt_update_int(s); 4265ec694b5SJean-Christophe DUBOIS 4271b914994SPeter Maydell ptimer_transaction_begin(s->timer); 42867110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 4291b914994SPeter Maydell ptimer_transaction_commit(s->timer); 4305ec694b5SJean-Christophe DUBOIS 4315ec694b5SJean-Christophe DUBOIS break; 432a50c0d6fSJean-Christophe DUBOIS 433a50c0d6fSJean-Christophe DUBOIS case 4: /* OCR1 -- output compare register */ 4345ec694b5SJean-Christophe DUBOIS s->ocr1 = value; 4355ec694b5SJean-Christophe DUBOIS 4361b914994SPeter Maydell ptimer_transaction_begin(s->timer); 437a50c0d6fSJean-Christophe DUBOIS /* In non-freerun mode, reset count when this register is written */ 438a50c0d6fSJean-Christophe DUBOIS if (!(s->cr & GPT_CR_FRR)) { 439203d65a4SMichael Tokarev s->next_timeout = GPT_TIMER_MAX; 440203d65a4SMichael Tokarev ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1); 441a50c0d6fSJean-Christophe DUBOIS } 4425ec694b5SJean-Christophe DUBOIS 4435ec694b5SJean-Christophe DUBOIS /* compute the new timeout */ 44467110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 4451b914994SPeter Maydell ptimer_transaction_commit(s->timer); 4465ec694b5SJean-Christophe DUBOIS 4475ec694b5SJean-Christophe DUBOIS break; 448a50c0d6fSJean-Christophe DUBOIS 449a50c0d6fSJean-Christophe DUBOIS case 5: /* OCR2 -- output compare register */ 4505ec694b5SJean-Christophe DUBOIS s->ocr2 = value; 4515ec694b5SJean-Christophe DUBOIS 4525ec694b5SJean-Christophe DUBOIS /* compute the new timeout */ 4531b914994SPeter Maydell ptimer_transaction_begin(s->timer); 45467110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 4551b914994SPeter Maydell ptimer_transaction_commit(s->timer); 4565ec694b5SJean-Christophe DUBOIS 4575ec694b5SJean-Christophe DUBOIS break; 4585ec694b5SJean-Christophe DUBOIS 459a50c0d6fSJean-Christophe DUBOIS case 6: /* OCR3 -- output compare register */ 4605ec694b5SJean-Christophe DUBOIS s->ocr3 = value; 4615ec694b5SJean-Christophe DUBOIS 4625ec694b5SJean-Christophe DUBOIS /* compute the new timeout */ 4631b914994SPeter Maydell ptimer_transaction_begin(s->timer); 46467110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 4651b914994SPeter Maydell ptimer_transaction_commit(s->timer); 4665ec694b5SJean-Christophe DUBOIS 4675ec694b5SJean-Christophe DUBOIS break; 4685ec694b5SJean-Christophe DUBOIS 469a50c0d6fSJean-Christophe DUBOIS default: 47005453526SJean-Christophe Dubois qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%" 47105453526SJean-Christophe Dubois HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset); 4725ec694b5SJean-Christophe DUBOIS break; 473a50c0d6fSJean-Christophe DUBOIS } 474a50c0d6fSJean-Christophe DUBOIS } 475a50c0d6fSJean-Christophe DUBOIS 47667110c3eSJean-Christophe DUBOIS static void imx_gpt_timeout(void *opaque) 477a50c0d6fSJean-Christophe DUBOIS { 47867110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(opaque); 479a50c0d6fSJean-Christophe DUBOIS 480*afd431e4SBernhard Beschow trace_imx_gpt_timeout(); 481a50c0d6fSJean-Christophe DUBOIS 4825ec694b5SJean-Christophe DUBOIS s->sr |= s->next_int; 4835ec694b5SJean-Christophe DUBOIS s->next_int = 0; 484a50c0d6fSJean-Christophe DUBOIS 48567110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, true); 4865ec694b5SJean-Christophe DUBOIS 48767110c3eSJean-Christophe DUBOIS imx_gpt_update_int(s); 4885ec694b5SJean-Christophe DUBOIS 4895ec694b5SJean-Christophe DUBOIS if (s->freq && (s->cr & GPT_CR_EN)) { 4905ec694b5SJean-Christophe DUBOIS ptimer_run(s->timer, 1); 4915ec694b5SJean-Christophe DUBOIS } 492a50c0d6fSJean-Christophe DUBOIS } 493a50c0d6fSJean-Christophe DUBOIS 49467110c3eSJean-Christophe DUBOIS static const MemoryRegionOps imx_gpt_ops = { 49567110c3eSJean-Christophe DUBOIS .read = imx_gpt_read, 49667110c3eSJean-Christophe DUBOIS .write = imx_gpt_write, 497a50c0d6fSJean-Christophe DUBOIS .endianness = DEVICE_NATIVE_ENDIAN, 498a50c0d6fSJean-Christophe DUBOIS }; 499a50c0d6fSJean-Christophe DUBOIS 500a50c0d6fSJean-Christophe DUBOIS 50167110c3eSJean-Christophe DUBOIS static void imx_gpt_realize(DeviceState *dev, Error **errp) 502a50c0d6fSJean-Christophe DUBOIS { 50367110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(dev); 50467110c3eSJean-Christophe DUBOIS SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 505a50c0d6fSJean-Christophe DUBOIS 50667110c3eSJean-Christophe DUBOIS sysbus_init_irq(sbd, &s->irq); 507853dca12SPaolo Bonzini memory_region_init_io(&s->iomem, OBJECT(s), &imx_gpt_ops, s, TYPE_IMX_GPT, 508a50c0d6fSJean-Christophe DUBOIS 0x00001000); 50967110c3eSJean-Christophe DUBOIS sysbus_init_mmio(sbd, &s->iomem); 510a50c0d6fSJean-Christophe DUBOIS 5119598c1bbSPeter Maydell s->timer = ptimer_init(imx_gpt_timeout, s, PTIMER_POLICY_LEGACY); 512a50c0d6fSJean-Christophe DUBOIS } 513a50c0d6fSJean-Christophe DUBOIS 51467110c3eSJean-Christophe DUBOIS static void imx_gpt_class_init(ObjectClass *klass, void *data) 515a50c0d6fSJean-Christophe DUBOIS { 516a50c0d6fSJean-Christophe DUBOIS DeviceClass *dc = DEVICE_CLASS(klass); 51767110c3eSJean-Christophe DUBOIS 51867110c3eSJean-Christophe DUBOIS dc->realize = imx_gpt_realize; 519e3d08143SPeter Maydell device_class_set_legacy_reset(dc, imx_gpt_reset); 52067110c3eSJean-Christophe DUBOIS dc->vmsd = &vmstate_imx_timer_gpt; 521a50c0d6fSJean-Christophe DUBOIS dc->desc = "i.MX general timer"; 522a50c0d6fSJean-Christophe DUBOIS } 523a50c0d6fSJean-Christophe DUBOIS 52466542f63SJean-Christophe Dubois static void imx25_gpt_init(Object *obj) 52566542f63SJean-Christophe Dubois { 52666542f63SJean-Christophe Dubois IMXGPTState *s = IMX_GPT(obj); 52766542f63SJean-Christophe Dubois 52866542f63SJean-Christophe Dubois s->clocks = imx25_gpt_clocks; 52966542f63SJean-Christophe Dubois } 53066542f63SJean-Christophe Dubois 53166542f63SJean-Christophe Dubois static void imx31_gpt_init(Object *obj) 53266542f63SJean-Christophe Dubois { 53366542f63SJean-Christophe Dubois IMXGPTState *s = IMX_GPT(obj); 53466542f63SJean-Christophe Dubois 53566542f63SJean-Christophe Dubois s->clocks = imx31_gpt_clocks; 53666542f63SJean-Christophe Dubois } 53766542f63SJean-Christophe Dubois 53866542f63SJean-Christophe Dubois static void imx6_gpt_init(Object *obj) 53966542f63SJean-Christophe Dubois { 54066542f63SJean-Christophe Dubois IMXGPTState *s = IMX_GPT(obj); 54166542f63SJean-Christophe Dubois 54266542f63SJean-Christophe Dubois s->clocks = imx6_gpt_clocks; 54366542f63SJean-Christophe Dubois } 54466542f63SJean-Christophe Dubois 545a1e03956SJean-Christophe Dubois static void imx6ul_gpt_init(Object *obj) 546a1e03956SJean-Christophe Dubois { 547a1e03956SJean-Christophe Dubois IMXGPTState *s = IMX_GPT(obj); 548a1e03956SJean-Christophe Dubois 549a1e03956SJean-Christophe Dubois s->clocks = imx6ul_gpt_clocks; 550a1e03956SJean-Christophe Dubois } 551a1e03956SJean-Christophe Dubois 552a62bf59fSAndrey Smirnov static void imx7_gpt_init(Object *obj) 553a62bf59fSAndrey Smirnov { 554a62bf59fSAndrey Smirnov IMXGPTState *s = IMX_GPT(obj); 555a62bf59fSAndrey Smirnov 556a62bf59fSAndrey Smirnov s->clocks = imx7_gpt_clocks; 557a62bf59fSAndrey Smirnov } 558a62bf59fSAndrey Smirnov 55966542f63SJean-Christophe Dubois static const TypeInfo imx25_gpt_info = { 56066542f63SJean-Christophe Dubois .name = TYPE_IMX25_GPT, 561a50c0d6fSJean-Christophe DUBOIS .parent = TYPE_SYS_BUS_DEVICE, 56267110c3eSJean-Christophe DUBOIS .instance_size = sizeof(IMXGPTState), 56366542f63SJean-Christophe Dubois .instance_init = imx25_gpt_init, 56467110c3eSJean-Christophe DUBOIS .class_init = imx_gpt_class_init, 565a50c0d6fSJean-Christophe DUBOIS }; 566a50c0d6fSJean-Christophe DUBOIS 56766542f63SJean-Christophe Dubois static const TypeInfo imx31_gpt_info = { 56866542f63SJean-Christophe Dubois .name = TYPE_IMX31_GPT, 56966542f63SJean-Christophe Dubois .parent = TYPE_IMX25_GPT, 57066542f63SJean-Christophe Dubois .instance_init = imx31_gpt_init, 57166542f63SJean-Christophe Dubois }; 57266542f63SJean-Christophe Dubois 57366542f63SJean-Christophe Dubois static const TypeInfo imx6_gpt_info = { 57466542f63SJean-Christophe Dubois .name = TYPE_IMX6_GPT, 57566542f63SJean-Christophe Dubois .parent = TYPE_IMX25_GPT, 57666542f63SJean-Christophe Dubois .instance_init = imx6_gpt_init, 57766542f63SJean-Christophe Dubois }; 57866542f63SJean-Christophe Dubois 579a1e03956SJean-Christophe Dubois static const TypeInfo imx6ul_gpt_info = { 580a1e03956SJean-Christophe Dubois .name = TYPE_IMX6UL_GPT, 581a1e03956SJean-Christophe Dubois .parent = TYPE_IMX25_GPT, 582a1e03956SJean-Christophe Dubois .instance_init = imx6ul_gpt_init, 583a1e03956SJean-Christophe Dubois }; 584a1e03956SJean-Christophe Dubois 585a62bf59fSAndrey Smirnov static const TypeInfo imx7_gpt_info = { 586a62bf59fSAndrey Smirnov .name = TYPE_IMX7_GPT, 587a62bf59fSAndrey Smirnov .parent = TYPE_IMX25_GPT, 588a62bf59fSAndrey Smirnov .instance_init = imx7_gpt_init, 589a62bf59fSAndrey Smirnov }; 590a62bf59fSAndrey Smirnov 59167110c3eSJean-Christophe DUBOIS static void imx_gpt_register_types(void) 592a50c0d6fSJean-Christophe DUBOIS { 59366542f63SJean-Christophe Dubois type_register_static(&imx25_gpt_info); 59466542f63SJean-Christophe Dubois type_register_static(&imx31_gpt_info); 59566542f63SJean-Christophe Dubois type_register_static(&imx6_gpt_info); 596a1e03956SJean-Christophe Dubois type_register_static(&imx6ul_gpt_info); 597a62bf59fSAndrey Smirnov type_register_static(&imx7_gpt_info); 598a50c0d6fSJean-Christophe DUBOIS } 599a50c0d6fSJean-Christophe DUBOIS 60067110c3eSJean-Christophe DUBOIS type_init(imx_gpt_register_types) 601