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"
29 #include "sysemu/sysemu.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 static void cuda_update(CUDAState *s);
112 static void cuda_receive_packet_from_host(CUDAState *s,
113 const uint8_t *data, int len);
114 static void cuda_timer_update(CUDAState *s, CUDATimer *ti,
115 int64_t current_time);
117 static void cuda_update_irq(CUDAState *s)
119 if (s->ifr & s->ier & (SR_INT | T1_INT)) {
120 qemu_irq_raise(s->irq);
122 qemu_irq_lower(s->irq);
126 static unsigned int get_counter(CUDATimer *s)
129 unsigned int counter;
131 d = muldiv64(qemu_get_clock_ns(vm_clock) - s->load_time,
132 CUDA_TIMER_FREQ, get_ticks_per_sec());
134 /* the timer goes down from latch to -1 (period of latch + 2) */
135 if (d <= (s->counter_value + 1)) {
136 counter = (s->counter_value - d) & 0xffff;
138 counter = (d - (s->counter_value + 1)) % (s->latch + 2);
139 counter = (s->latch - counter) & 0xffff;
142 counter = (s->counter_value - d) & 0xffff;
147 static void set_counter(CUDAState *s, CUDATimer *ti, unsigned int val)
149 CUDA_DPRINTF("T%d.counter=%d\n", 1 + (ti->timer == NULL), val);
150 ti->load_time = qemu_get_clock_ns(vm_clock);
151 ti->counter_value = val;
152 cuda_timer_update(s, ti, ti->load_time);
155 static int64_t get_next_irq_time(CUDATimer *s, int64_t current_time)
157 int64_t d, next_time;
158 unsigned int counter;
160 /* current counter value */
161 d = muldiv64(current_time - s->load_time,
162 CUDA_TIMER_FREQ, get_ticks_per_sec());
163 /* the timer goes down from latch to -1 (period of latch + 2) */
164 if (d <= (s->counter_value + 1)) {
165 counter = (s->counter_value - d) & 0xffff;
167 counter = (d - (s->counter_value + 1)) % (s->latch + 2);
168 counter = (s->latch - counter) & 0xffff;
171 /* Note: we consider the irq is raised on 0 */
172 if (counter == 0xffff) {
173 next_time = d + s->latch + 1;
174 } else if (counter == 0) {
175 next_time = d + s->latch + 2;
177 next_time = d + counter;
179 CUDA_DPRINTF("latch=%d counter=%" PRId64 " delta_next=%" PRId64 "\n",
180 s->latch, d, next_time - d);
181 next_time = muldiv64(next_time, get_ticks_per_sec(), CUDA_TIMER_FREQ) +
183 if (next_time <= current_time)
184 next_time = current_time + 1;
188 static void cuda_timer_update(CUDAState *s, CUDATimer *ti,
189 int64_t current_time)
193 if ((s->acr & T1MODE) != T1MODE_CONT) {
194 qemu_del_timer(ti->timer);
196 ti->next_irq_time = get_next_irq_time(ti, current_time);
197 qemu_mod_timer(ti->timer, ti->next_irq_time);
201 static void cuda_timer1(void *opaque)
203 CUDAState *s = opaque;
204 CUDATimer *ti = &s->timers[0];
206 cuda_timer_update(s, ti, ti->next_irq_time);
211 static uint32_t cuda_readb(void *opaque, hwaddr addr)
213 CUDAState *s = opaque;
216 addr = (addr >> 9) & 0xf;
231 val = get_counter(&s->timers[0]) & 0xff;
236 val = get_counter(&s->timers[0]) >> 8;
240 val = s->timers[0].latch & 0xff;
243 /* XXX: check this */
244 val = (s->timers[0].latch >> 8) & 0xff;
247 val = get_counter(&s->timers[1]) & 0xff;
251 val = get_counter(&s->timers[1]) >> 8;
277 if (addr != 13 || val != 0) {
278 CUDA_DPRINTF("read: reg=0x%x val=%02x\n", (int)addr, val);
284 static void cuda_writeb(void *opaque, hwaddr addr, uint32_t val)
286 CUDAState *s = opaque;
288 addr = (addr >> 9) & 0xf;
289 CUDA_DPRINTF("write: reg=0x%x val=%02x\n", (int)addr, val);
306 s->timers[0].latch = (s->timers[0].latch & 0xff00) | val;
307 cuda_timer_update(s, &s->timers[0], qemu_get_clock_ns(vm_clock));
310 s->timers[0].latch = (s->timers[0].latch & 0xff) | (val << 8);
312 set_counter(s, &s->timers[0], s->timers[0].latch);
315 s->timers[0].latch = (s->timers[0].latch & 0xff00) | val;
316 cuda_timer_update(s, &s->timers[0], qemu_get_clock_ns(vm_clock));
319 s->timers[0].latch = (s->timers[0].latch & 0xff) | (val << 8);
321 cuda_timer_update(s, &s->timers[0], qemu_get_clock_ns(vm_clock));
324 s->timers[1].latch = val;
325 set_counter(s, &s->timers[1], val);
328 set_counter(s, &s->timers[1], (val << 8) | s->timers[1].latch);
335 cuda_timer_update(s, &s->timers[0], qemu_get_clock_ns(vm_clock));
349 s->ier |= val & 0x7f;
363 /* NOTE: TIP and TREQ are negated */
364 static void cuda_update(CUDAState *s)
366 int packet_received, len;
370 /* transfer requested from host */
372 if (s->acr & SR_OUT) {
374 if ((s->b & (TACK | TIP)) != (s->last_b & (TACK | TIP))) {
375 if (s->data_out_index < sizeof(s->data_out)) {
376 CUDA_DPRINTF("send: %02x\n", s->sr);
377 s->data_out[s->data_out_index++] = s->sr;
383 if (s->data_in_index < s->data_in_size) {
385 if ((s->b & (TACK | TIP)) != (s->last_b & (TACK | TIP))) {
386 s->sr = s->data_in[s->data_in_index++];
387 CUDA_DPRINTF("recv: %02x\n", s->sr);
388 /* indicate end of transfer */
389 if (s->data_in_index >= s->data_in_size) {
390 s->b = (s->b | TREQ);
398 /* no transfer requested: handle sync case */
399 if ((s->last_b & TIP) && (s->b & TACK) != (s->last_b & TACK)) {
400 /* update TREQ state each time TACK change state */
402 s->b = (s->b | TREQ);
404 s->b = (s->b & ~TREQ);
408 if (!(s->last_b & TIP)) {
409 /* handle end of host to cuda transfer */
410 packet_received = (s->data_out_index > 0);
411 /* always an IRQ at the end of transfer */
415 /* signal if there is data to read */
416 if (s->data_in_index < s->data_in_size) {
417 s->b = (s->b & ~TREQ);
422 s->last_acr = s->acr;
425 /* NOTE: cuda_receive_packet_from_host() can call cuda_update()
427 if (packet_received) {
428 len = s->data_out_index;
429 s->data_out_index = 0;
430 cuda_receive_packet_from_host(s, s->data_out, len);
434 static void cuda_send_packet_to_host(CUDAState *s,
435 const uint8_t *data, int len)
437 #ifdef DEBUG_CUDA_PACKET
440 printf("cuda_send_packet_to_host:\n");
441 for(i = 0; i < len; i++)
442 printf(" %02x", data[i]);
446 memcpy(s->data_in, data, len);
447 s->data_in_size = len;
448 s->data_in_index = 0;
454 static void cuda_adb_poll(void *opaque)
456 CUDAState *s = opaque;
457 uint8_t obuf[ADB_MAX_OUT_LEN + 2];
460 olen = adb_poll(&s->adb_bus, obuf + 2);
462 obuf[0] = ADB_PACKET;
463 obuf[1] = 0x40; /* polled data */
464 cuda_send_packet_to_host(s, obuf, olen + 2);
466 qemu_mod_timer(s->adb_poll_timer,
467 qemu_get_clock_ns(vm_clock) +
468 (get_ticks_per_sec() / CUDA_ADB_POLL_FREQ));
471 static void cuda_receive_packet(CUDAState *s,
472 const uint8_t *data, int len)
480 autopoll = (data[1] != 0);
481 if (autopoll != s->autopoll) {
482 s->autopoll = autopoll;
484 qemu_mod_timer(s->adb_poll_timer,
485 qemu_get_clock_ns(vm_clock) +
486 (get_ticks_per_sec() / CUDA_ADB_POLL_FREQ));
488 qemu_del_timer(s->adb_poll_timer);
491 obuf[0] = CUDA_PACKET;
493 cuda_send_packet_to_host(s, obuf, 2);
496 ti = (((uint32_t)data[1]) << 24) + (((uint32_t)data[2]) << 16) + (((uint32_t)data[3]) << 8) + data[4];
497 s->tick_offset = ti - (qemu_get_clock_ns(vm_clock) / get_ticks_per_sec());
498 obuf[0] = CUDA_PACKET;
501 cuda_send_packet_to_host(s, obuf, 3);
504 ti = s->tick_offset + (qemu_get_clock_ns(vm_clock) / get_ticks_per_sec());
505 obuf[0] = CUDA_PACKET;
512 cuda_send_packet_to_host(s, obuf, 7);
514 case CUDA_FILE_SERVER_FLAG:
515 case CUDA_SET_DEVICE_LIST:
516 case CUDA_SET_AUTO_RATE:
517 case CUDA_SET_POWER_MESSAGES:
518 obuf[0] = CUDA_PACKET;
520 cuda_send_packet_to_host(s, obuf, 2);
523 obuf[0] = CUDA_PACKET;
525 cuda_send_packet_to_host(s, obuf, 2);
526 qemu_system_shutdown_request();
528 case CUDA_RESET_SYSTEM:
529 obuf[0] = CUDA_PACKET;
531 cuda_send_packet_to_host(s, obuf, 2);
532 qemu_system_reset_request();
539 static void cuda_receive_packet_from_host(CUDAState *s,
540 const uint8_t *data, int len)
542 #ifdef DEBUG_CUDA_PACKET
545 printf("cuda_receive_packet_from_host:\n");
546 for(i = 0; i < len; i++)
547 printf(" %02x", data[i]);
554 uint8_t obuf[ADB_MAX_OUT_LEN + 2];
556 olen = adb_request(&s->adb_bus, obuf + 2, data + 1, len - 1);
558 obuf[0] = ADB_PACKET;
562 obuf[0] = ADB_PACKET;
566 cuda_send_packet_to_host(s, obuf, olen + 2);
570 cuda_receive_packet(s, data + 1, len - 1);
575 static void cuda_writew (void *opaque, hwaddr addr, uint32_t value)
579 static void cuda_writel (void *opaque, hwaddr addr, uint32_t value)
583 static uint32_t cuda_readw (void *opaque, hwaddr addr)
588 static uint32_t cuda_readl (void *opaque, hwaddr addr)
593 static const MemoryRegionOps cuda_ops = {
606 .endianness = DEVICE_NATIVE_ENDIAN,
609 static bool cuda_timer_exist(void *opaque, int version_id)
611 CUDATimer *s = opaque;
613 return s->timer != NULL;
616 static const VMStateDescription vmstate_cuda_timer = {
617 .name = "cuda_timer",
619 .minimum_version_id = 0,
620 .minimum_version_id_old = 0,
621 .fields = (VMStateField[]) {
622 VMSTATE_UINT16(latch, CUDATimer),
623 VMSTATE_UINT16(counter_value, CUDATimer),
624 VMSTATE_INT64(load_time, CUDATimer),
625 VMSTATE_INT64(next_irq_time, CUDATimer),
626 VMSTATE_TIMER_TEST(timer, CUDATimer, cuda_timer_exist),
627 VMSTATE_END_OF_LIST()
631 static const VMStateDescription vmstate_cuda = {
634 .minimum_version_id = 1,
635 .minimum_version_id_old = 1,
636 .fields = (VMStateField[]) {
637 VMSTATE_UINT8(a, CUDAState),
638 VMSTATE_UINT8(b, CUDAState),
639 VMSTATE_UINT8(dira, CUDAState),
640 VMSTATE_UINT8(dirb, CUDAState),
641 VMSTATE_UINT8(sr, CUDAState),
642 VMSTATE_UINT8(acr, CUDAState),
643 VMSTATE_UINT8(pcr, CUDAState),
644 VMSTATE_UINT8(ifr, CUDAState),
645 VMSTATE_UINT8(ier, CUDAState),
646 VMSTATE_UINT8(anh, CUDAState),
647 VMSTATE_INT32(data_in_size, CUDAState),
648 VMSTATE_INT32(data_in_index, CUDAState),
649 VMSTATE_INT32(data_out_index, CUDAState),
650 VMSTATE_UINT8(autopoll, CUDAState),
651 VMSTATE_BUFFER(data_in, CUDAState),
652 VMSTATE_BUFFER(data_out, CUDAState),
653 VMSTATE_UINT32(tick_offset, CUDAState),
654 VMSTATE_STRUCT_ARRAY(timers, CUDAState, 2, 1,
655 vmstate_cuda_timer, CUDATimer),
656 VMSTATE_END_OF_LIST()
660 static void cuda_reset(DeviceState *dev)
662 CUDAState *s = CUDA(dev);
673 // s->ier = T1_INT | SR_INT;
676 s->data_in_index = 0;
677 s->data_out_index = 0;
680 s->timers[0].latch = 0xffff;
681 set_counter(s, &s->timers[0], 0xffff);
683 s->timers[1].latch = 0;
684 set_counter(s, &s->timers[1], 0xffff);
687 static void cuda_realizefn(DeviceState *dev, Error **errp)
689 CUDAState *s = CUDA(dev);
692 s->timers[0].timer = qemu_new_timer_ns(vm_clock, cuda_timer1, s);
694 qemu_get_timedate(&tm, 0);
695 s->tick_offset = (uint32_t)mktimegm(&tm) + RTC_OFFSET;
697 s->adb_poll_timer = qemu_new_timer_ns(vm_clock, cuda_adb_poll, s);
700 static void cuda_initfn(Object *obj)
702 SysBusDevice *d = SYS_BUS_DEVICE(obj);
703 CUDAState *s = CUDA(obj);
706 memory_region_init_io(&s->mem, &cuda_ops, s, "cuda", 0x2000);
707 sysbus_init_mmio(d, &s->mem);
708 sysbus_init_irq(d, &s->irq);
710 for (i = 0; i < ARRAY_SIZE(s->timers); i++) {
711 s->timers[i].index = i;
714 qbus_create_inplace((BusState *)&s->adb_bus, TYPE_ADB_BUS, DEVICE(obj),
718 static void cuda_class_init(ObjectClass *oc, void *data)
720 DeviceClass *dc = DEVICE_CLASS(oc);
722 dc->realize = cuda_realizefn;
723 dc->reset = cuda_reset;
724 dc->vmsd = &vmstate_cuda;
727 static const TypeInfo cuda_type_info = {
729 .parent = TYPE_SYS_BUS_DEVICE,
730 .instance_size = sizeof(CUDAState),
731 .instance_init = cuda_initfn,
732 .class_init = cuda_class_init,
735 static void cuda_register_types(void)
737 type_register_static(&cuda_type_info);
740 type_init(cuda_register_types)