2 * QEMU 8253/8254 interval timer emulation
4 * Copyright (c) 2003-2004 Fabrice Bellard
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
24 #include "qemu/osdep.h"
26 #include "hw/isa/isa.h"
27 #include "qemu/timer.h"
28 #include "hw/timer/i8254.h"
29 #include "hw/timer/i8254_internal.h"
33 #define RW_STATE_LSB 1
34 #define RW_STATE_MSB 2
35 #define RW_STATE_WORD0 3
36 #define RW_STATE_WORD1 4
38 #define PIT_CLASS(class) OBJECT_CLASS_CHECK(PITClass, (class), TYPE_I8254)
39 #define PIT_GET_CLASS(obj) OBJECT_GET_CLASS(PITClass, (obj), TYPE_I8254)
41 typedef struct PITClass {
42 PITCommonClass parent_class;
44 DeviceRealize parent_realize;
47 static void pit_irq_timer_update(PITChannelState *s, int64_t current_time);
49 static int pit_get_count(PITChannelState *s)
54 d = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - s->count_load_time, PIT_FREQ,
55 NANOSECONDS_PER_SECOND);
61 counter = (s->count - d) & 0xffff;
64 /* XXX: may be incorrect for odd counts */
65 counter = s->count - ((2 * d) % s->count);
68 counter = s->count - (d % s->count);
74 /* val must be 0 or 1 */
75 static void pit_set_channel_gate(PITCommonState *s, PITChannelState *sc,
82 /* XXX: just disable/enable counting */
87 /* restart counting on rising edge */
88 sc->count_load_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
89 pit_irq_timer_update(sc, sc->count_load_time);
95 /* restart counting on rising edge */
96 sc->count_load_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
97 pit_irq_timer_update(sc, sc->count_load_time);
99 /* XXX: disable/enable counting */
105 static inline void pit_load_count(PITChannelState *s, int val)
109 s->count_load_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
111 pit_irq_timer_update(s, s->count_load_time);
114 /* if already latched, do not latch again */
115 static void pit_latch_count(PITChannelState *s)
117 if (!s->count_latched) {
118 s->latched_count = pit_get_count(s);
119 s->count_latched = s->rw_mode;
123 static void pit_ioport_write(void *opaque, hwaddr addr,
124 uint64_t val, unsigned size)
126 PITCommonState *pit = opaque;
134 /* read back command */
135 for(channel = 0; channel < 3; channel++) {
136 s = &pit->channels[channel];
137 if (val & (2 << channel)) {
141 if (!(val & 0x10) && !s->status_latched) {
143 /* XXX: add BCD and null count */
146 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)) << 7) |
150 s->status_latched = 1;
155 s = &pit->channels[channel];
156 access = (val >> 4) & 3;
161 s->read_state = access;
162 s->write_state = access;
164 s->mode = (val >> 1) & 7;
166 /* XXX: update irq timer ? */
170 s = &pit->channels[addr];
171 switch(s->write_state) {
174 pit_load_count(s, val);
177 pit_load_count(s, val << 8);
180 s->write_latch = val;
181 s->write_state = RW_STATE_WORD1;
184 pit_load_count(s, s->write_latch | (val << 8));
185 s->write_state = RW_STATE_WORD0;
191 static uint64_t pit_ioport_read(void *opaque, hwaddr addr,
194 PITCommonState *pit = opaque;
201 /* Mode/Command register is write only, read is ignored */
205 s = &pit->channels[addr];
206 if (s->status_latched) {
207 s->status_latched = 0;
209 } else if (s->count_latched) {
210 switch(s->count_latched) {
213 ret = s->latched_count & 0xff;
214 s->count_latched = 0;
217 ret = s->latched_count >> 8;
218 s->count_latched = 0;
221 ret = s->latched_count & 0xff;
222 s->count_latched = RW_STATE_MSB;
226 switch(s->read_state) {
229 count = pit_get_count(s);
233 count = pit_get_count(s);
234 ret = (count >> 8) & 0xff;
237 count = pit_get_count(s);
239 s->read_state = RW_STATE_WORD1;
242 count = pit_get_count(s);
243 ret = (count >> 8) & 0xff;
244 s->read_state = RW_STATE_WORD0;
251 static void pit_irq_timer_update(PITChannelState *s, int64_t current_time)
256 if (!s->irq_timer || s->irq_disabled) {
259 expire_time = pit_get_next_transition_time(s, current_time);
260 irq_level = pit_get_out(s, current_time);
261 qemu_set_irq(s->irq, irq_level);
263 printf("irq_level=%d next_delay=%f\n",
265 (double)(expire_time - current_time) / NANOSECONDS_PER_SECOND);
267 s->next_transition_time = expire_time;
268 if (expire_time != -1)
269 timer_mod(s->irq_timer, expire_time);
271 timer_del(s->irq_timer);
274 static void pit_irq_timer(void *opaque)
276 PITChannelState *s = opaque;
278 pit_irq_timer_update(s, s->next_transition_time);
281 static void pit_reset(DeviceState *dev)
283 PITCommonState *pit = PIT_COMMON(dev);
286 pit_reset_common(pit);
288 s = &pit->channels[0];
289 if (!s->irq_disabled) {
290 timer_mod(s->irq_timer, s->next_transition_time);
294 /* When HPET is operating in legacy mode, suppress the ignored timer IRQ,
295 * reenable it when legacy mode is left again. */
296 static void pit_irq_control(void *opaque, int n, int enable)
298 PITCommonState *pit = opaque;
299 PITChannelState *s = &pit->channels[0];
303 pit_irq_timer_update(s, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
306 timer_del(s->irq_timer);
310 static const MemoryRegionOps pit_ioport_ops = {
311 .read = pit_ioport_read,
312 .write = pit_ioport_write,
314 .min_access_size = 1,
315 .max_access_size = 1,
317 .endianness = DEVICE_LITTLE_ENDIAN,
320 static void pit_post_load(PITCommonState *s)
322 PITChannelState *sc = &s->channels[0];
324 if (sc->next_transition_time != -1) {
325 timer_mod(sc->irq_timer, sc->next_transition_time);
327 timer_del(sc->irq_timer);
331 static void pit_realizefn(DeviceState *dev, Error **errp)
333 PITCommonState *pit = PIT_COMMON(dev);
334 PITClass *pc = PIT_GET_CLASS(dev);
337 s = &pit->channels[0];
338 /* the timer 0 is connected to an IRQ */
339 s->irq_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, pit_irq_timer, s);
340 qdev_init_gpio_out(dev, &s->irq, 1);
342 memory_region_init_io(&pit->ioports, OBJECT(pit), &pit_ioport_ops,
345 qdev_init_gpio_in(dev, pit_irq_control, 1);
347 pc->parent_realize(dev, errp);
350 static Property pit_properties[] = {
351 DEFINE_PROP_UINT32("iobase", PITCommonState, iobase, -1),
352 DEFINE_PROP_END_OF_LIST(),
355 static void pit_class_initfn(ObjectClass *klass, void *data)
357 PITClass *pc = PIT_CLASS(klass);
358 PITCommonClass *k = PIT_COMMON_CLASS(klass);
359 DeviceClass *dc = DEVICE_CLASS(klass);
361 device_class_set_parent_realize(dc, pit_realizefn, &pc->parent_realize);
362 k->set_channel_gate = pit_set_channel_gate;
363 k->get_channel_info = pit_get_channel_info_common;
364 k->post_load = pit_post_load;
365 dc->reset = pit_reset;
366 dc->props = pit_properties;
369 static const TypeInfo pit_info = {
371 .parent = TYPE_PIT_COMMON,
372 .instance_size = sizeof(PITCommonState),
373 .class_init = pit_class_initfn,
374 .class_size = sizeof(PITClass),
377 static void pit_register_types(void)
379 type_register_static(&pit_info);
382 type_init(pit_register_types)