2 * QEMU PowerMac CUDA device support
4 * Copyright (c) 2004-2007 Fabrice Bellard
5 * Copyright (c) 2007 Jocelyn Mayer
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
27 #include "qemu-timer.h"
30 /* XXX: implement all timer modes */
35 /* debug CUDA packets */
36 //#define DEBUG_CUDA_PACKET
39 #define CUDA_DPRINTF(fmt, ...) \
40 do { printf("CUDA: " fmt , ## __VA_ARGS__); } while (0)
42 #define CUDA_DPRINTF(fmt, ...)
45 /* Bits in B data register: all active low */
46 #define TREQ 0x08 /* Transfer request (input) */
47 #define TACK 0x10 /* Transfer acknowledge (output) */
48 #define TIP 0x20 /* Transfer in progress (output) */
51 #define SR_CTRL 0x1c /* Shift register control bits */
52 #define SR_EXT 0x0c /* Shift on external clock */
53 #define SR_OUT 0x10 /* Shift out if 1 */
55 /* Bits in IFR and IER */
56 #define IER_SET 0x80 /* set bits in IER */
57 #define IER_CLR 0 /* clear bits in IER */
58 #define SR_INT 0x04 /* Shift register full/empty */
59 #define T1_INT 0x40 /* Timer 1 interrupt */
60 #define T2_INT 0x20 /* Timer 2 interrupt */
63 #define T1MODE 0xc0 /* Timer 1 mode */
64 #define T1MODE_CONT 0x40 /* continuous interrupts */
66 /* commands (1st byte) */
69 #define ERROR_PACKET 2
70 #define TIMER_PACKET 3
71 #define POWER_PACKET 4
72 #define MACIIC_PACKET 5
76 /* CUDA commands (2nd byte) */
77 #define CUDA_WARM_START 0x0
78 #define CUDA_AUTOPOLL 0x1
79 #define CUDA_GET_6805_ADDR 0x2
80 #define CUDA_GET_TIME 0x3
81 #define CUDA_GET_PRAM 0x7
82 #define CUDA_SET_6805_ADDR 0x8
83 #define CUDA_SET_TIME 0x9
84 #define CUDA_POWERDOWN 0xa
85 #define CUDA_POWERUP_TIME 0xb
86 #define CUDA_SET_PRAM 0xc
87 #define CUDA_MS_RESET 0xd
88 #define CUDA_SEND_DFAC 0xe
89 #define CUDA_BATTERY_SWAP_SENSE 0x10
90 #define CUDA_RESET_SYSTEM 0x11
91 #define CUDA_SET_IPL 0x12
92 #define CUDA_FILE_SERVER_FLAG 0x13
93 #define CUDA_SET_AUTO_RATE 0x14
94 #define CUDA_GET_AUTO_RATE 0x16
95 #define CUDA_SET_DEVICE_LIST 0x19
96 #define CUDA_GET_DEVICE_LIST 0x1a
97 #define CUDA_SET_ONE_SECOND_MODE 0x1b
98 #define CUDA_SET_POWER_MESSAGES 0x21
99 #define CUDA_GET_SET_IIC 0x22
100 #define CUDA_WAKEUP 0x23
101 #define CUDA_TIMER_TICKLE 0x24
102 #define CUDA_COMBINED_FORMAT_IIC 0x25
104 #define CUDA_TIMER_FREQ (4700000 / 6)
105 #define CUDA_ADB_POLL_FREQ 50
107 /* CUDA returns time_t's offset from Jan 1, 1904, not 1970 */
108 #define RTC_OFFSET 2082844800
110 typedef struct CUDATimer
{
113 uint16_t counter_value
; /* counter value at load time */
115 int64_t next_irq_time
;
119 typedef struct CUDAState
{
122 uint8_t b
; /* B-side data */
123 uint8_t a
; /* A-side data */
124 uint8_t dirb
; /* B-side direction (1=output) */
125 uint8_t dira
; /* A-side direction (1=output) */
126 uint8_t sr
; /* Shift register */
127 uint8_t acr
; /* Auxiliary control register */
128 uint8_t pcr
; /* Peripheral control register */
129 uint8_t ifr
; /* Interrupt flag register */
130 uint8_t ier
; /* Interrupt enable register */
131 uint8_t anh
; /* A-side data, no handshake */
135 uint32_t tick_offset
;
137 uint8_t last_b
; /* last value of B register */
138 uint8_t last_acr
; /* last value of B register */
146 uint8_t data_in
[128];
147 uint8_t data_out
[16];
148 QEMUTimer
*adb_poll_timer
;
151 static CUDAState cuda_state
;
154 static void cuda_update(CUDAState
*s
);
155 static void cuda_receive_packet_from_host(CUDAState
*s
,
156 const uint8_t *data
, int len
);
157 static void cuda_timer_update(CUDAState
*s
, CUDATimer
*ti
,
158 int64_t current_time
);
160 static void cuda_update_irq(CUDAState
*s
)
162 if (s
->ifr
& s
->ier
& (SR_INT
| T1_INT
)) {
163 qemu_irq_raise(s
->irq
);
165 qemu_irq_lower(s
->irq
);
169 static unsigned int get_counter(CUDATimer
*s
)
172 unsigned int counter
;
174 d
= muldiv64(qemu_get_clock_ns(vm_clock
) - s
->load_time
,
175 CUDA_TIMER_FREQ
, get_ticks_per_sec());
177 /* the timer goes down from latch to -1 (period of latch + 2) */
178 if (d
<= (s
->counter_value
+ 1)) {
179 counter
= (s
->counter_value
- d
) & 0xffff;
181 counter
= (d
- (s
->counter_value
+ 1)) % (s
->latch
+ 2);
182 counter
= (s
->latch
- counter
) & 0xffff;
185 counter
= (s
->counter_value
- d
) & 0xffff;
190 static void set_counter(CUDAState
*s
, CUDATimer
*ti
, unsigned int val
)
192 CUDA_DPRINTF("T%d.counter=%d\n", 1 + (ti
->timer
== NULL
), val
);
193 ti
->load_time
= qemu_get_clock_ns(vm_clock
);
194 ti
->counter_value
= val
;
195 cuda_timer_update(s
, ti
, ti
->load_time
);
198 static int64_t get_next_irq_time(CUDATimer
*s
, int64_t current_time
)
200 int64_t d
, next_time
;
201 unsigned int counter
;
203 /* current counter value */
204 d
= muldiv64(current_time
- s
->load_time
,
205 CUDA_TIMER_FREQ
, get_ticks_per_sec());
206 /* the timer goes down from latch to -1 (period of latch + 2) */
207 if (d
<= (s
->counter_value
+ 1)) {
208 counter
= (s
->counter_value
- d
) & 0xffff;
210 counter
= (d
- (s
->counter_value
+ 1)) % (s
->latch
+ 2);
211 counter
= (s
->latch
- counter
) & 0xffff;
214 /* Note: we consider the irq is raised on 0 */
215 if (counter
== 0xffff) {
216 next_time
= d
+ s
->latch
+ 1;
217 } else if (counter
== 0) {
218 next_time
= d
+ s
->latch
+ 2;
220 next_time
= d
+ counter
;
222 CUDA_DPRINTF("latch=%d counter=%" PRId64
" delta_next=%" PRId64
"\n",
223 s
->latch
, d
, next_time
- d
);
224 next_time
= muldiv64(next_time
, get_ticks_per_sec(), CUDA_TIMER_FREQ
) +
226 if (next_time
<= current_time
)
227 next_time
= current_time
+ 1;
231 static void cuda_timer_update(CUDAState
*s
, CUDATimer
*ti
,
232 int64_t current_time
)
236 if ((s
->acr
& T1MODE
) != T1MODE_CONT
) {
237 qemu_del_timer(ti
->timer
);
239 ti
->next_irq_time
= get_next_irq_time(ti
, current_time
);
240 qemu_mod_timer(ti
->timer
, ti
->next_irq_time
);
244 static void cuda_timer1(void *opaque
)
246 CUDAState
*s
= opaque
;
247 CUDATimer
*ti
= &s
->timers
[0];
249 cuda_timer_update(s
, ti
, ti
->next_irq_time
);
254 static uint32_t cuda_readb(void *opaque
, target_phys_addr_t addr
)
256 CUDAState
*s
= opaque
;
259 addr
= (addr
>> 9) & 0xf;
274 val
= get_counter(&s
->timers
[0]) & 0xff;
279 val
= get_counter(&s
->timers
[0]) >> 8;
283 val
= s
->timers
[0].latch
& 0xff;
286 /* XXX: check this */
287 val
= (s
->timers
[0].latch
>> 8) & 0xff;
290 val
= get_counter(&s
->timers
[1]) & 0xff;
294 val
= get_counter(&s
->timers
[1]) >> 8;
320 if (addr
!= 13 || val
!= 0) {
321 CUDA_DPRINTF("read: reg=0x%x val=%02x\n", (int)addr
, val
);
327 static void cuda_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
329 CUDAState
*s
= opaque
;
331 addr
= (addr
>> 9) & 0xf;
332 CUDA_DPRINTF("write: reg=0x%x val=%02x\n", (int)addr
, val
);
349 s
->timers
[0].latch
= (s
->timers
[0].latch
& 0xff00) | val
;
350 cuda_timer_update(s
, &s
->timers
[0], qemu_get_clock_ns(vm_clock
));
353 s
->timers
[0].latch
= (s
->timers
[0].latch
& 0xff) | (val
<< 8);
355 set_counter(s
, &s
->timers
[0], s
->timers
[0].latch
);
358 s
->timers
[0].latch
= (s
->timers
[0].latch
& 0xff00) | val
;
359 cuda_timer_update(s
, &s
->timers
[0], qemu_get_clock_ns(vm_clock
));
362 s
->timers
[0].latch
= (s
->timers
[0].latch
& 0xff) | (val
<< 8);
364 cuda_timer_update(s
, &s
->timers
[0], qemu_get_clock_ns(vm_clock
));
367 s
->timers
[1].latch
= val
;
368 set_counter(s
, &s
->timers
[1], val
);
371 set_counter(s
, &s
->timers
[1], (val
<< 8) | s
->timers
[1].latch
);
378 cuda_timer_update(s
, &s
->timers
[0], qemu_get_clock_ns(vm_clock
));
392 s
->ier
|= val
& 0x7f;
406 /* NOTE: TIP and TREQ are negated */
407 static void cuda_update(CUDAState
*s
)
409 int packet_received
, len
;
413 /* transfer requested from host */
415 if (s
->acr
& SR_OUT
) {
417 if ((s
->b
& (TACK
| TIP
)) != (s
->last_b
& (TACK
| TIP
))) {
418 if (s
->data_out_index
< sizeof(s
->data_out
)) {
419 CUDA_DPRINTF("send: %02x\n", s
->sr
);
420 s
->data_out
[s
->data_out_index
++] = s
->sr
;
426 if (s
->data_in_index
< s
->data_in_size
) {
428 if ((s
->b
& (TACK
| TIP
)) != (s
->last_b
& (TACK
| TIP
))) {
429 s
->sr
= s
->data_in
[s
->data_in_index
++];
430 CUDA_DPRINTF("recv: %02x\n", s
->sr
);
431 /* indicate end of transfer */
432 if (s
->data_in_index
>= s
->data_in_size
) {
433 s
->b
= (s
->b
| TREQ
);
441 /* no transfer requested: handle sync case */
442 if ((s
->last_b
& TIP
) && (s
->b
& TACK
) != (s
->last_b
& TACK
)) {
443 /* update TREQ state each time TACK change state */
445 s
->b
= (s
->b
| TREQ
);
447 s
->b
= (s
->b
& ~TREQ
);
451 if (!(s
->last_b
& TIP
)) {
452 /* handle end of host to cuda transfer */
453 packet_received
= (s
->data_out_index
> 0);
454 /* always an IRQ at the end of transfer */
458 /* signal if there is data to read */
459 if (s
->data_in_index
< s
->data_in_size
) {
460 s
->b
= (s
->b
& ~TREQ
);
465 s
->last_acr
= s
->acr
;
468 /* NOTE: cuda_receive_packet_from_host() can call cuda_update()
470 if (packet_received
) {
471 len
= s
->data_out_index
;
472 s
->data_out_index
= 0;
473 cuda_receive_packet_from_host(s
, s
->data_out
, len
);
477 static void cuda_send_packet_to_host(CUDAState
*s
,
478 const uint8_t *data
, int len
)
480 #ifdef DEBUG_CUDA_PACKET
483 printf("cuda_send_packet_to_host:\n");
484 for(i
= 0; i
< len
; i
++)
485 printf(" %02x", data
[i
]);
489 memcpy(s
->data_in
, data
, len
);
490 s
->data_in_size
= len
;
491 s
->data_in_index
= 0;
497 static void cuda_adb_poll(void *opaque
)
499 CUDAState
*s
= opaque
;
500 uint8_t obuf
[ADB_MAX_OUT_LEN
+ 2];
503 olen
= adb_poll(&adb_bus
, obuf
+ 2);
505 obuf
[0] = ADB_PACKET
;
506 obuf
[1] = 0x40; /* polled data */
507 cuda_send_packet_to_host(s
, obuf
, olen
+ 2);
509 qemu_mod_timer(s
->adb_poll_timer
,
510 qemu_get_clock_ns(vm_clock
) +
511 (get_ticks_per_sec() / CUDA_ADB_POLL_FREQ
));
514 static void cuda_receive_packet(CUDAState
*s
,
515 const uint8_t *data
, int len
)
523 autopoll
= (data
[1] != 0);
524 if (autopoll
!= s
->autopoll
) {
525 s
->autopoll
= autopoll
;
527 qemu_mod_timer(s
->adb_poll_timer
,
528 qemu_get_clock_ns(vm_clock
) +
529 (get_ticks_per_sec() / CUDA_ADB_POLL_FREQ
));
531 qemu_del_timer(s
->adb_poll_timer
);
534 obuf
[0] = CUDA_PACKET
;
536 cuda_send_packet_to_host(s
, obuf
, 2);
539 ti
= (((uint32_t)data
[1]) << 24) + (((uint32_t)data
[2]) << 16) + (((uint32_t)data
[3]) << 8) + data
[4];
540 s
->tick_offset
= ti
- (qemu_get_clock_ns(vm_clock
) / get_ticks_per_sec());
541 obuf
[0] = CUDA_PACKET
;
544 cuda_send_packet_to_host(s
, obuf
, 3);
547 ti
= s
->tick_offset
+ (qemu_get_clock_ns(vm_clock
) / get_ticks_per_sec());
548 obuf
[0] = CUDA_PACKET
;
555 cuda_send_packet_to_host(s
, obuf
, 7);
557 case CUDA_FILE_SERVER_FLAG
:
558 case CUDA_SET_DEVICE_LIST
:
559 case CUDA_SET_AUTO_RATE
:
560 case CUDA_SET_POWER_MESSAGES
:
561 obuf
[0] = CUDA_PACKET
;
563 cuda_send_packet_to_host(s
, obuf
, 2);
566 obuf
[0] = CUDA_PACKET
;
568 cuda_send_packet_to_host(s
, obuf
, 2);
569 qemu_system_shutdown_request();
571 case CUDA_RESET_SYSTEM
:
572 obuf
[0] = CUDA_PACKET
;
574 cuda_send_packet_to_host(s
, obuf
, 2);
575 qemu_system_reset_request();
582 static void cuda_receive_packet_from_host(CUDAState
*s
,
583 const uint8_t *data
, int len
)
585 #ifdef DEBUG_CUDA_PACKET
588 printf("cuda_receive_packet_from_host:\n");
589 for(i
= 0; i
< len
; i
++)
590 printf(" %02x", data
[i
]);
597 uint8_t obuf
[ADB_MAX_OUT_LEN
+ 2];
599 olen
= adb_request(&adb_bus
, obuf
+ 2, data
+ 1, len
- 1);
601 obuf
[0] = ADB_PACKET
;
605 obuf
[0] = ADB_PACKET
;
609 cuda_send_packet_to_host(s
, obuf
, olen
+ 2);
613 cuda_receive_packet(s
, data
+ 1, len
- 1);
618 static void cuda_writew (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
622 static void cuda_writel (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
626 static uint32_t cuda_readw (void *opaque
, target_phys_addr_t addr
)
631 static uint32_t cuda_readl (void *opaque
, target_phys_addr_t addr
)
636 static CPUWriteMemoryFunc
* const cuda_write
[] = {
642 static CPUReadMemoryFunc
* const cuda_read
[] = {
648 static bool cuda_timer_exist(void *opaque
, int version_id
)
650 CUDATimer
*s
= opaque
;
652 return s
->timer
!= NULL
;
655 static const VMStateDescription vmstate_cuda_timer
= {
656 .name
= "cuda_timer",
658 .minimum_version_id
= 0,
659 .minimum_version_id_old
= 0,
660 .fields
= (VMStateField
[]) {
661 VMSTATE_UINT16(latch
, CUDATimer
),
662 VMSTATE_UINT16(counter_value
, CUDATimer
),
663 VMSTATE_INT64(load_time
, CUDATimer
),
664 VMSTATE_INT64(next_irq_time
, CUDATimer
),
665 VMSTATE_TIMER_TEST(timer
, CUDATimer
, cuda_timer_exist
),
666 VMSTATE_END_OF_LIST()
670 static const VMStateDescription vmstate_cuda
= {
673 .minimum_version_id
= 1,
674 .minimum_version_id_old
= 1,
675 .fields
= (VMStateField
[]) {
676 VMSTATE_UINT8(a
, CUDAState
),
677 VMSTATE_UINT8(b
, CUDAState
),
678 VMSTATE_UINT8(dira
, CUDAState
),
679 VMSTATE_UINT8(dirb
, CUDAState
),
680 VMSTATE_UINT8(sr
, CUDAState
),
681 VMSTATE_UINT8(acr
, CUDAState
),
682 VMSTATE_UINT8(pcr
, CUDAState
),
683 VMSTATE_UINT8(ifr
, CUDAState
),
684 VMSTATE_UINT8(ier
, CUDAState
),
685 VMSTATE_UINT8(anh
, CUDAState
),
686 VMSTATE_INT32(data_in_size
, CUDAState
),
687 VMSTATE_INT32(data_in_index
, CUDAState
),
688 VMSTATE_INT32(data_out_index
, CUDAState
),
689 VMSTATE_UINT8(autopoll
, CUDAState
),
690 VMSTATE_BUFFER(data_in
, CUDAState
),
691 VMSTATE_BUFFER(data_out
, CUDAState
),
692 VMSTATE_UINT32(tick_offset
, CUDAState
),
693 VMSTATE_STRUCT_ARRAY(timers
, CUDAState
, 2, 1,
694 vmstate_cuda_timer
, CUDATimer
),
695 VMSTATE_END_OF_LIST()
699 static void cuda_reset(void *opaque
)
701 CUDAState
*s
= opaque
;
712 // s->ier = T1_INT | SR_INT;
715 s
->data_in_index
= 0;
716 s
->data_out_index
= 0;
719 s
->timers
[0].latch
= 0xffff;
720 set_counter(s
, &s
->timers
[0], 0xffff);
722 s
->timers
[1].latch
= 0;
723 set_counter(s
, &s
->timers
[1], 0xffff);
726 void cuda_init (MemoryRegion
**cuda_mem
, qemu_irq irq
)
729 CUDAState
*s
= &cuda_state
;
733 s
->timers
[0].index
= 0;
734 s
->timers
[0].timer
= qemu_new_timer_ns(vm_clock
, cuda_timer1
, s
);
736 s
->timers
[1].index
= 1;
738 qemu_get_timedate(&tm
, 0);
739 s
->tick_offset
= (uint32_t)mktimegm(&tm
) + RTC_OFFSET
;
741 s
->adb_poll_timer
= qemu_new_timer_ns(vm_clock
, cuda_adb_poll
, s
);
742 cpu_register_io_memory(cuda_read
, cuda_write
, s
,
743 DEVICE_NATIVE_ENDIAN
);
745 vmstate_register(NULL
, -1, &vmstate_cuda
, s
);
746 qemu_register_reset(cuda_reset
, s
);