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