1cd1a3f68Sths /* 2cd1a3f68Sths * SuperH Timer modules. 3cd1a3f68Sths * 4cd1a3f68Sths * Copyright (c) 2007 Magnus Damm 5cd1a3f68Sths * Based on arm_timer.c by Paul Brook 6cd1a3f68Sths * Copyright (c) 2005-2006 CodeSourcery. 7cd1a3f68Sths * 88e31bf38SMatthew Fernandez * This code is licensed under the GPL. 9cd1a3f68Sths */ 10cd1a3f68Sths 11282bc81eSPeter Maydell #include "qemu/osdep.h" 12*95f4dc44SPhilippe Mathieu-Daudé #include "exec/memory.h" 1383c9f4caSPaolo Bonzini #include "hw/hw.h" 1464552b6bSMarkus Armbruster #include "hw/irq.h" 150d09e41aSPaolo Bonzini #include "hw/sh4/sh.h" 161de7afc9SPaolo Bonzini #include "qemu/timer.h" 17*95f4dc44SPhilippe Mathieu-Daudé #include "hw/timer/tmu012.h" 1883c9f4caSPaolo Bonzini #include "hw/ptimer.h" 19cd1a3f68Sths 20cd1a3f68Sths //#define DEBUG_TIMER 21cd1a3f68Sths 22cd1a3f68Sths #define TIMER_TCR_TPSC (7 << 0) 23cd1a3f68Sths #define TIMER_TCR_CKEG (3 << 3) 24cd1a3f68Sths #define TIMER_TCR_UNIE (1 << 5) 25cd1a3f68Sths #define TIMER_TCR_ICPE (3 << 6) 26cd1a3f68Sths #define TIMER_TCR_UNF (1 << 8) 27cd1a3f68Sths #define TIMER_TCR_ICPF (1 << 9) 28cd1a3f68Sths #define TIMER_TCR_RESERVED (0x3f << 10) 29cd1a3f68Sths 30cd1a3f68Sths #define TIMER_FEAT_CAPT (1 << 0) 31cd1a3f68Sths #define TIMER_FEAT_EXTCLK (1 << 1) 32cd1a3f68Sths 33e7786f27Saurel32 #define OFFSET_TCOR 0 34e7786f27Saurel32 #define OFFSET_TCNT 1 35e7786f27Saurel32 #define OFFSET_TCR 2 36e7786f27Saurel32 #define OFFSET_TCPR 3 37e7786f27Saurel32 38cd1a3f68Sths typedef struct { 39cd1a3f68Sths ptimer_state *timer; 40cd1a3f68Sths uint32_t tcnt; 41cd1a3f68Sths uint32_t tcor; 42cd1a3f68Sths uint32_t tcr; 43cd1a3f68Sths uint32_t tcpr; 44cd1a3f68Sths int freq; 45cd1a3f68Sths int int_level; 46703243a0Sbalrog int old_level; 47cd1a3f68Sths int feat; 48cd1a3f68Sths int enabled; 4996e2fc41Saurel32 qemu_irq irq; 50cd1a3f68Sths } sh_timer_state; 51cd1a3f68Sths 52cd1a3f68Sths /* Check all active timers, and schedule the next timer interrupt. */ 53cd1a3f68Sths 54cd1a3f68Sths static void sh_timer_update(sh_timer_state *s) 55cd1a3f68Sths { 56703243a0Sbalrog int new_level = s->int_level && (s->tcr & TIMER_TCR_UNIE); 57703243a0Sbalrog 58703243a0Sbalrog if (new_level != s->old_level) 5996e2fc41Saurel32 qemu_set_irq (s->irq, new_level); 60703243a0Sbalrog 61703243a0Sbalrog s->old_level = s->int_level; 62703243a0Sbalrog s->int_level = new_level; 63cd1a3f68Sths } 64cd1a3f68Sths 65a8170e5eSAvi Kivity static uint32_t sh_timer_read(void *opaque, hwaddr offset) 66cd1a3f68Sths { 67cd1a3f68Sths sh_timer_state *s = (sh_timer_state *)opaque; 68cd1a3f68Sths 69cd1a3f68Sths switch (offset >> 2) { 70e7786f27Saurel32 case OFFSET_TCOR: 71cd1a3f68Sths return s->tcor; 72e7786f27Saurel32 case OFFSET_TCNT: 73cd1a3f68Sths return ptimer_get_count(s->timer); 74e7786f27Saurel32 case OFFSET_TCR: 75cd1a3f68Sths return s->tcr | (s->int_level ? TIMER_TCR_UNF : 0); 76e7786f27Saurel32 case OFFSET_TCPR: 77cd1a3f68Sths if (s->feat & TIMER_FEAT_CAPT) 78cd1a3f68Sths return s->tcpr; 79edd7541bSPaolo Bonzini /* fall through */ 80cd1a3f68Sths default: 812ac71179SPaul Brook hw_error("sh_timer_read: Bad offset %x\n", (int)offset); 82cd1a3f68Sths return 0; 83cd1a3f68Sths } 84cd1a3f68Sths } 85cd1a3f68Sths 86a8170e5eSAvi Kivity static void sh_timer_write(void *opaque, hwaddr offset, 87cd1a3f68Sths uint32_t value) 88cd1a3f68Sths { 89cd1a3f68Sths sh_timer_state *s = (sh_timer_state *)opaque; 90cd1a3f68Sths int freq; 91cd1a3f68Sths 92cd1a3f68Sths switch (offset >> 2) { 93e7786f27Saurel32 case OFFSET_TCOR: 94cd1a3f68Sths s->tcor = value; 9528015830SPeter Maydell ptimer_transaction_begin(s->timer); 96cd1a3f68Sths ptimer_set_limit(s->timer, s->tcor, 0); 9728015830SPeter Maydell ptimer_transaction_commit(s->timer); 98cd1a3f68Sths break; 99e7786f27Saurel32 case OFFSET_TCNT: 100cd1a3f68Sths s->tcnt = value; 10128015830SPeter Maydell ptimer_transaction_begin(s->timer); 102cd1a3f68Sths ptimer_set_count(s->timer, s->tcnt); 10328015830SPeter Maydell ptimer_transaction_commit(s->timer); 104cd1a3f68Sths break; 105e7786f27Saurel32 case OFFSET_TCR: 10628015830SPeter Maydell ptimer_transaction_begin(s->timer); 107cd1a3f68Sths if (s->enabled) { 108cd1a3f68Sths /* Pause the timer if it is running. This may cause some 109cd1a3f68Sths inaccuracy dure to rounding, but avoids a whole lot of other 110cd1a3f68Sths messyness. */ 111cd1a3f68Sths ptimer_stop(s->timer); 112cd1a3f68Sths } 113cd1a3f68Sths freq = s->freq; 114cd1a3f68Sths /* ??? Need to recalculate expiry time after changing divisor. */ 115cd1a3f68Sths switch (value & TIMER_TCR_TPSC) { 116cd1a3f68Sths case 0: freq >>= 2; break; 117cd1a3f68Sths case 1: freq >>= 4; break; 118cd1a3f68Sths case 2: freq >>= 6; break; 119cd1a3f68Sths case 3: freq >>= 8; break; 120cd1a3f68Sths case 4: freq >>= 10; break; 121cd1a3f68Sths case 6: 122cd1a3f68Sths case 7: if (s->feat & TIMER_FEAT_EXTCLK) break; 1232ac71179SPaul Brook default: hw_error("sh_timer_write: Reserved TPSC value\n"); break; 124cd1a3f68Sths } 125cd1a3f68Sths switch ((value & TIMER_TCR_CKEG) >> 3) { 126cd1a3f68Sths case 0: break; 127cd1a3f68Sths case 1: 128cd1a3f68Sths case 2: 129cd1a3f68Sths case 3: if (s->feat & TIMER_FEAT_EXTCLK) break; 1302ac71179SPaul Brook default: hw_error("sh_timer_write: Reserved CKEG value\n"); break; 131cd1a3f68Sths } 132cd1a3f68Sths switch ((value & TIMER_TCR_ICPE) >> 6) { 133cd1a3f68Sths case 0: break; 134cd1a3f68Sths case 2: 135cd1a3f68Sths case 3: if (s->feat & TIMER_FEAT_CAPT) break; 1362ac71179SPaul Brook default: hw_error("sh_timer_write: Reserved ICPE value\n"); break; 137cd1a3f68Sths } 138cd1a3f68Sths if ((value & TIMER_TCR_UNF) == 0) 139cd1a3f68Sths s->int_level = 0; 140cd1a3f68Sths 141cd1a3f68Sths value &= ~TIMER_TCR_UNF; 142cd1a3f68Sths 143cd1a3f68Sths if ((value & TIMER_TCR_ICPF) && (!(s->feat & TIMER_FEAT_CAPT))) 1442ac71179SPaul Brook hw_error("sh_timer_write: Reserved ICPF value\n"); 145cd1a3f68Sths 146cd1a3f68Sths value &= ~TIMER_TCR_ICPF; /* capture not supported */ 147cd1a3f68Sths 148cd1a3f68Sths if (value & TIMER_TCR_RESERVED) 1492ac71179SPaul Brook hw_error("sh_timer_write: Reserved TCR bits set\n"); 150cd1a3f68Sths s->tcr = value; 151cd1a3f68Sths ptimer_set_limit(s->timer, s->tcor, 0); 152cd1a3f68Sths ptimer_set_freq(s->timer, freq); 153cd1a3f68Sths if (s->enabled) { 154cd1a3f68Sths /* Restart the timer if still enabled. */ 155cd1a3f68Sths ptimer_run(s->timer, 0); 156cd1a3f68Sths } 15728015830SPeter Maydell ptimer_transaction_commit(s->timer); 158cd1a3f68Sths break; 159e7786f27Saurel32 case OFFSET_TCPR: 160cd1a3f68Sths if (s->feat & TIMER_FEAT_CAPT) { 161cd1a3f68Sths s->tcpr = value; 162cd1a3f68Sths break; 163cd1a3f68Sths } 164cd1a3f68Sths default: 1652ac71179SPaul Brook hw_error("sh_timer_write: Bad offset %x\n", (int)offset); 166cd1a3f68Sths } 167cd1a3f68Sths sh_timer_update(s); 168cd1a3f68Sths } 169cd1a3f68Sths 170cd1a3f68Sths static void sh_timer_start_stop(void *opaque, int enable) 171cd1a3f68Sths { 172cd1a3f68Sths sh_timer_state *s = (sh_timer_state *)opaque; 173cd1a3f68Sths 174cd1a3f68Sths #ifdef DEBUG_TIMER 175cd1a3f68Sths printf("sh_timer_start_stop %d (%d)\n", enable, s->enabled); 176cd1a3f68Sths #endif 177cd1a3f68Sths 17828015830SPeter Maydell ptimer_transaction_begin(s->timer); 179cd1a3f68Sths if (s->enabled && !enable) { 180cd1a3f68Sths ptimer_stop(s->timer); 181cd1a3f68Sths } 182cd1a3f68Sths if (!s->enabled && enable) { 183cd1a3f68Sths ptimer_run(s->timer, 0); 184cd1a3f68Sths } 18528015830SPeter Maydell ptimer_transaction_commit(s->timer); 186cd1a3f68Sths s->enabled = !!enable; 187cd1a3f68Sths 188cd1a3f68Sths #ifdef DEBUG_TIMER 189cd1a3f68Sths printf("sh_timer_start_stop done %d\n", s->enabled); 190cd1a3f68Sths #endif 191cd1a3f68Sths } 192cd1a3f68Sths 193cd1a3f68Sths static void sh_timer_tick(void *opaque) 194cd1a3f68Sths { 195cd1a3f68Sths sh_timer_state *s = (sh_timer_state *)opaque; 196cd1a3f68Sths s->int_level = s->enabled; 197cd1a3f68Sths sh_timer_update(s); 198cd1a3f68Sths } 199cd1a3f68Sths 20096e2fc41Saurel32 static void *sh_timer_init(uint32_t freq, int feat, qemu_irq irq) 201cd1a3f68Sths { 202cd1a3f68Sths sh_timer_state *s; 203cd1a3f68Sths 2047267c094SAnthony Liguori s = (sh_timer_state *)g_malloc0(sizeof(sh_timer_state)); 205cd1a3f68Sths s->freq = freq; 206cd1a3f68Sths s->feat = feat; 207cd1a3f68Sths s->tcor = 0xffffffff; 208cd1a3f68Sths s->tcnt = 0xffffffff; 209cd1a3f68Sths s->tcpr = 0xdeadbeef; 210e7786f27Saurel32 s->tcr = 0; 211cd1a3f68Sths s->enabled = 0; 212703243a0Sbalrog s->irq = irq; 213cd1a3f68Sths 21428015830SPeter Maydell s->timer = ptimer_init(sh_timer_tick, s, PTIMER_POLICY_DEFAULT); 215e7786f27Saurel32 216e7786f27Saurel32 sh_timer_write(s, OFFSET_TCOR >> 2, s->tcor); 217e7786f27Saurel32 sh_timer_write(s, OFFSET_TCNT >> 2, s->tcnt); 218e7786f27Saurel32 sh_timer_write(s, OFFSET_TCPR >> 2, s->tcpr); 219e7786f27Saurel32 sh_timer_write(s, OFFSET_TCR >> 2, s->tcpr); 220cd1a3f68Sths /* ??? Save/restore. */ 221cd1a3f68Sths return s; 222cd1a3f68Sths } 223cd1a3f68Sths 224cd1a3f68Sths typedef struct { 22589e29451SBenoît Canet MemoryRegion iomem; 22689e29451SBenoît Canet MemoryRegion iomem_p4; 22789e29451SBenoît Canet MemoryRegion iomem_a7; 228cd1a3f68Sths void *timer[3]; 229cd1a3f68Sths int level[3]; 230cd1a3f68Sths uint32_t tocr; 231cd1a3f68Sths uint32_t tstr; 232cd1a3f68Sths int feat; 233cd1a3f68Sths } tmu012_state; 234cd1a3f68Sths 235a8170e5eSAvi Kivity static uint64_t tmu012_read(void *opaque, hwaddr offset, 23689e29451SBenoît Canet unsigned size) 237cd1a3f68Sths { 238cd1a3f68Sths tmu012_state *s = (tmu012_state *)opaque; 239cd1a3f68Sths 240cd1a3f68Sths #ifdef DEBUG_TIMER 241cd1a3f68Sths printf("tmu012_read 0x%lx\n", (unsigned long) offset); 242cd1a3f68Sths #endif 243cd1a3f68Sths 244cd1a3f68Sths if (offset >= 0x20) { 245cd1a3f68Sths if (!(s->feat & TMU012_FEAT_3CHAN)) 2462ac71179SPaul Brook hw_error("tmu012_write: Bad channel offset %x\n", (int)offset); 247cd1a3f68Sths return sh_timer_read(s->timer[2], offset - 0x20); 248cd1a3f68Sths } 249cd1a3f68Sths 250cd1a3f68Sths if (offset >= 0x14) 251cd1a3f68Sths return sh_timer_read(s->timer[1], offset - 0x14); 252cd1a3f68Sths 253cd1a3f68Sths if (offset >= 0x08) 254cd1a3f68Sths return sh_timer_read(s->timer[0], offset - 0x08); 255cd1a3f68Sths 256cd1a3f68Sths if (offset == 4) 257cd1a3f68Sths return s->tstr; 258cd1a3f68Sths 259cd1a3f68Sths if ((s->feat & TMU012_FEAT_TOCR) && offset == 0) 260cd1a3f68Sths return s->tocr; 261cd1a3f68Sths 2622ac71179SPaul Brook hw_error("tmu012_write: Bad offset %x\n", (int)offset); 263cd1a3f68Sths return 0; 264cd1a3f68Sths } 265cd1a3f68Sths 266a8170e5eSAvi Kivity static void tmu012_write(void *opaque, hwaddr offset, 26789e29451SBenoît Canet uint64_t value, unsigned size) 268cd1a3f68Sths { 269cd1a3f68Sths tmu012_state *s = (tmu012_state *)opaque; 270cd1a3f68Sths 271cd1a3f68Sths #ifdef DEBUG_TIMER 272cd1a3f68Sths printf("tmu012_write 0x%lx 0x%08x\n", (unsigned long) offset, value); 273cd1a3f68Sths #endif 274cd1a3f68Sths 275cd1a3f68Sths if (offset >= 0x20) { 276cd1a3f68Sths if (!(s->feat & TMU012_FEAT_3CHAN)) 2772ac71179SPaul Brook hw_error("tmu012_write: Bad channel offset %x\n", (int)offset); 278cd1a3f68Sths sh_timer_write(s->timer[2], offset - 0x20, value); 279cd1a3f68Sths return; 280cd1a3f68Sths } 281cd1a3f68Sths 282cd1a3f68Sths if (offset >= 0x14) { 283cd1a3f68Sths sh_timer_write(s->timer[1], offset - 0x14, value); 284cd1a3f68Sths return; 285cd1a3f68Sths } 286cd1a3f68Sths 287cd1a3f68Sths if (offset >= 0x08) { 288cd1a3f68Sths sh_timer_write(s->timer[0], offset - 0x08, value); 289cd1a3f68Sths return; 290cd1a3f68Sths } 291cd1a3f68Sths 292cd1a3f68Sths if (offset == 4) { 293cd1a3f68Sths sh_timer_start_stop(s->timer[0], value & (1 << 0)); 294cd1a3f68Sths sh_timer_start_stop(s->timer[1], value & (1 << 1)); 295cd1a3f68Sths if (s->feat & TMU012_FEAT_3CHAN) 296cd1a3f68Sths sh_timer_start_stop(s->timer[2], value & (1 << 2)); 297cd1a3f68Sths else 298cd1a3f68Sths if (value & (1 << 2)) 2992ac71179SPaul Brook hw_error("tmu012_write: Bad channel\n"); 300cd1a3f68Sths 301cd1a3f68Sths s->tstr = value; 302cd1a3f68Sths return; 303cd1a3f68Sths } 304cd1a3f68Sths 305cd1a3f68Sths if ((s->feat & TMU012_FEAT_TOCR) && offset == 0) { 306cd1a3f68Sths s->tocr = value & (1 << 0); 307cd1a3f68Sths } 308cd1a3f68Sths } 309cd1a3f68Sths 31089e29451SBenoît Canet static const MemoryRegionOps tmu012_ops = { 31189e29451SBenoît Canet .read = tmu012_read, 31289e29451SBenoît Canet .write = tmu012_write, 31389e29451SBenoît Canet .endianness = DEVICE_NATIVE_ENDIAN, 314cd1a3f68Sths }; 315cd1a3f68Sths 316a8170e5eSAvi Kivity void tmu012_init(MemoryRegion *sysmem, hwaddr base, 31789e29451SBenoît Canet int feat, uint32_t freq, 31896e2fc41Saurel32 qemu_irq ch0_irq, qemu_irq ch1_irq, 31996e2fc41Saurel32 qemu_irq ch2_irq0, qemu_irq ch2_irq1) 320cd1a3f68Sths { 321cd1a3f68Sths tmu012_state *s; 322cd1a3f68Sths int timer_feat = (feat & TMU012_FEAT_EXTCLK) ? TIMER_FEAT_EXTCLK : 0; 323cd1a3f68Sths 3247267c094SAnthony Liguori s = (tmu012_state *)g_malloc0(sizeof(tmu012_state)); 325cd1a3f68Sths s->feat = feat; 326703243a0Sbalrog s->timer[0] = sh_timer_init(freq, timer_feat, ch0_irq); 327703243a0Sbalrog s->timer[1] = sh_timer_init(freq, timer_feat, ch1_irq); 328cd1a3f68Sths if (feat & TMU012_FEAT_3CHAN) 329703243a0Sbalrog s->timer[2] = sh_timer_init(freq, timer_feat | TIMER_FEAT_CAPT, 330703243a0Sbalrog ch2_irq0); /* ch2_irq1 not supported */ 33189e29451SBenoît Canet 3322c9b15caSPaolo Bonzini memory_region_init_io(&s->iomem, NULL, &tmu012_ops, s, 33389e29451SBenoît Canet "timer", 0x100000000ULL); 33489e29451SBenoît Canet 3352c9b15caSPaolo Bonzini memory_region_init_alias(&s->iomem_p4, NULL, "timer-p4", 33689e29451SBenoît Canet &s->iomem, 0, 0x1000); 33789e29451SBenoît Canet memory_region_add_subregion(sysmem, P4ADDR(base), &s->iomem_p4); 33889e29451SBenoît Canet 3392c9b15caSPaolo Bonzini memory_region_init_alias(&s->iomem_a7, NULL, "timer-a7", 34089e29451SBenoît Canet &s->iomem, 0, 0x1000); 34189e29451SBenoît Canet memory_region_add_subregion(sysmem, A7ADDR(base), &s->iomem_a7); 342cd1a3f68Sths /* ??? Save/restore. */ 343cd1a3f68Sths } 344