bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 1 | /* |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame^] | 2 | * QEMU PowerMac CUDA device support |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame^] | 4 | * Copyright (c) 2004-2007 Fabrice Bellard |
| 5 | * Copyright (c) 2007 Jocelyn Mayer |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 6 | * |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | * THE SOFTWARE. |
| 24 | */ |
| 25 | #include "vl.h" |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame^] | 26 | #include "ppc_mac.h" |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 27 | |
bellard | 61271e5 | 2005-07-07 21:45:18 +0000 | [diff] [blame] | 28 | /* XXX: implement all timer modes */ |
| 29 | |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 30 | //#define DEBUG_CUDA |
| 31 | //#define DEBUG_CUDA_PACKET |
| 32 | |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 33 | /* Bits in B data register: all active low */ |
| 34 | #define TREQ 0x08 /* Transfer request (input) */ |
| 35 | #define TACK 0x10 /* Transfer acknowledge (output) */ |
| 36 | #define TIP 0x20 /* Transfer in progress (output) */ |
| 37 | |
| 38 | /* Bits in ACR */ |
| 39 | #define SR_CTRL 0x1c /* Shift register control bits */ |
| 40 | #define SR_EXT 0x0c /* Shift on external clock */ |
| 41 | #define SR_OUT 0x10 /* Shift out if 1 */ |
| 42 | |
| 43 | /* Bits in IFR and IER */ |
| 44 | #define IER_SET 0x80 /* set bits in IER */ |
| 45 | #define IER_CLR 0 /* clear bits in IER */ |
| 46 | #define SR_INT 0x04 /* Shift register full/empty */ |
| 47 | #define T1_INT 0x40 /* Timer 1 interrupt */ |
bellard | 61271e5 | 2005-07-07 21:45:18 +0000 | [diff] [blame] | 48 | #define T2_INT 0x20 /* Timer 2 interrupt */ |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 49 | |
| 50 | /* Bits in ACR */ |
| 51 | #define T1MODE 0xc0 /* Timer 1 mode */ |
| 52 | #define T1MODE_CONT 0x40 /* continuous interrupts */ |
| 53 | |
| 54 | /* commands (1st byte) */ |
| 55 | #define ADB_PACKET 0 |
| 56 | #define CUDA_PACKET 1 |
| 57 | #define ERROR_PACKET 2 |
| 58 | #define TIMER_PACKET 3 |
| 59 | #define POWER_PACKET 4 |
| 60 | #define MACIIC_PACKET 5 |
| 61 | #define PMU_PACKET 6 |
| 62 | |
| 63 | |
| 64 | /* CUDA commands (2nd byte) */ |
| 65 | #define CUDA_WARM_START 0x0 |
| 66 | #define CUDA_AUTOPOLL 0x1 |
| 67 | #define CUDA_GET_6805_ADDR 0x2 |
| 68 | #define CUDA_GET_TIME 0x3 |
| 69 | #define CUDA_GET_PRAM 0x7 |
| 70 | #define CUDA_SET_6805_ADDR 0x8 |
| 71 | #define CUDA_SET_TIME 0x9 |
| 72 | #define CUDA_POWERDOWN 0xa |
| 73 | #define CUDA_POWERUP_TIME 0xb |
| 74 | #define CUDA_SET_PRAM 0xc |
| 75 | #define CUDA_MS_RESET 0xd |
| 76 | #define CUDA_SEND_DFAC 0xe |
| 77 | #define CUDA_BATTERY_SWAP_SENSE 0x10 |
| 78 | #define CUDA_RESET_SYSTEM 0x11 |
| 79 | #define CUDA_SET_IPL 0x12 |
| 80 | #define CUDA_FILE_SERVER_FLAG 0x13 |
| 81 | #define CUDA_SET_AUTO_RATE 0x14 |
| 82 | #define CUDA_GET_AUTO_RATE 0x16 |
| 83 | #define CUDA_SET_DEVICE_LIST 0x19 |
| 84 | #define CUDA_GET_DEVICE_LIST 0x1a |
| 85 | #define CUDA_SET_ONE_SECOND_MODE 0x1b |
| 86 | #define CUDA_SET_POWER_MESSAGES 0x21 |
| 87 | #define CUDA_GET_SET_IIC 0x22 |
| 88 | #define CUDA_WAKEUP 0x23 |
| 89 | #define CUDA_TIMER_TICKLE 0x24 |
| 90 | #define CUDA_COMBINED_FORMAT_IIC 0x25 |
| 91 | |
| 92 | #define CUDA_TIMER_FREQ (4700000 / 6) |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 93 | #define CUDA_ADB_POLL_FREQ 50 |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 94 | |
bellard | d7ce296 | 2005-02-09 00:07:08 +0000 | [diff] [blame] | 95 | /* CUDA returns time_t's offset from Jan 1, 1904, not 1970 */ |
| 96 | #define RTC_OFFSET 2082844800 |
| 97 | |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 98 | typedef struct CUDATimer { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 99 | int index; |
bellard | 61271e5 | 2005-07-07 21:45:18 +0000 | [diff] [blame] | 100 | uint16_t latch; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 101 | uint16_t counter_value; /* counter value at load time */ |
| 102 | int64_t load_time; |
| 103 | int64_t next_irq_time; |
| 104 | QEMUTimer *timer; |
| 105 | } CUDATimer; |
| 106 | |
| 107 | typedef struct CUDAState { |
| 108 | /* cuda registers */ |
| 109 | uint8_t b; /* B-side data */ |
| 110 | uint8_t a; /* A-side data */ |
| 111 | uint8_t dirb; /* B-side direction (1=output) */ |
| 112 | uint8_t dira; /* A-side direction (1=output) */ |
| 113 | uint8_t sr; /* Shift register */ |
| 114 | uint8_t acr; /* Auxiliary control register */ |
| 115 | uint8_t pcr; /* Peripheral control register */ |
| 116 | uint8_t ifr; /* Interrupt flag register */ |
| 117 | uint8_t ier; /* Interrupt enable register */ |
| 118 | uint8_t anh; /* A-side data, no handshake */ |
| 119 | |
| 120 | CUDATimer timers[2]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 121 | |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 122 | uint8_t last_b; /* last value of B register */ |
| 123 | uint8_t last_acr; /* last value of B register */ |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 124 | |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 125 | int data_in_size; |
| 126 | int data_in_index; |
| 127 | int data_out_index; |
| 128 | |
pbrook | d537cf6 | 2007-04-07 18:14:41 +0000 | [diff] [blame] | 129 | qemu_irq irq; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 130 | uint8_t autopoll; |
| 131 | uint8_t data_in[128]; |
| 132 | uint8_t data_out[16]; |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 133 | QEMUTimer *adb_poll_timer; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 134 | } CUDAState; |
| 135 | |
| 136 | static CUDAState cuda_state; |
| 137 | ADBBusState adb_bus; |
| 138 | |
| 139 | static void cuda_update(CUDAState *s); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 140 | static void cuda_receive_packet_from_host(CUDAState *s, |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 141 | const uint8_t *data, int len); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 142 | static void cuda_timer_update(CUDAState *s, CUDATimer *ti, |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 143 | int64_t current_time); |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 144 | |
| 145 | static void cuda_update_irq(CUDAState *s) |
| 146 | { |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 147 | if (s->ifr & s->ier & (SR_INT | T1_INT)) { |
pbrook | d537cf6 | 2007-04-07 18:14:41 +0000 | [diff] [blame] | 148 | qemu_irq_raise(s->irq); |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 149 | } else { |
pbrook | d537cf6 | 2007-04-07 18:14:41 +0000 | [diff] [blame] | 150 | qemu_irq_lower(s->irq); |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | static unsigned int get_counter(CUDATimer *s) |
| 155 | { |
| 156 | int64_t d; |
| 157 | unsigned int counter; |
| 158 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 159 | d = muldiv64(qemu_get_clock(vm_clock) - s->load_time, |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 160 | CUDA_TIMER_FREQ, ticks_per_sec); |
bellard | 61271e5 | 2005-07-07 21:45:18 +0000 | [diff] [blame] | 161 | if (s->index == 0) { |
| 162 | /* the timer goes down from latch to -1 (period of latch + 2) */ |
| 163 | if (d <= (s->counter_value + 1)) { |
| 164 | counter = (s->counter_value - d) & 0xffff; |
| 165 | } else { |
| 166 | counter = (d - (s->counter_value + 1)) % (s->latch + 2); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 167 | counter = (s->latch - counter) & 0xffff; |
bellard | 61271e5 | 2005-07-07 21:45:18 +0000 | [diff] [blame] | 168 | } |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 169 | } else { |
bellard | 61271e5 | 2005-07-07 21:45:18 +0000 | [diff] [blame] | 170 | counter = (s->counter_value - d) & 0xffff; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 171 | } |
| 172 | return counter; |
| 173 | } |
| 174 | |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 175 | static void set_counter(CUDAState *s, CUDATimer *ti, unsigned int val) |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 176 | { |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 177 | #ifdef DEBUG_CUDA |
| 178 | printf("cuda: T%d.counter=%d\n", |
| 179 | 1 + (ti->timer == NULL), val); |
| 180 | #endif |
| 181 | ti->load_time = qemu_get_clock(vm_clock); |
| 182 | ti->counter_value = val; |
| 183 | cuda_timer_update(s, ti, ti->load_time); |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static int64_t get_next_irq_time(CUDATimer *s, int64_t current_time) |
| 187 | { |
bellard | 61271e5 | 2005-07-07 21:45:18 +0000 | [diff] [blame] | 188 | int64_t d, next_time; |
| 189 | unsigned int counter; |
| 190 | |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 191 | /* current counter value */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 192 | d = muldiv64(current_time - s->load_time, |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 193 | CUDA_TIMER_FREQ, ticks_per_sec); |
bellard | 61271e5 | 2005-07-07 21:45:18 +0000 | [diff] [blame] | 194 | /* the timer goes down from latch to -1 (period of latch + 2) */ |
| 195 | if (d <= (s->counter_value + 1)) { |
| 196 | counter = (s->counter_value - d) & 0xffff; |
| 197 | } else { |
| 198 | counter = (d - (s->counter_value + 1)) % (s->latch + 2); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 199 | counter = (s->latch - counter) & 0xffff; |
bellard | 61271e5 | 2005-07-07 21:45:18 +0000 | [diff] [blame] | 200 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 201 | |
bellard | 61271e5 | 2005-07-07 21:45:18 +0000 | [diff] [blame] | 202 | /* Note: we consider the irq is raised on 0 */ |
| 203 | if (counter == 0xffff) { |
| 204 | next_time = d + s->latch + 1; |
| 205 | } else if (counter == 0) { |
| 206 | next_time = d + s->latch + 2; |
| 207 | } else { |
| 208 | next_time = d + counter; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 209 | } |
bellard | dccfafc | 2005-04-23 18:16:54 +0000 | [diff] [blame] | 210 | #if 0 |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 211 | #ifdef DEBUG_CUDA |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 212 | printf("latch=%d counter=%" PRId64 " delta_next=%" PRId64 "\n", |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 213 | s->latch, d, next_time - d); |
| 214 | #endif |
bellard | dccfafc | 2005-04-23 18:16:54 +0000 | [diff] [blame] | 215 | #endif |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 216 | next_time = muldiv64(next_time, ticks_per_sec, CUDA_TIMER_FREQ) + |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 217 | s->load_time; |
| 218 | if (next_time <= current_time) |
| 219 | next_time = current_time + 1; |
| 220 | return next_time; |
| 221 | } |
| 222 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 223 | static void cuda_timer_update(CUDAState *s, CUDATimer *ti, |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 224 | int64_t current_time) |
| 225 | { |
| 226 | if (!ti->timer) |
| 227 | return; |
| 228 | if ((s->acr & T1MODE) != T1MODE_CONT) { |
| 229 | qemu_del_timer(ti->timer); |
| 230 | } else { |
| 231 | ti->next_irq_time = get_next_irq_time(ti, current_time); |
| 232 | qemu_mod_timer(ti->timer, ti->next_irq_time); |
| 233 | } |
| 234 | } |
| 235 | |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 236 | static void cuda_timer1(void *opaque) |
| 237 | { |
| 238 | CUDAState *s = opaque; |
| 239 | CUDATimer *ti = &s->timers[0]; |
| 240 | |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 241 | cuda_timer_update(s, ti, ti->next_irq_time); |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 242 | s->ifr |= T1_INT; |
| 243 | cuda_update_irq(s); |
| 244 | } |
| 245 | |
| 246 | static uint32_t cuda_readb(void *opaque, target_phys_addr_t addr) |
| 247 | { |
| 248 | CUDAState *s = opaque; |
| 249 | uint32_t val; |
| 250 | |
| 251 | addr = (addr >> 9) & 0xf; |
| 252 | switch(addr) { |
| 253 | case 0: |
| 254 | val = s->b; |
| 255 | break; |
| 256 | case 1: |
| 257 | val = s->a; |
| 258 | break; |
| 259 | case 2: |
| 260 | val = s->dirb; |
| 261 | break; |
| 262 | case 3: |
| 263 | val = s->dira; |
| 264 | break; |
| 265 | case 4: |
| 266 | val = get_counter(&s->timers[0]) & 0xff; |
| 267 | s->ifr &= ~T1_INT; |
| 268 | cuda_update_irq(s); |
| 269 | break; |
| 270 | case 5: |
| 271 | val = get_counter(&s->timers[0]) >> 8; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 272 | cuda_update_irq(s); |
| 273 | break; |
| 274 | case 6: |
| 275 | val = s->timers[0].latch & 0xff; |
| 276 | break; |
| 277 | case 7: |
bellard | 61271e5 | 2005-07-07 21:45:18 +0000 | [diff] [blame] | 278 | /* XXX: check this */ |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 279 | val = (s->timers[0].latch >> 8) & 0xff; |
| 280 | break; |
| 281 | case 8: |
| 282 | val = get_counter(&s->timers[1]) & 0xff; |
bellard | 61271e5 | 2005-07-07 21:45:18 +0000 | [diff] [blame] | 283 | s->ifr &= ~T2_INT; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 284 | break; |
| 285 | case 9: |
| 286 | val = get_counter(&s->timers[1]) >> 8; |
| 287 | break; |
| 288 | case 10: |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 289 | val = s->sr; |
| 290 | s->ifr &= ~SR_INT; |
| 291 | cuda_update_irq(s); |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 292 | break; |
| 293 | case 11: |
| 294 | val = s->acr; |
| 295 | break; |
| 296 | case 12: |
| 297 | val = s->pcr; |
| 298 | break; |
| 299 | case 13: |
| 300 | val = s->ifr; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 301 | if (s->ifr & s->ier) |
bellard | b7c7b18 | 2005-07-23 14:01:47 +0000 | [diff] [blame] | 302 | val |= 0x80; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 303 | break; |
| 304 | case 14: |
bellard | b7c7b18 | 2005-07-23 14:01:47 +0000 | [diff] [blame] | 305 | val = s->ier | 0x80; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 306 | break; |
| 307 | default: |
| 308 | case 15: |
| 309 | val = s->anh; |
| 310 | break; |
| 311 | } |
| 312 | #ifdef DEBUG_CUDA |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 313 | if (addr != 13 || val != 0) |
| 314 | printf("cuda: read: reg=0x%x val=%02x\n", addr, val); |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 315 | #endif |
| 316 | return val; |
| 317 | } |
| 318 | |
| 319 | static void cuda_writeb(void *opaque, target_phys_addr_t addr, uint32_t val) |
| 320 | { |
| 321 | CUDAState *s = opaque; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 322 | |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 323 | addr = (addr >> 9) & 0xf; |
| 324 | #ifdef DEBUG_CUDA |
| 325 | printf("cuda: write: reg=0x%x val=%02x\n", addr, val); |
| 326 | #endif |
| 327 | |
| 328 | switch(addr) { |
| 329 | case 0: |
| 330 | s->b = val; |
| 331 | cuda_update(s); |
| 332 | break; |
| 333 | case 1: |
| 334 | s->a = val; |
| 335 | break; |
| 336 | case 2: |
| 337 | s->dirb = val; |
| 338 | break; |
| 339 | case 3: |
| 340 | s->dira = val; |
| 341 | break; |
| 342 | case 4: |
bellard | 61271e5 | 2005-07-07 21:45:18 +0000 | [diff] [blame] | 343 | s->timers[0].latch = (s->timers[0].latch & 0xff00) | val; |
| 344 | cuda_timer_update(s, &s->timers[0], qemu_get_clock(vm_clock)); |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 345 | break; |
| 346 | case 5: |
bellard | 61271e5 | 2005-07-07 21:45:18 +0000 | [diff] [blame] | 347 | s->timers[0].latch = (s->timers[0].latch & 0xff) | (val << 8); |
| 348 | s->ifr &= ~T1_INT; |
| 349 | set_counter(s, &s->timers[0], s->timers[0].latch); |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 350 | break; |
| 351 | case 6: |
| 352 | s->timers[0].latch = (s->timers[0].latch & 0xff00) | val; |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 353 | cuda_timer_update(s, &s->timers[0], qemu_get_clock(vm_clock)); |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 354 | break; |
| 355 | case 7: |
| 356 | s->timers[0].latch = (s->timers[0].latch & 0xff) | (val << 8); |
bellard | 61271e5 | 2005-07-07 21:45:18 +0000 | [diff] [blame] | 357 | s->ifr &= ~T1_INT; |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 358 | cuda_timer_update(s, &s->timers[0], qemu_get_clock(vm_clock)); |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 359 | break; |
| 360 | case 8: |
bellard | 61271e5 | 2005-07-07 21:45:18 +0000 | [diff] [blame] | 361 | s->timers[1].latch = val; |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 362 | set_counter(s, &s->timers[1], val); |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 363 | break; |
| 364 | case 9: |
bellard | 61271e5 | 2005-07-07 21:45:18 +0000 | [diff] [blame] | 365 | set_counter(s, &s->timers[1], (val << 8) | s->timers[1].latch); |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 366 | break; |
| 367 | case 10: |
| 368 | s->sr = val; |
| 369 | break; |
| 370 | case 11: |
| 371 | s->acr = val; |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 372 | cuda_timer_update(s, &s->timers[0], qemu_get_clock(vm_clock)); |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 373 | cuda_update(s); |
| 374 | break; |
| 375 | case 12: |
| 376 | s->pcr = val; |
| 377 | break; |
| 378 | case 13: |
| 379 | /* reset bits */ |
| 380 | s->ifr &= ~val; |
| 381 | cuda_update_irq(s); |
| 382 | break; |
| 383 | case 14: |
| 384 | if (val & IER_SET) { |
| 385 | /* set bits */ |
| 386 | s->ier |= val & 0x7f; |
| 387 | } else { |
| 388 | /* reset bits */ |
| 389 | s->ier &= ~val; |
| 390 | } |
| 391 | cuda_update_irq(s); |
| 392 | break; |
| 393 | default: |
| 394 | case 15: |
| 395 | s->anh = val; |
| 396 | break; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /* NOTE: TIP and TREQ are negated */ |
| 401 | static void cuda_update(CUDAState *s) |
| 402 | { |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 403 | int packet_received, len; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 404 | |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 405 | packet_received = 0; |
| 406 | if (!(s->b & TIP)) { |
| 407 | /* transfer requested from host */ |
| 408 | |
| 409 | if (s->acr & SR_OUT) { |
| 410 | /* data output */ |
| 411 | if ((s->b & (TACK | TIP)) != (s->last_b & (TACK | TIP))) { |
| 412 | if (s->data_out_index < sizeof(s->data_out)) { |
| 413 | #ifdef DEBUG_CUDA |
| 414 | printf("cuda: send: %02x\n", s->sr); |
| 415 | #endif |
| 416 | s->data_out[s->data_out_index++] = s->sr; |
| 417 | s->ifr |= SR_INT; |
| 418 | cuda_update_irq(s); |
| 419 | } |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 420 | } |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 421 | } else { |
| 422 | if (s->data_in_index < s->data_in_size) { |
| 423 | /* data input */ |
| 424 | if ((s->b & (TACK | TIP)) != (s->last_b & (TACK | TIP))) { |
| 425 | s->sr = s->data_in[s->data_in_index++]; |
| 426 | #ifdef DEBUG_CUDA |
| 427 | printf("cuda: recv: %02x\n", s->sr); |
| 428 | #endif |
| 429 | /* indicate end of transfer */ |
| 430 | if (s->data_in_index >= s->data_in_size) { |
| 431 | s->b = (s->b | TREQ); |
| 432 | } |
| 433 | s->ifr |= SR_INT; |
| 434 | cuda_update_irq(s); |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | } else { |
| 439 | /* no transfer requested: handle sync case */ |
| 440 | if ((s->last_b & TIP) && (s->b & TACK) != (s->last_b & TACK)) { |
| 441 | /* update TREQ state each time TACK change state */ |
| 442 | if (s->b & TACK) |
| 443 | s->b = (s->b | TREQ); |
| 444 | else |
| 445 | s->b = (s->b & ~TREQ); |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 446 | s->ifr |= SR_INT; |
| 447 | cuda_update_irq(s); |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 448 | } else { |
| 449 | if (!(s->last_b & TIP)) { |
ths | e91c8a7 | 2007-06-03 13:35:16 +0000 | [diff] [blame] | 450 | /* handle end of host to cuda transfer */ |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 451 | packet_received = (s->data_out_index > 0); |
ths | e91c8a7 | 2007-06-03 13:35:16 +0000 | [diff] [blame] | 452 | /* always an IRQ at the end of transfer */ |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 453 | s->ifr |= SR_INT; |
| 454 | cuda_update_irq(s); |
| 455 | } |
| 456 | /* signal if there is data to read */ |
| 457 | if (s->data_in_index < s->data_in_size) { |
| 458 | s->b = (s->b & ~TREQ); |
| 459 | } |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 463 | s->last_acr = s->acr; |
| 464 | s->last_b = s->b; |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 465 | |
| 466 | /* NOTE: cuda_receive_packet_from_host() can call cuda_update() |
| 467 | recursively */ |
| 468 | if (packet_received) { |
| 469 | len = s->data_out_index; |
| 470 | s->data_out_index = 0; |
| 471 | cuda_receive_packet_from_host(s, s->data_out, len); |
| 472 | } |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 473 | } |
| 474 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 475 | static void cuda_send_packet_to_host(CUDAState *s, |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 476 | const uint8_t *data, int len) |
| 477 | { |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 478 | #ifdef DEBUG_CUDA_PACKET |
| 479 | { |
| 480 | int i; |
| 481 | printf("cuda_send_packet_to_host:\n"); |
| 482 | for(i = 0; i < len; i++) |
| 483 | printf(" %02x", data[i]); |
| 484 | printf("\n"); |
| 485 | } |
| 486 | #endif |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 487 | memcpy(s->data_in, data, len); |
| 488 | s->data_in_size = len; |
| 489 | s->data_in_index = 0; |
| 490 | cuda_update(s); |
| 491 | s->ifr |= SR_INT; |
| 492 | cuda_update_irq(s); |
| 493 | } |
| 494 | |
bellard | 7db4eea | 2004-07-06 20:57:47 +0000 | [diff] [blame] | 495 | static void cuda_adb_poll(void *opaque) |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 496 | { |
| 497 | CUDAState *s = opaque; |
| 498 | uint8_t obuf[ADB_MAX_OUT_LEN + 2]; |
| 499 | int olen; |
| 500 | |
| 501 | olen = adb_poll(&adb_bus, obuf + 2); |
| 502 | if (olen > 0) { |
| 503 | obuf[0] = ADB_PACKET; |
| 504 | obuf[1] = 0x40; /* polled data */ |
| 505 | cuda_send_packet_to_host(s, obuf, olen + 2); |
| 506 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 507 | qemu_mod_timer(s->adb_poll_timer, |
| 508 | qemu_get_clock(vm_clock) + |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 509 | (ticks_per_sec / CUDA_ADB_POLL_FREQ)); |
| 510 | } |
| 511 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 512 | static void cuda_receive_packet(CUDAState *s, |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 513 | const uint8_t *data, int len) |
| 514 | { |
| 515 | uint8_t obuf[16]; |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 516 | int ti, autopoll; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 517 | |
| 518 | switch(data[0]) { |
| 519 | case CUDA_AUTOPOLL: |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 520 | autopoll = (data[1] != 0); |
| 521 | if (autopoll != s->autopoll) { |
| 522 | s->autopoll = autopoll; |
| 523 | if (autopoll) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 524 | qemu_mod_timer(s->adb_poll_timer, |
| 525 | qemu_get_clock(vm_clock) + |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 526 | (ticks_per_sec / CUDA_ADB_POLL_FREQ)); |
| 527 | } else { |
| 528 | qemu_del_timer(s->adb_poll_timer); |
| 529 | } |
| 530 | } |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 531 | obuf[0] = CUDA_PACKET; |
| 532 | obuf[1] = data[1]; |
| 533 | cuda_send_packet_to_host(s, obuf, 2); |
| 534 | break; |
| 535 | case CUDA_GET_TIME: |
bellard | dccfafc | 2005-04-23 18:16:54 +0000 | [diff] [blame] | 536 | case CUDA_SET_TIME: |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 537 | /* XXX: add time support ? */ |
bellard | d7ce296 | 2005-02-09 00:07:08 +0000 | [diff] [blame] | 538 | ti = time(NULL) + RTC_OFFSET; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 539 | obuf[0] = CUDA_PACKET; |
| 540 | obuf[1] = 0; |
| 541 | obuf[2] = 0; |
| 542 | obuf[3] = ti >> 24; |
| 543 | obuf[4] = ti >> 16; |
| 544 | obuf[5] = ti >> 8; |
| 545 | obuf[6] = ti; |
| 546 | cuda_send_packet_to_host(s, obuf, 7); |
| 547 | break; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 548 | case CUDA_FILE_SERVER_FLAG: |
| 549 | case CUDA_SET_DEVICE_LIST: |
| 550 | case CUDA_SET_AUTO_RATE: |
| 551 | case CUDA_SET_POWER_MESSAGES: |
| 552 | obuf[0] = CUDA_PACKET; |
| 553 | obuf[1] = 0; |
| 554 | cuda_send_packet_to_host(s, obuf, 2); |
| 555 | break; |
bellard | d7ce296 | 2005-02-09 00:07:08 +0000 | [diff] [blame] | 556 | case CUDA_POWERDOWN: |
| 557 | obuf[0] = CUDA_PACKET; |
| 558 | obuf[1] = 0; |
| 559 | cuda_send_packet_to_host(s, obuf, 2); |
| 560 | qemu_system_shutdown_request(); |
| 561 | break; |
j_mayer | 0686970 | 2007-09-19 04:46:57 +0000 | [diff] [blame] | 562 | case CUDA_RESET_SYSTEM: |
| 563 | obuf[0] = CUDA_PACKET; |
| 564 | obuf[1] = 0; |
| 565 | cuda_send_packet_to_host(s, obuf, 2); |
| 566 | qemu_system_reset_request(); |
| 567 | break; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 568 | default: |
| 569 | break; |
| 570 | } |
| 571 | } |
| 572 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 573 | static void cuda_receive_packet_from_host(CUDAState *s, |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 574 | const uint8_t *data, int len) |
| 575 | { |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 576 | #ifdef DEBUG_CUDA_PACKET |
| 577 | { |
| 578 | int i; |
bellard | cadae95 | 2005-06-05 15:24:23 +0000 | [diff] [blame] | 579 | printf("cuda_receive_packet_from_host:\n"); |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 580 | for(i = 0; i < len; i++) |
| 581 | printf(" %02x", data[i]); |
| 582 | printf("\n"); |
| 583 | } |
| 584 | #endif |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 585 | switch(data[0]) { |
| 586 | case ADB_PACKET: |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 587 | { |
| 588 | uint8_t obuf[ADB_MAX_OUT_LEN + 2]; |
| 589 | int olen; |
| 590 | olen = adb_request(&adb_bus, obuf + 2, data + 1, len - 1); |
bellard | 38f0b14 | 2004-07-12 20:16:00 +0000 | [diff] [blame] | 591 | if (olen > 0) { |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 592 | obuf[0] = ADB_PACKET; |
| 593 | obuf[1] = 0x00; |
| 594 | } else { |
bellard | 38f0b14 | 2004-07-12 20:16:00 +0000 | [diff] [blame] | 595 | /* error */ |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 596 | obuf[0] = ADB_PACKET; |
bellard | 38f0b14 | 2004-07-12 20:16:00 +0000 | [diff] [blame] | 597 | obuf[1] = -olen; |
| 598 | olen = 0; |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 599 | } |
| 600 | cuda_send_packet_to_host(s, obuf, olen + 2); |
| 601 | } |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 602 | break; |
| 603 | case CUDA_PACKET: |
| 604 | cuda_receive_packet(s, data + 1, len - 1); |
| 605 | break; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | static void cuda_writew (void *opaque, target_phys_addr_t addr, uint32_t value) |
| 610 | { |
| 611 | } |
| 612 | |
| 613 | static void cuda_writel (void *opaque, target_phys_addr_t addr, uint32_t value) |
| 614 | { |
| 615 | } |
| 616 | |
| 617 | static uint32_t cuda_readw (void *opaque, target_phys_addr_t addr) |
| 618 | { |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | static uint32_t cuda_readl (void *opaque, target_phys_addr_t addr) |
| 623 | { |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | static CPUWriteMemoryFunc *cuda_write[] = { |
| 628 | &cuda_writeb, |
| 629 | &cuda_writew, |
| 630 | &cuda_writel, |
| 631 | }; |
| 632 | |
| 633 | static CPUReadMemoryFunc *cuda_read[] = { |
| 634 | &cuda_readb, |
| 635 | &cuda_readw, |
| 636 | &cuda_readl, |
| 637 | }; |
| 638 | |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame^] | 639 | void cuda_init (int *cuda_mem_index, qemu_irq irq) |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 640 | { |
| 641 | CUDAState *s = &cuda_state; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 642 | |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 643 | s->irq = irq; |
| 644 | |
bellard | 61271e5 | 2005-07-07 21:45:18 +0000 | [diff] [blame] | 645 | s->timers[0].index = 0; |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 646 | s->timers[0].timer = qemu_new_timer(vm_clock, cuda_timer1, s); |
bellard | 61271e5 | 2005-07-07 21:45:18 +0000 | [diff] [blame] | 647 | s->timers[0].latch = 0xffff; |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 648 | set_counter(s, &s->timers[0], 0xffff); |
bellard | 61271e5 | 2005-07-07 21:45:18 +0000 | [diff] [blame] | 649 | |
| 650 | s->timers[1].index = 1; |
| 651 | s->timers[1].latch = 0; |
bellard | cadae95 | 2005-06-05 15:24:23 +0000 | [diff] [blame] | 652 | // s->ier = T1_INT | SR_INT; |
| 653 | s->ier = 0; |
bellard | 819e712 | 2004-06-21 16:47:13 +0000 | [diff] [blame] | 654 | set_counter(s, &s->timers[1], 0xffff); |
bellard | e2733d2 | 2004-06-21 22:46:10 +0000 | [diff] [blame] | 655 | |
| 656 | s->adb_poll_timer = qemu_new_timer(vm_clock, cuda_adb_poll, s); |
j_mayer | 3cbee15 | 2007-10-28 23:42:18 +0000 | [diff] [blame^] | 657 | *cuda_mem_index = cpu_register_io_memory(0, cuda_read, cuda_write, s); |
bellard | 267002c | 2004-06-03 18:46:20 +0000 | [diff] [blame] | 658 | } |