1a50c0d6fSJean-Christophe DUBOIS /* 2a50c0d6fSJean-Christophe DUBOIS * IMX EPIT 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 8951cd00eSJean-Christophe Dubois * Updated by Jean-Christophe Dubois <jcd@tribudubois.net> 98d71beafSAxel Heider * Updated by Axel Heider 10a50c0d6fSJean-Christophe DUBOIS * 11a50c0d6fSJean-Christophe DUBOIS * This code is licensed under GPL version 2 or later. See 12a50c0d6fSJean-Christophe DUBOIS * the COPYING file in the top-level directory. 13a50c0d6fSJean-Christophe DUBOIS * 14a50c0d6fSJean-Christophe DUBOIS */ 15a50c0d6fSJean-Christophe DUBOIS 168ef94f0bSPeter Maydell #include "qemu/osdep.h" 17951cd00eSJean-Christophe Dubois #include "hw/timer/imx_epit.h" 18d6454270SMarkus Armbruster #include "migration/vmstate.h" 1964552b6bSMarkus Armbruster #include "hw/irq.h" 20951cd00eSJean-Christophe Dubois #include "hw/misc/imx_ccm.h" 210b8fa32fSMarkus Armbruster #include "qemu/module.h" 2203dd024fSPaolo Bonzini #include "qemu/log.h" 23a50c0d6fSJean-Christophe DUBOIS 244929f656SJean-Christophe Dubois #ifndef DEBUG_IMX_EPIT 254929f656SJean-Christophe Dubois #define DEBUG_IMX_EPIT 0 264929f656SJean-Christophe Dubois #endif 274929f656SJean-Christophe Dubois 284929f656SJean-Christophe Dubois #define DPRINTF(fmt, args...) \ 294929f656SJean-Christophe Dubois do { \ 304929f656SJean-Christophe Dubois if (DEBUG_IMX_EPIT) { \ 314929f656SJean-Christophe Dubois fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_EPIT, \ 324929f656SJean-Christophe Dubois __func__, ##args); \ 334929f656SJean-Christophe Dubois } \ 344929f656SJean-Christophe Dubois } while (0) 3595669e69SJean-Christophe DUBOIS 36d675765aSPeter Maydell static const char *imx_epit_reg_name(uint32_t reg) 3795669e69SJean-Christophe DUBOIS { 3895669e69SJean-Christophe DUBOIS switch (reg) { 3995669e69SJean-Christophe DUBOIS case 0: 4095669e69SJean-Christophe DUBOIS return "CR"; 4195669e69SJean-Christophe DUBOIS case 1: 4295669e69SJean-Christophe DUBOIS return "SR"; 4395669e69SJean-Christophe DUBOIS case 2: 4495669e69SJean-Christophe DUBOIS return "LR"; 4595669e69SJean-Christophe DUBOIS case 3: 4695669e69SJean-Christophe DUBOIS return "CMP"; 4795669e69SJean-Christophe DUBOIS case 4: 4895669e69SJean-Christophe DUBOIS return "CNT"; 4995669e69SJean-Christophe DUBOIS default: 5095669e69SJean-Christophe DUBOIS return "[?]"; 5195669e69SJean-Christophe DUBOIS } 5295669e69SJean-Christophe DUBOIS } 5395669e69SJean-Christophe DUBOIS 54a50c0d6fSJean-Christophe DUBOIS /* 55a50c0d6fSJean-Christophe DUBOIS * Exact clock frequencies vary from board to board. 56a50c0d6fSJean-Christophe DUBOIS * These are typical. 57a50c0d6fSJean-Christophe DUBOIS */ 5895669e69SJean-Christophe DUBOIS static const IMXClk imx_epit_clocks[] = { 59c91a5883SJean-Christophe Dubois CLK_NONE, /* 00 disabled */ 60aaa9ec3bSJean-Christophe Dubois CLK_IPG, /* 01 ipg_clk, ~532MHz */ 61d552f675SJean-Christophe Dubois CLK_IPG_HIGH, /* 10 ipg_clk_highfreq */ 62a50c0d6fSJean-Christophe DUBOIS CLK_32k, /* 11 ipg_clk_32k -- ~32kHz */ 63a50c0d6fSJean-Christophe DUBOIS }; 64a50c0d6fSJean-Christophe DUBOIS 65a50c0d6fSJean-Christophe DUBOIS /* 66a50c0d6fSJean-Christophe DUBOIS * Update interrupt status 67a50c0d6fSJean-Christophe DUBOIS */ 6895669e69SJean-Christophe DUBOIS static void imx_epit_update_int(IMXEPITState *s) 69a50c0d6fSJean-Christophe DUBOIS { 701ead962eSAxel Heider if ((s->sr & SR_OCIF) && (s->cr & CR_OCIEN) && (s->cr & CR_EN)) { 71a50c0d6fSJean-Christophe DUBOIS qemu_irq_raise(s->irq); 72a50c0d6fSJean-Christophe DUBOIS } else { 73a50c0d6fSJean-Christophe DUBOIS qemu_irq_lower(s->irq); 74a50c0d6fSJean-Christophe DUBOIS } 75a50c0d6fSJean-Christophe DUBOIS } 76a50c0d6fSJean-Christophe DUBOIS 77e662449aSAxel Heider static uint32_t imx_epit_get_freq(IMXEPITState *s) 78a50c0d6fSJean-Christophe DUBOIS { 79e662449aSAxel Heider uint32_t clksrc = extract32(s->cr, CR_CLKSRC_SHIFT, CR_CLKSRC_BITS); 80e662449aSAxel Heider uint32_t prescaler = 1 + extract32(s->cr, CR_PRESCALE_SHIFT, CR_PRESCALE_BITS); 81e662449aSAxel Heider uint32_t f_in = imx_ccm_get_clock_frequency(s->ccm, imx_epit_clocks[clksrc]); 82e662449aSAxel Heider uint32_t freq = f_in / prescaler; 83e662449aSAxel Heider DPRINTF("ptimer frequency is %u\n", freq); 84e662449aSAxel Heider return freq; 85a50c0d6fSJean-Christophe DUBOIS } 86a50c0d6fSJean-Christophe DUBOIS 87b9c993aaSAxel Heider /* 88b9c993aaSAxel Heider * This is called both on hardware (device) reset and software reset. 89b9c993aaSAxel Heider */ 903d461581SAxel Heider static void imx_epit_reset(IMXEPITState *s, bool is_hard_reset) 91a50c0d6fSJean-Christophe DUBOIS { 92b9c993aaSAxel Heider /* Soft reset doesn't touch some bits; hard reset clears them */ 933d461581SAxel Heider if (is_hard_reset) { 943d461581SAxel Heider s->cr = 0; 953d461581SAxel Heider } else { 9623005810SPeter Chubb s->cr &= (CR_EN|CR_ENMOD|CR_STOPEN|CR_DOZEN|CR_WAITEN|CR_DBGEN); 973d461581SAxel Heider } 98a50c0d6fSJean-Christophe DUBOIS s->sr = 0; 99203d65a4SMichael Tokarev s->lr = EPIT_TIMER_MAX; 100a50c0d6fSJean-Christophe DUBOIS s->cmp = 0; 101cc2722ecSPeter Maydell ptimer_transaction_begin(s->timer_cmp); 102cc2722ecSPeter Maydell ptimer_transaction_begin(s->timer_reload); 103e662449aSAxel Heider 104e662449aSAxel Heider /* 105e662449aSAxel Heider * The reset switches off the input clock, so even if the CR.EN is still 106e662449aSAxel Heider * set, the timers are no longer running. 107e662449aSAxel Heider */ 108e662449aSAxel Heider assert(imx_epit_get_freq(s) == 0); 109a50c0d6fSJean-Christophe DUBOIS ptimer_stop(s->timer_cmp); 110a50c0d6fSJean-Christophe DUBOIS ptimer_stop(s->timer_reload); 111203d65a4SMichael Tokarev /* init both timers to EPIT_TIMER_MAX */ 112203d65a4SMichael Tokarev ptimer_set_limit(s->timer_cmp, EPIT_TIMER_MAX, 1); 113203d65a4SMichael Tokarev ptimer_set_limit(s->timer_reload, EPIT_TIMER_MAX, 1); 114cc2722ecSPeter Maydell ptimer_transaction_commit(s->timer_cmp); 115cc2722ecSPeter Maydell ptimer_transaction_commit(s->timer_reload); 116a50c0d6fSJean-Christophe DUBOIS } 117a50c0d6fSJean-Christophe DUBOIS 11895669e69SJean-Christophe DUBOIS static uint64_t imx_epit_read(void *opaque, hwaddr offset, unsigned size) 119a50c0d6fSJean-Christophe DUBOIS { 12095669e69SJean-Christophe DUBOIS IMXEPITState *s = IMX_EPIT(opaque); 12195669e69SJean-Christophe DUBOIS uint32_t reg_value = 0; 122a50c0d6fSJean-Christophe DUBOIS 1234929f656SJean-Christophe Dubois switch (offset >> 2) { 124a50c0d6fSJean-Christophe DUBOIS case 0: /* Control Register */ 12595669e69SJean-Christophe DUBOIS reg_value = s->cr; 12695669e69SJean-Christophe DUBOIS break; 127a50c0d6fSJean-Christophe DUBOIS 128a50c0d6fSJean-Christophe DUBOIS case 1: /* Status Register */ 12995669e69SJean-Christophe DUBOIS reg_value = s->sr; 13095669e69SJean-Christophe DUBOIS break; 131a50c0d6fSJean-Christophe DUBOIS 132a50c0d6fSJean-Christophe DUBOIS case 2: /* LR - ticks*/ 13395669e69SJean-Christophe DUBOIS reg_value = s->lr; 13495669e69SJean-Christophe DUBOIS break; 135a50c0d6fSJean-Christophe DUBOIS 136a50c0d6fSJean-Christophe DUBOIS case 3: /* CMP */ 13795669e69SJean-Christophe DUBOIS reg_value = s->cmp; 13895669e69SJean-Christophe DUBOIS break; 139a50c0d6fSJean-Christophe DUBOIS 140a50c0d6fSJean-Christophe DUBOIS case 4: /* CNT */ 141e662449aSAxel Heider reg_value = ptimer_get_count(s->timer_reload); 14295669e69SJean-Christophe DUBOIS break; 14395669e69SJean-Christophe DUBOIS 14495669e69SJean-Christophe DUBOIS default: 1454929f656SJean-Christophe Dubois qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%" 1464929f656SJean-Christophe Dubois HWADDR_PRIx "\n", TYPE_IMX_EPIT, __func__, offset); 14795669e69SJean-Christophe DUBOIS break; 148a50c0d6fSJean-Christophe DUBOIS } 149a50c0d6fSJean-Christophe DUBOIS 1504929f656SJean-Christophe Dubois DPRINTF("(%s) = 0x%08x\n", imx_epit_reg_name(offset >> 2), reg_value); 15195669e69SJean-Christophe DUBOIS 15295669e69SJean-Christophe DUBOIS return reg_value; 153a50c0d6fSJean-Christophe DUBOIS } 154a50c0d6fSJean-Christophe DUBOIS 1558d71beafSAxel Heider /* 1568d71beafSAxel Heider * Must be called from a ptimer_transaction_begin/commit block for 1578d71beafSAxel Heider * s->timer_cmp, but outside of a transaction block of s->timer_reload, 1588d71beafSAxel Heider * so the proper counter value is read. 1598d71beafSAxel Heider */ 1608d71beafSAxel Heider static void imx_epit_update_compare_timer(IMXEPITState *s) 161a50c0d6fSJean-Christophe DUBOIS { 1628d71beafSAxel Heider uint64_t counter = 0; 1638d71beafSAxel Heider bool is_oneshot = false; 1648d71beafSAxel Heider /* 1658d71beafSAxel Heider * The compare timer only has to run if the timer peripheral is active 1668d71beafSAxel Heider * and there is an input clock, Otherwise it can be switched off. 1678d71beafSAxel Heider */ 1688d71beafSAxel Heider bool is_active = (s->cr & CR_EN) && imx_epit_get_freq(s); 1698d71beafSAxel Heider if (is_active) { 1708d71beafSAxel Heider /* 1718d71beafSAxel Heider * Calculate next timeout for compare timer. Reading the reload 1728d71beafSAxel Heider * counter returns proper results only if pending transactions 1738d71beafSAxel Heider * on it are committed here. Otherwise stale values are be read. 1748d71beafSAxel Heider */ 1758d71beafSAxel Heider counter = ptimer_get_count(s->timer_reload); 1768d71beafSAxel Heider uint64_t limit = ptimer_get_limit(s->timer_cmp); 1778d71beafSAxel Heider /* 1788d71beafSAxel Heider * The compare timer is a periodic timer if the limit is at least 1798d71beafSAxel Heider * the compare value. Otherwise it may fire at most once in the 1808d71beafSAxel Heider * current round. 1818d71beafSAxel Heider */ 18225d75817SAxel Heider is_oneshot = (limit < s->cmp); 1838d71beafSAxel Heider if (counter >= s->cmp) { 1848d71beafSAxel Heider /* The compare timer fires in the current round. */ 1858d71beafSAxel Heider counter -= s->cmp; 1868d71beafSAxel Heider } else if (!is_oneshot) { 1878d71beafSAxel Heider /* 1888d71beafSAxel Heider * The compare timer fires after a reload, as it is below the 1898d71beafSAxel Heider * compare value already in this round. Note that the counter 1908d71beafSAxel Heider * value calculated below can be above the 32-bit limit, which 1918d71beafSAxel Heider * is legal here because the compare timer is an internal 1928d71beafSAxel Heider * helper ptimer only. 1938d71beafSAxel Heider */ 1948d71beafSAxel Heider counter += limit - s->cmp; 1958d71beafSAxel Heider } else { 1968d71beafSAxel Heider /* 1978d71beafSAxel Heider * The compare timer won't fire in this round, and the limit is 1988d71beafSAxel Heider * set to a value below the compare value. This practically means 1998d71beafSAxel Heider * it will never fire, so it can be switched off. 2008d71beafSAxel Heider */ 2018d71beafSAxel Heider is_active = false; 202a50c0d6fSJean-Christophe DUBOIS } 203a50c0d6fSJean-Christophe DUBOIS } 2048d71beafSAxel Heider 2058d71beafSAxel Heider /* 2068d71beafSAxel Heider * Set the compare timer and let it run, or stop it. This is agnostic 2078d71beafSAxel Heider * of CR.OCIEN bit, as this bit affects interrupt generation only. The 2088d71beafSAxel Heider * compare timer needs to run even if no interrupts are to be generated, 2098d71beafSAxel Heider * because the SR.OCIF bit must be updated also. 2108d71beafSAxel Heider * Note that the timer might already be stopped or be running with 2118d71beafSAxel Heider * counter values. However, finding out when an update is needed and 2128d71beafSAxel Heider * when not is not trivial. It's much easier applying the setting again, 2138d71beafSAxel Heider * as this does not harm either and the overhead is negligible. 2148d71beafSAxel Heider */ 2158d71beafSAxel Heider if (is_active) { 2168d71beafSAxel Heider ptimer_set_count(s->timer_cmp, counter); 2178d71beafSAxel Heider ptimer_run(s->timer_cmp, is_oneshot ? 1 : 0); 2188d71beafSAxel Heider } else { 2198d71beafSAxel Heider ptimer_stop(s->timer_cmp); 2208d71beafSAxel Heider } 2218d71beafSAxel Heider 222a50c0d6fSJean-Christophe DUBOIS } 223a50c0d6fSJean-Christophe DUBOIS 224793a6ea0SAxel Heider static void imx_epit_write_cr(IMXEPITState *s, uint32_t value) 225a50c0d6fSJean-Christophe DUBOIS { 226793a6ea0SAxel Heider uint32_t oldcr = s->cr; 227a50c0d6fSJean-Christophe DUBOIS 228a50c0d6fSJean-Christophe DUBOIS s->cr = value & 0x03ffffff; 229793a6ea0SAxel Heider 230a50c0d6fSJean-Christophe DUBOIS if (s->cr & CR_SWR) { 2318d71beafSAxel Heider /* 2328d71beafSAxel Heider * Reset clears CR.SWR again. It does not touch CR.EN, but the timers 2338d71beafSAxel Heider * are still stopped because the input clock is disabled. 2348d71beafSAxel Heider */ 2353d461581SAxel Heider imx_epit_reset(s, false); 2368d71beafSAxel Heider } else { 2378d71beafSAxel Heider uint32_t freq; 2388d71beafSAxel Heider uint32_t toggled_cr_bits = oldcr ^ s->cr; 2398d71beafSAxel Heider /* re-initialize the limits if CR.RLD has changed */ 2408d71beafSAxel Heider bool set_limit = toggled_cr_bits & CR_RLD; 2418d71beafSAxel Heider /* set the counter if the timer got just enabled and CR.ENMOD is set */ 2428d71beafSAxel Heider bool is_switched_on = (toggled_cr_bits & s->cr) & CR_EN; 2438d71beafSAxel Heider bool set_counter = is_switched_on && (s->cr & CR_ENMOD); 2448d71beafSAxel Heider 2458d71beafSAxel Heider ptimer_transaction_begin(s->timer_cmp); 2468d71beafSAxel Heider ptimer_transaction_begin(s->timer_reload); 2478d71beafSAxel Heider freq = imx_epit_get_freq(s); 2488d71beafSAxel Heider if (freq) { 2498d71beafSAxel Heider ptimer_set_freq(s->timer_reload, freq); 2508d71beafSAxel Heider ptimer_set_freq(s->timer_cmp, freq); 2518d71beafSAxel Heider } 2528d71beafSAxel Heider 2538d71beafSAxel Heider if (set_limit || set_counter) { 2548d71beafSAxel Heider uint64_t limit = (s->cr & CR_RLD) ? s->lr : EPIT_TIMER_MAX; 2558d71beafSAxel Heider ptimer_set_limit(s->timer_reload, limit, set_counter ? 1 : 0); 2568d71beafSAxel Heider if (set_limit) { 2578d71beafSAxel Heider ptimer_set_limit(s->timer_cmp, limit, 0); 2588d71beafSAxel Heider } 2598d71beafSAxel Heider } 2608d71beafSAxel Heider /* 2618d71beafSAxel Heider * If there is an input clock and the peripheral is enabled, then 2628d71beafSAxel Heider * ensure the wall clock timer is ticking. Otherwise stop the timers. 2638d71beafSAxel Heider * The compare timer will be updated later. 2648d71beafSAxel Heider */ 2658d71beafSAxel Heider if (freq && (s->cr & CR_EN)) { 2668d71beafSAxel Heider ptimer_run(s->timer_reload, 0); 2678d71beafSAxel Heider } else { 2688d71beafSAxel Heider ptimer_stop(s->timer_reload); 2698d71beafSAxel Heider } 2708d71beafSAxel Heider /* Commit changes to reload timer, so they can propagate. */ 2718d71beafSAxel Heider ptimer_transaction_commit(s->timer_reload); 2728d71beafSAxel Heider /* Update compare timer based on the committed reload timer value. */ 2738d71beafSAxel Heider imx_epit_update_compare_timer(s); 2748d71beafSAxel Heider ptimer_transaction_commit(s->timer_cmp); 2752ca267fdSAxel Heider } 2762ca267fdSAxel Heider 27713557fd3SPeter Maydell /* 2782ca267fdSAxel Heider * The interrupt state can change due to: 2792ca267fdSAxel Heider * - reset clears both SR.OCIF and CR.OCIE 2802ca267fdSAxel Heider * - write to CR.EN or CR.OCIE 2812ca267fdSAxel Heider */ 2822ca267fdSAxel Heider imx_epit_update_int(s); 283793a6ea0SAxel Heider } 284a50c0d6fSJean-Christophe DUBOIS 285793a6ea0SAxel Heider static void imx_epit_write_sr(IMXEPITState *s, uint32_t value) 286793a6ea0SAxel Heider { 2871ead962eSAxel Heider /* writing 1 to SR.OCIF clears this bit and turns the interrupt off */ 2881ead962eSAxel Heider if (value & SR_OCIF) { 2891ead962eSAxel Heider s->sr = 0; /* SR.OCIF is the only bit in this register anyway */ 29095669e69SJean-Christophe DUBOIS imx_epit_update_int(s); 291a50c0d6fSJean-Christophe DUBOIS } 292793a6ea0SAxel Heider } 293a50c0d6fSJean-Christophe DUBOIS 294793a6ea0SAxel Heider static void imx_epit_write_lr(IMXEPITState *s, uint32_t value) 295793a6ea0SAxel Heider { 296a50c0d6fSJean-Christophe DUBOIS s->lr = value; 297a50c0d6fSJean-Christophe DUBOIS 298cc2722ecSPeter Maydell ptimer_transaction_begin(s->timer_cmp); 299cc2722ecSPeter Maydell ptimer_transaction_begin(s->timer_reload); 300a50c0d6fSJean-Christophe DUBOIS if (s->cr & CR_RLD) { 301a50c0d6fSJean-Christophe DUBOIS /* Also set the limit if the LRD bit is set */ 302a50c0d6fSJean-Christophe DUBOIS /* If IOVW bit is set then set the timer value */ 303a50c0d6fSJean-Christophe DUBOIS ptimer_set_limit(s->timer_reload, s->lr, s->cr & CR_IOVW); 30423005810SPeter Chubb ptimer_set_limit(s->timer_cmp, s->lr, 0); 305a50c0d6fSJean-Christophe DUBOIS } else if (s->cr & CR_IOVW) { 306a50c0d6fSJean-Christophe DUBOIS /* If IOVW bit is set then set the timer value */ 307a50c0d6fSJean-Christophe DUBOIS ptimer_set_count(s->timer_reload, s->lr); 308a50c0d6fSJean-Christophe DUBOIS } 3098d71beafSAxel Heider /* Commit the changes to s->timer_reload, so they can propagate. */ 3107719419dSAxel Heider ptimer_transaction_commit(s->timer_reload); 3118d71beafSAxel Heider /* Update the compare timer based on the committed reload timer value. */ 3128d71beafSAxel Heider imx_epit_update_compare_timer(s); 313cc2722ecSPeter Maydell ptimer_transaction_commit(s->timer_cmp); 314793a6ea0SAxel Heider } 315a50c0d6fSJean-Christophe DUBOIS 316793a6ea0SAxel Heider static void imx_epit_write_cmp(IMXEPITState *s, uint32_t value) 317793a6ea0SAxel Heider { 318a50c0d6fSJean-Christophe DUBOIS s->cmp = value; 319a50c0d6fSJean-Christophe DUBOIS 3208d71beafSAxel Heider /* Update the compare timer based on the committed reload timer value. */ 321cc2722ecSPeter Maydell ptimer_transaction_begin(s->timer_cmp); 3228d71beafSAxel Heider imx_epit_update_compare_timer(s); 323cc2722ecSPeter Maydell ptimer_transaction_commit(s->timer_cmp); 324793a6ea0SAxel Heider } 325a50c0d6fSJean-Christophe DUBOIS 326793a6ea0SAxel Heider static void imx_epit_write(void *opaque, hwaddr offset, uint64_t value, 327793a6ea0SAxel Heider unsigned size) 328793a6ea0SAxel Heider { 329793a6ea0SAxel Heider IMXEPITState *s = IMX_EPIT(opaque); 330793a6ea0SAxel Heider 331793a6ea0SAxel Heider DPRINTF("(%s, value = 0x%08x)\n", imx_epit_reg_name(offset >> 2), 332793a6ea0SAxel Heider (uint32_t)value); 333793a6ea0SAxel Heider 334793a6ea0SAxel Heider switch (offset >> 2) { 335793a6ea0SAxel Heider case 0: /* CR */ 336793a6ea0SAxel Heider imx_epit_write_cr(s, (uint32_t)value); 337793a6ea0SAxel Heider break; 338793a6ea0SAxel Heider 339793a6ea0SAxel Heider case 1: /* SR */ 340793a6ea0SAxel Heider imx_epit_write_sr(s, (uint32_t)value); 341793a6ea0SAxel Heider break; 342793a6ea0SAxel Heider 343793a6ea0SAxel Heider case 2: /* LR */ 344793a6ea0SAxel Heider imx_epit_write_lr(s, (uint32_t)value); 345793a6ea0SAxel Heider break; 346793a6ea0SAxel Heider 347793a6ea0SAxel Heider case 3: /* CMP */ 348793a6ea0SAxel Heider imx_epit_write_cmp(s, (uint32_t)value); 349a50c0d6fSJean-Christophe DUBOIS break; 350a50c0d6fSJean-Christophe DUBOIS 351a50c0d6fSJean-Christophe DUBOIS default: 3524929f656SJean-Christophe Dubois qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%" 3534929f656SJean-Christophe Dubois HWADDR_PRIx "\n", TYPE_IMX_EPIT, __func__, offset); 35495669e69SJean-Christophe DUBOIS break; 355a50c0d6fSJean-Christophe DUBOIS } 356a50c0d6fSJean-Christophe DUBOIS } 357793a6ea0SAxel Heider 35895669e69SJean-Christophe DUBOIS static void imx_epit_cmp(void *opaque) 359a50c0d6fSJean-Christophe DUBOIS { 36095669e69SJean-Christophe DUBOIS IMXEPITState *s = IMX_EPIT(opaque); 361a50c0d6fSJean-Christophe DUBOIS 3628d71beafSAxel Heider /* The cmp ptimer can't be running when the peripheral is disabled */ 3638d71beafSAxel Heider assert(s->cr & CR_EN); 3648d71beafSAxel Heider 36523005810SPeter Chubb DPRINTF("sr was %d\n", s->sr); 3661ead962eSAxel Heider /* Set interrupt status bit SR.OCIF and update the interrupt state */ 3671ead962eSAxel Heider s->sr |= SR_OCIF; 36895669e69SJean-Christophe DUBOIS imx_epit_update_int(s); 369a50c0d6fSJean-Christophe DUBOIS } 370a50c0d6fSJean-Christophe DUBOIS 371cc2722ecSPeter Maydell static void imx_epit_reload(void *opaque) 372cc2722ecSPeter Maydell { 373cc2722ecSPeter Maydell /* No action required on rollover of timer_reload */ 374cc2722ecSPeter Maydell } 375cc2722ecSPeter Maydell 37695669e69SJean-Christophe DUBOIS static const MemoryRegionOps imx_epit_ops = { 37795669e69SJean-Christophe DUBOIS .read = imx_epit_read, 37895669e69SJean-Christophe DUBOIS .write = imx_epit_write, 379a50c0d6fSJean-Christophe DUBOIS .endianness = DEVICE_NATIVE_ENDIAN, 380a50c0d6fSJean-Christophe DUBOIS }; 381a50c0d6fSJean-Christophe DUBOIS 38295669e69SJean-Christophe DUBOIS static const VMStateDescription vmstate_imx_timer_epit = { 383565328fcSJean-Christophe Dubois .name = TYPE_IMX_EPIT, 384e662449aSAxel Heider .version_id = 3, 385e662449aSAxel Heider .minimum_version_id = 3, 386ba324b3fSRichard Henderson .fields = (const VMStateField[]) { 38795669e69SJean-Christophe DUBOIS VMSTATE_UINT32(cr, IMXEPITState), 38895669e69SJean-Christophe DUBOIS VMSTATE_UINT32(sr, IMXEPITState), 38995669e69SJean-Christophe DUBOIS VMSTATE_UINT32(lr, IMXEPITState), 39095669e69SJean-Christophe DUBOIS VMSTATE_UINT32(cmp, IMXEPITState), 39195669e69SJean-Christophe DUBOIS VMSTATE_PTIMER(timer_reload, IMXEPITState), 39295669e69SJean-Christophe DUBOIS VMSTATE_PTIMER(timer_cmp, IMXEPITState), 393a50c0d6fSJean-Christophe DUBOIS VMSTATE_END_OF_LIST() 394a50c0d6fSJean-Christophe DUBOIS } 395a50c0d6fSJean-Christophe DUBOIS }; 396a50c0d6fSJean-Christophe DUBOIS 39795669e69SJean-Christophe DUBOIS static void imx_epit_realize(DeviceState *dev, Error **errp) 398a50c0d6fSJean-Christophe DUBOIS { 39995669e69SJean-Christophe DUBOIS IMXEPITState *s = IMX_EPIT(dev); 40095669e69SJean-Christophe DUBOIS SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 401a50c0d6fSJean-Christophe DUBOIS 40295669e69SJean-Christophe DUBOIS DPRINTF("\n"); 403a50c0d6fSJean-Christophe DUBOIS 40495669e69SJean-Christophe DUBOIS sysbus_init_irq(sbd, &s->irq); 405853dca12SPaolo Bonzini memory_region_init_io(&s->iomem, OBJECT(s), &imx_epit_ops, s, TYPE_IMX_EPIT, 40695669e69SJean-Christophe DUBOIS 0x00001000); 40795669e69SJean-Christophe DUBOIS sysbus_init_mmio(sbd, &s->iomem); 40895669e69SJean-Christophe DUBOIS 409b9c993aaSAxel Heider /* 410b9c993aaSAxel Heider * The reload timer keeps running when the peripheral is enabled. It is a 411b9c993aaSAxel Heider * kind of wall clock that does not generate any interrupts. The callback 412b9c993aaSAxel Heider * needs to be provided, but it does nothing as the ptimer already supports 413b9c993aaSAxel Heider * all necessary reloading functionality. 414b9c993aaSAxel Heider */ 4159598c1bbSPeter Maydell s->timer_reload = ptimer_init(imx_epit_reload, s, PTIMER_POLICY_LEGACY); 416a50c0d6fSJean-Christophe DUBOIS 417b9c993aaSAxel Heider /* 418b9c993aaSAxel Heider * The compare timer is running only when the peripheral configuration is 419b9c993aaSAxel Heider * in a state that will generate compare interrupts. 420b9c993aaSAxel Heider */ 4219598c1bbSPeter Maydell s->timer_cmp = ptimer_init(imx_epit_cmp, s, PTIMER_POLICY_LEGACY); 422a50c0d6fSJean-Christophe DUBOIS } 423a50c0d6fSJean-Christophe DUBOIS 4243d461581SAxel Heider static void imx_epit_dev_reset(DeviceState *dev) 4253d461581SAxel Heider { 4263d461581SAxel Heider IMXEPITState *s = IMX_EPIT(dev); 4273d461581SAxel Heider imx_epit_reset(s, true); 4283d461581SAxel Heider } 4293d461581SAxel Heider 43095669e69SJean-Christophe DUBOIS static void imx_epit_class_init(ObjectClass *klass, void *data) 431a50c0d6fSJean-Christophe DUBOIS { 432a50c0d6fSJean-Christophe DUBOIS DeviceClass *dc = DEVICE_CLASS(klass); 43395669e69SJean-Christophe DUBOIS 43495669e69SJean-Christophe DUBOIS dc->realize = imx_epit_realize; 435*e3d08143SPeter Maydell device_class_set_legacy_reset(dc, imx_epit_dev_reset); 43695669e69SJean-Christophe DUBOIS dc->vmsd = &vmstate_imx_timer_epit; 437a50c0d6fSJean-Christophe DUBOIS dc->desc = "i.MX periodic timer"; 438a50c0d6fSJean-Christophe DUBOIS } 439a50c0d6fSJean-Christophe DUBOIS 44095669e69SJean-Christophe DUBOIS static const TypeInfo imx_epit_info = { 44195669e69SJean-Christophe DUBOIS .name = TYPE_IMX_EPIT, 442a50c0d6fSJean-Christophe DUBOIS .parent = TYPE_SYS_BUS_DEVICE, 44395669e69SJean-Christophe DUBOIS .instance_size = sizeof(IMXEPITState), 44495669e69SJean-Christophe DUBOIS .class_init = imx_epit_class_init, 445a50c0d6fSJean-Christophe DUBOIS }; 446a50c0d6fSJean-Christophe DUBOIS 44795669e69SJean-Christophe DUBOIS static void imx_epit_register_types(void) 448a50c0d6fSJean-Christophe DUBOIS { 44995669e69SJean-Christophe DUBOIS type_register_static(&imx_epit_info); 450a50c0d6fSJean-Christophe DUBOIS } 451a50c0d6fSJean-Christophe DUBOIS 45295669e69SJean-Christophe DUBOIS type_init(imx_epit_register_types) 453