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)
67 static void syborg_timer_update(SyborgTimerState
*s
)
69 /* Update interrupt. */
70 if (s
->int_level
&& s
->int_enabled
) {
71 qemu_irq_raise(s
->irq
);
73 qemu_irq_lower(s
->irq
);
77 static void syborg_timer_tick(void *opaque
)
79 SyborgTimerState
*s
= (SyborgTimerState
*)opaque
;
80 //DPRINTF("Timer Tick\n");
84 syborg_timer_update(s
);
87 static uint64_t syborg_timer_read(void *opaque
, target_phys_addr_t offset
,
90 SyborgTimerState
*s
= (SyborgTimerState
*)opaque
;
92 DPRINTF("Reg read %d\n", (int)offset
);
94 switch (offset
>> 2) {
96 return SYBORG_ID_TIMER
;
104 return ptimer_get_count(s
->timer
);
105 case TIMER_INT_ENABLE
:
106 return s
->int_enabled
;
107 case TIMER_INT_STATUS
:
112 cpu_abort(cpu_single_env
, "syborg_timer_read: Bad offset %x\n",
118 static void syborg_timer_write(void *opaque
, target_phys_addr_t offset
,
119 uint64_t value
, unsigned size
)
121 SyborgTimerState
*s
= (SyborgTimerState
*)opaque
;
123 DPRINTF("Reg write %d\n", (int)offset
);
125 switch (offset
>> 2) {
127 if (value
== s
->running
)
131 ptimer_run(s
->timer
, s
->oneshot
);
133 ptimer_stop(s
->timer
);
138 ptimer_stop(s
->timer
);
142 ptimer_run(s
->timer
, s
->oneshot
);
147 ptimer_set_limit(s
->timer
, value
, 1);
150 ptimer_set_count(s
->timer
, value
);
152 case TIMER_INT_ENABLE
:
153 s
->int_enabled
= value
;
154 syborg_timer_update(s
);
156 case TIMER_INT_STATUS
:
157 s
->int_level
&= ~value
;
158 syborg_timer_update(s
);
161 cpu_abort(cpu_single_env
, "syborg_timer_write: Bad offset %x\n",
167 static const MemoryRegionOps syborg_timer_ops
= {
168 .read
= syborg_timer_read
,
169 .write
= syborg_timer_write
,
170 .endianness
= DEVICE_NATIVE_ENDIAN
,
173 static const VMStateDescription vmstate_syborg_timer
= {
174 .name
= "syborg_timer",
176 .minimum_version_id
= 1,
177 .minimum_version_id_old
= 1,
178 .fields
= (VMStateField
[]) {
179 VMSTATE_INT32(running
, SyborgTimerState
),
180 VMSTATE_INT32(oneshot
, SyborgTimerState
),
181 VMSTATE_UINT32(limit
, SyborgTimerState
),
182 VMSTATE_UINT32(int_level
, SyborgTimerState
),
183 VMSTATE_UINT32(int_enabled
, SyborgTimerState
),
184 VMSTATE_PTIMER(timer
, SyborgTimerState
),
185 VMSTATE_END_OF_LIST()
189 static int syborg_timer_init(SysBusDevice
*dev
)
191 SyborgTimerState
*s
= FROM_SYSBUS(SyborgTimerState
, dev
);
195 fprintf(stderr
, "syborg_timer: Zero/unset frequency\n");
198 sysbus_init_irq(dev
, &s
->irq
);
199 memory_region_init_io(&s
->iomem
, &syborg_timer_ops
, s
, "timer", 0x1000);
200 sysbus_init_mmio(dev
, &s
->iomem
);
202 bh
= qemu_bh_new(syborg_timer_tick
, s
);
203 s
->timer
= ptimer_init(bh
);
204 ptimer_set_freq(s
->timer
, s
->freq
);
205 vmstate_register(&dev
->qdev
, -1, &vmstate_syborg_timer
, s
);
209 static SysBusDeviceInfo syborg_timer_info
= {
210 .init
= syborg_timer_init
,
211 .qdev
.name
= "syborg,timer",
212 .qdev
.size
= sizeof(SyborgTimerState
),
213 .qdev
.props
= (Property
[]) {
214 DEFINE_PROP_UINT32("frequency",SyborgTimerState
, freq
, 0),
215 DEFINE_PROP_END_OF_LIST(),
219 static void syborg_timer_register_devices(void)
221 sysbus_register_withprop(&syborg_timer_info
);
224 device_init(syborg_timer_register_devices
)