2 * Syborg Interval Timer.
4 * Copyright (c) 2008 CodeSourcery
5 * Copyright (c) 2010, 2013 Stefan Weil
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "hw/sysbus.h"
27 #include "qemu/timer.h"
29 #include "ptimer.h" /* ptimer_state */
31 //#define DEBUG_SYBORG_TIMER
33 #ifdef DEBUG_SYBORG_TIMER
34 #define DPRINTF(fmt, ...) \
35 do { printf("syborg_timer: " fmt , ##args); } while (0)
36 #define BADF(fmt, ...) \
37 do { fprintf(stderr, "syborg_timer: error: " fmt , ## __VA_ARGS__); \
40 #define DPRINTF(fmt, ...) do {} while(0)
41 #define BADF(fmt, ...) \
42 do { fprintf(stderr, "syborg_timer: error: " fmt , ## __VA_ARGS__);} while (0)
69 static void syborg_timer_update(SyborgTimerState
*s
)
71 /* Update interrupt. */
72 if (s
->int_level
&& s
->int_enabled
) {
73 qemu_irq_raise(s
->irq
);
75 qemu_irq_lower(s
->irq
);
79 static void syborg_timer_tick(void *opaque
)
81 SyborgTimerState
*s
= (SyborgTimerState
*)opaque
;
82 //DPRINTF("Timer Tick\n");
86 syborg_timer_update(s
);
89 static uint64_t syborg_timer_read(void *opaque
, hwaddr offset
,
92 SyborgTimerState
*s
= (SyborgTimerState
*)opaque
;
94 DPRINTF("Reg read %d\n", (int)offset
);
96 switch (offset
>> 2) {
98 return SYBORG_ID_TIMER
;
106 return ptimer_get_count(s
->timer
);
107 case TIMER_INT_ENABLE
:
108 return s
->int_enabled
;
109 case TIMER_INT_STATUS
:
114 cpu_abort(cpu_single_env
, "syborg_timer_read: Bad offset %x\n",
120 static void syborg_timer_write(void *opaque
, hwaddr offset
,
121 uint64_t value
, unsigned size
)
123 SyborgTimerState
*s
= (SyborgTimerState
*)opaque
;
125 DPRINTF("Reg write %d\n", (int)offset
);
127 switch (offset
>> 2) {
129 if (value
== s
->running
)
133 ptimer_run(s
->timer
, s
->oneshot
);
135 ptimer_stop(s
->timer
);
140 ptimer_stop(s
->timer
);
144 ptimer_run(s
->timer
, s
->oneshot
);
149 ptimer_set_limit(s
->timer
, value
, 1);
152 ptimer_set_count(s
->timer
, value
);
154 case TIMER_INT_ENABLE
:
155 s
->int_enabled
= value
;
156 syborg_timer_update(s
);
158 case TIMER_INT_STATUS
:
159 s
->int_level
&= ~value
;
160 syborg_timer_update(s
);
163 cpu_abort(cpu_single_env
, "syborg_timer_write: Bad offset %x\n",
169 static const MemoryRegionOps syborg_timer_ops
= {
170 .read
= syborg_timer_read
,
171 .write
= syborg_timer_write
,
172 .endianness
= DEVICE_NATIVE_ENDIAN
,
175 static const VMStateDescription vmstate_syborg_timer
= {
176 .name
= "syborg_timer",
178 .minimum_version_id
= 1,
179 .minimum_version_id_old
= 1,
180 .fields
= (VMStateField
[]) {
181 VMSTATE_INT32(running
, SyborgTimerState
),
182 VMSTATE_INT32(oneshot
, SyborgTimerState
),
183 VMSTATE_UINT32(limit
, SyborgTimerState
),
184 VMSTATE_UINT32(int_level
, SyborgTimerState
),
185 VMSTATE_UINT32(int_enabled
, SyborgTimerState
),
186 VMSTATE_PTIMER(timer
, SyborgTimerState
),
187 VMSTATE_END_OF_LIST()
191 static int syborg_timer_init(SysBusDevice
*sbd
)
193 DeviceState
*dev
= DEVICE(sbd
);
194 SyborgTimerState
*s
= SYBORG_TIMER(dev
);
198 fprintf(stderr
, "syborg_timer: Zero/unset frequency\n");
201 sysbus_init_irq(dev
, &s
->irq
);
202 memory_region_init_io(&s
->iomem
, &syborg_timer_ops
, s
, "timer", 0x1000);
203 sysbus_init_mmio(sbd
, &s
->iomem
);
205 bh
= qemu_bh_new(syborg_timer_tick
, s
);
206 s
->timer
= ptimer_init(bh
);
207 ptimer_set_freq(s
->timer
, s
->freq
);
208 vmstate_register(&dev
->qdev
, -1, &vmstate_syborg_timer
, s
);
212 static Property syborg_timer_properties
[] = {
213 DEFINE_PROP_UINT32("frequency",SyborgTimerState
, freq
, 0),
214 DEFINE_PROP_END_OF_LIST()
217 static void syborg_timer_class_init(ObjectClass
*klass
, void *data
)
219 DeviceClass
*dc
= DEVICE_CLASS(klass
);
220 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
221 dc
->props
= syborg_timer_properties
;
222 k
->init
= syborg_timer_init
;
225 static const TypeInfo syborg_timer_info
= {
226 .name
= "syborg,timer",
227 .parent
= TYPE_SYS_BUS_DEVICE
,
228 .instance_size
= sizeof(SyborgTimerState
),
229 .class_init
= syborg_timer_class_init
232 static void syborg_timer_register_types(void)
234 type_register_static(&syborg_timer_info
);
237 type_init(syborg_timer_register_types
)