1 /* 2 * ASPEED AST2400 Timer 3 * 4 * Andrew Jeffery <andrew@aj.id.au> 5 * 6 * Copyright (C) 2016 IBM Corp. 7 * 8 * This code is licensed under the GPL version 2 or later. See 9 * the COPYING file in the top-level directory. 10 */ 11 12 #include "qemu/osdep.h" 13 #include "hw/ptimer.h" 14 #include "hw/sysbus.h" 15 #include "hw/timer/aspeed_timer.h" 16 #include "qemu-common.h" 17 #include "qemu/bitops.h" 18 #include "qemu/main-loop.h" 19 #include "qemu/timer.h" 20 #include "qemu/log.h" 21 #include "trace.h" 22 23 #define TIMER_NR_REGS 4 24 25 #define TIMER_CTRL_BITS 4 26 #define TIMER_CTRL_MASK ((1 << TIMER_CTRL_BITS) - 1) 27 28 #define TIMER_CLOCK_USE_EXT true 29 #define TIMER_CLOCK_EXT_HZ 1000000 30 #define TIMER_CLOCK_USE_APB false 31 #define TIMER_CLOCK_APB_HZ 24000000 32 33 #define TIMER_REG_STATUS 0 34 #define TIMER_REG_RELOAD 1 35 #define TIMER_REG_MATCH_FIRST 2 36 #define TIMER_REG_MATCH_SECOND 3 37 38 #define TIMER_FIRST_CAP_PULSE 4 39 40 enum timer_ctrl_op { 41 op_enable = 0, 42 op_external_clock, 43 op_overflow_interrupt, 44 op_pulse_enable 45 }; 46 47 /** 48 * Avoid mutual references between AspeedTimerCtrlState and AspeedTimer 49 * structs, as it's a waste of memory. The ptimer BH callback needs to know 50 * whether a specific AspeedTimer is enabled, but this information is held in 51 * AspeedTimerCtrlState. So, provide a helper to hoist ourselves from an 52 * arbitrary AspeedTimer to AspeedTimerCtrlState. 53 */ 54 static inline AspeedTimerCtrlState *timer_to_ctrl(AspeedTimer *t) 55 { 56 const AspeedTimer (*timers)[] = (void *)t - (t->id * sizeof(*t)); 57 return container_of(timers, AspeedTimerCtrlState, timers); 58 } 59 60 static inline bool timer_ctrl_status(AspeedTimer *t, enum timer_ctrl_op op) 61 { 62 return !!(timer_to_ctrl(t)->ctrl & BIT(t->id * TIMER_CTRL_BITS + op)); 63 } 64 65 static inline bool timer_enabled(AspeedTimer *t) 66 { 67 return timer_ctrl_status(t, op_enable); 68 } 69 70 static inline bool timer_overflow_interrupt(AspeedTimer *t) 71 { 72 return timer_ctrl_status(t, op_overflow_interrupt); 73 } 74 75 static inline bool timer_can_pulse(AspeedTimer *t) 76 { 77 return t->id >= TIMER_FIRST_CAP_PULSE; 78 } 79 80 static void aspeed_timer_expire(void *opaque) 81 { 82 AspeedTimer *t = opaque; 83 84 /* Only support interrupts on match values of zero for the moment - this is 85 * sufficient to boot an aspeed_defconfig Linux kernel. 86 * 87 * TODO: matching on arbitrary values (see e.g. hw/timer/a9gtimer.c) 88 */ 89 bool match = !(t->match[0] && t->match[1]); 90 bool interrupt = timer_overflow_interrupt(t) || match; 91 if (timer_enabled(t) && interrupt) { 92 t->level = !t->level; 93 qemu_set_irq(t->irq, t->level); 94 } 95 } 96 97 static uint64_t aspeed_timer_get_value(AspeedTimer *t, int reg) 98 { 99 uint64_t value; 100 101 switch (reg) { 102 case TIMER_REG_STATUS: 103 value = ptimer_get_count(t->timer); 104 break; 105 case TIMER_REG_RELOAD: 106 value = t->reload; 107 break; 108 case TIMER_REG_MATCH_FIRST: 109 case TIMER_REG_MATCH_SECOND: 110 value = t->match[reg - 2]; 111 break; 112 default: 113 qemu_log_mask(LOG_UNIMP, "%s: Programming error: unexpected reg: %d\n", 114 __func__, reg); 115 value = 0; 116 break; 117 } 118 return value; 119 } 120 121 static uint64_t aspeed_timer_read(void *opaque, hwaddr offset, unsigned size) 122 { 123 AspeedTimerCtrlState *s = opaque; 124 const int reg = (offset & 0xf) / 4; 125 uint64_t value; 126 127 switch (offset) { 128 case 0x30: /* Control Register */ 129 value = s->ctrl; 130 break; 131 case 0x34: /* Control Register 2 */ 132 value = s->ctrl2; 133 break; 134 case 0x00 ... 0x2c: /* Timers 1 - 4 */ 135 value = aspeed_timer_get_value(&s->timers[(offset >> 4)], reg); 136 break; 137 case 0x40 ... 0x8c: /* Timers 5 - 8 */ 138 value = aspeed_timer_get_value(&s->timers[(offset >> 4) - 1], reg); 139 break; 140 /* Illegal */ 141 case 0x38: 142 case 0x3C: 143 default: 144 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n", 145 __func__, offset); 146 value = 0; 147 break; 148 } 149 trace_aspeed_timer_read(offset, size, value); 150 return value; 151 } 152 153 static void aspeed_timer_set_value(AspeedTimerCtrlState *s, int timer, int reg, 154 uint32_t value) 155 { 156 AspeedTimer *t; 157 158 trace_aspeed_timer_set_value(timer, reg, value); 159 t = &s->timers[timer]; 160 switch (reg) { 161 case TIMER_REG_STATUS: 162 if (timer_enabled(t)) { 163 ptimer_set_count(t->timer, value); 164 } 165 break; 166 case TIMER_REG_RELOAD: 167 t->reload = value; 168 ptimer_set_limit(t->timer, value, 1); 169 break; 170 case TIMER_REG_MATCH_FIRST: 171 case TIMER_REG_MATCH_SECOND: 172 if (value) { 173 /* Non-zero match values are unsupported. As such an interrupt will 174 * always be triggered when the timer reaches zero even if the 175 * overflow interrupt control bit is clear. 176 */ 177 qemu_log_mask(LOG_UNIMP, "%s: Match value unsupported by device: " 178 "0x%" PRIx32 "\n", __func__, value); 179 } else { 180 t->match[reg - 2] = value; 181 } 182 break; 183 default: 184 qemu_log_mask(LOG_UNIMP, "%s: Programming error: unexpected reg: %d\n", 185 __func__, reg); 186 break; 187 } 188 } 189 190 /* Control register operations are broken out into helpers that can be 191 * explicitly called on aspeed_timer_reset(), but also from 192 * aspeed_timer_ctrl_op(). 193 */ 194 195 static void aspeed_timer_ctrl_enable(AspeedTimer *t, bool enable) 196 { 197 trace_aspeed_timer_ctrl_enable(t->id, enable); 198 if (enable) { 199 ptimer_run(t->timer, 0); 200 } else { 201 ptimer_stop(t->timer); 202 ptimer_set_limit(t->timer, t->reload, 1); 203 } 204 } 205 206 static void aspeed_timer_ctrl_external_clock(AspeedTimer *t, bool enable) 207 { 208 trace_aspeed_timer_ctrl_external_clock(t->id, enable); 209 if (enable) { 210 ptimer_set_freq(t->timer, TIMER_CLOCK_EXT_HZ); 211 } else { 212 ptimer_set_freq(t->timer, TIMER_CLOCK_APB_HZ); 213 } 214 } 215 216 static void aspeed_timer_ctrl_overflow_interrupt(AspeedTimer *t, bool enable) 217 { 218 trace_aspeed_timer_ctrl_overflow_interrupt(t->id, enable); 219 } 220 221 static void aspeed_timer_ctrl_pulse_enable(AspeedTimer *t, bool enable) 222 { 223 if (timer_can_pulse(t)) { 224 trace_aspeed_timer_ctrl_pulse_enable(t->id, enable); 225 } else { 226 qemu_log_mask(LOG_GUEST_ERROR, 227 "%s: Timer does not support pulse mode\n", __func__); 228 } 229 } 230 231 /** 232 * Given the actions are fixed in number and completely described in helper 233 * functions, dispatch with a lookup table rather than manage control flow with 234 * a switch statement. 235 */ 236 static void (*const ctrl_ops[])(AspeedTimer *, bool) = { 237 [op_enable] = aspeed_timer_ctrl_enable, 238 [op_external_clock] = aspeed_timer_ctrl_external_clock, 239 [op_overflow_interrupt] = aspeed_timer_ctrl_overflow_interrupt, 240 [op_pulse_enable] = aspeed_timer_ctrl_pulse_enable, 241 }; 242 243 /** 244 * Conditionally affect changes chosen by a timer's control bit. 245 * 246 * The aspeed_timer_ctrl_op() interface is convenient for the 247 * aspeed_timer_set_ctrl() function as the "no change" early exit can be 248 * calculated for all operations, which cleans up the caller code. However the 249 * interface isn't convenient for the reset function where we want to enter a 250 * specific state without artificially constructing old and new values that 251 * will fall through the change guard (and motivates extracting the actions 252 * out to helper functions). 253 * 254 * @t: The timer to manipulate 255 * @op: The type of operation to be performed 256 * @old: The old state of the timer's control bits 257 * @new: The incoming state for the timer's control bits 258 */ 259 static void aspeed_timer_ctrl_op(AspeedTimer *t, enum timer_ctrl_op op, 260 uint8_t old, uint8_t new) 261 { 262 const uint8_t mask = BIT(op); 263 const bool enable = !!(new & mask); 264 const bool changed = ((old ^ new) & mask); 265 if (!changed) { 266 return; 267 } 268 ctrl_ops[op](t, enable); 269 } 270 271 static void aspeed_timer_set_ctrl(AspeedTimerCtrlState *s, uint32_t reg) 272 { 273 int i; 274 int shift; 275 uint8_t t_old, t_new; 276 AspeedTimer *t; 277 const uint8_t enable_mask = BIT(op_enable); 278 279 /* Handle a dependency between the 'enable' and remaining three 280 * configuration bits - i.e. if more than one bit in the control set has 281 * changed, including the 'enable' bit, then we want either disable the 282 * timer and perform configuration, or perform configuration and then 283 * enable the timer 284 */ 285 for (i = 0; i < ASPEED_TIMER_NR_TIMERS; i++) { 286 t = &s->timers[i]; 287 shift = (i * TIMER_CTRL_BITS); 288 t_old = (s->ctrl >> shift) & TIMER_CTRL_MASK; 289 t_new = (reg >> shift) & TIMER_CTRL_MASK; 290 291 /* If we are disabling, do so first */ 292 if ((t_old & enable_mask) && !(t_new & enable_mask)) { 293 aspeed_timer_ctrl_enable(t, false); 294 } 295 aspeed_timer_ctrl_op(t, op_external_clock, t_old, t_new); 296 aspeed_timer_ctrl_op(t, op_overflow_interrupt, t_old, t_new); 297 aspeed_timer_ctrl_op(t, op_pulse_enable, t_old, t_new); 298 /* If we are enabling, do so last */ 299 if (!(t_old & enable_mask) && (t_new & enable_mask)) { 300 aspeed_timer_ctrl_enable(t, true); 301 } 302 } 303 s->ctrl = reg; 304 } 305 306 static void aspeed_timer_set_ctrl2(AspeedTimerCtrlState *s, uint32_t value) 307 { 308 trace_aspeed_timer_set_ctrl2(value); 309 } 310 311 static void aspeed_timer_write(void *opaque, hwaddr offset, uint64_t value, 312 unsigned size) 313 { 314 const uint32_t tv = (uint32_t)(value & 0xFFFFFFFF); 315 const int reg = (offset & 0xf) / 4; 316 AspeedTimerCtrlState *s = opaque; 317 318 switch (offset) { 319 /* Control Registers */ 320 case 0x30: 321 aspeed_timer_set_ctrl(s, tv); 322 break; 323 case 0x34: 324 aspeed_timer_set_ctrl2(s, tv); 325 break; 326 /* Timer Registers */ 327 case 0x00 ... 0x2c: 328 aspeed_timer_set_value(s, (offset >> TIMER_NR_REGS), reg, tv); 329 break; 330 case 0x40 ... 0x8c: 331 aspeed_timer_set_value(s, (offset >> TIMER_NR_REGS) - 1, reg, tv); 332 break; 333 /* Illegal */ 334 case 0x38: 335 case 0x3C: 336 default: 337 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n", 338 __func__, offset); 339 break; 340 } 341 } 342 343 static const MemoryRegionOps aspeed_timer_ops = { 344 .read = aspeed_timer_read, 345 .write = aspeed_timer_write, 346 .endianness = DEVICE_LITTLE_ENDIAN, 347 .valid.min_access_size = 4, 348 .valid.max_access_size = 4, 349 .valid.unaligned = false, 350 }; 351 352 static void aspeed_init_one_timer(AspeedTimerCtrlState *s, uint8_t id) 353 { 354 QEMUBH *bh; 355 AspeedTimer *t = &s->timers[id]; 356 357 t->id = id; 358 bh = qemu_bh_new(aspeed_timer_expire, t); 359 t->timer = ptimer_init(bh); 360 } 361 362 static void aspeed_timer_realize(DeviceState *dev, Error **errp) 363 { 364 int i; 365 SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 366 AspeedTimerCtrlState *s = ASPEED_TIMER(dev); 367 368 for (i = 0; i < ASPEED_TIMER_NR_TIMERS; i++) { 369 aspeed_init_one_timer(s, i); 370 sysbus_init_irq(sbd, &s->timers[i].irq); 371 } 372 memory_region_init_io(&s->iomem, OBJECT(s), &aspeed_timer_ops, s, 373 TYPE_ASPEED_TIMER, 0x1000); 374 sysbus_init_mmio(sbd, &s->iomem); 375 } 376 377 static void aspeed_timer_reset(DeviceState *dev) 378 { 379 int i; 380 AspeedTimerCtrlState *s = ASPEED_TIMER(dev); 381 382 for (i = 0; i < ASPEED_TIMER_NR_TIMERS; i++) { 383 AspeedTimer *t = &s->timers[i]; 384 /* Explicitly call helpers to avoid any conditional behaviour through 385 * aspeed_timer_set_ctrl(). 386 */ 387 aspeed_timer_ctrl_enable(t, false); 388 aspeed_timer_ctrl_external_clock(t, TIMER_CLOCK_USE_APB); 389 aspeed_timer_ctrl_overflow_interrupt(t, false); 390 aspeed_timer_ctrl_pulse_enable(t, false); 391 t->level = 0; 392 t->reload = 0; 393 t->match[0] = 0; 394 t->match[1] = 0; 395 } 396 s->ctrl = 0; 397 s->ctrl2 = 0; 398 } 399 400 static const VMStateDescription vmstate_aspeed_timer = { 401 .name = "aspeed.timer", 402 .version_id = 1, 403 .minimum_version_id = 1, 404 .fields = (VMStateField[]) { 405 VMSTATE_UINT8(id, AspeedTimer), 406 VMSTATE_INT32(level, AspeedTimer), 407 VMSTATE_PTIMER(timer, AspeedTimer), 408 VMSTATE_UINT32(reload, AspeedTimer), 409 VMSTATE_UINT32_ARRAY(match, AspeedTimer, 2), 410 VMSTATE_END_OF_LIST() 411 } 412 }; 413 414 static const VMStateDescription vmstate_aspeed_timer_state = { 415 .name = "aspeed.timerctrl", 416 .version_id = 1, 417 .minimum_version_id = 1, 418 .fields = (VMStateField[]) { 419 VMSTATE_UINT32(ctrl, AspeedTimerCtrlState), 420 VMSTATE_UINT32(ctrl2, AspeedTimerCtrlState), 421 VMSTATE_STRUCT_ARRAY(timers, AspeedTimerCtrlState, 422 ASPEED_TIMER_NR_TIMERS, 1, vmstate_aspeed_timer, 423 AspeedTimer), 424 VMSTATE_END_OF_LIST() 425 } 426 }; 427 428 static void timer_class_init(ObjectClass *klass, void *data) 429 { 430 DeviceClass *dc = DEVICE_CLASS(klass); 431 432 dc->realize = aspeed_timer_realize; 433 dc->reset = aspeed_timer_reset; 434 dc->desc = "ASPEED Timer"; 435 dc->vmsd = &vmstate_aspeed_timer_state; 436 } 437 438 static const TypeInfo aspeed_timer_info = { 439 .name = TYPE_ASPEED_TIMER, 440 .parent = TYPE_SYS_BUS_DEVICE, 441 .instance_size = sizeof(AspeedTimerCtrlState), 442 .class_init = timer_class_init, 443 }; 444 445 static void aspeed_timer_register_types(void) 446 { 447 type_register_static(&aspeed_timer_info); 448 } 449 450 type_init(aspeed_timer_register_types) 451