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" 1295f4dc44SPhilippe 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" 1695f4dc44SPhilippe Mathieu-Daudé #include "hw/timer/tmu012.h" 1783c9f4caSPaolo Bonzini #include "hw/ptimer.h" 18cd1a3f68Sths 19cd1a3f68Sths //#define DEBUG_TIMER 20cd1a3f68Sths 21cd1a3f68Sths #define TIMER_TCR_TPSC (7 << 0) 22cd1a3f68Sths #define TIMER_TCR_CKEG (3 << 3) 23cd1a3f68Sths #define TIMER_TCR_UNIE (1 << 5) 24cd1a3f68Sths #define TIMER_TCR_ICPE (3 << 6) 25cd1a3f68Sths #define TIMER_TCR_UNF (1 << 8) 26cd1a3f68Sths #define TIMER_TCR_ICPF (1 << 9) 27cd1a3f68Sths #define TIMER_TCR_RESERVED (0x3f << 10) 28cd1a3f68Sths 29cd1a3f68Sths #define TIMER_FEAT_CAPT (1 << 0) 30cd1a3f68Sths #define TIMER_FEAT_EXTCLK (1 << 1) 31cd1a3f68Sths 32e7786f27Saurel32 #define OFFSET_TCOR 0 33e7786f27Saurel32 #define OFFSET_TCNT 1 34e7786f27Saurel32 #define OFFSET_TCR 2 35e7786f27Saurel32 #define OFFSET_TCPR 3 36e7786f27Saurel32 37cd1a3f68Sths typedef struct { 38cd1a3f68Sths ptimer_state *timer; 39cd1a3f68Sths uint32_t tcnt; 40cd1a3f68Sths uint32_t tcor; 41cd1a3f68Sths uint32_t tcr; 42cd1a3f68Sths uint32_t tcpr; 43cd1a3f68Sths int freq; 44cd1a3f68Sths int int_level; 45703243a0Sbalrog int old_level; 46cd1a3f68Sths int feat; 47cd1a3f68Sths int enabled; 4896e2fc41Saurel32 qemu_irq irq; 49cd1a3f68Sths } sh_timer_state; 50cd1a3f68Sths 51cd1a3f68Sths /* Check all active timers, and schedule the next timer interrupt. */ 52cd1a3f68Sths 53cd1a3f68Sths static void sh_timer_update(sh_timer_state *s) 54cd1a3f68Sths { 55703243a0Sbalrog int new_level = s->int_level && (s->tcr & TIMER_TCR_UNIE); 56703243a0Sbalrog 57703243a0Sbalrog if (new_level != s->old_level) 5896e2fc41Saurel32 qemu_set_irq (s->irq, new_level); 59703243a0Sbalrog 60703243a0Sbalrog s->old_level = s->int_level; 61703243a0Sbalrog s->int_level = new_level; 62cd1a3f68Sths } 63cd1a3f68Sths 64a8170e5eSAvi Kivity static uint32_t sh_timer_read(void *opaque, hwaddr offset) 65cd1a3f68Sths { 66cd1a3f68Sths sh_timer_state *s = (sh_timer_state *)opaque; 67cd1a3f68Sths 68cd1a3f68Sths switch (offset >> 2) { 69e7786f27Saurel32 case OFFSET_TCOR: 70cd1a3f68Sths return s->tcor; 71e7786f27Saurel32 case OFFSET_TCNT: 72cd1a3f68Sths return ptimer_get_count(s->timer); 73e7786f27Saurel32 case OFFSET_TCR: 74cd1a3f68Sths return s->tcr | (s->int_level ? TIMER_TCR_UNF : 0); 75e7786f27Saurel32 case OFFSET_TCPR: 76cd1a3f68Sths if (s->feat & TIMER_FEAT_CAPT) 77cd1a3f68Sths return s->tcpr; 78edd7541bSPaolo Bonzini /* fall through */ 79cd1a3f68Sths default: 802ac71179SPaul Brook hw_error("sh_timer_read: Bad offset %x\n", (int)offset); 81cd1a3f68Sths return 0; 82cd1a3f68Sths } 83cd1a3f68Sths } 84cd1a3f68Sths 85a8170e5eSAvi Kivity static void sh_timer_write(void *opaque, hwaddr offset, 86cd1a3f68Sths uint32_t value) 87cd1a3f68Sths { 88cd1a3f68Sths sh_timer_state *s = (sh_timer_state *)opaque; 89cd1a3f68Sths int freq; 90cd1a3f68Sths 91cd1a3f68Sths switch (offset >> 2) { 92e7786f27Saurel32 case OFFSET_TCOR: 93cd1a3f68Sths s->tcor = value; 9428015830SPeter Maydell ptimer_transaction_begin(s->timer); 95cd1a3f68Sths ptimer_set_limit(s->timer, s->tcor, 0); 9628015830SPeter Maydell ptimer_transaction_commit(s->timer); 97cd1a3f68Sths break; 98e7786f27Saurel32 case OFFSET_TCNT: 99cd1a3f68Sths s->tcnt = value; 10028015830SPeter Maydell ptimer_transaction_begin(s->timer); 101cd1a3f68Sths ptimer_set_count(s->timer, s->tcnt); 10228015830SPeter Maydell ptimer_transaction_commit(s->timer); 103cd1a3f68Sths break; 104e7786f27Saurel32 case OFFSET_TCR: 10528015830SPeter Maydell ptimer_transaction_begin(s->timer); 106cd1a3f68Sths if (s->enabled) { 107cd1a3f68Sths /* Pause the timer if it is running. This may cause some 108cd1a3f68Sths inaccuracy dure to rounding, but avoids a whole lot of other 109cd1a3f68Sths messyness. */ 110cd1a3f68Sths ptimer_stop(s->timer); 111cd1a3f68Sths } 112cd1a3f68Sths freq = s->freq; 113cd1a3f68Sths /* ??? Need to recalculate expiry time after changing divisor. */ 114cd1a3f68Sths switch (value & TIMER_TCR_TPSC) { 115cd1a3f68Sths case 0: freq >>= 2; break; 116cd1a3f68Sths case 1: freq >>= 4; break; 117cd1a3f68Sths case 2: freq >>= 6; break; 118cd1a3f68Sths case 3: freq >>= 8; break; 119cd1a3f68Sths case 4: freq >>= 10; break; 120cd1a3f68Sths case 6: 1212f5af2dcSThomas Huth case 7: 1222f5af2dcSThomas Huth if (s->feat & TIMER_FEAT_EXTCLK) { 1232f5af2dcSThomas Huth break; 1242f5af2dcSThomas Huth } 125*97edd8baSThomas Huth /* fallthrough */ 1262f5af2dcSThomas Huth default: 1272f5af2dcSThomas Huth hw_error("sh_timer_write: Reserved TPSC value\n"); 1282f5af2dcSThomas Huth break; 129cd1a3f68Sths } 130cd1a3f68Sths switch ((value & TIMER_TCR_CKEG) >> 3) { 1312f5af2dcSThomas Huth case 0: 1322f5af2dcSThomas Huth break; 133cd1a3f68Sths case 1: 134cd1a3f68Sths case 2: 1352f5af2dcSThomas Huth case 3: 1362f5af2dcSThomas Huth if (s->feat & TIMER_FEAT_EXTCLK) { 1372f5af2dcSThomas Huth break; 1382f5af2dcSThomas Huth } 139*97edd8baSThomas Huth /* fallthrough */ 1402f5af2dcSThomas Huth default: 1412f5af2dcSThomas Huth hw_error("sh_timer_write: Reserved CKEG value\n"); 1422f5af2dcSThomas Huth break; 143cd1a3f68Sths } 144cd1a3f68Sths switch ((value & TIMER_TCR_ICPE) >> 6) { 1452f5af2dcSThomas Huth case 0: 1462f5af2dcSThomas Huth break; 147cd1a3f68Sths case 2: 1482f5af2dcSThomas Huth case 3: 1492f5af2dcSThomas Huth if (s->feat & TIMER_FEAT_CAPT) { 1502f5af2dcSThomas Huth break; 151cd1a3f68Sths } 152*97edd8baSThomas Huth /* fallthrough */ 1532f5af2dcSThomas Huth default: 1542f5af2dcSThomas Huth hw_error("sh_timer_write: Reserved ICPE value\n"); 1552f5af2dcSThomas Huth break; 1562f5af2dcSThomas Huth } 1572f5af2dcSThomas Huth if ((value & TIMER_TCR_UNF) == 0) { 158cd1a3f68Sths s->int_level = 0; 1592f5af2dcSThomas Huth } 160cd1a3f68Sths 161cd1a3f68Sths value &= ~TIMER_TCR_UNF; 162cd1a3f68Sths 1632f5af2dcSThomas Huth if ((value & TIMER_TCR_ICPF) && (!(s->feat & TIMER_FEAT_CAPT))) { 1642ac71179SPaul Brook hw_error("sh_timer_write: Reserved ICPF value\n"); 1652f5af2dcSThomas Huth } 166cd1a3f68Sths 167cd1a3f68Sths value &= ~TIMER_TCR_ICPF; /* capture not supported */ 168cd1a3f68Sths 1692f5af2dcSThomas Huth if (value & TIMER_TCR_RESERVED) { 1702ac71179SPaul Brook hw_error("sh_timer_write: Reserved TCR bits set\n"); 1712f5af2dcSThomas Huth } 172cd1a3f68Sths s->tcr = value; 173cd1a3f68Sths ptimer_set_limit(s->timer, s->tcor, 0); 174cd1a3f68Sths ptimer_set_freq(s->timer, freq); 175cd1a3f68Sths if (s->enabled) { 176cd1a3f68Sths /* Restart the timer if still enabled. */ 177cd1a3f68Sths ptimer_run(s->timer, 0); 178cd1a3f68Sths } 17928015830SPeter Maydell ptimer_transaction_commit(s->timer); 180cd1a3f68Sths break; 181e7786f27Saurel32 case OFFSET_TCPR: 182cd1a3f68Sths if (s->feat & TIMER_FEAT_CAPT) { 183cd1a3f68Sths s->tcpr = value; 184cd1a3f68Sths break; 185cd1a3f68Sths } 186*97edd8baSThomas Huth /* fallthrough */ 187cd1a3f68Sths default: 1882ac71179SPaul Brook hw_error("sh_timer_write: Bad offset %x\n", (int)offset); 189cd1a3f68Sths } 190cd1a3f68Sths sh_timer_update(s); 191cd1a3f68Sths } 192cd1a3f68Sths 193cd1a3f68Sths static void sh_timer_start_stop(void *opaque, int enable) 194cd1a3f68Sths { 195cd1a3f68Sths sh_timer_state *s = (sh_timer_state *)opaque; 196cd1a3f68Sths 197cd1a3f68Sths #ifdef DEBUG_TIMER 198cd1a3f68Sths printf("sh_timer_start_stop %d (%d)\n", enable, s->enabled); 199cd1a3f68Sths #endif 200cd1a3f68Sths 20128015830SPeter Maydell ptimer_transaction_begin(s->timer); 202cd1a3f68Sths if (s->enabled && !enable) { 203cd1a3f68Sths ptimer_stop(s->timer); 204cd1a3f68Sths } 205cd1a3f68Sths if (!s->enabled && enable) { 206cd1a3f68Sths ptimer_run(s->timer, 0); 207cd1a3f68Sths } 20828015830SPeter Maydell ptimer_transaction_commit(s->timer); 209cd1a3f68Sths s->enabled = !!enable; 210cd1a3f68Sths 211cd1a3f68Sths #ifdef DEBUG_TIMER 212cd1a3f68Sths printf("sh_timer_start_stop done %d\n", s->enabled); 213cd1a3f68Sths #endif 214cd1a3f68Sths } 215cd1a3f68Sths 216cd1a3f68Sths static void sh_timer_tick(void *opaque) 217cd1a3f68Sths { 218cd1a3f68Sths sh_timer_state *s = (sh_timer_state *)opaque; 219cd1a3f68Sths s->int_level = s->enabled; 220cd1a3f68Sths sh_timer_update(s); 221cd1a3f68Sths } 222cd1a3f68Sths 22396e2fc41Saurel32 static void *sh_timer_init(uint32_t freq, int feat, qemu_irq irq) 224cd1a3f68Sths { 225cd1a3f68Sths sh_timer_state *s; 226cd1a3f68Sths 2277267c094SAnthony Liguori s = (sh_timer_state *)g_malloc0(sizeof(sh_timer_state)); 228cd1a3f68Sths s->freq = freq; 229cd1a3f68Sths s->feat = feat; 230cd1a3f68Sths s->tcor = 0xffffffff; 231cd1a3f68Sths s->tcnt = 0xffffffff; 232cd1a3f68Sths s->tcpr = 0xdeadbeef; 233e7786f27Saurel32 s->tcr = 0; 234cd1a3f68Sths s->enabled = 0; 235703243a0Sbalrog s->irq = irq; 236cd1a3f68Sths 23728015830SPeter Maydell s->timer = ptimer_init(sh_timer_tick, s, PTIMER_POLICY_DEFAULT); 238e7786f27Saurel32 239e7786f27Saurel32 sh_timer_write(s, OFFSET_TCOR >> 2, s->tcor); 240e7786f27Saurel32 sh_timer_write(s, OFFSET_TCNT >> 2, s->tcnt); 241e7786f27Saurel32 sh_timer_write(s, OFFSET_TCPR >> 2, s->tcpr); 242e7786f27Saurel32 sh_timer_write(s, OFFSET_TCR >> 2, s->tcpr); 243cd1a3f68Sths /* ??? Save/restore. */ 244cd1a3f68Sths return s; 245cd1a3f68Sths } 246cd1a3f68Sths 247cd1a3f68Sths typedef struct { 24889e29451SBenoît Canet MemoryRegion iomem; 24989e29451SBenoît Canet MemoryRegion iomem_p4; 25089e29451SBenoît Canet MemoryRegion iomem_a7; 251cd1a3f68Sths void *timer[3]; 252cd1a3f68Sths int level[3]; 253cd1a3f68Sths uint32_t tocr; 254cd1a3f68Sths uint32_t tstr; 255cd1a3f68Sths int feat; 256cd1a3f68Sths } tmu012_state; 257cd1a3f68Sths 258a8170e5eSAvi Kivity static uint64_t tmu012_read(void *opaque, hwaddr offset, 25989e29451SBenoît Canet unsigned size) 260cd1a3f68Sths { 261cd1a3f68Sths tmu012_state *s = (tmu012_state *)opaque; 262cd1a3f68Sths 263cd1a3f68Sths #ifdef DEBUG_TIMER 264cd1a3f68Sths printf("tmu012_read 0x%lx\n", (unsigned long) offset); 265cd1a3f68Sths #endif 266cd1a3f68Sths 267cd1a3f68Sths if (offset >= 0x20) { 2682f5af2dcSThomas Huth if (!(s->feat & TMU012_FEAT_3CHAN)) { 2692ac71179SPaul Brook hw_error("tmu012_write: Bad channel offset %x\n", (int)offset); 2702f5af2dcSThomas Huth } 271cd1a3f68Sths return sh_timer_read(s->timer[2], offset - 0x20); 272cd1a3f68Sths } 273cd1a3f68Sths 274cd1a3f68Sths if (offset >= 0x14) 275cd1a3f68Sths return sh_timer_read(s->timer[1], offset - 0x14); 276cd1a3f68Sths 277cd1a3f68Sths if (offset >= 0x08) 278cd1a3f68Sths return sh_timer_read(s->timer[0], offset - 0x08); 279cd1a3f68Sths 280cd1a3f68Sths if (offset == 4) 281cd1a3f68Sths return s->tstr; 282cd1a3f68Sths 283cd1a3f68Sths if ((s->feat & TMU012_FEAT_TOCR) && offset == 0) 284cd1a3f68Sths return s->tocr; 285cd1a3f68Sths 2862ac71179SPaul Brook hw_error("tmu012_write: Bad offset %x\n", (int)offset); 287cd1a3f68Sths return 0; 288cd1a3f68Sths } 289cd1a3f68Sths 290a8170e5eSAvi Kivity static void tmu012_write(void *opaque, hwaddr offset, 29189e29451SBenoît Canet uint64_t value, unsigned size) 292cd1a3f68Sths { 293cd1a3f68Sths tmu012_state *s = (tmu012_state *)opaque; 294cd1a3f68Sths 295cd1a3f68Sths #ifdef DEBUG_TIMER 296cd1a3f68Sths printf("tmu012_write 0x%lx 0x%08x\n", (unsigned long) offset, value); 297cd1a3f68Sths #endif 298cd1a3f68Sths 299cd1a3f68Sths if (offset >= 0x20) { 3002f5af2dcSThomas Huth if (!(s->feat & TMU012_FEAT_3CHAN)) { 3012ac71179SPaul Brook hw_error("tmu012_write: Bad channel offset %x\n", (int)offset); 3022f5af2dcSThomas Huth } 303cd1a3f68Sths sh_timer_write(s->timer[2], offset - 0x20, value); 304cd1a3f68Sths return; 305cd1a3f68Sths } 306cd1a3f68Sths 307cd1a3f68Sths if (offset >= 0x14) { 308cd1a3f68Sths sh_timer_write(s->timer[1], offset - 0x14, value); 309cd1a3f68Sths return; 310cd1a3f68Sths } 311cd1a3f68Sths 312cd1a3f68Sths if (offset >= 0x08) { 313cd1a3f68Sths sh_timer_write(s->timer[0], offset - 0x08, value); 314cd1a3f68Sths return; 315cd1a3f68Sths } 316cd1a3f68Sths 317cd1a3f68Sths if (offset == 4) { 318cd1a3f68Sths sh_timer_start_stop(s->timer[0], value & (1 << 0)); 319cd1a3f68Sths sh_timer_start_stop(s->timer[1], value & (1 << 1)); 3202f5af2dcSThomas Huth if (s->feat & TMU012_FEAT_3CHAN) { 321cd1a3f68Sths sh_timer_start_stop(s->timer[2], value & (1 << 2)); 3222f5af2dcSThomas Huth } else { 3232f5af2dcSThomas Huth if (value & (1 << 2)) { 3242ac71179SPaul Brook hw_error("tmu012_write: Bad channel\n"); 3252f5af2dcSThomas Huth } 3262f5af2dcSThomas Huth } 327cd1a3f68Sths 328cd1a3f68Sths s->tstr = value; 329cd1a3f68Sths return; 330cd1a3f68Sths } 331cd1a3f68Sths 332cd1a3f68Sths if ((s->feat & TMU012_FEAT_TOCR) && offset == 0) { 333cd1a3f68Sths s->tocr = value & (1 << 0); 334cd1a3f68Sths } 335cd1a3f68Sths } 336cd1a3f68Sths 33789e29451SBenoît Canet static const MemoryRegionOps tmu012_ops = { 33889e29451SBenoît Canet .read = tmu012_read, 33989e29451SBenoît Canet .write = tmu012_write, 34089e29451SBenoît Canet .endianness = DEVICE_NATIVE_ENDIAN, 341cd1a3f68Sths }; 342cd1a3f68Sths 343a8170e5eSAvi Kivity void tmu012_init(MemoryRegion *sysmem, hwaddr base, 34489e29451SBenoît Canet int feat, uint32_t freq, 34596e2fc41Saurel32 qemu_irq ch0_irq, qemu_irq ch1_irq, 34696e2fc41Saurel32 qemu_irq ch2_irq0, qemu_irq ch2_irq1) 347cd1a3f68Sths { 348cd1a3f68Sths tmu012_state *s; 349cd1a3f68Sths int timer_feat = (feat & TMU012_FEAT_EXTCLK) ? TIMER_FEAT_EXTCLK : 0; 350cd1a3f68Sths 3517267c094SAnthony Liguori s = (tmu012_state *)g_malloc0(sizeof(tmu012_state)); 352cd1a3f68Sths s->feat = feat; 353703243a0Sbalrog s->timer[0] = sh_timer_init(freq, timer_feat, ch0_irq); 354703243a0Sbalrog s->timer[1] = sh_timer_init(freq, timer_feat, ch1_irq); 3552f5af2dcSThomas Huth if (feat & TMU012_FEAT_3CHAN) { 356703243a0Sbalrog s->timer[2] = sh_timer_init(freq, timer_feat | TIMER_FEAT_CAPT, 357703243a0Sbalrog ch2_irq0); /* ch2_irq1 not supported */ 3582f5af2dcSThomas Huth } 35989e29451SBenoît Canet 3602c9b15caSPaolo Bonzini memory_region_init_io(&s->iomem, NULL, &tmu012_ops, s, 36189e29451SBenoît Canet "timer", 0x100000000ULL); 36289e29451SBenoît Canet 3632c9b15caSPaolo Bonzini memory_region_init_alias(&s->iomem_p4, NULL, "timer-p4", 36489e29451SBenoît Canet &s->iomem, 0, 0x1000); 36589e29451SBenoît Canet memory_region_add_subregion(sysmem, P4ADDR(base), &s->iomem_p4); 36689e29451SBenoît Canet 3672c9b15caSPaolo Bonzini memory_region_init_alias(&s->iomem_a7, NULL, "timer-a7", 36889e29451SBenoît Canet &s->iomem, 0, 0x1000); 36989e29451SBenoît Canet memory_region_add_subregion(sysmem, A7ADDR(base), &s->iomem_a7); 370cd1a3f68Sths /* ??? Save/restore. */ 371cd1a3f68Sths } 372