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
28 #include "qemu-timer.h"
31 /* XXX: implement all timer modes */
36 /* debug CUDA packets */
37 //#define DEBUG_CUDA_PACKET
40 #define CUDA_DPRINTF(fmt, ...) \
41 do { printf("CUDA: " fmt , ## __VA_ARGS__); } while (0)
43 #define CUDA_DPRINTF(fmt, ...)
46 /* Bits in B data register: all active low */
47 #define TREQ 0x08 /* Transfer request (input) */
48 #define TACK 0x10 /* Transfer acknowledge (output) */
49 #define TIP 0x20 /* Transfer in progress (output) */
52 #define SR_CTRL 0x1c /* Shift register control bits */
53 #define SR_EXT 0x0c /* Shift on external clock */
54 #define SR_OUT 0x10 /* Shift out if 1 */
56 /* Bits in IFR and IER */
57 #define IER_SET 0x80 /* set bits in IER */
58 #define IER_CLR 0 /* clear bits in IER */
59 #define SR_INT 0x04 /* Shift register full/empty */
60 #define T1_INT 0x40 /* Timer 1 interrupt */
61 #define T2_INT 0x20 /* Timer 2 interrupt */
64 #define T1MODE 0xc0 /* Timer 1 mode */
65 #define T1MODE_CONT 0x40 /* continuous interrupts */
67 /* commands (1st byte) */
70 #define ERROR_PACKET 2
71 #define TIMER_PACKET 3
72 #define POWER_PACKET 4
73 #define MACIIC_PACKET 5
77 /* CUDA commands (2nd byte) */
78 #define CUDA_WARM_START 0x0
79 #define CUDA_AUTOPOLL 0x1
80 #define CUDA_GET_6805_ADDR 0x2
81 #define CUDA_GET_TIME 0x3
82 #define CUDA_GET_PRAM 0x7
83 #define CUDA_SET_6805_ADDR 0x8
84 #define CUDA_SET_TIME 0x9
85 #define CUDA_POWERDOWN 0xa
86 #define CUDA_POWERUP_TIME 0xb
87 #define CUDA_SET_PRAM 0xc
88 #define CUDA_MS_RESET 0xd
89 #define CUDA_SEND_DFAC 0xe
90 #define CUDA_BATTERY_SWAP_SENSE 0x10
91 #define CUDA_RESET_SYSTEM 0x11
92 #define CUDA_SET_IPL 0x12
93 #define CUDA_FILE_SERVER_FLAG 0x13
94 #define CUDA_SET_AUTO_RATE 0x14
95 #define CUDA_GET_AUTO_RATE 0x16
96 #define CUDA_SET_DEVICE_LIST 0x19
97 #define CUDA_GET_DEVICE_LIST 0x1a
98 #define CUDA_SET_ONE_SECOND_MODE 0x1b
99 #define CUDA_SET_POWER_MESSAGES 0x21
100 #define CUDA_GET_SET_IIC 0x22
101 #define CUDA_WAKEUP 0x23
102 #define CUDA_TIMER_TICKLE 0x24
103 #define CUDA_COMBINED_FORMAT_IIC 0x25
105 #define CUDA_TIMER_FREQ (4700000 / 6)
106 #define CUDA_ADB_POLL_FREQ 50
108 /* CUDA returns time_t's offset from Jan 1, 1904, not 1970 */
109 #define RTC_OFFSET 2082844800
111 typedef struct CUDATimer
{
114 uint16_t counter_value
; /* counter value at load time */
116 int64_t next_irq_time
;
120 typedef struct CUDAState
{
123 uint8_t b
; /* B-side data */
124 uint8_t a
; /* A-side data */
125 uint8_t dirb
; /* B-side direction (1=output) */
126 uint8_t dira
; /* A-side direction (1=output) */
127 uint8_t sr
; /* Shift register */
128 uint8_t acr
; /* Auxiliary control register */
129 uint8_t pcr
; /* Peripheral control register */
130 uint8_t ifr
; /* Interrupt flag register */
131 uint8_t ier
; /* Interrupt enable register */
132 uint8_t anh
; /* A-side data, no handshake */
136 uint32_t tick_offset
;
138 uint8_t last_b
; /* last value of B register */
139 uint8_t last_acr
; /* last value of B register */
147 uint8_t data_in
[128];
148 uint8_t data_out
[16];
149 QEMUTimer
*adb_poll_timer
;
152 static CUDAState cuda_state
;
155 static void cuda_update(CUDAState
*s
);
156 static void cuda_receive_packet_from_host(CUDAState
*s
,
157 const uint8_t *data
, int len
);
158 static void cuda_timer_update(CUDAState
*s
, CUDATimer
*ti
,
159 int64_t current_time
);
161 static void cuda_update_irq(CUDAState
*s
)
163 if (s
->ifr
& s
->ier
& (SR_INT
| T1_INT
)) {
164 qemu_irq_raise(s
->irq
);
166 qemu_irq_lower(s
->irq
);
170 static unsigned int get_counter(CUDATimer
*s
)
173 unsigned int counter
;
175 d
= muldiv64(qemu_get_clock_ns(vm_clock
) - s
->load_time
,
176 CUDA_TIMER_FREQ
, get_ticks_per_sec());
178 /* the timer goes down from latch to -1 (period of latch + 2) */
179 if (d
<= (s
->counter_value
+ 1)) {
180 counter
= (s
->counter_value
- d
) & 0xffff;
182 counter
= (d
- (s
->counter_value
+ 1)) % (s
->latch
+ 2);
183 counter
= (s
->latch
- counter
) & 0xffff;
186 counter
= (s
->counter_value
- d
) & 0xffff;
191 static void set_counter(CUDAState
*s
, CUDATimer
*ti
, unsigned int val
)
193 CUDA_DPRINTF("T%d.counter=%d\n", 1 + (ti
->timer
== NULL
), val
);
194 ti
->load_time
= qemu_get_clock_ns(vm_clock
);
195 ti
->counter_value
= val
;
196 cuda_timer_update(s
, ti
, ti
->load_time
);
199 static int64_t get_next_irq_time(CUDATimer
*s
, int64_t current_time
)
201 int64_t d
, next_time
;
202 unsigned int counter
;
204 /* current counter value */
205 d
= muldiv64(current_time
- s
->load_time
,
206 CUDA_TIMER_FREQ
, get_ticks_per_sec());
207 /* the timer goes down from latch to -1 (period of latch + 2) */
208 if (d
<= (s
->counter_value
+ 1)) {
209 counter
= (s
->counter_value
- d
) & 0xffff;
211 counter
= (d
- (s
->counter_value
+ 1)) % (s
->latch
+ 2);
212 counter
= (s
->latch
- counter
) & 0xffff;
215 /* Note: we consider the irq is raised on 0 */
216 if (counter
== 0xffff) {
217 next_time
= d
+ s
->latch
+ 1;
218 } else if (counter
== 0) {
219 next_time
= d
+ s
->latch
+ 2;
221 next_time
= d
+ counter
;
223 CUDA_DPRINTF("latch=%d counter=%" PRId64
" delta_next=%" PRId64
"\n",
224 s
->latch
, d
, next_time
- d
);
225 next_time
= muldiv64(next_time
, get_ticks_per_sec(), CUDA_TIMER_FREQ
) +
227 if (next_time
<= current_time
)
228 next_time
= current_time
+ 1;
232 static void cuda_timer_update(CUDAState
*s
, CUDATimer
*ti
,
233 int64_t current_time
)
237 if ((s
->acr
& T1MODE
) != T1MODE_CONT
) {
238 qemu_del_timer(ti
->timer
);
240 ti
->next_irq_time
= get_next_irq_time(ti
, current_time
);
241 qemu_mod_timer(ti
->timer
, ti
->next_irq_time
);
245 static void cuda_timer1(void *opaque
)
247 CUDAState
*s
= opaque
;
248 CUDATimer
*ti
= &s
->timers
[0];
250 cuda_timer_update(s
, ti
, ti
->next_irq_time
);
255 static uint32_t cuda_readb(void *opaque
, target_phys_addr_t addr
)
257 CUDAState
*s
= opaque
;
260 addr
= (addr
>> 9) & 0xf;
275 val
= get_counter(&s
->timers
[0]) & 0xff;
280 val
= get_counter(&s
->timers
[0]) >> 8;
284 val
= s
->timers
[0].latch
& 0xff;
287 /* XXX: check this */
288 val
= (s
->timers
[0].latch
>> 8) & 0xff;
291 val
= get_counter(&s
->timers
[1]) & 0xff;
295 val
= get_counter(&s
->timers
[1]) >> 8;
321 if (addr
!= 13 || val
!= 0) {
322 CUDA_DPRINTF("read: reg=0x%x val=%02x\n", (int)addr
, val
);
328 static void cuda_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
330 CUDAState
*s
= opaque
;
332 addr
= (addr
>> 9) & 0xf;
333 CUDA_DPRINTF("write: reg=0x%x val=%02x\n", (int)addr
, val
);
350 s
->timers
[0].latch
= (s
->timers
[0].latch
& 0xff00) | val
;
351 cuda_timer_update(s
, &s
->timers
[0], qemu_get_clock_ns(vm_clock
));
354 s
->timers
[0].latch
= (s
->timers
[0].latch
& 0xff) | (val
<< 8);
356 set_counter(s
, &s
->timers
[0], s
->timers
[0].latch
);
359 s
->timers
[0].latch
= (s
->timers
[0].latch
& 0xff00) | val
;
360 cuda_timer_update(s
, &s
->timers
[0], qemu_get_clock_ns(vm_clock
));
363 s
->timers
[0].latch
= (s
->timers
[0].latch
& 0xff) | (val
<< 8);
365 cuda_timer_update(s
, &s
->timers
[0], qemu_get_clock_ns(vm_clock
));
368 s
->timers
[1].latch
= val
;
369 set_counter(s
, &s
->timers
[1], val
);
372 set_counter(s
, &s
->timers
[1], (val
<< 8) | s
->timers
[1].latch
);
379 cuda_timer_update(s
, &s
->timers
[0], qemu_get_clock_ns(vm_clock
));
393 s
->ier
|= val
& 0x7f;
407 /* NOTE: TIP and TREQ are negated */
408 static void cuda_update(CUDAState
*s
)
410 int packet_received
, len
;
414 /* transfer requested from host */
416 if (s
->acr
& SR_OUT
) {
418 if ((s
->b
& (TACK
| TIP
)) != (s
->last_b
& (TACK
| TIP
))) {
419 if (s
->data_out_index
< sizeof(s
->data_out
)) {
420 CUDA_DPRINTF("send: %02x\n", s
->sr
);
421 s
->data_out
[s
->data_out_index
++] = s
->sr
;
427 if (s
->data_in_index
< s
->data_in_size
) {
429 if ((s
->b
& (TACK
| TIP
)) != (s
->last_b
& (TACK
| TIP
))) {
430 s
->sr
= s
->data_in
[s
->data_in_index
++];
431 CUDA_DPRINTF("recv: %02x\n", s
->sr
);
432 /* indicate end of transfer */
433 if (s
->data_in_index
>= s
->data_in_size
) {
434 s
->b
= (s
->b
| TREQ
);
442 /* no transfer requested: handle sync case */
443 if ((s
->last_b
& TIP
) && (s
->b
& TACK
) != (s
->last_b
& TACK
)) {
444 /* update TREQ state each time TACK change state */
446 s
->b
= (s
->b
| TREQ
);
448 s
->b
= (s
->b
& ~TREQ
);
452 if (!(s
->last_b
& TIP
)) {
453 /* handle end of host to cuda transfer */
454 packet_received
= (s
->data_out_index
> 0);
455 /* always an IRQ at the end of transfer */
459 /* signal if there is data to read */
460 if (s
->data_in_index
< s
->data_in_size
) {
461 s
->b
= (s
->b
& ~TREQ
);
466 s
->last_acr
= s
->acr
;
469 /* NOTE: cuda_receive_packet_from_host() can call cuda_update()
471 if (packet_received
) {
472 len
= s
->data_out_index
;
473 s
->data_out_index
= 0;
474 cuda_receive_packet_from_host(s
, s
->data_out
, len
);
478 static void cuda_send_packet_to_host(CUDAState
*s
,
479 const uint8_t *data
, int len
)
481 #ifdef DEBUG_CUDA_PACKET
484 printf("cuda_send_packet_to_host:\n");
485 for(i
= 0; i
< len
; i
++)
486 printf(" %02x", data
[i
]);
490 memcpy(s
->data_in
, data
, len
);
491 s
->data_in_size
= len
;
492 s
->data_in_index
= 0;
498 static void cuda_adb_poll(void *opaque
)
500 CUDAState
*s
= opaque
;
501 uint8_t obuf
[ADB_MAX_OUT_LEN
+ 2];
504 olen
= adb_poll(&adb_bus
, obuf
+ 2);
506 obuf
[0] = ADB_PACKET
;
507 obuf
[1] = 0x40; /* polled data */
508 cuda_send_packet_to_host(s
, obuf
, olen
+ 2);
510 qemu_mod_timer(s
->adb_poll_timer
,
511 qemu_get_clock_ns(vm_clock
) +
512 (get_ticks_per_sec() / CUDA_ADB_POLL_FREQ
));
515 static void cuda_receive_packet(CUDAState
*s
,
516 const uint8_t *data
, int len
)
524 autopoll
= (data
[1] != 0);
525 if (autopoll
!= s
->autopoll
) {
526 s
->autopoll
= autopoll
;
528 qemu_mod_timer(s
->adb_poll_timer
,
529 qemu_get_clock_ns(vm_clock
) +
530 (get_ticks_per_sec() / CUDA_ADB_POLL_FREQ
));
532 qemu_del_timer(s
->adb_poll_timer
);
535 obuf
[0] = CUDA_PACKET
;
537 cuda_send_packet_to_host(s
, obuf
, 2);
540 ti
= (((uint32_t)data
[1]) << 24) + (((uint32_t)data
[2]) << 16) + (((uint32_t)data
[3]) << 8) + data
[4];
541 s
->tick_offset
= ti
- (qemu_get_clock_ns(vm_clock
) / get_ticks_per_sec());
542 obuf
[0] = CUDA_PACKET
;
545 cuda_send_packet_to_host(s
, obuf
, 3);
548 ti
= s
->tick_offset
+ (qemu_get_clock_ns(vm_clock
) / get_ticks_per_sec());
549 obuf
[0] = CUDA_PACKET
;
556 cuda_send_packet_to_host(s
, obuf
, 7);
558 case CUDA_FILE_SERVER_FLAG
:
559 case CUDA_SET_DEVICE_LIST
:
560 case CUDA_SET_AUTO_RATE
:
561 case CUDA_SET_POWER_MESSAGES
:
562 obuf
[0] = CUDA_PACKET
;
564 cuda_send_packet_to_host(s
, obuf
, 2);
567 obuf
[0] = CUDA_PACKET
;
569 cuda_send_packet_to_host(s
, obuf
, 2);
570 qemu_system_shutdown_request();
572 case CUDA_RESET_SYSTEM
:
573 obuf
[0] = CUDA_PACKET
;
575 cuda_send_packet_to_host(s
, obuf
, 2);
576 qemu_system_reset_request();
583 static void cuda_receive_packet_from_host(CUDAState
*s
,
584 const uint8_t *data
, int len
)
586 #ifdef DEBUG_CUDA_PACKET
589 printf("cuda_receive_packet_from_host:\n");
590 for(i
= 0; i
< len
; i
++)
591 printf(" %02x", data
[i
]);
598 uint8_t obuf
[ADB_MAX_OUT_LEN
+ 2];
600 olen
= adb_request(&adb_bus
, obuf
+ 2, data
+ 1, len
- 1);
602 obuf
[0] = ADB_PACKET
;
606 obuf
[0] = ADB_PACKET
;
610 cuda_send_packet_to_host(s
, obuf
, olen
+ 2);
614 cuda_receive_packet(s
, data
+ 1, len
- 1);
619 static void cuda_writew (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
623 static void cuda_writel (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
627 static uint32_t cuda_readw (void *opaque
, target_phys_addr_t addr
)
632 static uint32_t cuda_readl (void *opaque
, target_phys_addr_t addr
)
637 static MemoryRegionOps cuda_ops
= {
650 .endianness
= DEVICE_NATIVE_ENDIAN
,
653 static bool cuda_timer_exist(void *opaque
, int version_id
)
655 CUDATimer
*s
= opaque
;
657 return s
->timer
!= NULL
;
660 static const VMStateDescription vmstate_cuda_timer
= {
661 .name
= "cuda_timer",
663 .minimum_version_id
= 0,
664 .minimum_version_id_old
= 0,
665 .fields
= (VMStateField
[]) {
666 VMSTATE_UINT16(latch
, CUDATimer
),
667 VMSTATE_UINT16(counter_value
, CUDATimer
),
668 VMSTATE_INT64(load_time
, CUDATimer
),
669 VMSTATE_INT64(next_irq_time
, CUDATimer
),
670 VMSTATE_TIMER_TEST(timer
, CUDATimer
, cuda_timer_exist
),
671 VMSTATE_END_OF_LIST()
675 static const VMStateDescription vmstate_cuda
= {
678 .minimum_version_id
= 1,
679 .minimum_version_id_old
= 1,
680 .fields
= (VMStateField
[]) {
681 VMSTATE_UINT8(a
, CUDAState
),
682 VMSTATE_UINT8(b
, CUDAState
),
683 VMSTATE_UINT8(dira
, CUDAState
),
684 VMSTATE_UINT8(dirb
, CUDAState
),
685 VMSTATE_UINT8(sr
, CUDAState
),
686 VMSTATE_UINT8(acr
, CUDAState
),
687 VMSTATE_UINT8(pcr
, CUDAState
),
688 VMSTATE_UINT8(ifr
, CUDAState
),
689 VMSTATE_UINT8(ier
, CUDAState
),
690 VMSTATE_UINT8(anh
, CUDAState
),
691 VMSTATE_INT32(data_in_size
, CUDAState
),
692 VMSTATE_INT32(data_in_index
, CUDAState
),
693 VMSTATE_INT32(data_out_index
, CUDAState
),
694 VMSTATE_UINT8(autopoll
, CUDAState
),
695 VMSTATE_BUFFER(data_in
, CUDAState
),
696 VMSTATE_BUFFER(data_out
, CUDAState
),
697 VMSTATE_UINT32(tick_offset
, CUDAState
),
698 VMSTATE_STRUCT_ARRAY(timers
, CUDAState
, 2, 1,
699 vmstate_cuda_timer
, CUDATimer
),
700 VMSTATE_END_OF_LIST()
704 static void cuda_reset(void *opaque
)
706 CUDAState
*s
= opaque
;
717 // s->ier = T1_INT | SR_INT;
720 s
->data_in_index
= 0;
721 s
->data_out_index
= 0;
724 s
->timers
[0].latch
= 0xffff;
725 set_counter(s
, &s
->timers
[0], 0xffff);
727 s
->timers
[1].latch
= 0;
728 set_counter(s
, &s
->timers
[1], 0xffff);
731 void cuda_init (MemoryRegion
**cuda_mem
, qemu_irq irq
)
734 CUDAState
*s
= &cuda_state
;
738 s
->timers
[0].index
= 0;
739 s
->timers
[0].timer
= qemu_new_timer_ns(vm_clock
, cuda_timer1
, s
);
741 s
->timers
[1].index
= 1;
743 qemu_get_timedate(&tm
, 0);
744 s
->tick_offset
= (uint32_t)mktimegm(&tm
) + RTC_OFFSET
;
746 s
->adb_poll_timer
= qemu_new_timer_ns(vm_clock
, cuda_adb_poll
, s
);
747 memory_region_init_io(&s
->mem
, &cuda_ops
, s
, "cuda", 0x2000);
750 vmstate_register(NULL
, -1, &vmstate_cuda
, s
);
751 qemu_register_reset(cuda_reset
, s
);