2 * QEMU PowerMac PMU device support
4 * Copyright (c) 2016 Benjamin Herrenschmidt, IBM Corp.
5 * Copyright (c) 2018 Mark Cave-Ayland
7 * Based on the CUDA device by:
9 * Copyright (c) 2004-2007 Fabrice Bellard
10 * Copyright (c) 2007 Jocelyn Mayer
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this software and associated documentation files (the "Software"), to deal
14 * in the Software without restriction, including without limitation the rights
15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 * copies of the Software, and to permit persons to whom the Software is
17 * furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31 #include "qemu/osdep.h"
32 #include "qemu-common.h"
33 #include "hw/ppc/mac.h"
34 #include "hw/qdev-properties.h"
35 #include "migration/vmstate.h"
36 #include "hw/input/adb.h"
38 #include "hw/misc/mos6522.h"
39 #include "hw/misc/macio/gpio.h"
40 #include "hw/misc/macio/pmu.h"
41 #include "qapi/error.h"
42 #include "qemu/timer.h"
43 #include "sysemu/runstate.h"
44 #include "qapi/error.h"
45 #include "qemu/cutils.h"
47 #include "qemu/module.h"
51 /* Bits in B data register: all active low */
52 #define TACK 0x08 /* Transfer request (input) */
53 #define TREQ 0x10 /* Transfer acknowledge (output) */
55 /* PMU returns time_t's offset from Jan 1, 1904, not 1970 */
56 #define RTC_OFFSET 2082844800
58 #define VIA_TIMER_FREQ (4700000 / 6)
60 static void via_update_irq(PMUState
*s
)
62 MOS6522PMUState
*mps
= MOS6522_PMU(&s
->mos6522_pmu
);
63 MOS6522State
*ms
= MOS6522(mps
);
65 bool new_state
= !!(ms
->ifr
& ms
->ier
& (SR_INT
| T1_INT
| T2_INT
));
67 if (new_state
!= s
->via_irq_state
) {
68 s
->via_irq_state
= new_state
;
69 qemu_set_irq(s
->via_irq
, new_state
);
73 static void via_set_sr_int(void *opaque
)
76 MOS6522PMUState
*mps
= MOS6522_PMU(&s
->mos6522_pmu
);
77 MOS6522State
*ms
= MOS6522(mps
);
78 MOS6522DeviceClass
*mdc
= MOS6522_DEVICE_GET_CLASS(ms
);
83 static void pmu_update_extirq(PMUState
*s
)
85 if ((s
->intbits
& s
->intmask
) != 0) {
86 macio_set_gpio(s
->gpio
, 1, false);
88 macio_set_gpio(s
->gpio
, 1, true);
92 static void pmu_adb_poll(void *opaque
)
95 ADBBusState
*adb_bus
= &s
->adb_bus
;
98 if (!(s
->intbits
& PMU_INT_ADB
)) {
99 olen
= adb_poll(adb_bus
, s
->adb_reply
, adb_bus
->autopoll_mask
);
100 trace_pmu_adb_poll(olen
);
103 s
->adb_reply_size
= olen
;
104 s
->intbits
|= PMU_INT_ADB
| PMU_INT_ADB_AUTO
;
105 pmu_update_extirq(s
);
110 static void pmu_one_sec_timer(void *opaque
)
112 PMUState
*s
= opaque
;
114 trace_pmu_one_sec_timer();
116 s
->intbits
|= PMU_INT_TICK
;
117 pmu_update_extirq(s
);
118 s
->one_sec_target
+= 1000;
120 timer_mod(s
->one_sec_timer
, s
->one_sec_target
);
123 static void pmu_cmd_int_ack(PMUState
*s
,
124 const uint8_t *in_data
, uint8_t in_len
,
125 uint8_t *out_data
, uint8_t *out_len
)
128 qemu_log_mask(LOG_GUEST_ERROR
,
129 "PMU: INT_ACK command, invalid len: %d want: 0\n",
134 /* Make appropriate reply packet */
135 if (s
->intbits
& PMU_INT_ADB
) {
136 if (!s
->adb_reply_size
) {
137 qemu_log_mask(LOG_GUEST_ERROR
,
138 "Odd, PMU_INT_ADB set with no reply in buffer\n");
141 memcpy(out_data
+ 1, s
->adb_reply
, s
->adb_reply_size
);
142 out_data
[0] = s
->intbits
& (PMU_INT_ADB
| PMU_INT_ADB_AUTO
);
143 *out_len
= s
->adb_reply_size
+ 1;
144 s
->intbits
&= ~(PMU_INT_ADB
| PMU_INT_ADB_AUTO
);
145 s
->adb_reply_size
= 0;
147 out_data
[0] = s
->intbits
;
152 pmu_update_extirq(s
);
155 static void pmu_cmd_set_int_mask(PMUState
*s
,
156 const uint8_t *in_data
, uint8_t in_len
,
157 uint8_t *out_data
, uint8_t *out_len
)
160 qemu_log_mask(LOG_GUEST_ERROR
,
161 "PMU: SET_INT_MASK command, invalid len: %d want: 1\n",
166 trace_pmu_cmd_set_int_mask(s
->intmask
);
167 s
->intmask
= in_data
[0];
169 pmu_update_extirq(s
);
172 static void pmu_cmd_set_adb_autopoll(PMUState
*s
, uint16_t mask
)
174 ADBBusState
*adb_bus
= &s
->adb_bus
;
176 trace_pmu_cmd_set_adb_autopoll(mask
);
179 adb_set_autopoll_mask(adb_bus
, mask
);
180 adb_set_autopoll_enabled(adb_bus
, true);
182 adb_set_autopoll_enabled(adb_bus
, false);
186 static void pmu_cmd_adb(PMUState
*s
,
187 const uint8_t *in_data
, uint8_t in_len
,
188 uint8_t *out_data
, uint8_t *out_len
)
191 uint8_t adb_cmd
[255];
194 qemu_log_mask(LOG_GUEST_ERROR
,
195 "PMU: ADB PACKET, invalid len: %d want at least 2\n",
203 trace_pmu_cmd_adb_nobus();
207 /* Set autopoll is a special form of the command */
208 if (in_data
[0] == 0 && in_data
[1] == 0x86) {
209 uint16_t mask
= in_data
[2];
210 mask
= (mask
<< 8) | in_data
[3];
212 qemu_log_mask(LOG_GUEST_ERROR
,
213 "PMU: ADB Autopoll requires 4 bytes, got %d\n",
218 pmu_cmd_set_adb_autopoll(s
, mask
);
222 trace_pmu_cmd_adb_request(in_len
, in_data
[0], in_data
[1], in_data
[2],
223 in_data
[3], in_data
[4]);
229 if (adblen
> (in_len
- 3)) {
230 qemu_log_mask(LOG_GUEST_ERROR
,
231 "PMU: ADB len is %d > %d (in_len -3)...erroring\n",
234 } else if (adblen
> 252) {
235 qemu_log_mask(LOG_GUEST_ERROR
, "PMU: ADB command too big!\n");
239 adb_cmd
[0] = in_data
[0];
240 memcpy(&adb_cmd
[1], &in_data
[3], in_len
- 3);
241 len
= adb_request(&s
->adb_bus
, s
->adb_reply
+ 2, adb_cmd
, in_len
- 2);
243 trace_pmu_cmd_adb_reply(len
);
248 s
->adb_reply_size
= len
+ 2;
249 s
->adb_reply
[0] = 0x01;
250 s
->adb_reply
[1] = len
;
253 s
->adb_reply_size
= 1;
254 s
->adb_reply
[0] = 0x00;
257 s
->intbits
|= PMU_INT_ADB
;
258 pmu_update_extirq(s
);
261 static void pmu_cmd_adb_poll_off(PMUState
*s
,
262 const uint8_t *in_data
, uint8_t in_len
,
263 uint8_t *out_data
, uint8_t *out_len
)
265 ADBBusState
*adb_bus
= &s
->adb_bus
;
268 qemu_log_mask(LOG_GUEST_ERROR
,
269 "PMU: ADB POLL OFF command, invalid len: %d want: 0\n",
275 adb_set_autopoll_enabled(adb_bus
, false);
279 static void pmu_cmd_shutdown(PMUState
*s
,
280 const uint8_t *in_data
, uint8_t in_len
,
281 uint8_t *out_data
, uint8_t *out_len
)
284 qemu_log_mask(LOG_GUEST_ERROR
,
285 "PMU: SHUTDOWN command, invalid len: %d want: 4\n",
293 if (in_data
[0] != 'M' || in_data
[1] != 'A' || in_data
[2] != 'T' ||
296 qemu_log_mask(LOG_GUEST_ERROR
,
297 "PMU: SHUTDOWN command, Bad MATT signature\n");
301 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN
);
304 static void pmu_cmd_reset(PMUState
*s
,
305 const uint8_t *in_data
, uint8_t in_len
,
306 uint8_t *out_data
, uint8_t *out_len
)
309 qemu_log_mask(LOG_GUEST_ERROR
,
310 "PMU: RESET command, invalid len: %d want: 0\n",
315 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
318 static void pmu_cmd_get_rtc(PMUState
*s
,
319 const uint8_t *in_data
, uint8_t in_len
,
320 uint8_t *out_data
, uint8_t *out_len
)
325 qemu_log_mask(LOG_GUEST_ERROR
,
326 "PMU: GET_RTC command, invalid len: %d want: 0\n",
331 ti
= s
->tick_offset
+ (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
)
332 / NANOSECONDS_PER_SECOND
);
333 out_data
[0] = ti
>> 24;
334 out_data
[1] = ti
>> 16;
335 out_data
[2] = ti
>> 8;
340 static void pmu_cmd_set_rtc(PMUState
*s
,
341 const uint8_t *in_data
, uint8_t in_len
,
342 uint8_t *out_data
, uint8_t *out_len
)
347 qemu_log_mask(LOG_GUEST_ERROR
,
348 "PMU: SET_RTC command, invalid len: %d want: 4\n",
353 ti
= (((uint32_t)in_data
[0]) << 24) + (((uint32_t)in_data
[1]) << 16)
354 + (((uint32_t)in_data
[2]) << 8) + in_data
[3];
356 s
->tick_offset
= ti
- (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
)
357 / NANOSECONDS_PER_SECOND
);
360 static void pmu_cmd_system_ready(PMUState
*s
,
361 const uint8_t *in_data
, uint8_t in_len
,
362 uint8_t *out_data
, uint8_t *out_len
)
367 static void pmu_cmd_get_version(PMUState
*s
,
368 const uint8_t *in_data
, uint8_t in_len
,
369 uint8_t *out_data
, uint8_t *out_len
)
372 *out_data
= 1; /* ??? Check what Apple does */
375 static void pmu_cmd_power_events(PMUState
*s
,
376 const uint8_t *in_data
, uint8_t in_len
,
377 uint8_t *out_data
, uint8_t *out_len
)
380 qemu_log_mask(LOG_GUEST_ERROR
,
381 "PMU: POWER EVENTS command, invalid len %d, want at least 1\n",
386 switch (in_data
[0]) {
387 /* Dummies for now */
388 case PMU_PWR_GET_POWERUP_EVENTS
:
393 case PMU_PWR_SET_POWERUP_EVENTS
:
394 case PMU_PWR_CLR_POWERUP_EVENTS
:
396 case PMU_PWR_GET_WAKEUP_EVENTS
:
401 case PMU_PWR_SET_WAKEUP_EVENTS
:
402 case PMU_PWR_CLR_WAKEUP_EVENTS
:
405 qemu_log_mask(LOG_GUEST_ERROR
,
406 "PMU: POWER EVENTS unknown subcommand 0x%02x\n",
411 static void pmu_cmd_get_cover(PMUState
*s
,
412 const uint8_t *in_data
, uint8_t in_len
,
413 uint8_t *out_data
, uint8_t *out_len
)
415 /* Not 100% sure here, will have to check what a real Mac
416 * returns other than byte 0 bit 0 is LID closed on laptops
422 static void pmu_cmd_download_status(PMUState
*s
,
423 const uint8_t *in_data
, uint8_t in_len
,
424 uint8_t *out_data
, uint8_t *out_len
)
426 /* This has to do with PMU firmware updates as far as I can tell.
428 * We return 0x62 which is what OpenPMU expects
434 static void pmu_cmd_read_pmu_ram(PMUState
*s
,
435 const uint8_t *in_data
, uint8_t in_len
,
436 uint8_t *out_data
, uint8_t *out_len
)
439 qemu_log_mask(LOG_GUEST_ERROR
,
440 "PMU: READ_PMU_RAM command, invalid len %d, expected 3\n",
445 qemu_log_mask(LOG_GUEST_ERROR
,
446 "PMU: Unsupported READ_PMU_RAM, args: %02x %02x %02x\n",
447 in_data
[0], in_data
[1], in_data
[2]);
452 /* description of commands */
453 typedef struct PMUCmdHandler
{
456 void (*handler
)(PMUState
*s
,
457 const uint8_t *in_args
, uint8_t in_len
,
458 uint8_t *out_args
, uint8_t *out_len
);
461 static const PMUCmdHandler PMUCmdHandlers
[] = {
462 { PMU_INT_ACK
, "INT ACK", pmu_cmd_int_ack
},
463 { PMU_SET_INTR_MASK
, "SET INT MASK", pmu_cmd_set_int_mask
},
464 { PMU_ADB_CMD
, "ADB COMMAND", pmu_cmd_adb
},
465 { PMU_ADB_POLL_OFF
, "ADB POLL OFF", pmu_cmd_adb_poll_off
},
466 { PMU_RESET
, "REBOOT", pmu_cmd_reset
},
467 { PMU_SHUTDOWN
, "SHUTDOWN", pmu_cmd_shutdown
},
468 { PMU_READ_RTC
, "GET RTC", pmu_cmd_get_rtc
},
469 { PMU_SET_RTC
, "SET RTC", pmu_cmd_set_rtc
},
470 { PMU_SYSTEM_READY
, "SYSTEM READY", pmu_cmd_system_ready
},
471 { PMU_GET_VERSION
, "GET VERSION", pmu_cmd_get_version
},
472 { PMU_POWER_EVENTS
, "POWER EVENTS", pmu_cmd_power_events
},
473 { PMU_GET_COVER
, "GET_COVER", pmu_cmd_get_cover
},
474 { PMU_DOWNLOAD_STATUS
, "DOWNLOAD STATUS", pmu_cmd_download_status
},
475 { PMU_READ_PMU_RAM
, "READ PMGR RAM", pmu_cmd_read_pmu_ram
},
478 static void pmu_dispatch_cmd(PMUState
*s
)
482 /* No response by default */
485 for (i
= 0; i
< ARRAY_SIZE(PMUCmdHandlers
); i
++) {
486 const PMUCmdHandler
*desc
= &PMUCmdHandlers
[i
];
488 if (desc
->command
!= s
->cmd
) {
492 trace_pmu_dispatch_cmd(desc
->name
);
493 desc
->handler(s
, s
->cmd_buf
, s
->cmd_buf_pos
,
494 s
->cmd_rsp
, &s
->cmd_rsp_sz
);
496 if (s
->rsplen
!= -1 && s
->rsplen
!= s
->cmd_rsp_sz
) {
497 trace_pmu_debug_protocol_string("QEMU internal cmd resp mismatch!");
499 trace_pmu_debug_protocol_resp_size(s
->cmd_rsp_sz
);
505 trace_pmu_dispatch_unknown_cmd(s
->cmd
);
507 /* Manufacture fake response with 0's */
508 if (s
->rsplen
== -1) {
511 s
->cmd_rsp_sz
= s
->rsplen
;
512 memset(s
->cmd_rsp
, 0, s
->rsplen
);
516 static void pmu_update(PMUState
*s
)
518 MOS6522PMUState
*mps
= &s
->mos6522_pmu
;
519 MOS6522State
*ms
= MOS6522(mps
);
520 ADBBusState
*adb_bus
= &s
->adb_bus
;
522 /* Only react to changes in reg B */
523 if (ms
->b
== s
->last_b
) {
528 /* Check the TREQ / TACK state */
529 switch (ms
->b
& (TREQ
| TACK
)) {
531 /* This is an ack release, handle it and bail out */
535 trace_pmu_debug_protocol_string("handshake: TREQ high, setting TACK");
538 /* This is a valid request, handle below */
541 /* This is an idle state */
544 /* Invalid state, log and ignore */
545 trace_pmu_debug_protocol_error(ms
->b
);
549 /* If we wanted to handle commands asynchronously, this is where
550 * we would delay the clearing of TACK until we are ready to send
554 /* We have a request, handshake TACK so we don't stay in
555 * an invalid state. If we were concurrent with the OS we
556 * should only do this after we grabbed the SR but that isn't
560 trace_pmu_debug_protocol_clear_treq(s
->cmd_state
);
565 /* Act according to state */
566 switch (s
->cmd_state
) {
568 if (!(ms
->acr
& SR_OUT
)) {
569 trace_pmu_debug_protocol_string("protocol error! "
570 "state idle, ACR reading");
576 s
->cmdlen
= pmu_data_len
[s
->cmd
][0];
577 s
->rsplen
= pmu_data_len
[s
->cmd
][1];
580 s
->cmd_state
= pmu_state_cmd
;
582 adb_autopoll_block(adb_bus
);
583 trace_pmu_debug_protocol_cmd(s
->cmd
, s
->cmdlen
, s
->rsplen
);
587 if (!(ms
->acr
& SR_OUT
)) {
588 trace_pmu_debug_protocol_string("protocol error! "
589 "state cmd, ACR reading");
593 if (s
->cmdlen
== -1) {
594 trace_pmu_debug_protocol_cmdlen(ms
->sr
);
597 if (s
->cmdlen
> sizeof(s
->cmd_buf
)) {
598 trace_pmu_debug_protocol_cmd_toobig(s
->cmdlen
);
600 } else if (s
->cmd_buf_pos
< sizeof(s
->cmd_buf
)) {
601 s
->cmd_buf
[s
->cmd_buf_pos
++] = ms
->sr
;
608 if (ms
->acr
& SR_OUT
) {
609 trace_pmu_debug_protocol_string("protocol error! "
610 "state resp, ACR writing");
614 if (s
->rsplen
== -1) {
615 trace_pmu_debug_protocol_cmd_send_resp_size(s
->cmd_rsp_sz
);
617 ms
->sr
= s
->cmd_rsp_sz
;
618 s
->rsplen
= s
->cmd_rsp_sz
;
619 } else if (s
->cmd_rsp_pos
< s
->cmd_rsp_sz
) {
620 trace_pmu_debug_protocol_cmd_send_resp(s
->cmd_rsp_pos
, s
->rsplen
);
622 ms
->sr
= s
->cmd_rsp
[s
->cmd_rsp_pos
++];
629 /* Check for state completion */
630 if (s
->cmd_state
== pmu_state_cmd
&& s
->cmdlen
== s
->cmd_buf_pos
) {
631 trace_pmu_debug_protocol_string("Command reception complete, "
635 s
->cmd_state
= pmu_state_rsp
;
638 if (s
->cmd_state
== pmu_state_rsp
&& s
->rsplen
== s
->cmd_rsp_pos
) {
639 trace_pmu_debug_protocol_cmd_resp_complete(ms
->ier
);
641 adb_autopoll_unblock(adb_bus
);
642 s
->cmd_state
= pmu_state_idle
;
646 static uint64_t mos6522_pmu_read(void *opaque
, hwaddr addr
, unsigned size
)
648 PMUState
*s
= opaque
;
649 MOS6522PMUState
*mps
= &s
->mos6522_pmu
;
650 MOS6522State
*ms
= MOS6522(mps
);
652 addr
= (addr
>> 9) & 0xf;
653 return mos6522_read(ms
, addr
, size
);
656 static void mos6522_pmu_write(void *opaque
, hwaddr addr
, uint64_t val
,
659 PMUState
*s
= opaque
;
660 MOS6522PMUState
*mps
= &s
->mos6522_pmu
;
661 MOS6522State
*ms
= MOS6522(mps
);
663 addr
= (addr
>> 9) & 0xf;
664 mos6522_write(ms
, addr
, val
, size
);
667 static const MemoryRegionOps mos6522_pmu_ops
= {
668 .read
= mos6522_pmu_read
,
669 .write
= mos6522_pmu_write
,
670 .endianness
= DEVICE_BIG_ENDIAN
,
672 .min_access_size
= 1,
673 .max_access_size
= 1,
677 static bool pmu_adb_state_needed(void *opaque
)
679 PMUState
*s
= opaque
;
684 static const VMStateDescription vmstate_pmu_adb
= {
687 .minimum_version_id
= 1,
688 .needed
= pmu_adb_state_needed
,
689 .fields
= (VMStateField
[]) {
690 VMSTATE_UINT8(adb_reply_size
, PMUState
),
691 VMSTATE_BUFFER(adb_reply
, PMUState
),
692 VMSTATE_END_OF_LIST()
696 static const VMStateDescription vmstate_pmu
= {
699 .minimum_version_id
= 1,
700 .fields
= (VMStateField
[]) {
701 VMSTATE_STRUCT(mos6522_pmu
.parent_obj
, PMUState
, 0, vmstate_mos6522
,
703 VMSTATE_UINT8(last_b
, PMUState
),
704 VMSTATE_UINT8(cmd
, PMUState
),
705 VMSTATE_UINT32(cmdlen
, PMUState
),
706 VMSTATE_UINT32(rsplen
, PMUState
),
707 VMSTATE_UINT8(cmd_buf_pos
, PMUState
),
708 VMSTATE_BUFFER(cmd_buf
, PMUState
),
709 VMSTATE_UINT8(cmd_rsp_pos
, PMUState
),
710 VMSTATE_UINT8(cmd_rsp_sz
, PMUState
),
711 VMSTATE_BUFFER(cmd_rsp
, PMUState
),
712 VMSTATE_UINT8(intbits
, PMUState
),
713 VMSTATE_UINT8(intmask
, PMUState
),
714 VMSTATE_UINT32(tick_offset
, PMUState
),
715 VMSTATE_TIMER_PTR(one_sec_timer
, PMUState
),
716 VMSTATE_INT64(one_sec_target
, PMUState
),
717 VMSTATE_END_OF_LIST()
719 .subsections
= (const VMStateDescription
* []) {
724 static void pmu_reset(DeviceState
*dev
)
726 PMUState
*s
= VIA_PMU(dev
);
728 /* OpenBIOS needs to do this? MacOS 9 needs it */
729 s
->intmask
= PMU_INT_ADB
| PMU_INT_TICK
;
732 s
->cmd_state
= pmu_state_idle
;
735 static void pmu_realize(DeviceState
*dev
, Error
**errp
)
737 PMUState
*s
= VIA_PMU(dev
);
739 ADBBusState
*adb_bus
= &s
->adb_bus
;
742 if (!sysbus_realize(SYS_BUS_DEVICE(&s
->mos6522_pmu
), errp
)) {
746 /* Pass IRQ from 6522 */
747 sbd
= SYS_BUS_DEVICE(s
);
748 sysbus_pass_irq(sbd
, SYS_BUS_DEVICE(&s
->mos6522_pmu
));
750 qemu_get_timedate(&tm
, 0);
751 s
->tick_offset
= (uint32_t)mktimegm(&tm
) + RTC_OFFSET
;
752 s
->one_sec_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
, pmu_one_sec_timer
, s
);
753 s
->one_sec_target
= qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + 1000;
754 timer_mod(s
->one_sec_timer
, s
->one_sec_target
);
757 qbus_create_inplace(&s
->adb_bus
, sizeof(s
->adb_bus
), TYPE_ADB_BUS
,
759 adb_register_autopoll_callback(adb_bus
, pmu_adb_poll
, s
);
763 static void pmu_init(Object
*obj
)
765 SysBusDevice
*d
= SYS_BUS_DEVICE(obj
);
766 PMUState
*s
= VIA_PMU(obj
);
768 object_property_add_link(obj
, "gpio", TYPE_MACIO_GPIO
,
769 (Object
**) &s
->gpio
,
770 qdev_prop_allow_set_link_before_realize
,
773 object_initialize_child(obj
, "mos6522-pmu", &s
->mos6522_pmu
,
776 memory_region_init_io(&s
->mem
, obj
, &mos6522_pmu_ops
, s
, "via-pmu",
778 sysbus_init_mmio(d
, &s
->mem
);
781 static Property pmu_properties
[] = {
782 DEFINE_PROP_BOOL("has-adb", PMUState
, has_adb
, true),
783 DEFINE_PROP_END_OF_LIST()
786 static void pmu_class_init(ObjectClass
*oc
, void *data
)
788 DeviceClass
*dc
= DEVICE_CLASS(oc
);
790 dc
->realize
= pmu_realize
;
791 dc
->reset
= pmu_reset
;
792 dc
->vmsd
= &vmstate_pmu
;
793 device_class_set_props(dc
, pmu_properties
);
794 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
797 static const TypeInfo pmu_type_info
= {
798 .name
= TYPE_VIA_PMU
,
799 .parent
= TYPE_SYS_BUS_DEVICE
,
800 .instance_size
= sizeof(PMUState
),
801 .instance_init
= pmu_init
,
802 .class_init
= pmu_class_init
,
805 static void mos6522_pmu_portB_write(MOS6522State
*s
)
807 MOS6522PMUState
*mps
= container_of(s
, MOS6522PMUState
, parent_obj
);
808 PMUState
*ps
= container_of(mps
, PMUState
, mos6522_pmu
);
810 if ((s
->pcr
& 0xe0) == 0x20 || (s
->pcr
& 0xe0) == 0x60) {
819 static void mos6522_pmu_portA_write(MOS6522State
*s
)
821 MOS6522PMUState
*mps
= container_of(s
, MOS6522PMUState
, parent_obj
);
822 PMUState
*ps
= container_of(mps
, PMUState
, mos6522_pmu
);
824 if ((s
->pcr
& 0x0e) == 0x02 || (s
->pcr
& 0x0e) == 0x06) {
832 static void mos6522_pmu_reset(DeviceState
*dev
)
834 MOS6522State
*ms
= MOS6522(dev
);
835 MOS6522PMUState
*mps
= container_of(ms
, MOS6522PMUState
, parent_obj
);
836 PMUState
*s
= container_of(mps
, PMUState
, mos6522_pmu
);
837 MOS6522DeviceClass
*mdc
= MOS6522_DEVICE_GET_CLASS(ms
);
839 mdc
->parent_reset(dev
);
841 ms
->timers
[0].frequency
= VIA_TIMER_FREQ
;
842 ms
->timers
[1].frequency
= (SCALE_US
* 6000) / 4700;
844 s
->last_b
= ms
->b
= TACK
| TREQ
;
847 static void mos6522_pmu_class_init(ObjectClass
*oc
, void *data
)
849 DeviceClass
*dc
= DEVICE_CLASS(oc
);
850 MOS6522DeviceClass
*mdc
= MOS6522_DEVICE_CLASS(oc
);
852 dc
->reset
= mos6522_pmu_reset
;
853 mdc
->portB_write
= mos6522_pmu_portB_write
;
854 mdc
->portA_write
= mos6522_pmu_portA_write
;
857 static const TypeInfo mos6522_pmu_type_info
= {
858 .name
= TYPE_MOS6522_PMU
,
859 .parent
= TYPE_MOS6522
,
860 .instance_size
= sizeof(MOS6522PMUState
),
861 .class_init
= mos6522_pmu_class_init
,
864 static void pmu_register_types(void)
866 type_register_static(&pmu_type_info
);
867 type_register_static(&mos6522_pmu_type_info
);
870 type_init(pmu_register_types
)