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
27 #include "qemu/timer.h"
29 #include "hw/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 static void pit_irq_timer_update(PITChannelState
*s
, int64_t current_time
);
40 static int pit_get_count(PITChannelState
*s
)
45 d
= muldiv64(qemu_get_clock_ns(vm_clock
) - s
->count_load_time
, PIT_FREQ
,
52 counter
= (s
->count
- d
) & 0xffff;
55 /* XXX: may be incorrect for odd counts */
56 counter
= s
->count
- ((2 * d
) % s
->count
);
59 counter
= s
->count
- (d
% s
->count
);
65 /* val must be 0 or 1 */
66 static void pit_set_channel_gate(PITCommonState
*s
, PITChannelState
*sc
,
73 /* XXX: just disable/enable counting */
78 /* restart counting on rising edge */
79 sc
->count_load_time
= qemu_get_clock_ns(vm_clock
);
80 pit_irq_timer_update(sc
, sc
->count_load_time
);
86 /* restart counting on rising edge */
87 sc
->count_load_time
= qemu_get_clock_ns(vm_clock
);
88 pit_irq_timer_update(sc
, sc
->count_load_time
);
90 /* XXX: disable/enable counting */
96 static inline void pit_load_count(PITChannelState
*s
, int val
)
100 s
->count_load_time
= qemu_get_clock_ns(vm_clock
);
102 pit_irq_timer_update(s
, s
->count_load_time
);
105 /* if already latched, do not latch again */
106 static void pit_latch_count(PITChannelState
*s
)
108 if (!s
->count_latched
) {
109 s
->latched_count
= pit_get_count(s
);
110 s
->count_latched
= s
->rw_mode
;
114 static void pit_ioport_write(void *opaque
, hwaddr addr
,
115 uint64_t val
, unsigned size
)
117 PITCommonState
*pit
= opaque
;
125 /* read back command */
126 for(channel
= 0; channel
< 3; channel
++) {
127 s
= &pit
->channels
[channel
];
128 if (val
& (2 << channel
)) {
132 if (!(val
& 0x10) && !s
->status_latched
) {
134 /* XXX: add BCD and null count */
137 qemu_get_clock_ns(vm_clock
)) << 7) |
141 s
->status_latched
= 1;
146 s
= &pit
->channels
[channel
];
147 access
= (val
>> 4) & 3;
152 s
->read_state
= access
;
153 s
->write_state
= access
;
155 s
->mode
= (val
>> 1) & 7;
157 /* XXX: update irq timer ? */
161 s
= &pit
->channels
[addr
];
162 switch(s
->write_state
) {
165 pit_load_count(s
, val
);
168 pit_load_count(s
, val
<< 8);
171 s
->write_latch
= val
;
172 s
->write_state
= RW_STATE_WORD1
;
175 pit_load_count(s
, s
->write_latch
| (val
<< 8));
176 s
->write_state
= RW_STATE_WORD0
;
182 static uint64_t pit_ioport_read(void *opaque
, hwaddr addr
,
185 PITCommonState
*pit
= opaque
;
190 s
= &pit
->channels
[addr
];
191 if (s
->status_latched
) {
192 s
->status_latched
= 0;
194 } else if (s
->count_latched
) {
195 switch(s
->count_latched
) {
198 ret
= s
->latched_count
& 0xff;
199 s
->count_latched
= 0;
202 ret
= s
->latched_count
>> 8;
203 s
->count_latched
= 0;
206 ret
= s
->latched_count
& 0xff;
207 s
->count_latched
= RW_STATE_MSB
;
211 switch(s
->read_state
) {
214 count
= pit_get_count(s
);
218 count
= pit_get_count(s
);
219 ret
= (count
>> 8) & 0xff;
222 count
= pit_get_count(s
);
224 s
->read_state
= RW_STATE_WORD1
;
227 count
= pit_get_count(s
);
228 ret
= (count
>> 8) & 0xff;
229 s
->read_state
= RW_STATE_WORD0
;
236 static void pit_irq_timer_update(PITChannelState
*s
, int64_t current_time
)
241 if (!s
->irq_timer
|| s
->irq_disabled
) {
244 expire_time
= pit_get_next_transition_time(s
, current_time
);
245 irq_level
= pit_get_out(s
, current_time
);
246 qemu_set_irq(s
->irq
, irq_level
);
248 printf("irq_level=%d next_delay=%f\n",
250 (double)(expire_time
- current_time
) / get_ticks_per_sec());
252 s
->next_transition_time
= expire_time
;
253 if (expire_time
!= -1)
254 qemu_mod_timer(s
->irq_timer
, expire_time
);
256 qemu_del_timer(s
->irq_timer
);
259 static void pit_irq_timer(void *opaque
)
261 PITChannelState
*s
= opaque
;
263 pit_irq_timer_update(s
, s
->next_transition_time
);
266 static void pit_reset(DeviceState
*dev
)
268 PITCommonState
*pit
= DO_UPCAST(PITCommonState
, dev
.qdev
, dev
);
271 pit_reset_common(pit
);
273 s
= &pit
->channels
[0];
274 if (!s
->irq_disabled
) {
275 qemu_mod_timer(s
->irq_timer
, s
->next_transition_time
);
279 /* When HPET is operating in legacy mode, suppress the ignored timer IRQ,
280 * reenable it when legacy mode is left again. */
281 static void pit_irq_control(void *opaque
, int n
, int enable
)
283 PITCommonState
*pit
= opaque
;
284 PITChannelState
*s
= &pit
->channels
[0];
288 pit_irq_timer_update(s
, qemu_get_clock_ns(vm_clock
));
291 qemu_del_timer(s
->irq_timer
);
295 static const MemoryRegionOps pit_ioport_ops
= {
296 .read
= pit_ioport_read
,
297 .write
= pit_ioport_write
,
299 .min_access_size
= 1,
300 .max_access_size
= 1,
302 .endianness
= DEVICE_LITTLE_ENDIAN
,
305 static void pit_post_load(PITCommonState
*s
)
307 PITChannelState
*sc
= &s
->channels
[0];
309 if (sc
->next_transition_time
!= -1) {
310 qemu_mod_timer(sc
->irq_timer
, sc
->next_transition_time
);
312 qemu_del_timer(sc
->irq_timer
);
316 static int pit_initfn(PITCommonState
*pit
)
320 s
= &pit
->channels
[0];
321 /* the timer 0 is connected to an IRQ */
322 s
->irq_timer
= qemu_new_timer_ns(vm_clock
, pit_irq_timer
, s
);
323 qdev_init_gpio_out(&pit
->dev
.qdev
, &s
->irq
, 1);
325 memory_region_init_io(&pit
->ioports
, &pit_ioport_ops
, pit
, "pit", 4);
327 qdev_init_gpio_in(&pit
->dev
.qdev
, pit_irq_control
, 1);
332 static Property pit_properties
[] = {
333 DEFINE_PROP_HEX32("iobase", PITCommonState
, iobase
, -1),
334 DEFINE_PROP_END_OF_LIST(),
337 static void pit_class_initfn(ObjectClass
*klass
, void *data
)
339 PITCommonClass
*k
= PIT_COMMON_CLASS(klass
);
340 DeviceClass
*dc
= DEVICE_CLASS(klass
);
342 k
->init
= pit_initfn
;
343 k
->set_channel_gate
= pit_set_channel_gate
;
344 k
->get_channel_info
= pit_get_channel_info_common
;
345 k
->post_load
= pit_post_load
;
346 dc
->reset
= pit_reset
;
347 dc
->props
= pit_properties
;
350 static const TypeInfo pit_info
= {
352 .parent
= TYPE_PIT_COMMON
,
353 .instance_size
= sizeof(PITCommonState
),
354 .class_init
= pit_class_initfn
,
357 static void pit_register_types(void)
359 type_register_static(&pit_info
);
362 type_init(pit_register_types
)