blob: 77bd5e822203f0ee74e90d9a02adff8edc91124e [file] [log] [blame]
bellard80cabfa2004-03-14 12:20:30 +00001/*
2 * QEMU 8253/8254 interval timer emulation
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard80cabfa2004-03-14 12:20:30 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard80cabfa2004-03-14 12:20:30 +00006 * 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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
pbrook87ecb682007-11-17 17:14:51 +000024#include "hw.h"
25#include "pc.h"
26#include "isa.h"
27#include "qemu-timer.h"
Jan Kiszkab1277b02012-02-01 20:31:39 +010028#include "i8254.h"
Jan Kiszkad11e8592012-03-02 20:28:46 +010029#include "i8254_internal.h"
bellard80cabfa2004-03-14 12:20:30 +000030
bellardb0a21b52004-03-31 18:58:38 +000031//#define DEBUG_PIT
32
bellardec844b92004-05-03 23:18:25 +000033#define RW_STATE_LSB 1
34#define RW_STATE_MSB 2
35#define RW_STATE_WORD0 3
36#define RW_STATE_WORD1 4
bellard80cabfa2004-03-14 12:20:30 +000037
bellardb0a21b52004-03-31 18:58:38 +000038static void pit_irq_timer_update(PITChannelState *s, int64_t current_time);
39
bellard80cabfa2004-03-14 12:20:30 +000040static int pit_get_count(PITChannelState *s)
41{
42 uint64_t d;
43 int counter;
44
Paolo Bonzini74475452011-03-11 16:47:48 +010045 d = muldiv64(qemu_get_clock_ns(vm_clock) - s->count_load_time, PIT_FREQ,
Juan Quintela6ee093c2009-09-10 03:04:26 +020046 get_ticks_per_sec());
bellard80cabfa2004-03-14 12:20:30 +000047 switch(s->mode) {
48 case 0:
49 case 1:
50 case 4:
51 case 5:
52 counter = (s->count - d) & 0xffff;
53 break;
54 case 3:
55 /* XXX: may be incorrect for odd counts */
56 counter = s->count - ((2 * d) % s->count);
57 break;
58 default:
59 counter = s->count - (d % s->count);
60 break;
61 }
62 return counter;
63}
64
bellard80cabfa2004-03-14 12:20:30 +000065/* val must be 0 or 1 */
Jan Kiszkad11e8592012-03-02 20:28:46 +010066static void pit_set_channel_gate(PITCommonState *s, PITChannelState *sc,
67 int val)
bellard80cabfa2004-03-14 12:20:30 +000068{
Jan Kiszkad11e8592012-03-02 20:28:46 +010069 switch (sc->mode) {
bellard80cabfa2004-03-14 12:20:30 +000070 default:
71 case 0:
72 case 4:
73 /* XXX: just disable/enable counting */
74 break;
75 case 1:
76 case 5:
Jan Kiszkad11e8592012-03-02 20:28:46 +010077 if (sc->gate < val) {
bellard80cabfa2004-03-14 12:20:30 +000078 /* restart counting on rising edge */
Jan Kiszkad11e8592012-03-02 20:28:46 +010079 sc->count_load_time = qemu_get_clock_ns(vm_clock);
80 pit_irq_timer_update(sc, sc->count_load_time);
bellard80cabfa2004-03-14 12:20:30 +000081 }
82 break;
83 case 2:
84 case 3:
Jan Kiszkad11e8592012-03-02 20:28:46 +010085 if (sc->gate < val) {
bellard80cabfa2004-03-14 12:20:30 +000086 /* restart counting on rising edge */
Jan Kiszkad11e8592012-03-02 20:28:46 +010087 sc->count_load_time = qemu_get_clock_ns(vm_clock);
88 pit_irq_timer_update(sc, sc->count_load_time);
bellard80cabfa2004-03-14 12:20:30 +000089 }
90 /* XXX: disable/enable counting */
91 break;
92 }
Jan Kiszkad11e8592012-03-02 20:28:46 +010093 sc->gate = val;
bellardfd06c372006-04-24 21:58:30 +000094}
95
bellard80cabfa2004-03-14 12:20:30 +000096static inline void pit_load_count(PITChannelState *s, int val)
97{
98 if (val == 0)
99 val = 0x10000;
Paolo Bonzini74475452011-03-11 16:47:48 +0100100 s->count_load_time = qemu_get_clock_ns(vm_clock);
bellard80cabfa2004-03-14 12:20:30 +0000101 s->count = val;
bellardb0a21b52004-03-31 18:58:38 +0000102 pit_irq_timer_update(s, s->count_load_time);
bellard80cabfa2004-03-14 12:20:30 +0000103}
104
bellardec844b92004-05-03 23:18:25 +0000105/* if already latched, do not latch again */
106static void pit_latch_count(PITChannelState *s)
107{
108 if (!s->count_latched) {
109 s->latched_count = pit_get_count(s);
110 s->count_latched = s->rw_mode;
111 }
112}
113
bellardb41a2cd2004-03-14 21:46:48 +0000114static void pit_ioport_write(void *opaque, uint32_t addr, uint32_t val)
bellard80cabfa2004-03-14 12:20:30 +0000115{
Jan Kiszkad11e8592012-03-02 20:28:46 +0100116 PITCommonState *pit = opaque;
bellard80cabfa2004-03-14 12:20:30 +0000117 int channel, access;
118 PITChannelState *s;
119
120 addr &= 3;
121 if (addr == 3) {
122 channel = val >> 6;
bellardec844b92004-05-03 23:18:25 +0000123 if (channel == 3) {
124 /* read back command */
125 for(channel = 0; channel < 3; channel++) {
126 s = &pit->channels[channel];
127 if (val & (2 << channel)) {
128 if (!(val & 0x20)) {
129 pit_latch_count(s);
130 }
131 if (!(val & 0x10) && !s->status_latched) {
132 /* status latch */
133 /* XXX: add BCD and null count */
Jan Kiszka4aa5d282012-02-01 20:31:43 +0100134 s->status =
135 (pit_get_out(s,
136 qemu_get_clock_ns(vm_clock)) << 7) |
bellardec844b92004-05-03 23:18:25 +0000137 (s->rw_mode << 4) |
138 (s->mode << 1) |
139 s->bcd;
140 s->status_latched = 1;
141 }
142 }
143 }
144 } else {
145 s = &pit->channels[channel];
146 access = (val >> 4) & 3;
147 if (access == 0) {
148 pit_latch_count(s);
149 } else {
150 s->rw_mode = access;
151 s->read_state = access;
152 s->write_state = access;
153
154 s->mode = (val >> 1) & 7;
155 s->bcd = val & 1;
156 /* XXX: update irq timer ? */
157 }
bellard80cabfa2004-03-14 12:20:30 +0000158 }
159 } else {
bellardec844b92004-05-03 23:18:25 +0000160 s = &pit->channels[addr];
161 switch(s->write_state) {
162 default:
bellard80cabfa2004-03-14 12:20:30 +0000163 case RW_STATE_LSB:
164 pit_load_count(s, val);
165 break;
166 case RW_STATE_MSB:
167 pit_load_count(s, val << 8);
168 break;
169 case RW_STATE_WORD0:
bellardec844b92004-05-03 23:18:25 +0000170 s->write_latch = val;
171 s->write_state = RW_STATE_WORD1;
172 break;
bellard80cabfa2004-03-14 12:20:30 +0000173 case RW_STATE_WORD1:
bellardec844b92004-05-03 23:18:25 +0000174 pit_load_count(s, s->write_latch | (val << 8));
175 s->write_state = RW_STATE_WORD0;
bellard80cabfa2004-03-14 12:20:30 +0000176 break;
177 }
178 }
179}
180
bellardb41a2cd2004-03-14 21:46:48 +0000181static uint32_t pit_ioport_read(void *opaque, uint32_t addr)
bellard80cabfa2004-03-14 12:20:30 +0000182{
Jan Kiszkad11e8592012-03-02 20:28:46 +0100183 PITCommonState *pit = opaque;
bellard80cabfa2004-03-14 12:20:30 +0000184 int ret, count;
185 PITChannelState *s;
ths3b46e622007-09-17 08:09:54 +0000186
bellard80cabfa2004-03-14 12:20:30 +0000187 addr &= 3;
bellardec844b92004-05-03 23:18:25 +0000188 s = &pit->channels[addr];
189 if (s->status_latched) {
190 s->status_latched = 0;
191 ret = s->status;
192 } else if (s->count_latched) {
193 switch(s->count_latched) {
194 default:
195 case RW_STATE_LSB:
bellard80cabfa2004-03-14 12:20:30 +0000196 ret = s->latched_count & 0xff;
bellardec844b92004-05-03 23:18:25 +0000197 s->count_latched = 0;
198 break;
199 case RW_STATE_MSB:
200 ret = s->latched_count >> 8;
201 s->count_latched = 0;
202 break;
203 case RW_STATE_WORD0:
204 ret = s->latched_count & 0xff;
205 s->count_latched = RW_STATE_MSB;
206 break;
207 }
208 } else {
209 switch(s->read_state) {
210 default:
211 case RW_STATE_LSB:
212 count = pit_get_count(s);
213 ret = count & 0xff;
214 break;
215 case RW_STATE_MSB:
216 count = pit_get_count(s);
217 ret = (count >> 8) & 0xff;
218 break;
219 case RW_STATE_WORD0:
220 count = pit_get_count(s);
221 ret = count & 0xff;
222 s->read_state = RW_STATE_WORD1;
223 break;
224 case RW_STATE_WORD1:
225 count = pit_get_count(s);
226 ret = (count >> 8) & 0xff;
227 s->read_state = RW_STATE_WORD0;
228 break;
229 }
bellard80cabfa2004-03-14 12:20:30 +0000230 }
231 return ret;
232}
233
bellardb0a21b52004-03-31 18:58:38 +0000234static void pit_irq_timer_update(PITChannelState *s, int64_t current_time)
235{
236 int64_t expire_time;
237 int irq_level;
238
Jan Kiszkace967e22012-02-01 20:31:41 +0100239 if (!s->irq_timer || s->irq_disabled) {
bellardb0a21b52004-03-31 18:58:38 +0000240 return;
Jan Kiszkace967e22012-02-01 20:31:41 +0100241 }
bellardb0a21b52004-03-31 18:58:38 +0000242 expire_time = pit_get_next_transition_time(s, current_time);
Jan Kiszka4aa5d282012-02-01 20:31:43 +0100243 irq_level = pit_get_out(s, current_time);
pbrookd537cf62007-04-07 18:14:41 +0000244 qemu_set_irq(s->irq, irq_level);
bellardb0a21b52004-03-31 18:58:38 +0000245#ifdef DEBUG_PIT
246 printf("irq_level=%d next_delay=%f\n",
ths5fafdf22007-09-16 21:08:06 +0000247 irq_level,
Juan Quintela6ee093c2009-09-10 03:04:26 +0200248 (double)(expire_time - current_time) / get_ticks_per_sec());
bellardb0a21b52004-03-31 18:58:38 +0000249#endif
250 s->next_transition_time = expire_time;
251 if (expire_time != -1)
252 qemu_mod_timer(s->irq_timer, expire_time);
253 else
254 qemu_del_timer(s->irq_timer);
255}
256
257static void pit_irq_timer(void *opaque)
258{
259 PITChannelState *s = opaque;
260
261 pit_irq_timer_update(s, s->next_transition_time);
262}
263
Blue Swirl64d7e9a2011-02-13 19:54:40 +0000264static void pit_reset(DeviceState *dev)
bellard80cabfa2004-03-14 12:20:30 +0000265{
Jan Kiszkad11e8592012-03-02 20:28:46 +0100266 PITCommonState *pit = DO_UPCAST(PITCommonState, dev.qdev, dev);
bellard80cabfa2004-03-14 12:20:30 +0000267 PITChannelState *s;
bellard80cabfa2004-03-14 12:20:30 +0000268
Jan Kiszkad11e8592012-03-02 20:28:46 +0100269 pit_reset_common(pit);
270
271 s = &pit->channels[0];
272 if (!s->irq_disabled) {
273 qemu_mod_timer(s->irq_timer, s->next_transition_time);
bellard80cabfa2004-03-14 12:20:30 +0000274 }
bellardd7d02e32004-06-20 12:58:36 +0000275}
276
Jan Kiszkace967e22012-02-01 20:31:41 +0100277/* When HPET is operating in legacy mode, suppress the ignored timer IRQ,
278 * reenable it when legacy mode is left again. */
279static void pit_irq_control(void *opaque, int n, int enable)
aliguori16b29ae2008-12-17 23:28:44 +0000280{
Jan Kiszkad11e8592012-03-02 20:28:46 +0100281 PITCommonState *pit = opaque;
Jan Kiszkace967e22012-02-01 20:31:41 +0100282 PITChannelState *s = &pit->channels[0];
283
284 if (enable) {
285 s->irq_disabled = 0;
286 pit_irq_timer_update(s, qemu_get_clock_ns(vm_clock));
287 } else {
288 s->irq_disabled = 1;
289 qemu_del_timer(s->irq_timer);
290 }
aliguori16b29ae2008-12-17 23:28:44 +0000291}
292
Richard Henderson60ea6aa2011-08-10 15:28:15 -0700293static const MemoryRegionPortio pit_portio[] = {
294 { 0, 4, 1, .write = pit_ioport_write },
295 { 0, 3, 1, .read = pit_ioport_read },
296 PORTIO_END_OF_LIST()
297};
298
299static const MemoryRegionOps pit_ioport_ops = {
300 .old_portio = pit_portio
301};
302
Jan Kiszka3fbc1c02012-03-02 20:28:47 +0100303static void pit_post_load(PITCommonState *s)
304{
305 PITChannelState *sc = &s->channels[0];
306
307 if (sc->next_transition_time != -1) {
308 qemu_mod_timer(sc->irq_timer, sc->next_transition_time);
309 } else {
310 qemu_del_timer(sc->irq_timer);
311 }
312}
313
Jan Kiszkad11e8592012-03-02 20:28:46 +0100314static int pit_initfn(PITCommonState *pit)
bellardd7d02e32004-06-20 12:58:36 +0000315{
bellardd7d02e32004-06-20 12:58:36 +0000316 PITChannelState *s;
317
318 s = &pit->channels[0];
319 /* the timer 0 is connected to an IRQ */
Paolo Bonzini74475452011-03-11 16:47:48 +0100320 s->irq_timer = qemu_new_timer_ns(vm_clock, pit_irq_timer, s);
Jan Kiszkad11e8592012-03-02 20:28:46 +0100321 qdev_init_gpio_out(&pit->dev.qdev, &s->irq, 1);
bellard80cabfa2004-03-14 12:20:30 +0000322
Richard Henderson60ea6aa2011-08-10 15:28:15 -0700323 memory_region_init_io(&pit->ioports, &pit_ioport_ops, pit, "pit", 4);
bellardd7d02e32004-06-20 12:58:36 +0000324
Jan Kiszkad11e8592012-03-02 20:28:46 +0100325 qdev_init_gpio_in(&pit->dev.qdev, pit_irq_control, 1);
Jan Kiszkaca22a3a2011-03-06 16:09:49 +0100326
Blue Swirl64d7e9a2011-02-13 19:54:40 +0000327 return 0;
bellard80cabfa2004-03-14 12:20:30 +0000328}
Blue Swirl64d7e9a2011-02-13 19:54:40 +0000329
Anthony Liguori39bffca2011-12-07 21:34:16 -0600330static Property pit_properties[] = {
Jan Kiszkad11e8592012-03-02 20:28:46 +0100331 DEFINE_PROP_HEX32("iobase", PITCommonState, iobase, -1),
Anthony Liguori39bffca2011-12-07 21:34:16 -0600332 DEFINE_PROP_END_OF_LIST(),
333};
334
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600335static void pit_class_initfn(ObjectClass *klass, void *data)
336{
Jan Kiszkad11e8592012-03-02 20:28:46 +0100337 PITCommonClass *k = PIT_COMMON_CLASS(klass);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600338 DeviceClass *dc = DEVICE_CLASS(klass);
Jan Kiszkad11e8592012-03-02 20:28:46 +0100339
340 k->init = pit_initfn;
341 k->set_channel_gate = pit_set_channel_gate;
342 k->get_channel_info = pit_get_channel_info_common;
Jan Kiszka3fbc1c02012-03-02 20:28:47 +0100343 k->post_load = pit_post_load;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600344 dc->reset = pit_reset;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600345 dc->props = pit_properties;
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600346}
347
Anthony Liguori39bffca2011-12-07 21:34:16 -0600348static TypeInfo pit_info = {
349 .name = "isa-pit",
Jan Kiszkad11e8592012-03-02 20:28:46 +0100350 .parent = TYPE_PIT_COMMON,
351 .instance_size = sizeof(PITCommonState),
Anthony Liguori39bffca2011-12-07 21:34:16 -0600352 .class_init = pit_class_initfn,
Blue Swirl64d7e9a2011-02-13 19:54:40 +0000353};
354
Andreas Färber83f7d432012-02-09 15:20:55 +0100355static void pit_register_types(void)
Blue Swirl64d7e9a2011-02-13 19:54:40 +0000356{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600357 type_register_static(&pit_info);
Blue Swirl64d7e9a2011-02-13 19:54:40 +0000358}
Andreas Färber83f7d432012-02-09 15:20:55 +0100359
360type_init(pit_register_types)