]> git.proxmox.com Git - mirror_qemu.git/blame - hw/timer/slavio_timer.c
i.MX: Standardize i.MX GPT debug
[mirror_qemu.git] / hw / timer / slavio_timer.c
CommitLineData
e80cfcfc
FB
1/*
2 * QEMU Sparc SLAVIO timer controller emulation
3 *
66321a11 4 * Copyright (c) 2003-2005 Fabrice Bellard
5fafdf24 5 *
e80cfcfc
FB
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
c70c59ee 24
0d09e41a 25#include "hw/sparc/sun4m.h"
1de7afc9 26#include "qemu/timer.h"
83c9f4ca
PB
27#include "hw/ptimer.h"
28#include "hw/sysbus.h"
97bf4851 29#include "trace.h"
6a1751b7 30#include "qemu/main-loop.h"
66321a11 31
e80cfcfc
FB
32/*
33 * Registers of hardware timer in sun4m.
34 *
35 * This is the timer/counter part of chip STP2001 (Slave I/O), also
36 * produced as NCR89C105. See
37 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
5fafdf24 38 *
e80cfcfc
FB
39 * The 31-bit counter is incremented every 500ns by bit 9. Bits 8..0
40 * are zero. Bit 31 is 1 when count has been reached.
41 *
ba3c64fb
FB
42 * Per-CPU timers interrupt local CPU, system timer uses normal
43 * interrupt routing.
44 *
e80cfcfc
FB
45 */
46
81732d19
BS
47#define MAX_CPUS 16
48
7204ff9c 49typedef struct CPUTimerState {
d7edfd27 50 qemu_irq irq;
8d05ea8a
BS
51 ptimer_state *timer;
52 uint32_t count, counthigh, reached;
f90074f4 53 /* processor only */
ead4cf04 54 uint32_t run;
f90074f4 55 uint64_t limit;
7204ff9c
BS
56} CPUTimerState;
57
c275471e
AF
58#define TYPE_SLAVIO_TIMER "slavio_timer"
59#define SLAVIO_TIMER(obj) \
60 OBJECT_CHECK(SLAVIO_TIMERState, (obj), TYPE_SLAVIO_TIMER)
61
7204ff9c 62typedef struct SLAVIO_TIMERState {
c275471e
AF
63 SysBusDevice parent_obj;
64
7204ff9c 65 uint32_t num_cpus;
7204ff9c 66 uint32_t cputimer_mode;
f90074f4 67 CPUTimerState cputimer[MAX_CPUS + 1];
e80cfcfc
FB
68} SLAVIO_TIMERState;
69
7204ff9c 70typedef struct TimerContext {
a3d12d07 71 MemoryRegion iomem;
7204ff9c
BS
72 SLAVIO_TIMERState *s;
73 unsigned int timer_index; /* 0 for system, 1 ... MAX_CPUS for CPU timers */
74} TimerContext;
75
115646b6 76#define SYS_TIMER_SIZE 0x14
81732d19 77#define CPU_TIMER_SIZE 0x10
e80cfcfc 78
d2c38b24
BS
79#define TIMER_LIMIT 0
80#define TIMER_COUNTER 1
81#define TIMER_COUNTER_NORST 2
82#define TIMER_STATUS 3
83#define TIMER_MODE 4
84
85#define TIMER_COUNT_MASK32 0xfffffe00
86#define TIMER_LIMIT_MASK32 0x7fffffff
87#define TIMER_MAX_COUNT64 0x7ffffffffffffe00ULL
88#define TIMER_MAX_COUNT32 0x7ffffe00ULL
89#define TIMER_REACHED 0x80000000
90#define TIMER_PERIOD 500ULL // 500ns
68fb89a2
BS
91#define LIMIT_TO_PERIODS(l) (((l) >> 9) - 1)
92#define PERIODS_TO_LIMIT(l) (((l) + 1) << 9)
d2c38b24 93
7204ff9c 94static int slavio_timer_is_user(TimerContext *tc)
115646b6 95{
7204ff9c
BS
96 SLAVIO_TIMERState *s = tc->s;
97 unsigned int timer_index = tc->timer_index;
98
99 return timer_index != 0 && (s->cputimer_mode & (1 << (timer_index - 1)));
115646b6
BS
100}
101
e80cfcfc 102// Update count, set irq, update expire_time
8d05ea8a 103// Convert from ptimer countdown units
7204ff9c 104static void slavio_timer_get_out(CPUTimerState *t)
e80cfcfc 105{
bd7e2875 106 uint64_t count, limit;
e80cfcfc 107
7204ff9c 108 if (t->limit == 0) { /* free-run system or processor counter */
bd7e2875 109 limit = TIMER_MAX_COUNT32;
7204ff9c
BS
110 } else {
111 limit = t->limit;
112 }
9ebec28b
BS
113 count = limit - PERIODS_TO_LIMIT(ptimer_get_count(t->timer));
114
97bf4851 115 trace_slavio_timer_get_out(t->limit, t->counthigh, t->count);
7204ff9c
BS
116 t->count = count & TIMER_COUNT_MASK32;
117 t->counthigh = count >> 32;
e80cfcfc
FB
118}
119
120// timer callback
121static void slavio_timer_irq(void *opaque)
122{
7204ff9c
BS
123 TimerContext *tc = opaque;
124 SLAVIO_TIMERState *s = tc->s;
125 CPUTimerState *t = &s->cputimer[tc->timer_index];
126
127 slavio_timer_get_out(t);
97bf4851 128 trace_slavio_timer_irq(t->counthigh, t->count);
68fb89a2
BS
129 /* if limit is 0 (free-run), there will be no match */
130 if (t->limit != 0) {
131 t->reached = TIMER_REACHED;
132 }
452efba6
BS
133 /* there is no interrupt if user timer or free-run */
134 if (!slavio_timer_is_user(tc) && t->limit != 0) {
7204ff9c
BS
135 qemu_irq_raise(t->irq);
136 }
e80cfcfc
FB
137}
138
a8170e5e 139static uint64_t slavio_timer_mem_readl(void *opaque, hwaddr addr,
a3d12d07 140 unsigned size)
e80cfcfc 141{
7204ff9c
BS
142 TimerContext *tc = opaque;
143 SLAVIO_TIMERState *s = tc->s;
8d05ea8a 144 uint32_t saddr, ret;
7204ff9c
BS
145 unsigned int timer_index = tc->timer_index;
146 CPUTimerState *t = &s->cputimer[timer_index];
e80cfcfc 147
e64d7d59 148 saddr = addr >> 2;
e80cfcfc 149 switch (saddr) {
d2c38b24 150 case TIMER_LIMIT:
f930d07e
BS
151 // read limit (system counter mode) or read most signifying
152 // part of counter (user mode)
7204ff9c 153 if (slavio_timer_is_user(tc)) {
115646b6 154 // read user timer MSW
7204ff9c
BS
155 slavio_timer_get_out(t);
156 ret = t->counthigh | t->reached;
115646b6
BS
157 } else {
158 // read limit
f930d07e 159 // clear irq
7204ff9c
BS
160 qemu_irq_lower(t->irq);
161 t->reached = 0;
162 ret = t->limit & TIMER_LIMIT_MASK32;
f930d07e 163 }
8d05ea8a 164 break;
d2c38b24 165 case TIMER_COUNTER:
f930d07e
BS
166 // read counter and reached bit (system mode) or read lsbits
167 // of counter (user mode)
7204ff9c
BS
168 slavio_timer_get_out(t);
169 if (slavio_timer_is_user(tc)) { // read user timer LSW
170 ret = t->count & TIMER_MAX_COUNT64;
171 } else { // read limit
172 ret = (t->count & TIMER_MAX_COUNT32) |
173 t->reached;
174 }
8d05ea8a 175 break;
d2c38b24 176 case TIMER_STATUS:
115646b6 177 // only available in processor counter/timer
f930d07e 178 // read start/stop status
7204ff9c 179 if (timer_index > 0) {
ead4cf04 180 ret = t->run;
7204ff9c
BS
181 } else {
182 ret = 0;
183 }
8d05ea8a 184 break;
d2c38b24 185 case TIMER_MODE:
115646b6 186 // only available in system counter
f930d07e 187 // read user/system mode
7204ff9c 188 ret = s->cputimer_mode;
8d05ea8a 189 break;
e80cfcfc 190 default:
97bf4851 191 trace_slavio_timer_mem_readl_invalid(addr);
8d05ea8a
BS
192 ret = 0;
193 break;
e80cfcfc 194 }
97bf4851 195 trace_slavio_timer_mem_readl(addr, ret);
8d05ea8a 196 return ret;
e80cfcfc
FB
197}
198
a8170e5e 199static void slavio_timer_mem_writel(void *opaque, hwaddr addr,
a3d12d07 200 uint64_t val, unsigned size)
e80cfcfc 201{
7204ff9c
BS
202 TimerContext *tc = opaque;
203 SLAVIO_TIMERState *s = tc->s;
e80cfcfc 204 uint32_t saddr;
7204ff9c
BS
205 unsigned int timer_index = tc->timer_index;
206 CPUTimerState *t = &s->cputimer[timer_index];
e80cfcfc 207
97bf4851 208 trace_slavio_timer_mem_writel(addr, val);
e64d7d59 209 saddr = addr >> 2;
e80cfcfc 210 switch (saddr) {
d2c38b24 211 case TIMER_LIMIT:
7204ff9c 212 if (slavio_timer_is_user(tc)) {
e1cb9502
BS
213 uint64_t count;
214
115646b6 215 // set user counter MSW, reset counter
7204ff9c
BS
216 t->limit = TIMER_MAX_COUNT64;
217 t->counthigh = val & (TIMER_MAX_COUNT64 >> 32);
218 t->reached = 0;
219 count = ((uint64_t)t->counthigh << 32) | t->count;
97bf4851 220 trace_slavio_timer_mem_writel_limit(timer_index, count);
9ebec28b 221 ptimer_set_count(t->timer, LIMIT_TO_PERIODS(t->limit - count));
115646b6
BS
222 } else {
223 // set limit, reset counter
7204ff9c
BS
224 qemu_irq_lower(t->irq);
225 t->limit = val & TIMER_MAX_COUNT32;
226 if (t->timer) {
227 if (t->limit == 0) { /* free-run */
228 ptimer_set_limit(t->timer,
77f193da 229 LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
7204ff9c
BS
230 } else {
231 ptimer_set_limit(t->timer, LIMIT_TO_PERIODS(t->limit), 1);
232 }
85e3023e 233 }
81732d19 234 }
115646b6 235 break;
d2c38b24 236 case TIMER_COUNTER:
7204ff9c 237 if (slavio_timer_is_user(tc)) {
e1cb9502
BS
238 uint64_t count;
239
115646b6 240 // set user counter LSW, reset counter
7204ff9c
BS
241 t->limit = TIMER_MAX_COUNT64;
242 t->count = val & TIMER_MAX_COUNT64;
243 t->reached = 0;
244 count = ((uint64_t)t->counthigh) << 32 | t->count;
97bf4851 245 trace_slavio_timer_mem_writel_limit(timer_index, count);
9ebec28b 246 ptimer_set_count(t->timer, LIMIT_TO_PERIODS(t->limit - count));
97bf4851
BS
247 } else {
248 trace_slavio_timer_mem_writel_counter_invalid();
249 }
115646b6 250 break;
d2c38b24 251 case TIMER_COUNTER_NORST:
f930d07e 252 // set limit without resetting counter
7204ff9c 253 t->limit = val & TIMER_MAX_COUNT32;
9ebec28b
BS
254 if (t->limit == 0) { /* free-run */
255 ptimer_set_limit(t->timer, LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 0);
256 } else {
257 ptimer_set_limit(t->timer, LIMIT_TO_PERIODS(t->limit), 0);
85e3023e 258 }
f930d07e 259 break;
d2c38b24 260 case TIMER_STATUS:
7204ff9c 261 if (slavio_timer_is_user(tc)) {
115646b6 262 // start/stop user counter
ead4cf04 263 if (val & 1) {
97bf4851 264 trace_slavio_timer_mem_writel_status_start(timer_index);
9ebec28b 265 ptimer_run(t->timer, 0);
ead4cf04 266 } else {
97bf4851 267 trace_slavio_timer_mem_writel_status_stop(timer_index);
9ebec28b 268 ptimer_stop(t->timer);
f930d07e
BS
269 }
270 }
ead4cf04 271 t->run = val & 1;
f930d07e 272 break;
d2c38b24 273 case TIMER_MODE:
7204ff9c 274 if (timer_index == 0) {
81732d19
BS
275 unsigned int i;
276
7204ff9c 277 for (i = 0; i < s->num_cpus; i++) {
67e42751 278 unsigned int processor = 1 << i;
7204ff9c 279 CPUTimerState *curr_timer = &s->cputimer[i + 1];
67e42751
BS
280
281 // check for a change in timer mode for this processor
7204ff9c 282 if ((val & processor) != (s->cputimer_mode & processor)) {
67e42751 283 if (val & processor) { // counter -> user timer
7204ff9c 284 qemu_irq_lower(curr_timer->irq);
67e42751 285 // counters are always running
ead4cf04
MCA
286 if (!curr_timer->run) {
287 ptimer_stop(curr_timer->timer);
288 }
67e42751 289 // user timer limit is always the same
7204ff9c
BS
290 curr_timer->limit = TIMER_MAX_COUNT64;
291 ptimer_set_limit(curr_timer->timer,
292 LIMIT_TO_PERIODS(curr_timer->limit),
77f193da 293 1);
67e42751
BS
294 // set this processors user timer bit in config
295 // register
7204ff9c 296 s->cputimer_mode |= processor;
97bf4851 297 trace_slavio_timer_mem_writel_mode_user(timer_index);
67e42751 298 } else { // user timer -> counter
67e42751 299 // start the counter
7204ff9c 300 ptimer_run(curr_timer->timer, 0);
67e42751
BS
301 // clear this processors user timer bit in config
302 // register
7204ff9c 303 s->cputimer_mode &= ~processor;
97bf4851 304 trace_slavio_timer_mem_writel_mode_counter(timer_index);
67e42751 305 }
115646b6 306 }
81732d19 307 }
7204ff9c 308 } else {
97bf4851 309 trace_slavio_timer_mem_writel_mode_invalid();
7204ff9c 310 }
f930d07e 311 break;
e80cfcfc 312 default:
97bf4851 313 trace_slavio_timer_mem_writel_invalid(addr);
f930d07e 314 break;
e80cfcfc
FB
315 }
316}
317
a3d12d07
BC
318static const MemoryRegionOps slavio_timer_mem_ops = {
319 .read = slavio_timer_mem_readl,
320 .write = slavio_timer_mem_writel,
321 .endianness = DEVICE_NATIVE_ENDIAN,
322 .valid = {
323 .min_access_size = 4,
324 .max_access_size = 4,
325 },
e80cfcfc
FB
326};
327
f4b19cd0
BS
328static const VMStateDescription vmstate_timer = {
329 .name ="timer",
330 .version_id = 3,
331 .minimum_version_id = 3,
35d08458 332 .fields = (VMStateField[]) {
f4b19cd0
BS
333 VMSTATE_UINT64(limit, CPUTimerState),
334 VMSTATE_UINT32(count, CPUTimerState),
335 VMSTATE_UINT32(counthigh, CPUTimerState),
336 VMSTATE_UINT32(reached, CPUTimerState),
ead4cf04 337 VMSTATE_UINT32(run , CPUTimerState),
f4b19cd0
BS
338 VMSTATE_PTIMER(timer, CPUTimerState),
339 VMSTATE_END_OF_LIST()
7204ff9c 340 }
f4b19cd0 341};
e80cfcfc 342
f4b19cd0
BS
343static const VMStateDescription vmstate_slavio_timer = {
344 .name ="slavio_timer",
345 .version_id = 3,
346 .minimum_version_id = 3,
35d08458 347 .fields = (VMStateField[]) {
f4b19cd0
BS
348 VMSTATE_STRUCT_ARRAY(cputimer, SLAVIO_TIMERState, MAX_CPUS + 1, 3,
349 vmstate_timer, CPUTimerState),
350 VMSTATE_END_OF_LIST()
7204ff9c 351 }
f4b19cd0 352};
e80cfcfc 353
0e0bfeea 354static void slavio_timer_reset(DeviceState *d)
e80cfcfc 355{
c275471e 356 SLAVIO_TIMERState *s = SLAVIO_TIMER(d);
7204ff9c
BS
357 unsigned int i;
358 CPUTimerState *curr_timer;
359
360 for (i = 0; i <= MAX_CPUS; i++) {
361 curr_timer = &s->cputimer[i];
362 curr_timer->limit = 0;
363 curr_timer->count = 0;
364 curr_timer->reached = 0;
5933e8a9 365 if (i <= s->num_cpus) {
7204ff9c
BS
366 ptimer_set_limit(curr_timer->timer,
367 LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
368 ptimer_run(curr_timer->timer, 0);
ead4cf04 369 curr_timer->run = 1;
7204ff9c 370 }
85e3023e 371 }
7204ff9c 372 s->cputimer_mode = 0;
e80cfcfc
FB
373}
374
81a322d4 375static int slavio_timer_init1(SysBusDevice *dev)
c70c59ee 376{
c275471e 377 SLAVIO_TIMERState *s = SLAVIO_TIMER(dev);
8d05ea8a 378 QEMUBH *bh;
7204ff9c
BS
379 unsigned int i;
380 TimerContext *tc;
e80cfcfc 381
7204ff9c 382 for (i = 0; i <= MAX_CPUS; i++) {
a3d12d07
BC
383 uint64_t size;
384 char timer_name[20];
385
7267c094 386 tc = g_malloc0(sizeof(TimerContext));
7204ff9c
BS
387 tc->s = s;
388 tc->timer_index = i;
c70c59ee 389
7204ff9c
BS
390 bh = qemu_bh_new(slavio_timer_irq, tc);
391 s->cputimer[i].timer = ptimer_init(bh);
392 ptimer_set_period(s->cputimer[i].timer, TIMER_PERIOD);
e80cfcfc 393
a3d12d07
BC
394 size = i == 0 ? SYS_TIMER_SIZE : CPU_TIMER_SIZE;
395 snprintf(timer_name, sizeof(timer_name), "timer-%i", i);
853dca12 396 memory_region_init_io(&tc->iomem, OBJECT(s), &slavio_timer_mem_ops, tc,
a3d12d07 397 timer_name, size);
750ecd44 398 sysbus_init_mmio(dev, &tc->iomem);
7204ff9c
BS
399
400 sysbus_init_irq(dev, &s->cputimer[i].irq);
c70c59ee
BS
401 }
402
81a322d4 403 return 0;
81732d19
BS
404}
405
999e12bb
AL
406static Property slavio_timer_properties[] = {
407 DEFINE_PROP_UINT32("num_cpus", SLAVIO_TIMERState, num_cpus, 0),
408 DEFINE_PROP_END_OF_LIST(),
409};
410
411static void slavio_timer_class_init(ObjectClass *klass, void *data)
412{
39bffca2 413 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb
AL
414 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
415
416 k->init = slavio_timer_init1;
39bffca2
AL
417 dc->reset = slavio_timer_reset;
418 dc->vmsd = &vmstate_slavio_timer;
419 dc->props = slavio_timer_properties;
999e12bb
AL
420}
421
8c43a6f0 422static const TypeInfo slavio_timer_info = {
c275471e 423 .name = TYPE_SLAVIO_TIMER,
39bffca2
AL
424 .parent = TYPE_SYS_BUS_DEVICE,
425 .instance_size = sizeof(SLAVIO_TIMERState),
426 .class_init = slavio_timer_class_init,
c70c59ee
BS
427};
428
83f7d43a 429static void slavio_timer_register_types(void)
c70c59ee 430{
39bffca2 431 type_register_static(&slavio_timer_info);
c70c59ee
BS
432}
433
83f7d43a 434type_init(slavio_timer_register_types)