2 * Syborg Interval Timer.
4 * Copyright (c) 2008 CodeSourcery
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
26 #include "qemu-timer.h"
29 //#define DEBUG_SYBORG_TIMER
31 #ifdef DEBUG_SYBORG_TIMER
32 #define DPRINTF(fmt, ...) \
33 do { printf("syborg_timer: " fmt , ##args); } while (0)
34 #define BADF(fmt, ...) \
35 do { fprintf(stderr, "syborg_timer: error: " fmt , ## __VA_ARGS__); \
38 #define DPRINTF(fmt, ...) do {} while(0)
39 #define BADF(fmt, ...) \
40 do { fprintf(stderr, "syborg_timer: error: " fmt , ## __VA_ARGS__);} while (0)
66 static void syborg_timer_update(SyborgTimerState *s)
68 /* Update interrupt. */
69 if (s->int_level && s->int_enabled) {
70 qemu_irq_raise(s->irq);
72 qemu_irq_lower(s->irq);
76 static void syborg_timer_tick(void *opaque)
78 SyborgTimerState *s = (SyborgTimerState *)opaque;
79 //DPRINTF("Timer Tick\n");
83 syborg_timer_update(s);
86 static uint32_t syborg_timer_read(void *opaque, target_phys_addr_t offset)
88 SyborgTimerState *s = (SyborgTimerState *)opaque;
90 DPRINTF("Reg read %d\n", (int)offset);
92 switch (offset >> 2) {
94 return SYBORG_ID_TIMER;
102 return ptimer_get_count(s->timer);
103 case TIMER_INT_ENABLE:
104 return s->int_enabled;
105 case TIMER_INT_STATUS:
110 cpu_abort(cpu_single_env, "syborg_timer_read: Bad offset %x\n",
116 static void syborg_timer_write(void *opaque, target_phys_addr_t offset,
119 SyborgTimerState *s = (SyborgTimerState *)opaque;
121 DPRINTF("Reg write %d\n", (int)offset);
123 switch (offset >> 2) {
125 if (value == s->running)
129 ptimer_run(s->timer, s->oneshot);
131 ptimer_stop(s->timer);
136 ptimer_stop(s->timer);
140 ptimer_run(s->timer, s->oneshot);
145 ptimer_set_limit(s->timer, value, 1);
148 ptimer_set_count(s->timer, value);
150 case TIMER_INT_ENABLE:
151 s->int_enabled = value;
152 syborg_timer_update(s);
154 case TIMER_INT_STATUS:
155 s->int_level &= ~value;
156 syborg_timer_update(s);
159 cpu_abort(cpu_single_env, "syborg_timer_write: Bad offset %x\n",
165 static CPUReadMemoryFunc * const syborg_timer_readfn[] = {
171 static CPUWriteMemoryFunc * const syborg_timer_writefn[] = {
177 static const VMStateDescription vmstate_syborg_timer = {
178 .name = "syborg_timer",
180 .minimum_version_id = 1,
181 .minimum_version_id_old = 1,
182 .fields = (VMStateField[]) {
183 VMSTATE_INT32(running, SyborgTimerState),
184 VMSTATE_INT32(oneshot, SyborgTimerState),
185 VMSTATE_UINT32(limit, SyborgTimerState),
186 VMSTATE_UINT32(int_level, SyborgTimerState),
187 VMSTATE_UINT32(int_enabled, SyborgTimerState),
188 VMSTATE_PTIMER(timer, SyborgTimerState),
189 VMSTATE_END_OF_LIST()
193 static int syborg_timer_init(SysBusDevice *dev)
195 SyborgTimerState *s = FROM_SYSBUS(SyborgTimerState, dev);
200 fprintf(stderr, "syborg_timer: Zero/unset frequency\n");
203 sysbus_init_irq(dev, &s->irq);
204 iomemtype = cpu_register_io_memory(syborg_timer_readfn,
205 syborg_timer_writefn, s,
206 DEVICE_NATIVE_ENDIAN);
207 sysbus_init_mmio(dev, 0x1000, iomemtype);
209 bh = qemu_bh_new(syborg_timer_tick, s);
210 s->timer = ptimer_init(bh);
211 ptimer_set_freq(s->timer, s->freq);
212 vmstate_register(&dev->qdev, -1, &vmstate_syborg_timer, s);
216 static SysBusDeviceInfo syborg_timer_info = {
217 .init = syborg_timer_init,
218 .qdev.name = "syborg,timer",
219 .qdev.size = sizeof(SyborgTimerState),
220 .qdev.props = (Property[]) {
221 DEFINE_PROP_UINT32("frequency",SyborgTimerState, freq, 0),
222 DEFINE_PROP_END_OF_LIST(),
226 static void syborg_timer_register_devices(void)
228 sysbus_register_withprop(&syborg_timer_info);
231 device_init(syborg_timer_register_devices)