]> git.proxmox.com Git - qemu.git/blame - hw/i8254.c
tcg-s390: Fix merge error in tgen_brcond
[qemu.git] / hw / i8254.c
CommitLineData
80cabfad
FB
1/*
2 * QEMU 8253/8254 interval timer emulation
5fafdf24 3 *
80cabfad 4 * Copyright (c) 2003-2004 Fabrice Bellard
5fafdf24 5 *
80cabfad
FB
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:
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 */
83c9f4ca
PB
24#include "hw/hw.h"
25#include "hw/pc.h"
26#include "hw/isa.h"
1de7afc9 27#include "qemu/timer.h"
83c9f4ca
PB
28#include "hw/i8254.h"
29#include "hw/i8254_internal.h"
80cabfad 30
b0a21b53
FB
31//#define DEBUG_PIT
32
ec844b96
FB
33#define RW_STATE_LSB 1
34#define RW_STATE_MSB 2
35#define RW_STATE_WORD0 3
36#define RW_STATE_WORD1 4
80cabfad 37
b0a21b53
FB
38static void pit_irq_timer_update(PITChannelState *s, int64_t current_time);
39
80cabfad
FB
40static int pit_get_count(PITChannelState *s)
41{
42 uint64_t d;
43 int counter;
44
74475455 45 d = muldiv64(qemu_get_clock_ns(vm_clock) - s->count_load_time, PIT_FREQ,
6ee093c9 46 get_ticks_per_sec());
80cabfad
FB
47 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
80cabfad 65/* val must be 0 or 1 */
d11e859e
JK
66static void pit_set_channel_gate(PITCommonState *s, PITChannelState *sc,
67 int val)
80cabfad 68{
d11e859e 69 switch (sc->mode) {
80cabfad
FB
70 default:
71 case 0:
72 case 4:
73 /* XXX: just disable/enable counting */
74 break;
75 case 1:
76 case 5:
d11e859e 77 if (sc->gate < val) {
80cabfad 78 /* restart counting on rising edge */
d11e859e
JK
79 sc->count_load_time = qemu_get_clock_ns(vm_clock);
80 pit_irq_timer_update(sc, sc->count_load_time);
80cabfad
FB
81 }
82 break;
83 case 2:
84 case 3:
d11e859e 85 if (sc->gate < val) {
80cabfad 86 /* restart counting on rising edge */
d11e859e
JK
87 sc->count_load_time = qemu_get_clock_ns(vm_clock);
88 pit_irq_timer_update(sc, sc->count_load_time);
80cabfad
FB
89 }
90 /* XXX: disable/enable counting */
91 break;
92 }
d11e859e 93 sc->gate = val;
fd06c375
FB
94}
95
80cabfad
FB
96static inline void pit_load_count(PITChannelState *s, int val)
97{
98 if (val == 0)
99 val = 0x10000;
74475455 100 s->count_load_time = qemu_get_clock_ns(vm_clock);
80cabfad 101 s->count = val;
b0a21b53 102 pit_irq_timer_update(s, s->count_load_time);
80cabfad
FB
103}
104
ec844b96
FB
105/* 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
0505bcde
AG
114static void pit_ioport_write(void *opaque, hwaddr addr,
115 uint64_t val, unsigned size)
80cabfad 116{
d11e859e 117 PITCommonState *pit = opaque;
80cabfad
FB
118 int channel, access;
119 PITChannelState *s;
120
121 addr &= 3;
122 if (addr == 3) {
123 channel = val >> 6;
ec844b96
FB
124 if (channel == 3) {
125 /* read back command */
126 for(channel = 0; channel < 3; channel++) {
127 s = &pit->channels[channel];
128 if (val & (2 << channel)) {
129 if (!(val & 0x20)) {
130 pit_latch_count(s);
131 }
132 if (!(val & 0x10) && !s->status_latched) {
133 /* status latch */
134 /* XXX: add BCD and null count */
4aa5d285
JK
135 s->status =
136 (pit_get_out(s,
137 qemu_get_clock_ns(vm_clock)) << 7) |
ec844b96
FB
138 (s->rw_mode << 4) |
139 (s->mode << 1) |
140 s->bcd;
141 s->status_latched = 1;
142 }
143 }
144 }
145 } else {
146 s = &pit->channels[channel];
147 access = (val >> 4) & 3;
148 if (access == 0) {
149 pit_latch_count(s);
150 } else {
151 s->rw_mode = access;
152 s->read_state = access;
153 s->write_state = access;
154
155 s->mode = (val >> 1) & 7;
156 s->bcd = val & 1;
157 /* XXX: update irq timer ? */
158 }
80cabfad
FB
159 }
160 } else {
ec844b96
FB
161 s = &pit->channels[addr];
162 switch(s->write_state) {
163 default:
80cabfad
FB
164 case RW_STATE_LSB:
165 pit_load_count(s, val);
166 break;
167 case RW_STATE_MSB:
168 pit_load_count(s, val << 8);
169 break;
170 case RW_STATE_WORD0:
ec844b96
FB
171 s->write_latch = val;
172 s->write_state = RW_STATE_WORD1;
173 break;
80cabfad 174 case RW_STATE_WORD1:
ec844b96
FB
175 pit_load_count(s, s->write_latch | (val << 8));
176 s->write_state = RW_STATE_WORD0;
80cabfad
FB
177 break;
178 }
179 }
180}
181
0505bcde
AG
182static uint64_t pit_ioport_read(void *opaque, hwaddr addr,
183 unsigned size)
80cabfad 184{
d11e859e 185 PITCommonState *pit = opaque;
80cabfad
FB
186 int ret, count;
187 PITChannelState *s;
3b46e624 188
80cabfad 189 addr &= 3;
ec844b96
FB
190 s = &pit->channels[addr];
191 if (s->status_latched) {
192 s->status_latched = 0;
193 ret = s->status;
194 } else if (s->count_latched) {
195 switch(s->count_latched) {
196 default:
197 case RW_STATE_LSB:
198 ret = s->latched_count & 0xff;
199 s->count_latched = 0;
200 break;
201 case RW_STATE_MSB:
80cabfad 202 ret = s->latched_count >> 8;
ec844b96
FB
203 s->count_latched = 0;
204 break;
205 case RW_STATE_WORD0:
80cabfad 206 ret = s->latched_count & 0xff;
ec844b96
FB
207 s->count_latched = RW_STATE_MSB;
208 break;
209 }
210 } else {
211 switch(s->read_state) {
212 default:
213 case RW_STATE_LSB:
214 count = pit_get_count(s);
215 ret = count & 0xff;
216 break;
217 case RW_STATE_MSB:
218 count = pit_get_count(s);
219 ret = (count >> 8) & 0xff;
220 break;
221 case RW_STATE_WORD0:
222 count = pit_get_count(s);
223 ret = count & 0xff;
224 s->read_state = RW_STATE_WORD1;
225 break;
226 case RW_STATE_WORD1:
227 count = pit_get_count(s);
228 ret = (count >> 8) & 0xff;
229 s->read_state = RW_STATE_WORD0;
230 break;
231 }
80cabfad
FB
232 }
233 return ret;
234}
235
b0a21b53
FB
236static void pit_irq_timer_update(PITChannelState *s, int64_t current_time)
237{
238 int64_t expire_time;
239 int irq_level;
240
ce967e2f 241 if (!s->irq_timer || s->irq_disabled) {
b0a21b53 242 return;
ce967e2f 243 }
b0a21b53 244 expire_time = pit_get_next_transition_time(s, current_time);
4aa5d285 245 irq_level = pit_get_out(s, current_time);
d537cf6c 246 qemu_set_irq(s->irq, irq_level);
b0a21b53
FB
247#ifdef DEBUG_PIT
248 printf("irq_level=%d next_delay=%f\n",
5fafdf24 249 irq_level,
6ee093c9 250 (double)(expire_time - current_time) / get_ticks_per_sec());
b0a21b53
FB
251#endif
252 s->next_transition_time = expire_time;
253 if (expire_time != -1)
254 qemu_mod_timer(s->irq_timer, expire_time);
255 else
256 qemu_del_timer(s->irq_timer);
257}
258
259static void pit_irq_timer(void *opaque)
260{
261 PITChannelState *s = opaque;
262
263 pit_irq_timer_update(s, s->next_transition_time);
264}
265
d11e859e 266static void pit_reset(DeviceState *dev)
b0a21b53 267{
d11e859e 268 PITCommonState *pit = DO_UPCAST(PITCommonState, dev.qdev, dev);
b0a21b53 269 PITChannelState *s;
b0a21b53 270
d11e859e 271 pit_reset_common(pit);
5122b431 272
d11e859e
JK
273 s = &pit->channels[0];
274 if (!s->irq_disabled) {
275 qemu_mod_timer(s->irq_timer, s->next_transition_time);
80cabfad 276 }
d7d02e3c
FB
277}
278
ce967e2f
JK
279/* When HPET is operating in legacy mode, suppress the ignored timer IRQ,
280 * reenable it when legacy mode is left again. */
281static void pit_irq_control(void *opaque, int n, int enable)
16b29ae1 282{
d11e859e 283 PITCommonState *pit = opaque;
ce967e2f
JK
284 PITChannelState *s = &pit->channels[0];
285
286 if (enable) {
287 s->irq_disabled = 0;
288 pit_irq_timer_update(s, qemu_get_clock_ns(vm_clock));
289 } else {
290 s->irq_disabled = 1;
291 qemu_del_timer(s->irq_timer);
292 }
16b29ae1
AL
293}
294
60ea6aa8 295static const MemoryRegionOps pit_ioport_ops = {
0505bcde
AG
296 .read = pit_ioport_read,
297 .write = pit_ioport_write,
298 .impl = {
299 .min_access_size = 1,
300 .max_access_size = 1,
301 },
302 .endianness = DEVICE_LITTLE_ENDIAN,
60ea6aa8
RH
303};
304
3fbc1c0c
JK
305static void pit_post_load(PITCommonState *s)
306{
307 PITChannelState *sc = &s->channels[0];
308
309 if (sc->next_transition_time != -1) {
310 qemu_mod_timer(sc->irq_timer, sc->next_transition_time);
311 } else {
312 qemu_del_timer(sc->irq_timer);
313 }
314}
315
d11e859e 316static int pit_initfn(PITCommonState *pit)
d7d02e3c 317{
d7d02e3c
FB
318 PITChannelState *s;
319
320 s = &pit->channels[0];
321 /* the timer 0 is connected to an IRQ */
74475455 322 s->irq_timer = qemu_new_timer_ns(vm_clock, pit_irq_timer, s);
d11e859e 323 qdev_init_gpio_out(&pit->dev.qdev, &s->irq, 1);
80cabfad 324
60ea6aa8 325 memory_region_init_io(&pit->ioports, &pit_ioport_ops, pit, "pit", 4);
ce967e2f 326
d11e859e 327 qdev_init_gpio_in(&pit->dev.qdev, pit_irq_control, 1);
ca22a3a3 328
64d7e9a4
BS
329 return 0;
330}
331
39bffca2 332static Property pit_properties[] = {
d11e859e 333 DEFINE_PROP_HEX32("iobase", PITCommonState, iobase, -1),
39bffca2
AL
334 DEFINE_PROP_END_OF_LIST(),
335};
336
8f04ee08
AL
337static void pit_class_initfn(ObjectClass *klass, void *data)
338{
d11e859e 339 PITCommonClass *k = PIT_COMMON_CLASS(klass);
39bffca2 340 DeviceClass *dc = DEVICE_CLASS(klass);
d11e859e
JK
341
342 k->init = pit_initfn;
343 k->set_channel_gate = pit_set_channel_gate;
344 k->get_channel_info = pit_get_channel_info_common;
3fbc1c0c 345 k->post_load = pit_post_load;
39bffca2 346 dc->reset = pit_reset;
39bffca2 347 dc->props = pit_properties;
8f04ee08
AL
348}
349
8c43a6f0 350static const TypeInfo pit_info = {
39bffca2 351 .name = "isa-pit",
d11e859e
JK
352 .parent = TYPE_PIT_COMMON,
353 .instance_size = sizeof(PITCommonState),
39bffca2 354 .class_init = pit_class_initfn,
64d7e9a4
BS
355};
356
83f7d43a 357static void pit_register_types(void)
64d7e9a4 358{
39bffca2 359 type_register_static(&pit_info);
80cabfad 360}
83f7d43a
AF
361
362type_init(pit_register_types)