4 * Copyright (c) 2004 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
27 //#define DEBUG_CUDA_PACKET
29 /* Bits in B data register: all active low */
30 #define TREQ 0x08 /* Transfer request (input) */
31 #define TACK 0x10 /* Transfer acknowledge (output) */
32 #define TIP 0x20 /* Transfer in progress (output) */
35 #define SR_CTRL 0x1c /* Shift register control bits */
36 #define SR_EXT 0x0c /* Shift on external clock */
37 #define SR_OUT 0x10 /* Shift out if 1 */
39 /* Bits in IFR and IER */
40 #define IER_SET 0x80 /* set bits in IER */
41 #define IER_CLR 0 /* clear bits in IER */
42 #define SR_INT 0x04 /* Shift register full/empty */
43 #define T1_INT 0x40 /* Timer 1 interrupt */
46 #define T1MODE 0xc0 /* Timer 1 mode */
47 #define T1MODE_CONT 0x40 /* continuous interrupts */
49 /* commands (1st byte) */
52 #define ERROR_PACKET 2
53 #define TIMER_PACKET 3
54 #define POWER_PACKET 4
55 #define MACIIC_PACKET 5
59 /* CUDA commands (2nd byte) */
60 #define CUDA_WARM_START 0x0
61 #define CUDA_AUTOPOLL 0x1
62 #define CUDA_GET_6805_ADDR 0x2
63 #define CUDA_GET_TIME 0x3
64 #define CUDA_GET_PRAM 0x7
65 #define CUDA_SET_6805_ADDR 0x8
66 #define CUDA_SET_TIME 0x9
67 #define CUDA_POWERDOWN 0xa
68 #define CUDA_POWERUP_TIME 0xb
69 #define CUDA_SET_PRAM 0xc
70 #define CUDA_MS_RESET 0xd
71 #define CUDA_SEND_DFAC 0xe
72 #define CUDA_BATTERY_SWAP_SENSE 0x10
73 #define CUDA_RESET_SYSTEM 0x11
74 #define CUDA_SET_IPL 0x12
75 #define CUDA_FILE_SERVER_FLAG 0x13
76 #define CUDA_SET_AUTO_RATE 0x14
77 #define CUDA_GET_AUTO_RATE 0x16
78 #define CUDA_SET_DEVICE_LIST 0x19
79 #define CUDA_GET_DEVICE_LIST 0x1a
80 #define CUDA_SET_ONE_SECOND_MODE 0x1b
81 #define CUDA_SET_POWER_MESSAGES 0x21
82 #define CUDA_GET_SET_IIC 0x22
83 #define CUDA_WAKEUP 0x23
84 #define CUDA_TIMER_TICKLE 0x24
85 #define CUDA_COMBINED_FORMAT_IIC 0x25
87 #define CUDA_TIMER_FREQ (4700000 / 6)
89 typedef struct CUDATimer {
91 uint16_t counter_value; /* counter value at load time */
93 int64_t next_irq_time;
97 typedef struct CUDAState {
99 uint8_t b; /* B-side data */
100 uint8_t a; /* A-side data */
101 uint8_t dirb; /* B-side direction (1=output) */
102 uint8_t dira; /* A-side direction (1=output) */
103 uint8_t sr; /* Shift register */
104 uint8_t acr; /* Auxiliary control register */
105 uint8_t pcr; /* Peripheral control register */
106 uint8_t ifr; /* Interrupt flag register */
107 uint8_t ier; /* Interrupt enable register */
108 uint8_t anh; /* A-side data, no handshake */
112 uint8_t last_b; /* last value of B register */
113 uint8_t last_acr; /* last value of B register */
122 uint8_t data_in[128];
123 uint8_t data_out[16];
126 static CUDAState cuda_state;
129 static void cuda_update(CUDAState *s);
130 static void cuda_receive_packet_from_host(CUDAState *s,
131 const uint8_t *data, int len);
132 static void cuda_timer_update(CUDAState *s, CUDATimer *ti,
133 int64_t current_time);
135 static void cuda_update_irq(CUDAState *s)
137 if (s->ifr & s->ier & (SR_INT | T1_INT)) {
138 openpic_set_irq(s->openpic, s->irq, 1);
140 openpic_set_irq(s->openpic, s->irq, 0);
144 static unsigned int get_counter(CUDATimer *s)
147 unsigned int counter;
149 d = muldiv64(qemu_get_clock(vm_clock) - s->load_time,
150 CUDA_TIMER_FREQ, ticks_per_sec);
151 if (d <= s->counter_value) {
154 counter = s->latch - 1 - ((d - s->counter_value) % s->latch);
159 static void set_counter(CUDAState *s, CUDATimer *ti, unsigned int val)
162 printf("cuda: T%d.counter=%d\n",
163 1 + (ti->timer == NULL), val);
165 ti->load_time = qemu_get_clock(vm_clock);
166 ti->counter_value = val;
167 cuda_timer_update(s, ti, ti->load_time);
170 static int64_t get_next_irq_time(CUDATimer *s, int64_t current_time)
172 int64_t d, next_time, base;
173 /* current counter value */
174 d = muldiv64(current_time - s->load_time,
175 CUDA_TIMER_FREQ, ticks_per_sec);
176 if (d <= s->counter_value) {
177 next_time = s->counter_value + 1;
179 base = ((d - s->counter_value) / s->latch);
180 base = (base * s->latch) + s->counter_value;
181 next_time = base + s->latch;
184 printf("latch=%d counter=%lld delta_next=%lld\n",
185 s->latch, d, next_time - d);
187 next_time = muldiv64(next_time, ticks_per_sec, CUDA_TIMER_FREQ) +
189 if (next_time <= current_time)
190 next_time = current_time + 1;
194 static void cuda_timer_update(CUDAState *s, CUDATimer *ti,
195 int64_t current_time)
199 if ((s->acr & T1MODE) != T1MODE_CONT) {
200 qemu_del_timer(ti->timer);
202 ti->next_irq_time = get_next_irq_time(ti, current_time);
203 qemu_mod_timer(ti->timer, ti->next_irq_time);
207 static void cuda_timer1(void *opaque)
209 CUDAState *s = opaque;
210 CUDATimer *ti = &s->timers[0];
212 cuda_timer_update(s, ti, ti->next_irq_time);
217 static uint32_t cuda_readb(void *opaque, target_phys_addr_t addr)
219 CUDAState *s = opaque;
222 addr = (addr >> 9) & 0xf;
237 val = get_counter(&s->timers[0]) & 0xff;
242 val = get_counter(&s->timers[0]) >> 8;
247 val = s->timers[0].latch & 0xff;
250 val = (s->timers[0].latch >> 8) & 0xff;
253 val = get_counter(&s->timers[1]) & 0xff;
256 val = get_counter(&s->timers[1]) >> 8;
281 if (addr != 13 || val != 0)
282 printf("cuda: read: reg=0x%x val=%02x\n", addr, val);
287 static void cuda_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
289 CUDAState *s = opaque;
291 addr = (addr >> 9) & 0xf;
293 printf("cuda: write: reg=0x%x val=%02x\n", addr, val);
311 val = val | (get_counter(&s->timers[0]) & 0xff00);
312 set_counter(s, &s->timers[0], val);
315 val = (val << 8) | (get_counter(&s->timers[0]) & 0xff);
316 set_counter(s, &s->timers[0], val);
319 s->timers[0].latch = (s->timers[0].latch & 0xff00) | val;
320 cuda_timer_update(s, &s->timers[0], qemu_get_clock(vm_clock));
323 s->timers[0].latch = (s->timers[0].latch & 0xff) | (val << 8);
324 cuda_timer_update(s, &s->timers[0], qemu_get_clock(vm_clock));
327 val = val | (get_counter(&s->timers[1]) & 0xff00);
328 set_counter(s, &s->timers[1], val);
331 val = (val << 8) | (get_counter(&s->timers[1]) & 0xff);
332 set_counter(s, &s->timers[1], val);
339 cuda_timer_update(s, &s->timers[0], qemu_get_clock(vm_clock));
353 s->ier |= val & 0x7f;
367 /* NOTE: TIP and TREQ are negated */
368 static void cuda_update(CUDAState *s)
370 int packet_received, len;
374 /* transfer requested from host */
376 if (s->acr & SR_OUT) {
378 if ((s->b & (TACK | TIP)) != (s->last_b & (TACK | TIP))) {
379 if (s->data_out_index < sizeof(s->data_out)) {
381 printf("cuda: send: %02x\n", s->sr);
383 s->data_out[s->data_out_index++] = s->sr;
389 if (s->data_in_index < s->data_in_size) {
391 if ((s->b & (TACK | TIP)) != (s->last_b & (TACK | TIP))) {
392 s->sr = s->data_in[s->data_in_index++];
394 printf("cuda: recv: %02x\n", s->sr);
396 /* indicate end of transfer */
397 if (s->data_in_index >= s->data_in_size) {
398 s->b = (s->b | TREQ);
406 /* no transfer requested: handle sync case */
407 if ((s->last_b & TIP) && (s->b & TACK) != (s->last_b & TACK)) {
408 /* update TREQ state each time TACK change state */
410 s->b = (s->b | TREQ);
412 s->b = (s->b & ~TREQ);
416 if (!(s->last_b & TIP)) {
417 /* handle end of host to cuda transfert */
418 packet_received = (s->data_out_index > 0);
419 /* always an IRQ at the end of transfert */
423 /* signal if there is data to read */
424 if (s->data_in_index < s->data_in_size) {
425 s->b = (s->b & ~TREQ);
430 s->last_acr = s->acr;
433 /* NOTE: cuda_receive_packet_from_host() can call cuda_update()
435 if (packet_received) {
436 len = s->data_out_index;
437 s->data_out_index = 0;
438 cuda_receive_packet_from_host(s, s->data_out, len);
442 static void cuda_send_packet_to_host(CUDAState *s,
443 const uint8_t *data, int len)
445 #ifdef DEBUG_CUDA_PACKET
448 printf("cuda_send_packet_to_host:\n");
449 for(i = 0; i < len; i++)
450 printf(" %02x", data[i]);
454 memcpy(s->data_in, data, len);
455 s->data_in_size = len;
456 s->data_in_index = 0;
462 void adb_send_packet(ADBBusState *bus, const uint8_t *buf, int len)
464 CUDAState *s = &cuda_state;
467 memcpy(data + 1, buf, len);
468 data[0] = ADB_PACKET;
469 cuda_send_packet_to_host(s, data, len + 1);
472 static void cuda_receive_packet(CUDAState *s,
473 const uint8_t *data, int len)
480 s->autopoll = data[1];
481 obuf[0] = CUDA_PACKET;
483 cuda_send_packet_to_host(s, obuf, 2);
486 /* XXX: add time support ? */
488 obuf[0] = CUDA_PACKET;
495 cuda_send_packet_to_host(s, obuf, 7);
498 case CUDA_FILE_SERVER_FLAG:
499 case CUDA_SET_DEVICE_LIST:
500 case CUDA_SET_AUTO_RATE:
501 case CUDA_SET_POWER_MESSAGES:
502 obuf[0] = CUDA_PACKET;
504 cuda_send_packet_to_host(s, obuf, 2);
511 static void cuda_receive_packet_from_host(CUDAState *s,
512 const uint8_t *data, int len)
514 #ifdef DEBUG_CUDA_PACKET
517 printf("cuda_receive_packet_to_host:\n");
518 for(i = 0; i < len; i++)
519 printf(" %02x", data[i]);
525 adb_receive_packet(&adb_bus, data + 1, len - 1);
528 cuda_receive_packet(s, data + 1, len - 1);
533 static void cuda_writew (void *opaque, target_phys_addr_t addr, uint32_t value)
537 static void cuda_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
541 static uint32_t cuda_readw (void *opaque, target_phys_addr_t addr)
546 static uint32_t cuda_readl (void *opaque, target_phys_addr_t addr)
551 static CPUWriteMemoryFunc *cuda_write[] = {
557 static CPUReadMemoryFunc *cuda_read[] = {
563 int cuda_init(openpic_t *openpic, int irq)
565 CUDAState *s = &cuda_state;
568 s->openpic = openpic;
571 s->timers[0].timer = qemu_new_timer(vm_clock, cuda_timer1, s);
572 s->timers[0].latch = 0x10000;
573 set_counter(s, &s->timers[0], 0xffff);
574 s->timers[1].latch = 0x10000;
575 s->ier = T1_INT | SR_INT;
576 set_counter(s, &s->timers[1], 0xffff);
577 cuda_mem_index = cpu_register_io_memory(0, cuda_read, cuda_write, s);
578 return cuda_mem_index;