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)
88 #define CUDA_ADB_POLL_FREQ 50
90 /* CUDA returns time_t's offset from Jan 1, 1904, not 1970 */
91 #define RTC_OFFSET 2082844800
93 typedef struct CUDATimer
{
95 uint16_t counter_value
; /* counter value at load time */
97 int64_t next_irq_time
;
101 typedef struct CUDAState
{
103 uint8_t b
; /* B-side data */
104 uint8_t a
; /* A-side data */
105 uint8_t dirb
; /* B-side direction (1=output) */
106 uint8_t dira
; /* A-side direction (1=output) */
107 uint8_t sr
; /* Shift register */
108 uint8_t acr
; /* Auxiliary control register */
109 uint8_t pcr
; /* Peripheral control register */
110 uint8_t ifr
; /* Interrupt flag register */
111 uint8_t ier
; /* Interrupt enable register */
112 uint8_t anh
; /* A-side data, no handshake */
116 uint8_t last_b
; /* last value of B register */
117 uint8_t last_acr
; /* last value of B register */
126 uint8_t data_in
[128];
127 uint8_t data_out
[16];
128 QEMUTimer
*adb_poll_timer
;
131 static CUDAState cuda_state
;
134 static void cuda_update(CUDAState
*s
);
135 static void cuda_receive_packet_from_host(CUDAState
*s
,
136 const uint8_t *data
, int len
);
137 static void cuda_timer_update(CUDAState
*s
, CUDATimer
*ti
,
138 int64_t current_time
);
140 static void cuda_update_irq(CUDAState
*s
)
142 if (s
->ifr
& s
->ier
& (SR_INT
| T1_INT
)) {
143 openpic_set_irq(s
->openpic
, s
->irq
, 1);
145 openpic_set_irq(s
->openpic
, s
->irq
, 0);
149 static unsigned int get_counter(CUDATimer
*s
)
152 unsigned int counter
;
154 d
= muldiv64(qemu_get_clock(vm_clock
) - s
->load_time
,
155 CUDA_TIMER_FREQ
, ticks_per_sec
);
156 if (d
<= s
->counter_value
) {
159 counter
= s
->latch
- 1 - ((d
- s
->counter_value
) % s
->latch
);
164 static void set_counter(CUDAState
*s
, CUDATimer
*ti
, unsigned int val
)
167 printf("cuda: T%d.counter=%d\n",
168 1 + (ti
->timer
== NULL
), val
);
170 ti
->load_time
= qemu_get_clock(vm_clock
);
171 ti
->counter_value
= val
;
172 cuda_timer_update(s
, ti
, ti
->load_time
);
175 static int64_t get_next_irq_time(CUDATimer
*s
, int64_t current_time
)
177 int64_t d
, next_time
, base
;
178 /* current counter value */
179 d
= muldiv64(current_time
- s
->load_time
,
180 CUDA_TIMER_FREQ
, ticks_per_sec
);
181 if (d
< s
->counter_value
) {
182 next_time
= s
->counter_value
+ 1;
185 base
= ((d
- s
->counter_value
+ 1) / s
->latch
);
186 base
= (base
* s
->latch
) + s
->counter_value
;
187 next_time
= base
+ s
->latch
;
191 printf("latch=%d counter=%lld delta_next=%lld\n",
192 s
->latch
, d
, next_time
- d
);
195 next_time
= muldiv64(next_time
, ticks_per_sec
, CUDA_TIMER_FREQ
) +
197 if (next_time
<= current_time
)
198 next_time
= current_time
+ 1;
202 static void cuda_timer_update(CUDAState
*s
, CUDATimer
*ti
,
203 int64_t current_time
)
207 if ((s
->acr
& T1MODE
) != T1MODE_CONT
) {
208 qemu_del_timer(ti
->timer
);
210 ti
->next_irq_time
= get_next_irq_time(ti
, current_time
);
211 qemu_mod_timer(ti
->timer
, ti
->next_irq_time
);
215 static void cuda_timer1(void *opaque
)
217 CUDAState
*s
= opaque
;
218 CUDATimer
*ti
= &s
->timers
[0];
220 cuda_timer_update(s
, ti
, ti
->next_irq_time
);
225 static uint32_t cuda_readb(void *opaque
, target_phys_addr_t addr
)
227 CUDAState
*s
= opaque
;
230 addr
= (addr
>> 9) & 0xf;
245 val
= get_counter(&s
->timers
[0]) & 0xff;
250 val
= get_counter(&s
->timers
[0]) >> 8;
255 val
= s
->timers
[0].latch
& 0xff;
258 val
= (s
->timers
[0].latch
>> 8) & 0xff;
261 val
= get_counter(&s
->timers
[1]) & 0xff;
264 val
= get_counter(&s
->timers
[1]) >> 8;
289 if (addr
!= 13 || val
!= 0)
290 printf("cuda: read: reg=0x%x val=%02x\n", addr
, val
);
295 static void cuda_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
297 CUDAState
*s
= opaque
;
299 addr
= (addr
>> 9) & 0xf;
301 printf("cuda: write: reg=0x%x val=%02x\n", addr
, val
);
319 val
= val
| (get_counter(&s
->timers
[0]) & 0xff00);
320 set_counter(s
, &s
->timers
[0], val
);
323 val
= (val
<< 8) | (get_counter(&s
->timers
[0]) & 0xff);
324 set_counter(s
, &s
->timers
[0], val
);
327 s
->timers
[0].latch
= (s
->timers
[0].latch
& 0xff00) | val
;
328 cuda_timer_update(s
, &s
->timers
[0], qemu_get_clock(vm_clock
));
331 s
->timers
[0].latch
= (s
->timers
[0].latch
& 0xff) | (val
<< 8);
332 cuda_timer_update(s
, &s
->timers
[0], qemu_get_clock(vm_clock
));
335 val
= val
| (get_counter(&s
->timers
[1]) & 0xff00);
336 set_counter(s
, &s
->timers
[1], val
);
339 val
= (val
<< 8) | (get_counter(&s
->timers
[1]) & 0xff);
340 set_counter(s
, &s
->timers
[1], val
);
347 cuda_timer_update(s
, &s
->timers
[0], qemu_get_clock(vm_clock
));
361 s
->ier
|= val
& 0x7f;
375 /* NOTE: TIP and TREQ are negated */
376 static void cuda_update(CUDAState
*s
)
378 int packet_received
, len
;
382 /* transfer requested from host */
384 if (s
->acr
& SR_OUT
) {
386 if ((s
->b
& (TACK
| TIP
)) != (s
->last_b
& (TACK
| TIP
))) {
387 if (s
->data_out_index
< sizeof(s
->data_out
)) {
389 printf("cuda: send: %02x\n", s
->sr
);
391 s
->data_out
[s
->data_out_index
++] = s
->sr
;
397 if (s
->data_in_index
< s
->data_in_size
) {
399 if ((s
->b
& (TACK
| TIP
)) != (s
->last_b
& (TACK
| TIP
))) {
400 s
->sr
= s
->data_in
[s
->data_in_index
++];
402 printf("cuda: recv: %02x\n", s
->sr
);
404 /* indicate end of transfer */
405 if (s
->data_in_index
>= s
->data_in_size
) {
406 s
->b
= (s
->b
| TREQ
);
414 /* no transfer requested: handle sync case */
415 if ((s
->last_b
& TIP
) && (s
->b
& TACK
) != (s
->last_b
& TACK
)) {
416 /* update TREQ state each time TACK change state */
418 s
->b
= (s
->b
| TREQ
);
420 s
->b
= (s
->b
& ~TREQ
);
424 if (!(s
->last_b
& TIP
)) {
425 /* handle end of host to cuda transfert */
426 packet_received
= (s
->data_out_index
> 0);
427 /* always an IRQ at the end of transfert */
431 /* signal if there is data to read */
432 if (s
->data_in_index
< s
->data_in_size
) {
433 s
->b
= (s
->b
& ~TREQ
);
438 s
->last_acr
= s
->acr
;
441 /* NOTE: cuda_receive_packet_from_host() can call cuda_update()
443 if (packet_received
) {
444 len
= s
->data_out_index
;
445 s
->data_out_index
= 0;
446 cuda_receive_packet_from_host(s
, s
->data_out
, len
);
450 static void cuda_send_packet_to_host(CUDAState
*s
,
451 const uint8_t *data
, int len
)
453 #ifdef DEBUG_CUDA_PACKET
456 printf("cuda_send_packet_to_host:\n");
457 for(i
= 0; i
< len
; i
++)
458 printf(" %02x", data
[i
]);
462 memcpy(s
->data_in
, data
, len
);
463 s
->data_in_size
= len
;
464 s
->data_in_index
= 0;
470 static void cuda_adb_poll(void *opaque
)
472 CUDAState
*s
= opaque
;
473 uint8_t obuf
[ADB_MAX_OUT_LEN
+ 2];
476 olen
= adb_poll(&adb_bus
, obuf
+ 2);
478 obuf
[0] = ADB_PACKET
;
479 obuf
[1] = 0x40; /* polled data */
480 cuda_send_packet_to_host(s
, obuf
, olen
+ 2);
482 qemu_mod_timer(s
->adb_poll_timer
,
483 qemu_get_clock(vm_clock
) +
484 (ticks_per_sec
/ CUDA_ADB_POLL_FREQ
));
487 static void cuda_receive_packet(CUDAState
*s
,
488 const uint8_t *data
, int len
)
495 autopoll
= (data
[1] != 0);
496 if (autopoll
!= s
->autopoll
) {
497 s
->autopoll
= autopoll
;
499 qemu_mod_timer(s
->adb_poll_timer
,
500 qemu_get_clock(vm_clock
) +
501 (ticks_per_sec
/ CUDA_ADB_POLL_FREQ
));
503 qemu_del_timer(s
->adb_poll_timer
);
506 obuf
[0] = CUDA_PACKET
;
508 cuda_send_packet_to_host(s
, obuf
, 2);
512 /* XXX: add time support ? */
513 ti
= time(NULL
) + RTC_OFFSET
;
514 obuf
[0] = CUDA_PACKET
;
521 cuda_send_packet_to_host(s
, obuf
, 7);
523 case CUDA_FILE_SERVER_FLAG
:
524 case CUDA_SET_DEVICE_LIST
:
525 case CUDA_SET_AUTO_RATE
:
526 case CUDA_SET_POWER_MESSAGES
:
527 obuf
[0] = CUDA_PACKET
;
529 cuda_send_packet_to_host(s
, obuf
, 2);
532 obuf
[0] = CUDA_PACKET
;
534 cuda_send_packet_to_host(s
, obuf
, 2);
535 qemu_system_shutdown_request();
542 static void cuda_receive_packet_from_host(CUDAState
*s
,
543 const uint8_t *data
, int len
)
545 #ifdef DEBUG_CUDA_PACKET
548 printf("cuda_receive_packet_to_host:\n");
549 for(i
= 0; i
< len
; i
++)
550 printf(" %02x", data
[i
]);
557 uint8_t obuf
[ADB_MAX_OUT_LEN
+ 2];
559 olen
= adb_request(&adb_bus
, obuf
+ 2, data
+ 1, len
- 1);
561 obuf
[0] = ADB_PACKET
;
565 obuf
[0] = ADB_PACKET
;
569 cuda_send_packet_to_host(s
, obuf
, olen
+ 2);
573 cuda_receive_packet(s
, data
+ 1, len
- 1);
578 static void cuda_writew (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
582 static void cuda_writel (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
586 static uint32_t cuda_readw (void *opaque
, target_phys_addr_t addr
)
591 static uint32_t cuda_readl (void *opaque
, target_phys_addr_t addr
)
596 static CPUWriteMemoryFunc
*cuda_write
[] = {
602 static CPUReadMemoryFunc
*cuda_read
[] = {
608 int cuda_init(openpic_t
*openpic
, int irq
)
610 CUDAState
*s
= &cuda_state
;
613 s
->openpic
= openpic
;
616 s
->timers
[0].timer
= qemu_new_timer(vm_clock
, cuda_timer1
, s
);
617 s
->timers
[0].latch
= 0x10000;
618 set_counter(s
, &s
->timers
[0], 0xffff);
619 s
->timers
[1].latch
= 0x10000;
620 s
->ier
= T1_INT
| SR_INT
;
621 set_counter(s
, &s
->timers
[1], 0xffff);
623 s
->adb_poll_timer
= qemu_new_timer(vm_clock
, cuda_adb_poll
, s
);
624 cuda_mem_index
= cpu_register_io_memory(0, cuda_read
, cuda_write
, s
);
625 return cuda_mem_index
;