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" 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; 137*4833e15fSJean-Christophe Dubois uint32_t count; 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 145*4833e15fSJean-Christophe Dubois /* update the count */ 146*4833e15fSJean-Christophe Dubois count = imx_gpt_update_count(s); 147*4833e15fSJean-Christophe Dubois 1485ec694b5SJean-Christophe DUBOIS if (event) { 149a50c0d6fSJean-Christophe DUBOIS /* 150*4833e15fSJean-Christophe Dubois * This is an event (the ptimer reached 0 and stopped), and the 151*4833e15fSJean-Christophe Dubois * timer counter is now equal to s->next_timeout. 152a50c0d6fSJean-Christophe DUBOIS */ 153*4833e15fSJean-Christophe Dubois if (!(s->cr & GPT_CR_FRR) && (count == s->ocr1)) { 154*4833e15fSJean-Christophe Dubois /* We are in restart mode and we crossed the compare channel 1 155*4833e15fSJean-Christophe Dubois * value. We need to reset the counter to 0. 156*4833e15fSJean-Christophe Dubois */ 157*4833e15fSJean-Christophe Dubois count = s->cnt = s->next_timeout = 0; 158*4833e15fSJean-Christophe Dubois } else if (count == GPT_TIMER_MAX) { 159*4833e15fSJean-Christophe Dubois /* We reached GPT_TIMER_MAX so we need to rollover */ 160*4833e15fSJean-Christophe Dubois count = s->cnt = s->next_timeout = 0; 161a50c0d6fSJean-Christophe DUBOIS } 1625ec694b5SJean-Christophe DUBOIS } 1635ec694b5SJean-Christophe DUBOIS 1645ec694b5SJean-Christophe DUBOIS /* now, find the next timeout related to count */ 1655ec694b5SJean-Christophe DUBOIS 1665ec694b5SJean-Christophe DUBOIS if (s->ir & GPT_IR_OF1IE) { 16767110c3eSJean-Christophe DUBOIS timeout = imx_gpt_find_limit(count, s->ocr1, timeout); 1685ec694b5SJean-Christophe DUBOIS } 1695ec694b5SJean-Christophe DUBOIS if (s->ir & GPT_IR_OF2IE) { 17067110c3eSJean-Christophe DUBOIS timeout = imx_gpt_find_limit(count, s->ocr2, timeout); 1715ec694b5SJean-Christophe DUBOIS } 1725ec694b5SJean-Christophe DUBOIS if (s->ir & GPT_IR_OF3IE) { 17367110c3eSJean-Christophe DUBOIS timeout = imx_gpt_find_limit(count, s->ocr3, timeout); 1745ec694b5SJean-Christophe DUBOIS } 1755ec694b5SJean-Christophe DUBOIS 1765ec694b5SJean-Christophe DUBOIS /* find the next set of interrupts to raise for next timer event */ 1775ec694b5SJean-Christophe DUBOIS 1785ec694b5SJean-Christophe DUBOIS s->next_int = 0; 1795ec694b5SJean-Christophe DUBOIS if ((s->ir & GPT_IR_OF1IE) && (timeout == s->ocr1)) { 1805ec694b5SJean-Christophe DUBOIS s->next_int |= GPT_SR_OF1; 1815ec694b5SJean-Christophe DUBOIS } 1825ec694b5SJean-Christophe DUBOIS if ((s->ir & GPT_IR_OF2IE) && (timeout == s->ocr2)) { 1835ec694b5SJean-Christophe DUBOIS s->next_int |= GPT_SR_OF2; 1845ec694b5SJean-Christophe DUBOIS } 1855ec694b5SJean-Christophe DUBOIS if ((s->ir & GPT_IR_OF3IE) && (timeout == s->ocr3)) { 1865ec694b5SJean-Christophe DUBOIS s->next_int |= GPT_SR_OF3; 1875ec694b5SJean-Christophe DUBOIS } 188203d65a4SMichael Tokarev if ((s->ir & GPT_IR_ROVIE) && (timeout == GPT_TIMER_MAX)) { 1895ec694b5SJean-Christophe DUBOIS s->next_int |= GPT_SR_ROV; 1905ec694b5SJean-Christophe DUBOIS } 1915ec694b5SJean-Christophe DUBOIS 1925ec694b5SJean-Christophe DUBOIS /* the new range to count down from */ 19367110c3eSJean-Christophe DUBOIS limit = timeout - imx_gpt_update_count(s); 1945ec694b5SJean-Christophe DUBOIS 1955ec694b5SJean-Christophe DUBOIS if (limit < 0) { 1965ec694b5SJean-Christophe DUBOIS /* 1975ec694b5SJean-Christophe DUBOIS * if we reach here, then QEMU is running too slow and we pass the 1985ec694b5SJean-Christophe DUBOIS * timeout limit while computing it. Let's deliver the interrupt 1995ec694b5SJean-Christophe DUBOIS * and compute a new limit. 2005ec694b5SJean-Christophe DUBOIS */ 2015ec694b5SJean-Christophe DUBOIS s->sr |= s->next_int; 2025ec694b5SJean-Christophe DUBOIS 20367110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, event); 2045ec694b5SJean-Christophe DUBOIS 20567110c3eSJean-Christophe DUBOIS imx_gpt_update_int(s); 2065ec694b5SJean-Christophe DUBOIS } else { 2075ec694b5SJean-Christophe DUBOIS /* New timeout value */ 2085ec694b5SJean-Christophe DUBOIS s->next_timeout = timeout; 2095ec694b5SJean-Christophe DUBOIS 2105ec694b5SJean-Christophe DUBOIS /* reset the limit to the computed range */ 2115ec694b5SJean-Christophe DUBOIS ptimer_set_limit(s->timer, limit, 1); 2125ec694b5SJean-Christophe DUBOIS } 213a50c0d6fSJean-Christophe DUBOIS } 214a50c0d6fSJean-Christophe DUBOIS 21567110c3eSJean-Christophe DUBOIS static uint64_t imx_gpt_read(void *opaque, hwaddr offset, unsigned size) 216a50c0d6fSJean-Christophe DUBOIS { 21767110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(opaque); 2185ec694b5SJean-Christophe DUBOIS uint32_t reg_value = 0; 219a50c0d6fSJean-Christophe DUBOIS 22005453526SJean-Christophe Dubois switch (offset >> 2) { 221a50c0d6fSJean-Christophe DUBOIS case 0: /* Control Register */ 2225ec694b5SJean-Christophe DUBOIS reg_value = s->cr; 2235ec694b5SJean-Christophe DUBOIS break; 224a50c0d6fSJean-Christophe DUBOIS 225a50c0d6fSJean-Christophe DUBOIS case 1: /* prescaler */ 2265ec694b5SJean-Christophe DUBOIS reg_value = s->pr; 2275ec694b5SJean-Christophe DUBOIS break; 228a50c0d6fSJean-Christophe DUBOIS 229a50c0d6fSJean-Christophe DUBOIS case 2: /* Status Register */ 2305ec694b5SJean-Christophe DUBOIS reg_value = s->sr; 2315ec694b5SJean-Christophe DUBOIS break; 232a50c0d6fSJean-Christophe DUBOIS 233a50c0d6fSJean-Christophe DUBOIS case 3: /* Interrupt Register */ 2345ec694b5SJean-Christophe DUBOIS reg_value = s->ir; 2355ec694b5SJean-Christophe DUBOIS break; 236a50c0d6fSJean-Christophe DUBOIS 237a50c0d6fSJean-Christophe DUBOIS case 4: /* Output Compare Register 1 */ 2385ec694b5SJean-Christophe DUBOIS reg_value = s->ocr1; 2395ec694b5SJean-Christophe DUBOIS break; 240a50c0d6fSJean-Christophe DUBOIS 241a50c0d6fSJean-Christophe DUBOIS case 5: /* Output Compare Register 2 */ 2425ec694b5SJean-Christophe DUBOIS reg_value = s->ocr2; 2435ec694b5SJean-Christophe DUBOIS break; 244a50c0d6fSJean-Christophe DUBOIS 245a50c0d6fSJean-Christophe DUBOIS case 6: /* Output Compare Register 3 */ 2465ec694b5SJean-Christophe DUBOIS reg_value = s->ocr3; 2475ec694b5SJean-Christophe DUBOIS break; 248a50c0d6fSJean-Christophe DUBOIS 249a50c0d6fSJean-Christophe DUBOIS case 7: /* input Capture Register 1 */ 25005453526SJean-Christophe Dubois qemu_log_mask(LOG_UNIMP, "[%s]%s: icr1 feature is not implemented\n", 25105453526SJean-Christophe Dubois TYPE_IMX_GPT, __func__); 2525ec694b5SJean-Christophe DUBOIS reg_value = s->icr1; 2535ec694b5SJean-Christophe DUBOIS break; 254a50c0d6fSJean-Christophe DUBOIS 255a50c0d6fSJean-Christophe DUBOIS case 8: /* input Capture Register 2 */ 25605453526SJean-Christophe Dubois qemu_log_mask(LOG_UNIMP, "[%s]%s: icr2 feature is not implemented\n", 25705453526SJean-Christophe Dubois TYPE_IMX_GPT, __func__); 2585ec694b5SJean-Christophe DUBOIS reg_value = s->icr2; 2595ec694b5SJean-Christophe DUBOIS break; 260a50c0d6fSJean-Christophe DUBOIS 261a50c0d6fSJean-Christophe DUBOIS case 9: /* cnt */ 26267110c3eSJean-Christophe DUBOIS imx_gpt_update_count(s); 2635ec694b5SJean-Christophe DUBOIS reg_value = s->cnt; 2645ec694b5SJean-Christophe DUBOIS break; 2655ec694b5SJean-Christophe DUBOIS 2665ec694b5SJean-Christophe DUBOIS default: 26705453526SJean-Christophe Dubois qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%" 26805453526SJean-Christophe Dubois HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset); 2695ec694b5SJean-Christophe DUBOIS break; 270a50c0d6fSJean-Christophe DUBOIS } 271a50c0d6fSJean-Christophe DUBOIS 27205453526SJean-Christophe Dubois DPRINTF("(%s) = 0x%08x\n", imx_gpt_reg_name(offset >> 2), reg_value); 273a50c0d6fSJean-Christophe DUBOIS 2745ec694b5SJean-Christophe DUBOIS return reg_value; 275a50c0d6fSJean-Christophe DUBOIS } 276a50c0d6fSJean-Christophe DUBOIS 27767110c3eSJean-Christophe DUBOIS static void imx_gpt_reset(DeviceState *dev) 278a50c0d6fSJean-Christophe DUBOIS { 27967110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(dev); 280a50c0d6fSJean-Christophe DUBOIS 2815ec694b5SJean-Christophe DUBOIS /* stop timer */ 2825ec694b5SJean-Christophe DUBOIS ptimer_stop(s->timer); 2835ec694b5SJean-Christophe DUBOIS 284a50c0d6fSJean-Christophe DUBOIS /* 285a50c0d6fSJean-Christophe DUBOIS * Soft reset doesn't touch some bits; hard reset clears them 286a50c0d6fSJean-Christophe DUBOIS */ 287a50c0d6fSJean-Christophe DUBOIS s->cr &= ~(GPT_CR_EN|GPT_CR_ENMOD|GPT_CR_STOPEN|GPT_CR_DOZEN| 288a50c0d6fSJean-Christophe DUBOIS GPT_CR_WAITEN|GPT_CR_DBGEN); 289a50c0d6fSJean-Christophe DUBOIS s->sr = 0; 290a50c0d6fSJean-Christophe DUBOIS s->pr = 0; 291a50c0d6fSJean-Christophe DUBOIS s->ir = 0; 292a50c0d6fSJean-Christophe DUBOIS s->cnt = 0; 293203d65a4SMichael Tokarev s->ocr1 = GPT_TIMER_MAX; 294203d65a4SMichael Tokarev s->ocr2 = GPT_TIMER_MAX; 295203d65a4SMichael Tokarev s->ocr3 = GPT_TIMER_MAX; 296a50c0d6fSJean-Christophe DUBOIS s->icr1 = 0; 297a50c0d6fSJean-Christophe DUBOIS s->icr2 = 0; 2985ec694b5SJean-Christophe DUBOIS 299203d65a4SMichael Tokarev s->next_timeout = GPT_TIMER_MAX; 3005ec694b5SJean-Christophe DUBOIS s->next_int = 0; 3015ec694b5SJean-Christophe DUBOIS 3025ec694b5SJean-Christophe DUBOIS /* compute new freq */ 30367110c3eSJean-Christophe DUBOIS imx_gpt_set_freq(s); 3045ec694b5SJean-Christophe DUBOIS 305203d65a4SMichael Tokarev /* reset the limit to GPT_TIMER_MAX */ 306203d65a4SMichael Tokarev ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1); 3075ec694b5SJean-Christophe DUBOIS 3085ec694b5SJean-Christophe DUBOIS /* if the timer is still enabled, restart it */ 3095ec694b5SJean-Christophe DUBOIS if (s->freq && (s->cr & GPT_CR_EN)) { 3105ec694b5SJean-Christophe DUBOIS ptimer_run(s->timer, 1); 3115ec694b5SJean-Christophe DUBOIS } 312a50c0d6fSJean-Christophe DUBOIS } 313a50c0d6fSJean-Christophe DUBOIS 31467110c3eSJean-Christophe DUBOIS static void imx_gpt_write(void *opaque, hwaddr offset, uint64_t value, 31567110c3eSJean-Christophe DUBOIS unsigned size) 316a50c0d6fSJean-Christophe DUBOIS { 31767110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(opaque); 3185ec694b5SJean-Christophe DUBOIS uint32_t oldreg; 319a50c0d6fSJean-Christophe DUBOIS 32005453526SJean-Christophe Dubois DPRINTF("(%s, value = 0x%08x)\n", imx_gpt_reg_name(offset >> 2), 3215ec694b5SJean-Christophe DUBOIS (uint32_t)value); 322a50c0d6fSJean-Christophe DUBOIS 32305453526SJean-Christophe Dubois switch (offset >> 2) { 3245ec694b5SJean-Christophe DUBOIS case 0: 3255ec694b5SJean-Christophe DUBOIS oldreg = s->cr; 3265ec694b5SJean-Christophe DUBOIS s->cr = value & ~0x7c14; 3275ec694b5SJean-Christophe DUBOIS if (s->cr & GPT_CR_SWR) { /* force reset */ 3285ec694b5SJean-Christophe DUBOIS /* handle the reset */ 32967110c3eSJean-Christophe DUBOIS imx_gpt_reset(DEVICE(s)); 330a50c0d6fSJean-Christophe DUBOIS } else { 3315ec694b5SJean-Christophe DUBOIS /* set our freq, as the source might have changed */ 33267110c3eSJean-Christophe DUBOIS imx_gpt_set_freq(s); 3335ec694b5SJean-Christophe DUBOIS 3345ec694b5SJean-Christophe DUBOIS if ((oldreg ^ s->cr) & GPT_CR_EN) { 3355ec694b5SJean-Christophe DUBOIS if (s->cr & GPT_CR_EN) { 3365ec694b5SJean-Christophe DUBOIS if (s->cr & GPT_CR_ENMOD) { 337203d65a4SMichael Tokarev s->next_timeout = GPT_TIMER_MAX; 338203d65a4SMichael Tokarev ptimer_set_count(s->timer, GPT_TIMER_MAX); 33967110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 3405ec694b5SJean-Christophe DUBOIS } 3415ec694b5SJean-Christophe DUBOIS ptimer_run(s->timer, 1); 3425ec694b5SJean-Christophe DUBOIS } else { 3435ec694b5SJean-Christophe DUBOIS /* stop timer */ 344a50c0d6fSJean-Christophe DUBOIS ptimer_stop(s->timer); 345a50c0d6fSJean-Christophe DUBOIS } 346a50c0d6fSJean-Christophe DUBOIS } 3475ec694b5SJean-Christophe DUBOIS } 3485ec694b5SJean-Christophe DUBOIS break; 349a50c0d6fSJean-Christophe DUBOIS 350a50c0d6fSJean-Christophe DUBOIS case 1: /* Prescaler */ 351a50c0d6fSJean-Christophe DUBOIS s->pr = value & 0xfff; 35267110c3eSJean-Christophe DUBOIS imx_gpt_set_freq(s); 3535ec694b5SJean-Christophe DUBOIS break; 354a50c0d6fSJean-Christophe DUBOIS 355a50c0d6fSJean-Christophe DUBOIS case 2: /* SR */ 3565ec694b5SJean-Christophe DUBOIS s->sr &= ~(value & 0x3f); 35767110c3eSJean-Christophe DUBOIS imx_gpt_update_int(s); 3585ec694b5SJean-Christophe DUBOIS break; 359a50c0d6fSJean-Christophe DUBOIS 360a50c0d6fSJean-Christophe DUBOIS case 3: /* IR -- interrupt register */ 361a50c0d6fSJean-Christophe DUBOIS s->ir = value & 0x3f; 36267110c3eSJean-Christophe DUBOIS imx_gpt_update_int(s); 3635ec694b5SJean-Christophe DUBOIS 36467110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 3655ec694b5SJean-Christophe DUBOIS 3665ec694b5SJean-Christophe DUBOIS break; 367a50c0d6fSJean-Christophe DUBOIS 368a50c0d6fSJean-Christophe DUBOIS case 4: /* OCR1 -- output compare register */ 3695ec694b5SJean-Christophe DUBOIS s->ocr1 = value; 3705ec694b5SJean-Christophe DUBOIS 371a50c0d6fSJean-Christophe DUBOIS /* In non-freerun mode, reset count when this register is written */ 372a50c0d6fSJean-Christophe DUBOIS if (!(s->cr & GPT_CR_FRR)) { 373203d65a4SMichael Tokarev s->next_timeout = GPT_TIMER_MAX; 374203d65a4SMichael Tokarev ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1); 375a50c0d6fSJean-Christophe DUBOIS } 3765ec694b5SJean-Christophe DUBOIS 3775ec694b5SJean-Christophe DUBOIS /* compute the new timeout */ 37867110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 3795ec694b5SJean-Christophe DUBOIS 3805ec694b5SJean-Christophe DUBOIS break; 381a50c0d6fSJean-Christophe DUBOIS 382a50c0d6fSJean-Christophe DUBOIS case 5: /* OCR2 -- output compare register */ 3835ec694b5SJean-Christophe DUBOIS s->ocr2 = value; 3845ec694b5SJean-Christophe DUBOIS 3855ec694b5SJean-Christophe DUBOIS /* compute the new timeout */ 38667110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 3875ec694b5SJean-Christophe DUBOIS 3885ec694b5SJean-Christophe DUBOIS break; 3895ec694b5SJean-Christophe DUBOIS 390a50c0d6fSJean-Christophe DUBOIS case 6: /* OCR3 -- output compare register */ 3915ec694b5SJean-Christophe DUBOIS s->ocr3 = value; 3925ec694b5SJean-Christophe DUBOIS 3935ec694b5SJean-Christophe DUBOIS /* compute the new timeout */ 39467110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, false); 3955ec694b5SJean-Christophe DUBOIS 3965ec694b5SJean-Christophe DUBOIS break; 3975ec694b5SJean-Christophe DUBOIS 398a50c0d6fSJean-Christophe DUBOIS default: 39905453526SJean-Christophe Dubois qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%" 40005453526SJean-Christophe Dubois HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset); 4015ec694b5SJean-Christophe DUBOIS break; 402a50c0d6fSJean-Christophe DUBOIS } 403a50c0d6fSJean-Christophe DUBOIS } 404a50c0d6fSJean-Christophe DUBOIS 40567110c3eSJean-Christophe DUBOIS static void imx_gpt_timeout(void *opaque) 406a50c0d6fSJean-Christophe DUBOIS { 40767110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(opaque); 408a50c0d6fSJean-Christophe DUBOIS 4095ec694b5SJean-Christophe DUBOIS DPRINTF("\n"); 410a50c0d6fSJean-Christophe DUBOIS 4115ec694b5SJean-Christophe DUBOIS s->sr |= s->next_int; 4125ec694b5SJean-Christophe DUBOIS s->next_int = 0; 413a50c0d6fSJean-Christophe DUBOIS 41467110c3eSJean-Christophe DUBOIS imx_gpt_compute_next_timeout(s, true); 4155ec694b5SJean-Christophe DUBOIS 41667110c3eSJean-Christophe DUBOIS imx_gpt_update_int(s); 4175ec694b5SJean-Christophe DUBOIS 4185ec694b5SJean-Christophe DUBOIS if (s->freq && (s->cr & GPT_CR_EN)) { 4195ec694b5SJean-Christophe DUBOIS ptimer_run(s->timer, 1); 4205ec694b5SJean-Christophe DUBOIS } 421a50c0d6fSJean-Christophe DUBOIS } 422a50c0d6fSJean-Christophe DUBOIS 42367110c3eSJean-Christophe DUBOIS static const MemoryRegionOps imx_gpt_ops = { 42467110c3eSJean-Christophe DUBOIS .read = imx_gpt_read, 42567110c3eSJean-Christophe DUBOIS .write = imx_gpt_write, 426a50c0d6fSJean-Christophe DUBOIS .endianness = DEVICE_NATIVE_ENDIAN, 427a50c0d6fSJean-Christophe DUBOIS }; 428a50c0d6fSJean-Christophe DUBOIS 429a50c0d6fSJean-Christophe DUBOIS 43067110c3eSJean-Christophe DUBOIS static void imx_gpt_realize(DeviceState *dev, Error **errp) 431a50c0d6fSJean-Christophe DUBOIS { 43267110c3eSJean-Christophe DUBOIS IMXGPTState *s = IMX_GPT(dev); 43367110c3eSJean-Christophe DUBOIS SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 434a50c0d6fSJean-Christophe DUBOIS QEMUBH *bh; 435a50c0d6fSJean-Christophe DUBOIS 43667110c3eSJean-Christophe DUBOIS sysbus_init_irq(sbd, &s->irq); 437853dca12SPaolo Bonzini memory_region_init_io(&s->iomem, OBJECT(s), &imx_gpt_ops, s, TYPE_IMX_GPT, 438a50c0d6fSJean-Christophe DUBOIS 0x00001000); 43967110c3eSJean-Christophe DUBOIS sysbus_init_mmio(sbd, &s->iomem); 440a50c0d6fSJean-Christophe DUBOIS 44167110c3eSJean-Christophe DUBOIS bh = qemu_bh_new(imx_gpt_timeout, s); 442a50c0d6fSJean-Christophe DUBOIS s->timer = ptimer_init(bh); 443a50c0d6fSJean-Christophe DUBOIS } 444a50c0d6fSJean-Christophe DUBOIS 44567110c3eSJean-Christophe DUBOIS static void imx_gpt_class_init(ObjectClass *klass, void *data) 446a50c0d6fSJean-Christophe DUBOIS { 447a50c0d6fSJean-Christophe DUBOIS DeviceClass *dc = DEVICE_CLASS(klass); 44867110c3eSJean-Christophe DUBOIS 44967110c3eSJean-Christophe DUBOIS dc->realize = imx_gpt_realize; 45067110c3eSJean-Christophe DUBOIS dc->reset = imx_gpt_reset; 45167110c3eSJean-Christophe DUBOIS dc->vmsd = &vmstate_imx_timer_gpt; 452a50c0d6fSJean-Christophe DUBOIS dc->desc = "i.MX general timer"; 453a50c0d6fSJean-Christophe DUBOIS } 454a50c0d6fSJean-Christophe DUBOIS 45567110c3eSJean-Christophe DUBOIS static const TypeInfo imx_gpt_info = { 4565ec694b5SJean-Christophe DUBOIS .name = TYPE_IMX_GPT, 457a50c0d6fSJean-Christophe DUBOIS .parent = TYPE_SYS_BUS_DEVICE, 45867110c3eSJean-Christophe DUBOIS .instance_size = sizeof(IMXGPTState), 45967110c3eSJean-Christophe DUBOIS .class_init = imx_gpt_class_init, 460a50c0d6fSJean-Christophe DUBOIS }; 461a50c0d6fSJean-Christophe DUBOIS 46267110c3eSJean-Christophe DUBOIS static void imx_gpt_register_types(void) 463a50c0d6fSJean-Christophe DUBOIS { 46467110c3eSJean-Christophe DUBOIS type_register_static(&imx_gpt_info); 465a50c0d6fSJean-Christophe DUBOIS } 466a50c0d6fSJean-Christophe DUBOIS 46767110c3eSJean-Christophe DUBOIS type_init(imx_gpt_register_types) 468