bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU 8253/8254 interval timer emulation |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003-2004 Fabrice Bellard |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5 | * |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 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 | */ |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 24 | #include "hw.h" |
| 25 | #include "pc.h" |
| 26 | #include "isa.h" |
| 27 | #include "qemu-timer.h" |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 28 | |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 29 | //#define DEBUG_PIT |
| 30 | |
bellard | ec844b9 | 2004-05-03 23:18:25 +0000 | [diff] [blame] | 31 | #define RW_STATE_LSB 1 |
| 32 | #define RW_STATE_MSB 2 |
| 33 | #define RW_STATE_WORD0 3 |
| 34 | #define RW_STATE_WORD1 4 |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 35 | |
bellard | ec844b9 | 2004-05-03 23:18:25 +0000 | [diff] [blame] | 36 | typedef struct PITChannelState { |
| 37 | int count; /* can be 65536 */ |
| 38 | uint16_t latched_count; |
| 39 | uint8_t count_latched; |
| 40 | uint8_t status_latched; |
| 41 | uint8_t status; |
| 42 | uint8_t read_state; |
| 43 | uint8_t write_state; |
| 44 | uint8_t write_latch; |
| 45 | uint8_t rw_mode; |
| 46 | uint8_t mode; |
| 47 | uint8_t bcd; /* not supported */ |
| 48 | uint8_t gate; /* timer start */ |
| 49 | int64_t count_load_time; |
| 50 | /* irq handling */ |
| 51 | int64_t next_transition_time; |
| 52 | QEMUTimer *irq_timer; |
pbrook | d537cf6 | 2007-04-07 18:14:41 +0000 | [diff] [blame] | 53 | qemu_irq irq; |
bellard | ec844b9 | 2004-05-03 23:18:25 +0000 | [diff] [blame] | 54 | } PITChannelState; |
| 55 | |
| 56 | struct PITState { |
| 57 | PITChannelState channels[3]; |
| 58 | }; |
| 59 | |
| 60 | static PITState pit_state; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 61 | |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 62 | static void pit_irq_timer_update(PITChannelState *s, int64_t current_time); |
| 63 | |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 64 | static int pit_get_count(PITChannelState *s) |
| 65 | { |
| 66 | uint64_t d; |
| 67 | int counter; |
| 68 | |
Juan Quintela | 6ee093c | 2009-09-10 03:04:26 +0200 | [diff] [blame] | 69 | d = muldiv64(qemu_get_clock(vm_clock) - s->count_load_time, PIT_FREQ, |
| 70 | get_ticks_per_sec()); |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 71 | switch(s->mode) { |
| 72 | case 0: |
| 73 | case 1: |
| 74 | case 4: |
| 75 | case 5: |
| 76 | counter = (s->count - d) & 0xffff; |
| 77 | break; |
| 78 | case 3: |
| 79 | /* XXX: may be incorrect for odd counts */ |
| 80 | counter = s->count - ((2 * d) % s->count); |
| 81 | break; |
| 82 | default: |
| 83 | counter = s->count - (d % s->count); |
| 84 | break; |
| 85 | } |
| 86 | return counter; |
| 87 | } |
| 88 | |
| 89 | /* get pit output bit */ |
bellard | ec844b9 | 2004-05-03 23:18:25 +0000 | [diff] [blame] | 90 | static int pit_get_out1(PITChannelState *s, int64_t current_time) |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 91 | { |
| 92 | uint64_t d; |
| 93 | int out; |
| 94 | |
Juan Quintela | 6ee093c | 2009-09-10 03:04:26 +0200 | [diff] [blame] | 95 | d = muldiv64(current_time - s->count_load_time, PIT_FREQ, |
| 96 | get_ticks_per_sec()); |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 97 | switch(s->mode) { |
| 98 | default: |
| 99 | case 0: |
| 100 | out = (d >= s->count); |
| 101 | break; |
| 102 | case 1: |
| 103 | out = (d < s->count); |
| 104 | break; |
| 105 | case 2: |
| 106 | if ((d % s->count) == 0 && d != 0) |
| 107 | out = 1; |
| 108 | else |
| 109 | out = 0; |
| 110 | break; |
| 111 | case 3: |
| 112 | out = (d % s->count) < ((s->count + 1) >> 1); |
| 113 | break; |
| 114 | case 4: |
| 115 | case 5: |
| 116 | out = (d == s->count); |
| 117 | break; |
| 118 | } |
| 119 | return out; |
| 120 | } |
| 121 | |
bellard | ec844b9 | 2004-05-03 23:18:25 +0000 | [diff] [blame] | 122 | int pit_get_out(PITState *pit, int channel, int64_t current_time) |
| 123 | { |
| 124 | PITChannelState *s = &pit->channels[channel]; |
| 125 | return pit_get_out1(s, current_time); |
| 126 | } |
| 127 | |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 128 | /* return -1 if no transition will occur. */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 129 | static int64_t pit_get_next_transition_time(PITChannelState *s, |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 130 | int64_t current_time) |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 131 | { |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 132 | uint64_t d, next_time, base; |
| 133 | int period2; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 134 | |
Juan Quintela | 6ee093c | 2009-09-10 03:04:26 +0200 | [diff] [blame] | 135 | d = muldiv64(current_time - s->count_load_time, PIT_FREQ, |
| 136 | get_ticks_per_sec()); |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 137 | switch(s->mode) { |
| 138 | default: |
| 139 | case 0: |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 140 | case 1: |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 141 | if (d < s->count) |
| 142 | next_time = s->count; |
| 143 | else |
| 144 | return -1; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 145 | break; |
| 146 | case 2: |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 147 | base = (d / s->count) * s->count; |
| 148 | if ((d - base) == 0 && d != 0) |
| 149 | next_time = base + s->count; |
| 150 | else |
| 151 | next_time = base + s->count + 1; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 152 | break; |
| 153 | case 3: |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 154 | base = (d / s->count) * s->count; |
| 155 | period2 = ((s->count + 1) >> 1); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 156 | if ((d - base) < period2) |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 157 | next_time = base + period2; |
| 158 | else |
| 159 | next_time = base + s->count; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 160 | break; |
| 161 | case 4: |
| 162 | case 5: |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 163 | if (d < s->count) |
| 164 | next_time = s->count; |
| 165 | else if (d == s->count) |
| 166 | next_time = s->count + 1; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 167 | else |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 168 | return -1; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 169 | break; |
| 170 | } |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 171 | /* convert to timer units */ |
Juan Quintela | 6ee093c | 2009-09-10 03:04:26 +0200 | [diff] [blame] | 172 | next_time = s->count_load_time + muldiv64(next_time, get_ticks_per_sec(), |
| 173 | PIT_FREQ); |
bellard | 1154e44 | 2004-04-02 20:58:56 +0000 | [diff] [blame] | 174 | /* fix potential rounding problems */ |
| 175 | /* XXX: better solution: use a clock at PIT_FREQ Hz */ |
| 176 | if (next_time <= current_time) |
| 177 | next_time = current_time + 1; |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 178 | return next_time; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* val must be 0 or 1 */ |
bellard | ec844b9 | 2004-05-03 23:18:25 +0000 | [diff] [blame] | 182 | void pit_set_gate(PITState *pit, int channel, int val) |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 183 | { |
bellard | ec844b9 | 2004-05-03 23:18:25 +0000 | [diff] [blame] | 184 | PITChannelState *s = &pit->channels[channel]; |
| 185 | |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 186 | switch(s->mode) { |
| 187 | default: |
| 188 | case 0: |
| 189 | case 4: |
| 190 | /* XXX: just disable/enable counting */ |
| 191 | break; |
| 192 | case 1: |
| 193 | case 5: |
| 194 | if (s->gate < val) { |
| 195 | /* restart counting on rising edge */ |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 196 | s->count_load_time = qemu_get_clock(vm_clock); |
| 197 | pit_irq_timer_update(s, s->count_load_time); |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 198 | } |
| 199 | break; |
| 200 | case 2: |
| 201 | case 3: |
| 202 | if (s->gate < val) { |
| 203 | /* restart counting on rising edge */ |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 204 | s->count_load_time = qemu_get_clock(vm_clock); |
| 205 | pit_irq_timer_update(s, s->count_load_time); |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 206 | } |
| 207 | /* XXX: disable/enable counting */ |
| 208 | break; |
| 209 | } |
| 210 | s->gate = val; |
| 211 | } |
| 212 | |
bellard | ec844b9 | 2004-05-03 23:18:25 +0000 | [diff] [blame] | 213 | int pit_get_gate(PITState *pit, int channel) |
| 214 | { |
| 215 | PITChannelState *s = &pit->channels[channel]; |
| 216 | return s->gate; |
| 217 | } |
| 218 | |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 219 | int pit_get_initial_count(PITState *pit, int channel) |
| 220 | { |
| 221 | PITChannelState *s = &pit->channels[channel]; |
| 222 | return s->count; |
| 223 | } |
| 224 | |
| 225 | int pit_get_mode(PITState *pit, int channel) |
| 226 | { |
| 227 | PITChannelState *s = &pit->channels[channel]; |
| 228 | return s->mode; |
| 229 | } |
| 230 | |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 231 | static inline void pit_load_count(PITChannelState *s, int val) |
| 232 | { |
| 233 | if (val == 0) |
| 234 | val = 0x10000; |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 235 | s->count_load_time = qemu_get_clock(vm_clock); |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 236 | s->count = val; |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 237 | pit_irq_timer_update(s, s->count_load_time); |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 238 | } |
| 239 | |
bellard | ec844b9 | 2004-05-03 23:18:25 +0000 | [diff] [blame] | 240 | /* if already latched, do not latch again */ |
| 241 | static void pit_latch_count(PITChannelState *s) |
| 242 | { |
| 243 | if (!s->count_latched) { |
| 244 | s->latched_count = pit_get_count(s); |
| 245 | s->count_latched = s->rw_mode; |
| 246 | } |
| 247 | } |
| 248 | |
bellard | b41a2cd | 2004-03-14 21:46:48 +0000 | [diff] [blame] | 249 | static void pit_ioport_write(void *opaque, uint32_t addr, uint32_t val) |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 250 | { |
bellard | ec844b9 | 2004-05-03 23:18:25 +0000 | [diff] [blame] | 251 | PITState *pit = opaque; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 252 | int channel, access; |
| 253 | PITChannelState *s; |
| 254 | |
| 255 | addr &= 3; |
| 256 | if (addr == 3) { |
| 257 | channel = val >> 6; |
bellard | ec844b9 | 2004-05-03 23:18:25 +0000 | [diff] [blame] | 258 | if (channel == 3) { |
| 259 | /* read back command */ |
| 260 | for(channel = 0; channel < 3; channel++) { |
| 261 | s = &pit->channels[channel]; |
| 262 | if (val & (2 << channel)) { |
| 263 | if (!(val & 0x20)) { |
| 264 | pit_latch_count(s); |
| 265 | } |
| 266 | if (!(val & 0x10) && !s->status_latched) { |
| 267 | /* status latch */ |
| 268 | /* XXX: add BCD and null count */ |
| 269 | s->status = (pit_get_out1(s, qemu_get_clock(vm_clock)) << 7) | |
| 270 | (s->rw_mode << 4) | |
| 271 | (s->mode << 1) | |
| 272 | s->bcd; |
| 273 | s->status_latched = 1; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | } else { |
| 278 | s = &pit->channels[channel]; |
| 279 | access = (val >> 4) & 3; |
| 280 | if (access == 0) { |
| 281 | pit_latch_count(s); |
| 282 | } else { |
| 283 | s->rw_mode = access; |
| 284 | s->read_state = access; |
| 285 | s->write_state = access; |
| 286 | |
| 287 | s->mode = (val >> 1) & 7; |
| 288 | s->bcd = val & 1; |
| 289 | /* XXX: update irq timer ? */ |
| 290 | } |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 291 | } |
| 292 | } else { |
bellard | ec844b9 | 2004-05-03 23:18:25 +0000 | [diff] [blame] | 293 | s = &pit->channels[addr]; |
| 294 | switch(s->write_state) { |
| 295 | default: |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 296 | case RW_STATE_LSB: |
| 297 | pit_load_count(s, val); |
| 298 | break; |
| 299 | case RW_STATE_MSB: |
| 300 | pit_load_count(s, val << 8); |
| 301 | break; |
| 302 | case RW_STATE_WORD0: |
bellard | ec844b9 | 2004-05-03 23:18:25 +0000 | [diff] [blame] | 303 | s->write_latch = val; |
| 304 | s->write_state = RW_STATE_WORD1; |
| 305 | break; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 306 | case RW_STATE_WORD1: |
bellard | ec844b9 | 2004-05-03 23:18:25 +0000 | [diff] [blame] | 307 | pit_load_count(s, s->write_latch | (val << 8)); |
| 308 | s->write_state = RW_STATE_WORD0; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 309 | break; |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
bellard | b41a2cd | 2004-03-14 21:46:48 +0000 | [diff] [blame] | 314 | static uint32_t pit_ioport_read(void *opaque, uint32_t addr) |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 315 | { |
bellard | ec844b9 | 2004-05-03 23:18:25 +0000 | [diff] [blame] | 316 | PITState *pit = opaque; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 317 | int ret, count; |
| 318 | PITChannelState *s; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 319 | |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 320 | addr &= 3; |
bellard | ec844b9 | 2004-05-03 23:18:25 +0000 | [diff] [blame] | 321 | s = &pit->channels[addr]; |
| 322 | if (s->status_latched) { |
| 323 | s->status_latched = 0; |
| 324 | ret = s->status; |
| 325 | } else if (s->count_latched) { |
| 326 | switch(s->count_latched) { |
| 327 | default: |
| 328 | case RW_STATE_LSB: |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 329 | ret = s->latched_count & 0xff; |
bellard | ec844b9 | 2004-05-03 23:18:25 +0000 | [diff] [blame] | 330 | s->count_latched = 0; |
| 331 | break; |
| 332 | case RW_STATE_MSB: |
| 333 | ret = s->latched_count >> 8; |
| 334 | s->count_latched = 0; |
| 335 | break; |
| 336 | case RW_STATE_WORD0: |
| 337 | ret = s->latched_count & 0xff; |
| 338 | s->count_latched = RW_STATE_MSB; |
| 339 | break; |
| 340 | } |
| 341 | } else { |
| 342 | switch(s->read_state) { |
| 343 | default: |
| 344 | case RW_STATE_LSB: |
| 345 | count = pit_get_count(s); |
| 346 | ret = count & 0xff; |
| 347 | break; |
| 348 | case RW_STATE_MSB: |
| 349 | count = pit_get_count(s); |
| 350 | ret = (count >> 8) & 0xff; |
| 351 | break; |
| 352 | case RW_STATE_WORD0: |
| 353 | count = pit_get_count(s); |
| 354 | ret = count & 0xff; |
| 355 | s->read_state = RW_STATE_WORD1; |
| 356 | break; |
| 357 | case RW_STATE_WORD1: |
| 358 | count = pit_get_count(s); |
| 359 | ret = (count >> 8) & 0xff; |
| 360 | s->read_state = RW_STATE_WORD0; |
| 361 | break; |
| 362 | } |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 363 | } |
| 364 | return ret; |
| 365 | } |
| 366 | |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 367 | static void pit_irq_timer_update(PITChannelState *s, int64_t current_time) |
| 368 | { |
| 369 | int64_t expire_time; |
| 370 | int irq_level; |
| 371 | |
| 372 | if (!s->irq_timer) |
| 373 | return; |
| 374 | expire_time = pit_get_next_transition_time(s, current_time); |
bellard | ec844b9 | 2004-05-03 23:18:25 +0000 | [diff] [blame] | 375 | irq_level = pit_get_out1(s, current_time); |
pbrook | d537cf6 | 2007-04-07 18:14:41 +0000 | [diff] [blame] | 376 | qemu_set_irq(s->irq, irq_level); |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 377 | #ifdef DEBUG_PIT |
| 378 | printf("irq_level=%d next_delay=%f\n", |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 379 | irq_level, |
Juan Quintela | 6ee093c | 2009-09-10 03:04:26 +0200 | [diff] [blame] | 380 | (double)(expire_time - current_time) / get_ticks_per_sec()); |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 381 | #endif |
| 382 | s->next_transition_time = expire_time; |
| 383 | if (expire_time != -1) |
| 384 | qemu_mod_timer(s->irq_timer, expire_time); |
| 385 | else |
| 386 | qemu_del_timer(s->irq_timer); |
| 387 | } |
| 388 | |
| 389 | static void pit_irq_timer(void *opaque) |
| 390 | { |
| 391 | PITChannelState *s = opaque; |
| 392 | |
| 393 | pit_irq_timer_update(s, s->next_transition_time); |
| 394 | } |
| 395 | |
Juan Quintela | 5122b43 | 2009-08-20 19:42:31 +0200 | [diff] [blame] | 396 | static const VMStateDescription vmstate_pit_channel = { |
| 397 | .name = "pit channel", |
| 398 | .version_id = 2, |
| 399 | .minimum_version_id = 2, |
| 400 | .minimum_version_id_old = 2, |
| 401 | .fields = (VMStateField []) { |
| 402 | VMSTATE_INT32(count, PITChannelState), |
| 403 | VMSTATE_UINT16(latched_count, PITChannelState), |
| 404 | VMSTATE_UINT8(count_latched, PITChannelState), |
| 405 | VMSTATE_UINT8(status_latched, PITChannelState), |
| 406 | VMSTATE_UINT8(status, PITChannelState), |
| 407 | VMSTATE_UINT8(read_state, PITChannelState), |
| 408 | VMSTATE_UINT8(write_state, PITChannelState), |
| 409 | VMSTATE_UINT8(write_latch, PITChannelState), |
| 410 | VMSTATE_UINT8(rw_mode, PITChannelState), |
| 411 | VMSTATE_UINT8(mode, PITChannelState), |
| 412 | VMSTATE_UINT8(bcd, PITChannelState), |
| 413 | VMSTATE_UINT8(gate, PITChannelState), |
| 414 | VMSTATE_INT64(count_load_time, PITChannelState), |
| 415 | VMSTATE_INT64(next_transition_time, PITChannelState), |
| 416 | VMSTATE_END_OF_LIST() |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 417 | } |
Juan Quintela | 5122b43 | 2009-08-20 19:42:31 +0200 | [diff] [blame] | 418 | }; |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 419 | |
Juan Quintela | 5122b43 | 2009-08-20 19:42:31 +0200 | [diff] [blame] | 420 | static int pit_load_old(QEMUFile *f, void *opaque, int version_id) |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 421 | { |
bellard | ec844b9 | 2004-05-03 23:18:25 +0000 | [diff] [blame] | 422 | PITState *pit = opaque; |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 423 | PITChannelState *s; |
| 424 | int i; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 425 | |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 426 | if (version_id != 1) |
| 427 | return -EINVAL; |
| 428 | |
| 429 | for(i = 0; i < 3; i++) { |
bellard | ec844b9 | 2004-05-03 23:18:25 +0000 | [diff] [blame] | 430 | s = &pit->channels[i]; |
ths | bee8d68 | 2007-12-16 23:41:11 +0000 | [diff] [blame] | 431 | s->count=qemu_get_be32(f); |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 432 | qemu_get_be16s(f, &s->latched_count); |
bellard | ec844b9 | 2004-05-03 23:18:25 +0000 | [diff] [blame] | 433 | qemu_get_8s(f, &s->count_latched); |
| 434 | qemu_get_8s(f, &s->status_latched); |
| 435 | qemu_get_8s(f, &s->status); |
| 436 | qemu_get_8s(f, &s->read_state); |
| 437 | qemu_get_8s(f, &s->write_state); |
| 438 | qemu_get_8s(f, &s->write_latch); |
| 439 | qemu_get_8s(f, &s->rw_mode); |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 440 | qemu_get_8s(f, &s->mode); |
| 441 | qemu_get_8s(f, &s->bcd); |
| 442 | qemu_get_8s(f, &s->gate); |
ths | bee8d68 | 2007-12-16 23:41:11 +0000 | [diff] [blame] | 443 | s->count_load_time=qemu_get_be64(f); |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 444 | if (s->irq_timer) { |
ths | bee8d68 | 2007-12-16 23:41:11 +0000 | [diff] [blame] | 445 | s->next_transition_time=qemu_get_be64(f); |
bellard | b0a21b5 | 2004-03-31 18:58:38 +0000 | [diff] [blame] | 446 | qemu_get_timer(f, s->irq_timer); |
| 447 | } |
| 448 | } |
| 449 | return 0; |
| 450 | } |
| 451 | |
Juan Quintela | 5122b43 | 2009-08-20 19:42:31 +0200 | [diff] [blame] | 452 | static const VMStateDescription vmstate_pit = { |
| 453 | .name = "i8254", |
| 454 | .version_id = 2, |
| 455 | .minimum_version_id = 2, |
| 456 | .minimum_version_id_old = 1, |
| 457 | .load_state_old = pit_load_old, |
| 458 | .fields = (VMStateField []) { |
| 459 | VMSTATE_STRUCT_ARRAY(channels, PITState, 3, 2, vmstate_pit_channel, PITChannelState), |
| 460 | VMSTATE_TIMER(channels[0].irq_timer, PITState), |
| 461 | VMSTATE_END_OF_LIST() |
| 462 | } |
| 463 | }; |
| 464 | |
bellard | d7d02e3 | 2004-06-20 12:58:36 +0000 | [diff] [blame] | 465 | static void pit_reset(void *opaque) |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 466 | { |
bellard | d7d02e3 | 2004-06-20 12:58:36 +0000 | [diff] [blame] | 467 | PITState *pit = opaque; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 468 | PITChannelState *s; |
| 469 | int i; |
| 470 | |
| 471 | for(i = 0;i < 3; i++) { |
bellard | ec844b9 | 2004-05-03 23:18:25 +0000 | [diff] [blame] | 472 | s = &pit->channels[i]; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 473 | s->mode = 3; |
| 474 | s->gate = (i != 2); |
| 475 | pit_load_count(s, 0); |
| 476 | } |
bellard | d7d02e3 | 2004-06-20 12:58:36 +0000 | [diff] [blame] | 477 | } |
| 478 | |
aliguori | 16b29ae | 2008-12-17 23:28:44 +0000 | [diff] [blame] | 479 | /* When HPET is operating in legacy mode, i8254 timer0 is disabled */ |
| 480 | void hpet_pit_disable(void) { |
| 481 | PITChannelState *s; |
| 482 | s = &pit_state.channels[0]; |
aliguori | e0dd114 | 2009-01-26 20:32:18 +0000 | [diff] [blame] | 483 | if (s->irq_timer) |
| 484 | qemu_del_timer(s->irq_timer); |
aliguori | 16b29ae | 2008-12-17 23:28:44 +0000 | [diff] [blame] | 485 | } |
| 486 | |
aurel32 | c50c2d6 | 2008-12-18 22:42:43 +0000 | [diff] [blame] | 487 | /* When HPET is reset or leaving legacy mode, it must reenable i8254 |
aliguori | 16b29ae | 2008-12-17 23:28:44 +0000 | [diff] [blame] | 488 | * timer 0 |
| 489 | */ |
| 490 | |
| 491 | void hpet_pit_enable(void) |
| 492 | { |
| 493 | PITState *pit = &pit_state; |
| 494 | PITChannelState *s; |
| 495 | s = &pit->channels[0]; |
| 496 | s->mode = 3; |
| 497 | s->gate = 1; |
| 498 | pit_load_count(s, 0); |
| 499 | } |
| 500 | |
pbrook | d537cf6 | 2007-04-07 18:14:41 +0000 | [diff] [blame] | 501 | PITState *pit_init(int base, qemu_irq irq) |
bellard | d7d02e3 | 2004-06-20 12:58:36 +0000 | [diff] [blame] | 502 | { |
| 503 | PITState *pit = &pit_state; |
| 504 | PITChannelState *s; |
| 505 | |
| 506 | s = &pit->channels[0]; |
| 507 | /* the timer 0 is connected to an IRQ */ |
| 508 | s->irq_timer = qemu_new_timer(vm_clock, pit_irq_timer, s); |
| 509 | s->irq = irq; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 510 | |
Juan Quintela | 5122b43 | 2009-08-20 19:42:31 +0200 | [diff] [blame] | 511 | vmstate_register(base, &vmstate_pit, pit); |
Jan Kiszka | a08d436 | 2009-06-27 09:25:07 +0200 | [diff] [blame] | 512 | qemu_register_reset(pit_reset, pit); |
bellard | ec844b9 | 2004-05-03 23:18:25 +0000 | [diff] [blame] | 513 | register_ioport_write(base, 4, 1, pit_ioport_write, pit); |
| 514 | register_ioport_read(base, 3, 1, pit_ioport_read, pit); |
bellard | d7d02e3 | 2004-06-20 12:58:36 +0000 | [diff] [blame] | 515 | |
| 516 | pit_reset(pit); |
| 517 | |
bellard | ec844b9 | 2004-05-03 23:18:25 +0000 | [diff] [blame] | 518 | return pit; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 519 | } |