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
25 #include "qemu/osdep.h"
27 #include "qemu/module.h"
28 #include "qemu/timer.h"
29 #include "hw/timer/i8254.h"
30 #include "hw/timer/i8254_internal.h"
31 #include "qom/object.h"
35 #define RW_STATE_LSB 1
36 #define RW_STATE_MSB 2
37 #define RW_STATE_WORD0 3
38 #define RW_STATE_WORD1 4
40 typedef struct PITClass PITClass
;
41 DECLARE_CLASS_CHECKERS(PITClass
, PIT
,
45 PITCommonClass parent_class
;
47 DeviceRealize parent_realize
;
50 static void pit_irq_timer_update(PITChannelState
*s
, int64_t current_time
);
52 static int pit_get_count(PITChannelState
*s
)
57 d
= muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) - s
->count_load_time
, PIT_FREQ
,
58 NANOSECONDS_PER_SECOND
);
64 counter
= (s
->count
- d
) & 0xffff;
67 /* XXX: may be incorrect for odd counts */
68 counter
= s
->count
- ((2 * d
) % s
->count
);
71 counter
= s
->count
- (d
% s
->count
);
77 /* val must be 0 or 1 */
78 static void pit_set_channel_gate(PITCommonState
*s
, PITChannelState
*sc
,
85 /* XXX: just disable/enable counting */
90 /* restart counting on rising edge */
91 sc
->count_load_time
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
92 pit_irq_timer_update(sc
, sc
->count_load_time
);
98 /* restart counting on rising edge */
99 sc
->count_load_time
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
100 pit_irq_timer_update(sc
, sc
->count_load_time
);
102 /* XXX: disable/enable counting */
108 static inline void pit_load_count(PITChannelState
*s
, int val
)
112 s
->count_load_time
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
114 pit_irq_timer_update(s
, s
->count_load_time
);
117 /* if already latched, do not latch again */
118 static void pit_latch_count(PITChannelState
*s
)
120 if (!s
->count_latched
) {
121 s
->latched_count
= pit_get_count(s
);
122 s
->count_latched
= s
->rw_mode
;
126 static void pit_ioport_write(void *opaque
, hwaddr addr
,
127 uint64_t val
, unsigned size
)
129 PITCommonState
*pit
= opaque
;
137 /* read back command */
138 for(channel
= 0; channel
< 3; channel
++) {
139 s
= &pit
->channels
[channel
];
140 if (val
& (2 << channel
)) {
144 if (!(val
& 0x10) && !s
->status_latched
) {
146 /* XXX: add BCD and null count */
149 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
)) << 7) |
153 s
->status_latched
= 1;
158 s
= &pit
->channels
[channel
];
159 access
= (val
>> 4) & 3;
164 s
->read_state
= access
;
165 s
->write_state
= access
;
167 s
->mode
= (val
>> 1) & 7;
169 /* XXX: update irq timer ? */
173 s
= &pit
->channels
[addr
];
174 switch(s
->write_state
) {
177 pit_load_count(s
, val
);
180 pit_load_count(s
, val
<< 8);
183 s
->write_latch
= val
;
184 s
->write_state
= RW_STATE_WORD1
;
187 pit_load_count(s
, s
->write_latch
| (val
<< 8));
188 s
->write_state
= RW_STATE_WORD0
;
194 static uint64_t pit_ioport_read(void *opaque
, hwaddr addr
,
197 PITCommonState
*pit
= opaque
;
204 /* Mode/Command register is write only, read is ignored */
208 s
= &pit
->channels
[addr
];
209 if (s
->status_latched
) {
210 s
->status_latched
= 0;
212 } else if (s
->count_latched
) {
213 switch(s
->count_latched
) {
216 ret
= s
->latched_count
& 0xff;
217 s
->count_latched
= 0;
220 ret
= s
->latched_count
>> 8;
221 s
->count_latched
= 0;
224 ret
= s
->latched_count
& 0xff;
225 s
->count_latched
= RW_STATE_MSB
;
229 switch(s
->read_state
) {
232 count
= pit_get_count(s
);
236 count
= pit_get_count(s
);
237 ret
= (count
>> 8) & 0xff;
240 count
= pit_get_count(s
);
242 s
->read_state
= RW_STATE_WORD1
;
245 count
= pit_get_count(s
);
246 ret
= (count
>> 8) & 0xff;
247 s
->read_state
= RW_STATE_WORD0
;
254 static void pit_irq_timer_update(PITChannelState
*s
, int64_t current_time
)
259 if (!s
->irq_timer
|| s
->irq_disabled
) {
262 expire_time
= pit_get_next_transition_time(s
, current_time
);
263 irq_level
= pit_get_out(s
, current_time
);
264 qemu_set_irq(s
->irq
, irq_level
);
266 printf("irq_level=%d next_delay=%f\n",
268 (double)(expire_time
- current_time
) / NANOSECONDS_PER_SECOND
);
270 s
->next_transition_time
= expire_time
;
271 if (expire_time
!= -1)
272 timer_mod(s
->irq_timer
, expire_time
);
274 timer_del(s
->irq_timer
);
277 static void pit_irq_timer(void *opaque
)
279 PITChannelState
*s
= opaque
;
281 pit_irq_timer_update(s
, s
->next_transition_time
);
284 static void pit_reset(DeviceState
*dev
)
286 PITCommonState
*pit
= PIT_COMMON(dev
);
289 pit_reset_common(pit
);
291 s
= &pit
->channels
[0];
292 if (!s
->irq_disabled
) {
293 timer_mod(s
->irq_timer
, s
->next_transition_time
);
297 /* When HPET is operating in legacy mode, suppress the ignored timer IRQ,
298 * reenable it when legacy mode is left again. */
299 static void pit_irq_control(void *opaque
, int n
, int enable
)
301 PITCommonState
*pit
= opaque
;
302 PITChannelState
*s
= &pit
->channels
[0];
306 pit_irq_timer_update(s
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
309 timer_del(s
->irq_timer
);
313 static const MemoryRegionOps pit_ioport_ops
= {
314 .read
= pit_ioport_read
,
315 .write
= pit_ioport_write
,
317 .min_access_size
= 1,
318 .max_access_size
= 1,
320 .endianness
= DEVICE_LITTLE_ENDIAN
,
323 static void pit_post_load(PITCommonState
*s
)
325 PITChannelState
*sc
= &s
->channels
[0];
327 if (sc
->next_transition_time
!= -1 && !sc
->irq_disabled
) {
328 timer_mod(sc
->irq_timer
, sc
->next_transition_time
);
330 timer_del(sc
->irq_timer
);
334 static void pit_realizefn(DeviceState
*dev
, Error
**errp
)
336 PITCommonState
*pit
= PIT_COMMON(dev
);
337 PITClass
*pc
= PIT_GET_CLASS(dev
);
340 s
= &pit
->channels
[0];
341 /* the timer 0 is connected to an IRQ */
342 s
->irq_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, pit_irq_timer
, s
);
343 qdev_init_gpio_out(dev
, &s
->irq
, 1);
345 memory_region_init_io(&pit
->ioports
, OBJECT(pit
), &pit_ioport_ops
,
348 qdev_init_gpio_in(dev
, pit_irq_control
, 1);
350 pc
->parent_realize(dev
, errp
);
353 static void pit_class_initfn(ObjectClass
*klass
, void *data
)
355 PITClass
*pc
= PIT_CLASS(klass
);
356 PITCommonClass
*k
= PIT_COMMON_CLASS(klass
);
357 DeviceClass
*dc
= DEVICE_CLASS(klass
);
359 device_class_set_parent_realize(dc
, pit_realizefn
, &pc
->parent_realize
);
360 k
->set_channel_gate
= pit_set_channel_gate
;
361 k
->get_channel_info
= pit_get_channel_info_common
;
362 k
->post_load
= pit_post_load
;
363 dc
->reset
= pit_reset
;
366 static const TypeInfo pit_info
= {
368 .parent
= TYPE_PIT_COMMON
,
369 .instance_size
= sizeof(PITCommonState
),
370 .class_init
= pit_class_initfn
,
371 .class_size
= sizeof(PITClass
),
374 static void pit_register_types(void)
376 type_register_static(&pit_info
);
379 type_init(pit_register_types
)