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