13bd88451SPaolo Bonzini /* 23bd88451SPaolo Bonzini * Intel XScale PXA255/270 OS Timers. 33bd88451SPaolo Bonzini * 43bd88451SPaolo Bonzini * Copyright (c) 2006 Openedhand Ltd. 53bd88451SPaolo Bonzini * Copyright (c) 2006 Thorsten Zitterell 63bd88451SPaolo Bonzini * 73bd88451SPaolo Bonzini * This code is licensed under the GPL. 83bd88451SPaolo Bonzini */ 93bd88451SPaolo Bonzini 108ef94f0bSPeter Maydell #include "qemu/osdep.h" 113bd88451SPaolo Bonzini #include "hw/hw.h" 123bd88451SPaolo Bonzini #include "qemu/timer.h" 133bd88451SPaolo Bonzini #include "sysemu/sysemu.h" 143bd88451SPaolo Bonzini #include "hw/arm/pxa.h" 153bd88451SPaolo Bonzini #include "hw/sysbus.h" 163bd88451SPaolo Bonzini 173bd88451SPaolo Bonzini #define OSMR0 0x00 183bd88451SPaolo Bonzini #define OSMR1 0x04 193bd88451SPaolo Bonzini #define OSMR2 0x08 203bd88451SPaolo Bonzini #define OSMR3 0x0c 213bd88451SPaolo Bonzini #define OSMR4 0x80 223bd88451SPaolo Bonzini #define OSMR5 0x84 233bd88451SPaolo Bonzini #define OSMR6 0x88 243bd88451SPaolo Bonzini #define OSMR7 0x8c 253bd88451SPaolo Bonzini #define OSMR8 0x90 263bd88451SPaolo Bonzini #define OSMR9 0x94 273bd88451SPaolo Bonzini #define OSMR10 0x98 283bd88451SPaolo Bonzini #define OSMR11 0x9c 293bd88451SPaolo Bonzini #define OSCR 0x10 /* OS Timer Count */ 303bd88451SPaolo Bonzini #define OSCR4 0x40 313bd88451SPaolo Bonzini #define OSCR5 0x44 323bd88451SPaolo Bonzini #define OSCR6 0x48 333bd88451SPaolo Bonzini #define OSCR7 0x4c 343bd88451SPaolo Bonzini #define OSCR8 0x50 353bd88451SPaolo Bonzini #define OSCR9 0x54 363bd88451SPaolo Bonzini #define OSCR10 0x58 373bd88451SPaolo Bonzini #define OSCR11 0x5c 383bd88451SPaolo Bonzini #define OSSR 0x14 /* Timer status register */ 393bd88451SPaolo Bonzini #define OWER 0x18 403bd88451SPaolo Bonzini #define OIER 0x1c /* Interrupt enable register 3-0 to E3-E0 */ 413bd88451SPaolo Bonzini #define OMCR4 0xc0 /* OS Match Control registers */ 423bd88451SPaolo Bonzini #define OMCR5 0xc4 433bd88451SPaolo Bonzini #define OMCR6 0xc8 443bd88451SPaolo Bonzini #define OMCR7 0xcc 453bd88451SPaolo Bonzini #define OMCR8 0xd0 463bd88451SPaolo Bonzini #define OMCR9 0xd4 473bd88451SPaolo Bonzini #define OMCR10 0xd8 483bd88451SPaolo Bonzini #define OMCR11 0xdc 493bd88451SPaolo Bonzini #define OSNR 0x20 503bd88451SPaolo Bonzini 513bd88451SPaolo Bonzini #define PXA25X_FREQ 3686400 /* 3.6864 MHz */ 523bd88451SPaolo Bonzini #define PXA27X_FREQ 3250000 /* 3.25 MHz */ 533bd88451SPaolo Bonzini 543bd88451SPaolo Bonzini static int pxa2xx_timer4_freq[8] = { 553bd88451SPaolo Bonzini [0] = 0, 563bd88451SPaolo Bonzini [1] = 32768, 573bd88451SPaolo Bonzini [2] = 1000, 583bd88451SPaolo Bonzini [3] = 1, 593bd88451SPaolo Bonzini [4] = 1000000, 603bd88451SPaolo Bonzini /* [5] is the "Externally supplied clock". Assign if necessary. */ 613bd88451SPaolo Bonzini [5 ... 7] = 0, 623bd88451SPaolo Bonzini }; 633bd88451SPaolo Bonzini 64feea4361SAndreas Färber #define TYPE_PXA2XX_TIMER "pxa2xx-timer" 65feea4361SAndreas Färber #define PXA2XX_TIMER(obj) \ 66feea4361SAndreas Färber OBJECT_CHECK(PXA2xxTimerInfo, (obj), TYPE_PXA2XX_TIMER) 67feea4361SAndreas Färber 683bd88451SPaolo Bonzini typedef struct PXA2xxTimerInfo PXA2xxTimerInfo; 693bd88451SPaolo Bonzini 703bd88451SPaolo Bonzini typedef struct { 713bd88451SPaolo Bonzini uint32_t value; 723bd88451SPaolo Bonzini qemu_irq irq; 733bd88451SPaolo Bonzini QEMUTimer *qtimer; 743bd88451SPaolo Bonzini int num; 753bd88451SPaolo Bonzini PXA2xxTimerInfo *info; 763bd88451SPaolo Bonzini } PXA2xxTimer0; 773bd88451SPaolo Bonzini 783bd88451SPaolo Bonzini typedef struct { 793bd88451SPaolo Bonzini PXA2xxTimer0 tm; 803bd88451SPaolo Bonzini int32_t oldclock; 813bd88451SPaolo Bonzini int32_t clock; 823bd88451SPaolo Bonzini uint64_t lastload; 833bd88451SPaolo Bonzini uint32_t freq; 843bd88451SPaolo Bonzini uint32_t control; 853bd88451SPaolo Bonzini } PXA2xxTimer4; 863bd88451SPaolo Bonzini 873bd88451SPaolo Bonzini struct PXA2xxTimerInfo { 88feea4361SAndreas Färber SysBusDevice parent_obj; 89feea4361SAndreas Färber 903bd88451SPaolo Bonzini MemoryRegion iomem; 913bd88451SPaolo Bonzini uint32_t flags; 923bd88451SPaolo Bonzini 933bd88451SPaolo Bonzini int32_t clock; 943bd88451SPaolo Bonzini int32_t oldclock; 953bd88451SPaolo Bonzini uint64_t lastload; 963bd88451SPaolo Bonzini uint32_t freq; 973bd88451SPaolo Bonzini PXA2xxTimer0 timer[4]; 983bd88451SPaolo Bonzini uint32_t events; 993bd88451SPaolo Bonzini uint32_t irq_enabled; 1003bd88451SPaolo Bonzini uint32_t reset3; 1013bd88451SPaolo Bonzini uint32_t snapshot; 1023bd88451SPaolo Bonzini 1033bd88451SPaolo Bonzini qemu_irq irq4; 1043bd88451SPaolo Bonzini PXA2xxTimer4 tm4[8]; 1053bd88451SPaolo Bonzini }; 1063bd88451SPaolo Bonzini 1073bd88451SPaolo Bonzini #define PXA2XX_TIMER_HAVE_TM4 0 1083bd88451SPaolo Bonzini 1093bd88451SPaolo Bonzini static inline int pxa2xx_timer_has_tm4(PXA2xxTimerInfo *s) 1103bd88451SPaolo Bonzini { 1113bd88451SPaolo Bonzini return s->flags & (1 << PXA2XX_TIMER_HAVE_TM4); 1123bd88451SPaolo Bonzini } 1133bd88451SPaolo Bonzini 1143bd88451SPaolo Bonzini static void pxa2xx_timer_update(void *opaque, uint64_t now_qemu) 1153bd88451SPaolo Bonzini { 1163bd88451SPaolo Bonzini PXA2xxTimerInfo *s = (PXA2xxTimerInfo *) opaque; 1173bd88451SPaolo Bonzini int i; 1183bd88451SPaolo Bonzini uint32_t now_vm; 1193bd88451SPaolo Bonzini uint64_t new_qemu; 1203bd88451SPaolo Bonzini 1213bd88451SPaolo Bonzini now_vm = s->clock + 1223bd88451SPaolo Bonzini muldiv64(now_qemu - s->lastload, s->freq, get_ticks_per_sec()); 1233bd88451SPaolo Bonzini 1243bd88451SPaolo Bonzini for (i = 0; i < 4; i ++) { 1253bd88451SPaolo Bonzini new_qemu = now_qemu + muldiv64((uint32_t) (s->timer[i].value - now_vm), 1263bd88451SPaolo Bonzini get_ticks_per_sec(), s->freq); 127bc72ad67SAlex Bligh timer_mod(s->timer[i].qtimer, new_qemu); 1283bd88451SPaolo Bonzini } 1293bd88451SPaolo Bonzini } 1303bd88451SPaolo Bonzini 1313bd88451SPaolo Bonzini static void pxa2xx_timer_update4(void *opaque, uint64_t now_qemu, int n) 1323bd88451SPaolo Bonzini { 1333bd88451SPaolo Bonzini PXA2xxTimerInfo *s = (PXA2xxTimerInfo *) opaque; 1343bd88451SPaolo Bonzini uint32_t now_vm; 1353bd88451SPaolo Bonzini uint64_t new_qemu; 1363bd88451SPaolo Bonzini static const int counters[8] = { 0, 0, 0, 0, 4, 4, 6, 6 }; 1373bd88451SPaolo Bonzini int counter; 1383bd88451SPaolo Bonzini 1393bd88451SPaolo Bonzini if (s->tm4[n].control & (1 << 7)) 1403bd88451SPaolo Bonzini counter = n; 1413bd88451SPaolo Bonzini else 1423bd88451SPaolo Bonzini counter = counters[n]; 1433bd88451SPaolo Bonzini 1443bd88451SPaolo Bonzini if (!s->tm4[counter].freq) { 145bc72ad67SAlex Bligh timer_del(s->tm4[n].tm.qtimer); 1463bd88451SPaolo Bonzini return; 1473bd88451SPaolo Bonzini } 1483bd88451SPaolo Bonzini 1493bd88451SPaolo Bonzini now_vm = s->tm4[counter].clock + muldiv64(now_qemu - 1503bd88451SPaolo Bonzini s->tm4[counter].lastload, 1513bd88451SPaolo Bonzini s->tm4[counter].freq, get_ticks_per_sec()); 1523bd88451SPaolo Bonzini 1533bd88451SPaolo Bonzini new_qemu = now_qemu + muldiv64((uint32_t) (s->tm4[n].tm.value - now_vm), 1543bd88451SPaolo Bonzini get_ticks_per_sec(), s->tm4[counter].freq); 155bc72ad67SAlex Bligh timer_mod(s->tm4[n].tm.qtimer, new_qemu); 1563bd88451SPaolo Bonzini } 1573bd88451SPaolo Bonzini 1583bd88451SPaolo Bonzini static uint64_t pxa2xx_timer_read(void *opaque, hwaddr offset, 1593bd88451SPaolo Bonzini unsigned size) 1603bd88451SPaolo Bonzini { 1613bd88451SPaolo Bonzini PXA2xxTimerInfo *s = (PXA2xxTimerInfo *) opaque; 1623bd88451SPaolo Bonzini int tm = 0; 1633bd88451SPaolo Bonzini 1643bd88451SPaolo Bonzini switch (offset) { 1653bd88451SPaolo Bonzini case OSMR3: tm ++; 1663bd88451SPaolo Bonzini /* fall through */ 1673bd88451SPaolo Bonzini case OSMR2: tm ++; 1683bd88451SPaolo Bonzini /* fall through */ 1693bd88451SPaolo Bonzini case OSMR1: tm ++; 1703bd88451SPaolo Bonzini /* fall through */ 1713bd88451SPaolo Bonzini case OSMR0: 1723bd88451SPaolo Bonzini return s->timer[tm].value; 1733bd88451SPaolo Bonzini case OSMR11: tm ++; 1743bd88451SPaolo Bonzini /* fall through */ 1753bd88451SPaolo Bonzini case OSMR10: tm ++; 1763bd88451SPaolo Bonzini /* fall through */ 1773bd88451SPaolo Bonzini case OSMR9: tm ++; 1783bd88451SPaolo Bonzini /* fall through */ 1793bd88451SPaolo Bonzini case OSMR8: tm ++; 1803bd88451SPaolo Bonzini /* fall through */ 1813bd88451SPaolo Bonzini case OSMR7: tm ++; 1823bd88451SPaolo Bonzini /* fall through */ 1833bd88451SPaolo Bonzini case OSMR6: tm ++; 1843bd88451SPaolo Bonzini /* fall through */ 1853bd88451SPaolo Bonzini case OSMR5: tm ++; 1863bd88451SPaolo Bonzini /* fall through */ 1873bd88451SPaolo Bonzini case OSMR4: 1883bd88451SPaolo Bonzini if (!pxa2xx_timer_has_tm4(s)) 1893bd88451SPaolo Bonzini goto badreg; 1903bd88451SPaolo Bonzini return s->tm4[tm].tm.value; 1913bd88451SPaolo Bonzini case OSCR: 192bc72ad67SAlex Bligh return s->clock + muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - 1933bd88451SPaolo Bonzini s->lastload, s->freq, get_ticks_per_sec()); 1943bd88451SPaolo Bonzini case OSCR11: tm ++; 1953bd88451SPaolo Bonzini /* fall through */ 1963bd88451SPaolo Bonzini case OSCR10: tm ++; 1973bd88451SPaolo Bonzini /* fall through */ 1983bd88451SPaolo Bonzini case OSCR9: tm ++; 1993bd88451SPaolo Bonzini /* fall through */ 2003bd88451SPaolo Bonzini case OSCR8: tm ++; 2013bd88451SPaolo Bonzini /* fall through */ 2023bd88451SPaolo Bonzini case OSCR7: tm ++; 2033bd88451SPaolo Bonzini /* fall through */ 2043bd88451SPaolo Bonzini case OSCR6: tm ++; 2053bd88451SPaolo Bonzini /* fall through */ 2063bd88451SPaolo Bonzini case OSCR5: tm ++; 2073bd88451SPaolo Bonzini /* fall through */ 2083bd88451SPaolo Bonzini case OSCR4: 2093bd88451SPaolo Bonzini if (!pxa2xx_timer_has_tm4(s)) 2103bd88451SPaolo Bonzini goto badreg; 2113bd88451SPaolo Bonzini 2123bd88451SPaolo Bonzini if ((tm == 9 - 4 || tm == 11 - 4) && (s->tm4[tm].control & (1 << 9))) { 2133bd88451SPaolo Bonzini if (s->tm4[tm - 1].freq) 2143bd88451SPaolo Bonzini s->snapshot = s->tm4[tm - 1].clock + muldiv64( 215bc72ad67SAlex Bligh qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - 2163bd88451SPaolo Bonzini s->tm4[tm - 1].lastload, 2173bd88451SPaolo Bonzini s->tm4[tm - 1].freq, get_ticks_per_sec()); 2183bd88451SPaolo Bonzini else 2193bd88451SPaolo Bonzini s->snapshot = s->tm4[tm - 1].clock; 2203bd88451SPaolo Bonzini } 2213bd88451SPaolo Bonzini 2223bd88451SPaolo Bonzini if (!s->tm4[tm].freq) 2233bd88451SPaolo Bonzini return s->tm4[tm].clock; 224bc72ad67SAlex Bligh return s->tm4[tm].clock + muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - 2253bd88451SPaolo Bonzini s->tm4[tm].lastload, s->tm4[tm].freq, get_ticks_per_sec()); 2263bd88451SPaolo Bonzini case OIER: 2273bd88451SPaolo Bonzini return s->irq_enabled; 2283bd88451SPaolo Bonzini case OSSR: /* Status register */ 2293bd88451SPaolo Bonzini return s->events; 2303bd88451SPaolo Bonzini case OWER: 2313bd88451SPaolo Bonzini return s->reset3; 2323bd88451SPaolo Bonzini case OMCR11: tm ++; 2333bd88451SPaolo Bonzini /* fall through */ 2343bd88451SPaolo Bonzini case OMCR10: tm ++; 2353bd88451SPaolo Bonzini /* fall through */ 2363bd88451SPaolo Bonzini case OMCR9: tm ++; 2373bd88451SPaolo Bonzini /* fall through */ 2383bd88451SPaolo Bonzini case OMCR8: tm ++; 2393bd88451SPaolo Bonzini /* fall through */ 2403bd88451SPaolo Bonzini case OMCR7: tm ++; 2413bd88451SPaolo Bonzini /* fall through */ 2423bd88451SPaolo Bonzini case OMCR6: tm ++; 2433bd88451SPaolo Bonzini /* fall through */ 2443bd88451SPaolo Bonzini case OMCR5: tm ++; 2453bd88451SPaolo Bonzini /* fall through */ 2463bd88451SPaolo Bonzini case OMCR4: 2473bd88451SPaolo Bonzini if (!pxa2xx_timer_has_tm4(s)) 2483bd88451SPaolo Bonzini goto badreg; 2493bd88451SPaolo Bonzini return s->tm4[tm].control; 2503bd88451SPaolo Bonzini case OSNR: 2513bd88451SPaolo Bonzini return s->snapshot; 2523bd88451SPaolo Bonzini default: 2533bd88451SPaolo Bonzini badreg: 2543bd88451SPaolo Bonzini hw_error("pxa2xx_timer_read: Bad offset " REG_FMT "\n", offset); 2553bd88451SPaolo Bonzini } 2563bd88451SPaolo Bonzini 2573bd88451SPaolo Bonzini return 0; 2583bd88451SPaolo Bonzini } 2593bd88451SPaolo Bonzini 2603bd88451SPaolo Bonzini static void pxa2xx_timer_write(void *opaque, hwaddr offset, 2613bd88451SPaolo Bonzini uint64_t value, unsigned size) 2623bd88451SPaolo Bonzini { 2633bd88451SPaolo Bonzini int i, tm = 0; 2643bd88451SPaolo Bonzini PXA2xxTimerInfo *s = (PXA2xxTimerInfo *) opaque; 2653bd88451SPaolo Bonzini 2663bd88451SPaolo Bonzini switch (offset) { 2673bd88451SPaolo Bonzini case OSMR3: tm ++; 2683bd88451SPaolo Bonzini /* fall through */ 2693bd88451SPaolo Bonzini case OSMR2: tm ++; 2703bd88451SPaolo Bonzini /* fall through */ 2713bd88451SPaolo Bonzini case OSMR1: tm ++; 2723bd88451SPaolo Bonzini /* fall through */ 2733bd88451SPaolo Bonzini case OSMR0: 2743bd88451SPaolo Bonzini s->timer[tm].value = value; 275bc72ad67SAlex Bligh pxa2xx_timer_update(s, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)); 2763bd88451SPaolo Bonzini break; 2773bd88451SPaolo Bonzini case OSMR11: tm ++; 2783bd88451SPaolo Bonzini /* fall through */ 2793bd88451SPaolo Bonzini case OSMR10: tm ++; 2803bd88451SPaolo Bonzini /* fall through */ 2813bd88451SPaolo Bonzini case OSMR9: tm ++; 2823bd88451SPaolo Bonzini /* fall through */ 2833bd88451SPaolo Bonzini case OSMR8: tm ++; 2843bd88451SPaolo Bonzini /* fall through */ 2853bd88451SPaolo Bonzini case OSMR7: tm ++; 2863bd88451SPaolo Bonzini /* fall through */ 2873bd88451SPaolo Bonzini case OSMR6: tm ++; 2883bd88451SPaolo Bonzini /* fall through */ 2893bd88451SPaolo Bonzini case OSMR5: tm ++; 2903bd88451SPaolo Bonzini /* fall through */ 2913bd88451SPaolo Bonzini case OSMR4: 2923bd88451SPaolo Bonzini if (!pxa2xx_timer_has_tm4(s)) 2933bd88451SPaolo Bonzini goto badreg; 2943bd88451SPaolo Bonzini s->tm4[tm].tm.value = value; 295bc72ad67SAlex Bligh pxa2xx_timer_update4(s, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tm); 2963bd88451SPaolo Bonzini break; 2973bd88451SPaolo Bonzini case OSCR: 2983bd88451SPaolo Bonzini s->oldclock = s->clock; 299bc72ad67SAlex Bligh s->lastload = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 3003bd88451SPaolo Bonzini s->clock = value; 3013bd88451SPaolo Bonzini pxa2xx_timer_update(s, s->lastload); 3023bd88451SPaolo Bonzini break; 3033bd88451SPaolo Bonzini case OSCR11: tm ++; 3043bd88451SPaolo Bonzini /* fall through */ 3053bd88451SPaolo Bonzini case OSCR10: tm ++; 3063bd88451SPaolo Bonzini /* fall through */ 3073bd88451SPaolo Bonzini case OSCR9: tm ++; 3083bd88451SPaolo Bonzini /* fall through */ 3093bd88451SPaolo Bonzini case OSCR8: tm ++; 3103bd88451SPaolo Bonzini /* fall through */ 3113bd88451SPaolo Bonzini case OSCR7: tm ++; 3123bd88451SPaolo Bonzini /* fall through */ 3133bd88451SPaolo Bonzini case OSCR6: tm ++; 3143bd88451SPaolo Bonzini /* fall through */ 3153bd88451SPaolo Bonzini case OSCR5: tm ++; 3163bd88451SPaolo Bonzini /* fall through */ 3173bd88451SPaolo Bonzini case OSCR4: 3183bd88451SPaolo Bonzini if (!pxa2xx_timer_has_tm4(s)) 3193bd88451SPaolo Bonzini goto badreg; 3203bd88451SPaolo Bonzini s->tm4[tm].oldclock = s->tm4[tm].clock; 321bc72ad67SAlex Bligh s->tm4[tm].lastload = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 3223bd88451SPaolo Bonzini s->tm4[tm].clock = value; 3233bd88451SPaolo Bonzini pxa2xx_timer_update4(s, s->tm4[tm].lastload, tm); 3243bd88451SPaolo Bonzini break; 3253bd88451SPaolo Bonzini case OIER: 3263bd88451SPaolo Bonzini s->irq_enabled = value & 0xfff; 3273bd88451SPaolo Bonzini break; 3283bd88451SPaolo Bonzini case OSSR: /* Status register */ 3293bd88451SPaolo Bonzini value &= s->events; 3303bd88451SPaolo Bonzini s->events &= ~value; 3313bd88451SPaolo Bonzini for (i = 0; i < 4; i ++, value >>= 1) 3323bd88451SPaolo Bonzini if (value & 1) 3333bd88451SPaolo Bonzini qemu_irq_lower(s->timer[i].irq); 3343bd88451SPaolo Bonzini if (pxa2xx_timer_has_tm4(s) && !(s->events & 0xff0) && value) 3353bd88451SPaolo Bonzini qemu_irq_lower(s->irq4); 3363bd88451SPaolo Bonzini break; 3373bd88451SPaolo Bonzini case OWER: /* XXX: Reset on OSMR3 match? */ 3383bd88451SPaolo Bonzini s->reset3 = value; 3393bd88451SPaolo Bonzini break; 3403bd88451SPaolo Bonzini case OMCR7: tm ++; 3413bd88451SPaolo Bonzini /* fall through */ 3423bd88451SPaolo Bonzini case OMCR6: tm ++; 3433bd88451SPaolo Bonzini /* fall through */ 3443bd88451SPaolo Bonzini case OMCR5: tm ++; 3453bd88451SPaolo Bonzini /* fall through */ 3463bd88451SPaolo Bonzini case OMCR4: 3473bd88451SPaolo Bonzini if (!pxa2xx_timer_has_tm4(s)) 3483bd88451SPaolo Bonzini goto badreg; 3493bd88451SPaolo Bonzini s->tm4[tm].control = value & 0x0ff; 3503bd88451SPaolo Bonzini /* XXX Stop if running (shouldn't happen) */ 3513bd88451SPaolo Bonzini if ((value & (1 << 7)) || tm == 0) 3523bd88451SPaolo Bonzini s->tm4[tm].freq = pxa2xx_timer4_freq[value & 7]; 3533bd88451SPaolo Bonzini else { 3543bd88451SPaolo Bonzini s->tm4[tm].freq = 0; 355bc72ad67SAlex Bligh pxa2xx_timer_update4(s, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tm); 3563bd88451SPaolo Bonzini } 3573bd88451SPaolo Bonzini break; 3583bd88451SPaolo Bonzini case OMCR11: tm ++; 3593bd88451SPaolo Bonzini /* fall through */ 3603bd88451SPaolo Bonzini case OMCR10: tm ++; 3613bd88451SPaolo Bonzini /* fall through */ 3623bd88451SPaolo Bonzini case OMCR9: tm ++; 3633bd88451SPaolo Bonzini /* fall through */ 3643bd88451SPaolo Bonzini case OMCR8: tm += 4; 3653bd88451SPaolo Bonzini if (!pxa2xx_timer_has_tm4(s)) 3663bd88451SPaolo Bonzini goto badreg; 3673bd88451SPaolo Bonzini s->tm4[tm].control = value & 0x3ff; 3683bd88451SPaolo Bonzini /* XXX Stop if running (shouldn't happen) */ 3693bd88451SPaolo Bonzini if ((value & (1 << 7)) || !(tm & 1)) 3703bd88451SPaolo Bonzini s->tm4[tm].freq = 3713bd88451SPaolo Bonzini pxa2xx_timer4_freq[(value & (1 << 8)) ? 0 : (value & 7)]; 3723bd88451SPaolo Bonzini else { 3733bd88451SPaolo Bonzini s->tm4[tm].freq = 0; 374bc72ad67SAlex Bligh pxa2xx_timer_update4(s, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), tm); 3753bd88451SPaolo Bonzini } 3763bd88451SPaolo Bonzini break; 3773bd88451SPaolo Bonzini default: 3783bd88451SPaolo Bonzini badreg: 3793bd88451SPaolo Bonzini hw_error("pxa2xx_timer_write: Bad offset " REG_FMT "\n", offset); 3803bd88451SPaolo Bonzini } 3813bd88451SPaolo Bonzini } 3823bd88451SPaolo Bonzini 3833bd88451SPaolo Bonzini static const MemoryRegionOps pxa2xx_timer_ops = { 3843bd88451SPaolo Bonzini .read = pxa2xx_timer_read, 3853bd88451SPaolo Bonzini .write = pxa2xx_timer_write, 3863bd88451SPaolo Bonzini .endianness = DEVICE_NATIVE_ENDIAN, 3873bd88451SPaolo Bonzini }; 3883bd88451SPaolo Bonzini 3893bd88451SPaolo Bonzini static void pxa2xx_timer_tick(void *opaque) 3903bd88451SPaolo Bonzini { 3913bd88451SPaolo Bonzini PXA2xxTimer0 *t = (PXA2xxTimer0 *) opaque; 3923bd88451SPaolo Bonzini PXA2xxTimerInfo *i = t->info; 3933bd88451SPaolo Bonzini 3943bd88451SPaolo Bonzini if (i->irq_enabled & (1 << t->num)) { 3953bd88451SPaolo Bonzini i->events |= 1 << t->num; 3963bd88451SPaolo Bonzini qemu_irq_raise(t->irq); 3973bd88451SPaolo Bonzini } 3983bd88451SPaolo Bonzini 3993bd88451SPaolo Bonzini if (t->num == 3) 4003bd88451SPaolo Bonzini if (i->reset3 & 1) { 4013bd88451SPaolo Bonzini i->reset3 = 0; 4023bd88451SPaolo Bonzini qemu_system_reset_request(); 4033bd88451SPaolo Bonzini } 4043bd88451SPaolo Bonzini } 4053bd88451SPaolo Bonzini 4063bd88451SPaolo Bonzini static void pxa2xx_timer_tick4(void *opaque) 4073bd88451SPaolo Bonzini { 4083bd88451SPaolo Bonzini PXA2xxTimer4 *t = (PXA2xxTimer4 *) opaque; 4093bd88451SPaolo Bonzini PXA2xxTimerInfo *i = (PXA2xxTimerInfo *) t->tm.info; 4103bd88451SPaolo Bonzini 4113bd88451SPaolo Bonzini pxa2xx_timer_tick(&t->tm); 4123bd88451SPaolo Bonzini if (t->control & (1 << 3)) 4133bd88451SPaolo Bonzini t->clock = 0; 4143bd88451SPaolo Bonzini if (t->control & (1 << 6)) 415bc72ad67SAlex Bligh pxa2xx_timer_update4(i, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), t->tm.num - 4); 4163bd88451SPaolo Bonzini if (i->events & 0xff0) 4173bd88451SPaolo Bonzini qemu_irq_raise(i->irq4); 4183bd88451SPaolo Bonzini } 4193bd88451SPaolo Bonzini 4203bd88451SPaolo Bonzini static int pxa25x_timer_post_load(void *opaque, int version_id) 4213bd88451SPaolo Bonzini { 4223bd88451SPaolo Bonzini PXA2xxTimerInfo *s = (PXA2xxTimerInfo *) opaque; 4233bd88451SPaolo Bonzini int64_t now; 4243bd88451SPaolo Bonzini int i; 4253bd88451SPaolo Bonzini 426bc72ad67SAlex Bligh now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 4273bd88451SPaolo Bonzini pxa2xx_timer_update(s, now); 4283bd88451SPaolo Bonzini 4293bd88451SPaolo Bonzini if (pxa2xx_timer_has_tm4(s)) 4303bd88451SPaolo Bonzini for (i = 0; i < 8; i ++) 4313bd88451SPaolo Bonzini pxa2xx_timer_update4(s, now, i); 4323bd88451SPaolo Bonzini 4333bd88451SPaolo Bonzini return 0; 4343bd88451SPaolo Bonzini } 4353bd88451SPaolo Bonzini 436*5d83e348Sxiaoqiang.zhao static void pxa2xx_timer_init(Object *obj) 4373bd88451SPaolo Bonzini { 438*5d83e348Sxiaoqiang.zhao PXA2xxTimerInfo *s = PXA2XX_TIMER(obj); 439*5d83e348Sxiaoqiang.zhao SysBusDevice *dev = SYS_BUS_DEVICE(obj); 4403bd88451SPaolo Bonzini 4413bd88451SPaolo Bonzini s->irq_enabled = 0; 4423bd88451SPaolo Bonzini s->oldclock = 0; 4433bd88451SPaolo Bonzini s->clock = 0; 444bc72ad67SAlex Bligh s->lastload = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 4453bd88451SPaolo Bonzini s->reset3 = 0; 4463bd88451SPaolo Bonzini 447*5d83e348Sxiaoqiang.zhao memory_region_init_io(&s->iomem, obj, &pxa2xx_timer_ops, s, 448*5d83e348Sxiaoqiang.zhao "pxa2xx-timer", 0x00001000); 449*5d83e348Sxiaoqiang.zhao sysbus_init_mmio(dev, &s->iomem); 450*5d83e348Sxiaoqiang.zhao } 451*5d83e348Sxiaoqiang.zhao 452*5d83e348Sxiaoqiang.zhao static void pxa2xx_timer_realize(DeviceState *dev, Error **errp) 453*5d83e348Sxiaoqiang.zhao { 454*5d83e348Sxiaoqiang.zhao PXA2xxTimerInfo *s = PXA2XX_TIMER(dev); 455*5d83e348Sxiaoqiang.zhao SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 456*5d83e348Sxiaoqiang.zhao int i; 457*5d83e348Sxiaoqiang.zhao 4583bd88451SPaolo Bonzini for (i = 0; i < 4; i ++) { 4593bd88451SPaolo Bonzini s->timer[i].value = 0; 460*5d83e348Sxiaoqiang.zhao sysbus_init_irq(sbd, &s->timer[i].irq); 4613bd88451SPaolo Bonzini s->timer[i].info = s; 4623bd88451SPaolo Bonzini s->timer[i].num = i; 463bc72ad67SAlex Bligh s->timer[i].qtimer = timer_new_ns(QEMU_CLOCK_VIRTUAL, 4643bd88451SPaolo Bonzini pxa2xx_timer_tick, &s->timer[i]); 4653bd88451SPaolo Bonzini } 466*5d83e348Sxiaoqiang.zhao 4673bd88451SPaolo Bonzini if (s->flags & (1 << PXA2XX_TIMER_HAVE_TM4)) { 468*5d83e348Sxiaoqiang.zhao sysbus_init_irq(sbd, &s->irq4); 4693bd88451SPaolo Bonzini 4703bd88451SPaolo Bonzini for (i = 0; i < 8; i ++) { 4713bd88451SPaolo Bonzini s->tm4[i].tm.value = 0; 4723bd88451SPaolo Bonzini s->tm4[i].tm.info = s; 4733bd88451SPaolo Bonzini s->tm4[i].tm.num = i + 4; 4743bd88451SPaolo Bonzini s->tm4[i].freq = 0; 4753bd88451SPaolo Bonzini s->tm4[i].control = 0x0; 476bc72ad67SAlex Bligh s->tm4[i].tm.qtimer = timer_new_ns(QEMU_CLOCK_VIRTUAL, 4773bd88451SPaolo Bonzini pxa2xx_timer_tick4, &s->tm4[i]); 4783bd88451SPaolo Bonzini } 4793bd88451SPaolo Bonzini } 4803bd88451SPaolo Bonzini } 4813bd88451SPaolo Bonzini 4823bd88451SPaolo Bonzini static const VMStateDescription vmstate_pxa2xx_timer0_regs = { 4833bd88451SPaolo Bonzini .name = "pxa2xx_timer0", 4843bd88451SPaolo Bonzini .version_id = 2, 4853bd88451SPaolo Bonzini .minimum_version_id = 2, 4863bd88451SPaolo Bonzini .fields = (VMStateField[]) { 4873bd88451SPaolo Bonzini VMSTATE_UINT32(value, PXA2xxTimer0), 4883bd88451SPaolo Bonzini VMSTATE_END_OF_LIST(), 4893bd88451SPaolo Bonzini }, 4903bd88451SPaolo Bonzini }; 4913bd88451SPaolo Bonzini 4923bd88451SPaolo Bonzini static const VMStateDescription vmstate_pxa2xx_timer4_regs = { 4933bd88451SPaolo Bonzini .name = "pxa2xx_timer4", 4943bd88451SPaolo Bonzini .version_id = 1, 4953bd88451SPaolo Bonzini .minimum_version_id = 1, 4963bd88451SPaolo Bonzini .fields = (VMStateField[]) { 4973bd88451SPaolo Bonzini VMSTATE_STRUCT(tm, PXA2xxTimer4, 1, 4983bd88451SPaolo Bonzini vmstate_pxa2xx_timer0_regs, PXA2xxTimer0), 4993bd88451SPaolo Bonzini VMSTATE_INT32(oldclock, PXA2xxTimer4), 5003bd88451SPaolo Bonzini VMSTATE_INT32(clock, PXA2xxTimer4), 5013bd88451SPaolo Bonzini VMSTATE_UINT64(lastload, PXA2xxTimer4), 5023bd88451SPaolo Bonzini VMSTATE_UINT32(freq, PXA2xxTimer4), 5033bd88451SPaolo Bonzini VMSTATE_UINT32(control, PXA2xxTimer4), 5043bd88451SPaolo Bonzini VMSTATE_END_OF_LIST(), 5053bd88451SPaolo Bonzini }, 5063bd88451SPaolo Bonzini }; 5073bd88451SPaolo Bonzini 5083bd88451SPaolo Bonzini static bool pxa2xx_timer_has_tm4_test(void *opaque, int version_id) 5093bd88451SPaolo Bonzini { 5103bd88451SPaolo Bonzini return pxa2xx_timer_has_tm4(opaque); 5113bd88451SPaolo Bonzini } 5123bd88451SPaolo Bonzini 5133bd88451SPaolo Bonzini static const VMStateDescription vmstate_pxa2xx_timer_regs = { 5143bd88451SPaolo Bonzini .name = "pxa2xx_timer", 5153bd88451SPaolo Bonzini .version_id = 1, 5163bd88451SPaolo Bonzini .minimum_version_id = 1, 5173bd88451SPaolo Bonzini .post_load = pxa25x_timer_post_load, 5183bd88451SPaolo Bonzini .fields = (VMStateField[]) { 5193bd88451SPaolo Bonzini VMSTATE_INT32(clock, PXA2xxTimerInfo), 5203bd88451SPaolo Bonzini VMSTATE_INT32(oldclock, PXA2xxTimerInfo), 5213bd88451SPaolo Bonzini VMSTATE_UINT64(lastload, PXA2xxTimerInfo), 5223bd88451SPaolo Bonzini VMSTATE_STRUCT_ARRAY(timer, PXA2xxTimerInfo, 4, 1, 5233bd88451SPaolo Bonzini vmstate_pxa2xx_timer0_regs, PXA2xxTimer0), 5243bd88451SPaolo Bonzini VMSTATE_UINT32(events, PXA2xxTimerInfo), 5253bd88451SPaolo Bonzini VMSTATE_UINT32(irq_enabled, PXA2xxTimerInfo), 5263bd88451SPaolo Bonzini VMSTATE_UINT32(reset3, PXA2xxTimerInfo), 5273bd88451SPaolo Bonzini VMSTATE_UINT32(snapshot, PXA2xxTimerInfo), 5283bd88451SPaolo Bonzini VMSTATE_STRUCT_ARRAY_TEST(tm4, PXA2xxTimerInfo, 8, 5293bd88451SPaolo Bonzini pxa2xx_timer_has_tm4_test, 0, 5303bd88451SPaolo Bonzini vmstate_pxa2xx_timer4_regs, PXA2xxTimer4), 5313bd88451SPaolo Bonzini VMSTATE_END_OF_LIST(), 5323bd88451SPaolo Bonzini } 5333bd88451SPaolo Bonzini }; 5343bd88451SPaolo Bonzini 5353bd88451SPaolo Bonzini static Property pxa25x_timer_dev_properties[] = { 5363bd88451SPaolo Bonzini DEFINE_PROP_UINT32("freq", PXA2xxTimerInfo, freq, PXA25X_FREQ), 5373bd88451SPaolo Bonzini DEFINE_PROP_BIT("tm4", PXA2xxTimerInfo, flags, 5383bd88451SPaolo Bonzini PXA2XX_TIMER_HAVE_TM4, false), 5393bd88451SPaolo Bonzini DEFINE_PROP_END_OF_LIST(), 5403bd88451SPaolo Bonzini }; 5413bd88451SPaolo Bonzini 5423bd88451SPaolo Bonzini static void pxa25x_timer_dev_class_init(ObjectClass *klass, void *data) 5433bd88451SPaolo Bonzini { 5443bd88451SPaolo Bonzini DeviceClass *dc = DEVICE_CLASS(klass); 5453bd88451SPaolo Bonzini 5463bd88451SPaolo Bonzini dc->desc = "PXA25x timer"; 5473bd88451SPaolo Bonzini dc->props = pxa25x_timer_dev_properties; 5483bd88451SPaolo Bonzini } 5493bd88451SPaolo Bonzini 5503bd88451SPaolo Bonzini static const TypeInfo pxa25x_timer_dev_info = { 5513bd88451SPaolo Bonzini .name = "pxa25x-timer", 552feea4361SAndreas Färber .parent = TYPE_PXA2XX_TIMER, 5533bd88451SPaolo Bonzini .instance_size = sizeof(PXA2xxTimerInfo), 5543bd88451SPaolo Bonzini .class_init = pxa25x_timer_dev_class_init, 5553bd88451SPaolo Bonzini }; 5563bd88451SPaolo Bonzini 5573bd88451SPaolo Bonzini static Property pxa27x_timer_dev_properties[] = { 5583bd88451SPaolo Bonzini DEFINE_PROP_UINT32("freq", PXA2xxTimerInfo, freq, PXA27X_FREQ), 5593bd88451SPaolo Bonzini DEFINE_PROP_BIT("tm4", PXA2xxTimerInfo, flags, 5603bd88451SPaolo Bonzini PXA2XX_TIMER_HAVE_TM4, true), 5613bd88451SPaolo Bonzini DEFINE_PROP_END_OF_LIST(), 5623bd88451SPaolo Bonzini }; 5633bd88451SPaolo Bonzini 5643bd88451SPaolo Bonzini static void pxa27x_timer_dev_class_init(ObjectClass *klass, void *data) 5653bd88451SPaolo Bonzini { 5663bd88451SPaolo Bonzini DeviceClass *dc = DEVICE_CLASS(klass); 5673bd88451SPaolo Bonzini 5683bd88451SPaolo Bonzini dc->desc = "PXA27x timer"; 5693bd88451SPaolo Bonzini dc->props = pxa27x_timer_dev_properties; 5703bd88451SPaolo Bonzini } 5713bd88451SPaolo Bonzini 5723bd88451SPaolo Bonzini static const TypeInfo pxa27x_timer_dev_info = { 5733bd88451SPaolo Bonzini .name = "pxa27x-timer", 574feea4361SAndreas Färber .parent = TYPE_PXA2XX_TIMER, 5753bd88451SPaolo Bonzini .instance_size = sizeof(PXA2xxTimerInfo), 5763bd88451SPaolo Bonzini .class_init = pxa27x_timer_dev_class_init, 5773bd88451SPaolo Bonzini }; 5783bd88451SPaolo Bonzini 579feea4361SAndreas Färber static void pxa2xx_timer_class_init(ObjectClass *oc, void *data) 580feea4361SAndreas Färber { 581feea4361SAndreas Färber DeviceClass *dc = DEVICE_CLASS(oc); 582feea4361SAndreas Färber 583*5d83e348Sxiaoqiang.zhao dc->realize = pxa2xx_timer_realize; 584feea4361SAndreas Färber dc->vmsd = &vmstate_pxa2xx_timer_regs; 585feea4361SAndreas Färber } 586feea4361SAndreas Färber 587feea4361SAndreas Färber static const TypeInfo pxa2xx_timer_type_info = { 588feea4361SAndreas Färber .name = TYPE_PXA2XX_TIMER, 589feea4361SAndreas Färber .parent = TYPE_SYS_BUS_DEVICE, 590feea4361SAndreas Färber .instance_size = sizeof(PXA2xxTimerInfo), 591*5d83e348Sxiaoqiang.zhao .instance_init = pxa2xx_timer_init, 592feea4361SAndreas Färber .abstract = true, 593feea4361SAndreas Färber .class_init = pxa2xx_timer_class_init, 594feea4361SAndreas Färber }; 595feea4361SAndreas Färber 5963bd88451SPaolo Bonzini static void pxa2xx_timer_register_types(void) 5973bd88451SPaolo Bonzini { 598feea4361SAndreas Färber type_register_static(&pxa2xx_timer_type_info); 5993bd88451SPaolo Bonzini type_register_static(&pxa25x_timer_dev_info); 6003bd88451SPaolo Bonzini type_register_static(&pxa27x_timer_dev_info); 6013bd88451SPaolo Bonzini } 6023bd88451SPaolo Bonzini 6033bd88451SPaolo Bonzini type_init(pxa2xx_timer_register_types) 604