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, args...) \
40 do { printf("CUDA: " fmt , ##args); } while (0)
42 #define CUDA_DPRINTF(fmt, args...)
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 {
121 uint8_t b; /* B-side data */
122 uint8_t a; /* A-side data */
123 uint8_t dirb; /* B-side direction (1=output) */
124 uint8_t dira; /* A-side direction (1=output) */
125 uint8_t sr; /* Shift register */
126 uint8_t acr; /* Auxiliary control register */
127 uint8_t pcr; /* Peripheral control register */
128 uint8_t ifr; /* Interrupt flag register */
129 uint8_t ier; /* Interrupt enable register */
130 uint8_t anh; /* A-side data, no handshake */
134 uint8_t last_b; /* last value of B register */
135 uint8_t last_acr; /* last value of B register */
143 uint8_t data_in[128];
144 uint8_t data_out[16];
145 QEMUTimer *adb_poll_timer;
148 static CUDAState cuda_state;
151 static void cuda_update(CUDAState *s);
152 static void cuda_receive_packet_from_host(CUDAState *s,
153 const uint8_t *data, int len);
154 static void cuda_timer_update(CUDAState *s, CUDATimer *ti,
155 int64_t current_time);
157 static void cuda_update_irq(CUDAState *s)
159 if (s->ifr & s->ier & (SR_INT | T1_INT)) {
160 qemu_irq_raise(s->irq);
162 qemu_irq_lower(s->irq);
166 static unsigned int get_counter(CUDATimer *s)
169 unsigned int counter;
171 d = muldiv64(qemu_get_clock(vm_clock) - s->load_time,
172 CUDA_TIMER_FREQ, ticks_per_sec);
174 /* the timer goes down from latch to -1 (period of latch + 2) */
175 if (d <= (s->counter_value + 1)) {
176 counter = (s->counter_value - d) & 0xffff;
178 counter = (d - (s->counter_value + 1)) % (s->latch + 2);
179 counter = (s->latch - counter) & 0xffff;
182 counter = (s->counter_value - d) & 0xffff;
187 static void set_counter(CUDAState *s, CUDATimer *ti, unsigned int val)
189 CUDA_DPRINTF("T%d.counter=%d\n", 1 + (ti->timer == NULL), val);
190 ti->load_time = qemu_get_clock(vm_clock);
191 ti->counter_value = val;
192 cuda_timer_update(s, ti, ti->load_time);
195 static int64_t get_next_irq_time(CUDATimer *s, int64_t current_time)
197 int64_t d, next_time;
198 unsigned int counter;
200 /* current counter value */
201 d = muldiv64(current_time - s->load_time,
202 CUDA_TIMER_FREQ, ticks_per_sec);
203 /* the timer goes down from latch to -1 (period of latch + 2) */
204 if (d <= (s->counter_value + 1)) {
205 counter = (s->counter_value - d) & 0xffff;
207 counter = (d - (s->counter_value + 1)) % (s->latch + 2);
208 counter = (s->latch - counter) & 0xffff;
211 /* Note: we consider the irq is raised on 0 */
212 if (counter == 0xffff) {
213 next_time = d + s->latch + 1;
214 } else if (counter == 0) {
215 next_time = d + s->latch + 2;
217 next_time = d + counter;
219 CUDA_DPRINTF("latch=%d counter=%" PRId64 " delta_next=%" PRId64 "\n",
220 s->latch, d, next_time - d);
221 next_time = muldiv64(next_time, ticks_per_sec, CUDA_TIMER_FREQ) +
223 if (next_time <= current_time)
224 next_time = current_time + 1;
228 static void cuda_timer_update(CUDAState *s, CUDATimer *ti,
229 int64_t current_time)
233 if ((s->acr & T1MODE) != T1MODE_CONT) {
234 qemu_del_timer(ti->timer);
236 ti->next_irq_time = get_next_irq_time(ti, current_time);
237 qemu_mod_timer(ti->timer, ti->next_irq_time);
241 static void cuda_timer1(void *opaque)
243 CUDAState *s = opaque;
244 CUDATimer *ti = &s->timers[0];
246 cuda_timer_update(s, ti, ti->next_irq_time);
251 static uint32_t cuda_readb(void *opaque, target_phys_addr_t addr)
253 CUDAState *s = opaque;
256 addr = (addr >> 9) & 0xf;
271 val = get_counter(&s->timers[0]) & 0xff;
276 val = get_counter(&s->timers[0]) >> 8;
280 val = s->timers[0].latch & 0xff;
283 /* XXX: check this */
284 val = (s->timers[0].latch >> 8) & 0xff;
287 val = get_counter(&s->timers[1]) & 0xff;
291 val = get_counter(&s->timers[1]) >> 8;
317 if (addr != 13 || val != 0)
318 CUDA_DPRINTF("read: reg=0x%x val=%02x\n", (int)addr, val);
322 static void cuda_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
324 CUDAState *s = opaque;
326 addr = (addr >> 9) & 0xf;
327 CUDA_DPRINTF("write: reg=0x%x val=%02x\n", (int)addr, val);
344 s->timers[0].latch = (s->timers[0].latch & 0xff00) | val;
345 cuda_timer_update(s, &s->timers[0], qemu_get_clock(vm_clock));
348 s->timers[0].latch = (s->timers[0].latch & 0xff) | (val << 8);
350 set_counter(s, &s->timers[0], s->timers[0].latch);
353 s->timers[0].latch = (s->timers[0].latch & 0xff00) | val;
354 cuda_timer_update(s, &s->timers[0], qemu_get_clock(vm_clock));
357 s->timers[0].latch = (s->timers[0].latch & 0xff) | (val << 8);
359 cuda_timer_update(s, &s->timers[0], qemu_get_clock(vm_clock));
362 s->timers[1].latch = val;
363 set_counter(s, &s->timers[1], val);
366 set_counter(s, &s->timers[1], (val << 8) | s->timers[1].latch);
373 cuda_timer_update(s, &s->timers[0], qemu_get_clock(vm_clock));
387 s->ier |= val & 0x7f;
401 /* NOTE: TIP and TREQ are negated */
402 static void cuda_update(CUDAState *s)
404 int packet_received, len;
408 /* transfer requested from host */
410 if (s->acr & SR_OUT) {
412 if ((s->b & (TACK | TIP)) != (s->last_b & (TACK | TIP))) {
413 if (s->data_out_index < sizeof(s->data_out)) {
414 CUDA_DPRINTF("send: %02x\n", s->sr);
415 s->data_out[s->data_out_index++] = s->sr;
421 if (s->data_in_index < s->data_in_size) {
423 if ((s->b & (TACK | TIP)) != (s->last_b & (TACK | TIP))) {
424 s->sr = s->data_in[s->data_in_index++];
425 CUDA_DPRINTF("recv: %02x\n", s->sr);
426 /* indicate end of transfer */
427 if (s->data_in_index >= s->data_in_size) {
428 s->b = (s->b | TREQ);
436 /* no transfer requested: handle sync case */
437 if ((s->last_b & TIP) && (s->b & TACK) != (s->last_b & TACK)) {
438 /* update TREQ state each time TACK change state */
440 s->b = (s->b | TREQ);
442 s->b = (s->b & ~TREQ);
446 if (!(s->last_b & TIP)) {
447 /* handle end of host to cuda transfer */
448 packet_received = (s->data_out_index > 0);
449 /* always an IRQ at the end of transfer */
453 /* signal if there is data to read */
454 if (s->data_in_index < s->data_in_size) {
455 s->b = (s->b & ~TREQ);
460 s->last_acr = s->acr;
463 /* NOTE: cuda_receive_packet_from_host() can call cuda_update()
465 if (packet_received) {
466 len = s->data_out_index;
467 s->data_out_index = 0;
468 cuda_receive_packet_from_host(s, s->data_out, len);
472 static void cuda_send_packet_to_host(CUDAState *s,
473 const uint8_t *data, int len)
475 #ifdef DEBUG_CUDA_PACKET
478 printf("cuda_send_packet_to_host:\n");
479 for(i = 0; i < len; i++)
480 printf(" %02x", data[i]);
484 memcpy(s->data_in, data, len);
485 s->data_in_size = len;
486 s->data_in_index = 0;
492 static void cuda_adb_poll(void *opaque)
494 CUDAState *s = opaque;
495 uint8_t obuf[ADB_MAX_OUT_LEN + 2];
498 olen = adb_poll(&adb_bus, obuf + 2);
500 obuf[0] = ADB_PACKET;
501 obuf[1] = 0x40; /* polled data */
502 cuda_send_packet_to_host(s, obuf, olen + 2);
504 qemu_mod_timer(s->adb_poll_timer,
505 qemu_get_clock(vm_clock) +
506 (ticks_per_sec / CUDA_ADB_POLL_FREQ));
509 static void cuda_receive_packet(CUDAState *s,
510 const uint8_t *data, int len)
517 autopoll = (data[1] != 0);
518 if (autopoll != s->autopoll) {
519 s->autopoll = autopoll;
521 qemu_mod_timer(s->adb_poll_timer,
522 qemu_get_clock(vm_clock) +
523 (ticks_per_sec / CUDA_ADB_POLL_FREQ));
525 qemu_del_timer(s->adb_poll_timer);
528 obuf[0] = CUDA_PACKET;
530 cuda_send_packet_to_host(s, obuf, 2);
534 /* XXX: add time support ? */
535 ti = time(NULL) + RTC_OFFSET;
536 obuf[0] = CUDA_PACKET;
543 cuda_send_packet_to_host(s, obuf, 7);
545 case CUDA_FILE_SERVER_FLAG:
546 case CUDA_SET_DEVICE_LIST:
547 case CUDA_SET_AUTO_RATE:
548 case CUDA_SET_POWER_MESSAGES:
549 obuf[0] = CUDA_PACKET;
551 cuda_send_packet_to_host(s, obuf, 2);
554 obuf[0] = CUDA_PACKET;
556 cuda_send_packet_to_host(s, obuf, 2);
557 qemu_system_shutdown_request();
559 case CUDA_RESET_SYSTEM:
560 obuf[0] = CUDA_PACKET;
562 cuda_send_packet_to_host(s, obuf, 2);
563 qemu_system_reset_request();
570 static void cuda_receive_packet_from_host(CUDAState *s,
571 const uint8_t *data, int len)
573 #ifdef DEBUG_CUDA_PACKET
576 printf("cuda_receive_packet_from_host:\n");
577 for(i = 0; i < len; i++)
578 printf(" %02x", data[i]);
585 uint8_t obuf[ADB_MAX_OUT_LEN + 2];
587 olen = adb_request(&adb_bus, obuf + 2, data + 1, len - 1);
589 obuf[0] = ADB_PACKET;
593 obuf[0] = ADB_PACKET;
597 cuda_send_packet_to_host(s, obuf, olen + 2);
601 cuda_receive_packet(s, data + 1, len - 1);
606 static void cuda_writew (void *opaque, target_phys_addr_t addr, uint32_t value)
610 static void cuda_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
614 static uint32_t cuda_readw (void *opaque, target_phys_addr_t addr)
619 static uint32_t cuda_readl (void *opaque, target_phys_addr_t addr)
624 static CPUWriteMemoryFunc *cuda_write[] = {
630 static CPUReadMemoryFunc *cuda_read[] = {
636 static void cuda_save_timer(QEMUFile *f, CUDATimer *s)
638 qemu_put_be16s(f, &s->latch);
639 qemu_put_be16s(f, &s->counter_value);
640 qemu_put_sbe64s(f, &s->load_time);
641 qemu_put_sbe64s(f, &s->next_irq_time);
643 qemu_put_timer(f, s->timer);
646 static void cuda_save(QEMUFile *f, void *opaque)
648 CUDAState *s = (CUDAState *)opaque;
650 qemu_put_ubyte(f, s->b);
651 qemu_put_ubyte(f, s->a);
652 qemu_put_ubyte(f, s->dirb);
653 qemu_put_ubyte(f, s->dira);
654 qemu_put_ubyte(f, s->sr);
655 qemu_put_ubyte(f, s->acr);
656 qemu_put_ubyte(f, s->pcr);
657 qemu_put_ubyte(f, s->ifr);
658 qemu_put_ubyte(f, s->ier);
659 qemu_put_ubyte(f, s->anh);
660 qemu_put_sbe32s(f, &s->data_in_size);
661 qemu_put_sbe32s(f, &s->data_in_index);
662 qemu_put_sbe32s(f, &s->data_out_index);
663 qemu_put_ubyte(f, s->autopoll);
664 qemu_put_buffer(f, s->data_in, sizeof(s->data_in));
665 qemu_put_buffer(f, s->data_out, sizeof(s->data_out));
666 cuda_save_timer(f, &s->timers[0]);
667 cuda_save_timer(f, &s->timers[1]);
670 static void cuda_load_timer(QEMUFile *f, CUDATimer *s)
672 qemu_get_be16s(f, &s->latch);
673 qemu_get_be16s(f, &s->counter_value);
674 qemu_get_sbe64s(f, &s->load_time);
675 qemu_get_sbe64s(f, &s->next_irq_time);
677 qemu_get_timer(f, s->timer);
680 static int cuda_load(QEMUFile *f, void *opaque, int version_id)
682 CUDAState *s = (CUDAState *)opaque;
687 s->b = qemu_get_ubyte(f);
688 s->a = qemu_get_ubyte(f);
689 s->dirb = qemu_get_ubyte(f);
690 s->dira = qemu_get_ubyte(f);
691 s->sr = qemu_get_ubyte(f);
692 s->acr = qemu_get_ubyte(f);
693 s->pcr = qemu_get_ubyte(f);
694 s->ifr = qemu_get_ubyte(f);
695 s->ier = qemu_get_ubyte(f);
696 s->anh = qemu_get_ubyte(f);
697 qemu_get_sbe32s(f, &s->data_in_size);
698 qemu_get_sbe32s(f, &s->data_in_index);
699 qemu_get_sbe32s(f, &s->data_out_index);
700 s->autopoll = qemu_get_ubyte(f);
701 qemu_get_buffer(f, s->data_in, sizeof(s->data_in));
702 qemu_get_buffer(f, s->data_out, sizeof(s->data_out));
703 cuda_load_timer(f, &s->timers[0]);
704 cuda_load_timer(f, &s->timers[1]);
709 static void cuda_reset(void *opaque)
711 CUDAState *s = opaque;
722 // s->ier = T1_INT | SR_INT;
725 s->data_in_index = 0;
726 s->data_out_index = 0;
729 s->timers[0].latch = 0xffff;
730 set_counter(s, &s->timers[0], 0xffff);
732 s->timers[1].latch = 0;
733 set_counter(s, &s->timers[1], 0xffff);
736 void cuda_init (int *cuda_mem_index, qemu_irq irq)
738 CUDAState *s = &cuda_state;
742 s->timers[0].index = 0;
743 s->timers[0].timer = qemu_new_timer(vm_clock, cuda_timer1, s);
745 s->timers[1].index = 1;
747 s->adb_poll_timer = qemu_new_timer(vm_clock, cuda_adb_poll, s);
748 *cuda_mem_index = cpu_register_io_memory(0, cuda_read, cuda_write, s);
749 register_savevm("cuda", -1, 1, cuda_save, cuda_load, s);
750 qemu_register_reset(cuda_reset, s);