4 * Copyright (c) 2006 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License version 2 as published by the Free Software Foundation.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "qemu-timer.h"
29 /* i82731AB (PIIX4) compatible power management function */
30 #define PM_FREQ 3579545
32 #define ACPI_DBG_IO_ADDR 0xb044
34 typedef struct PIIX4PMState {
42 int64_t tmr_overflow_time;
56 #define RTC_EN (1 << 10)
57 #define PWRBTN_EN (1 << 8)
58 #define GBL_EN (1 << 5)
59 #define TMROF_EN (1 << 0)
60 #define TIMER_OVERFLOW_CNT (1 << 23)
61 #define TIMER_MASK 0xffffffLL
63 #define SCI_EN (1 << 0)
65 #define SUS_EN (1 << 13)
67 #define ACPI_ENABLE 0xf1
68 #define ACPI_DISABLE 0xf0
70 #define SMBHSTSTS 0x00
71 #define SMBHSTCNT 0x02
72 #define SMBHSTCMD 0x03
73 #define SMBHSTADD 0x04
74 #define SMBHSTDAT0 0x05
75 #define SMBHSTDAT1 0x06
76 #define SMBBLKDAT 0x07
78 PIIX4PMState *pm_state;
80 static void update_pmtmr(PIIX4PMState *s)
84 pmtmr = muldiv64(qemu_get_clock(vm_clock), PM_FREQ, ticks_per_sec)
87 if (!(s->pmsts & TMROF_EN)) {
88 if ((pmtmr ^ s->pmtmr) & TIMER_OVERFLOW_CNT) {
90 if (s->pmen & TMROF_EN)
91 qemu_set_irq(s->irq, 1);
93 /* Calculate when the timer will neet to set
94 * the overflow bit again */
95 uint64_t delta = TIMER_OVERFLOW_CNT -
96 (pmtmr & (TIMER_OVERFLOW_CNT - 1));
98 delta = muldiv64(delta, ticks_per_sec, PM_FREQ);
99 qemu_mod_timer(s->tmr_timer, qemu_get_clock(vm_clock) + delta);
106 static uint32_t get_pmtmr(PIIX4PMState *s)
109 return s->pmtmr & TIMER_MASK;
113 static int get_pmsts(PIIX4PMState *s)
115 /* Just increase the accurancy by double computing the timer value */
121 static void pm_update_sci(PIIX4PMState *s)
125 sci_level = (((s->pmsts & s->pmen) &
126 (RTC_EN | PWRBTN_EN | GBL_EN | TMROF_EN)) != 0);
128 qemu_set_irq(s->irq, sci_level);
131 static void pm_tmr_timer(void *opaque)
133 PIIX4PMState *s = opaque;
137 static void pm_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
139 PIIX4PMState *s = opaque;
154 s->pmcntrl = val & ~(SUS_EN);
156 /* change suspend type */
157 sus_typ = (val >> 10) & 3;
159 case 0: /* soft power off */
160 qemu_system_shutdown_request();
172 printf("PM writew port=0x%04x val=0x%04x\n", addr, val);
176 static uint32_t pm_ioport_readw(void *opaque, uint32_t addr)
178 PIIX4PMState *s = opaque;
197 printf("PM readw port=0x%04x val=0x%04x\n", addr, val);
202 static void pm_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
204 // PIIX4PMState *s = opaque;
207 printf("PM writel port=0x%04x val=0x%08x\n", addr, val);
211 static uint32_t pm_ioport_readl(void *opaque, uint32_t addr)
213 PIIX4PMState *s = opaque;
226 printf("PM readl port=0x%04x val=0x%08x\n", addr, val);
231 static void pm_smi_writeb(void *opaque, uint32_t addr, uint32_t val)
233 PIIX4PMState *s = opaque;
236 printf("pm_smi_writeb addr=0x%x val=0x%02x\n", addr, val);
241 /* ACPI specs 3.0, 4.7.2.5 */
242 if (val == ACPI_ENABLE) {
243 s->pmcntrl |= SCI_EN;
244 } else if (val == ACPI_DISABLE) {
245 s->pmcntrl &= ~SCI_EN;
248 if (s->dev.config[0x5b] & (1 << 1)) {
249 cpu_interrupt(first_cpu, CPU_INTERRUPT_SMI);
256 static uint32_t pm_smi_readb(void *opaque, uint32_t addr)
258 PIIX4PMState *s = opaque;
268 printf("pm_smi_readb addr=0x%x val=0x%02x\n", addr, val);
273 static void acpi_dbg_writel(void *opaque, uint32_t addr, uint32_t val)
276 printf("ACPI: DBG: 0x%08x\n", val);
280 static void smb_transaction(PIIX4PMState *s)
282 uint8_t prot = (s->smb_ctl >> 2) & 0x07;
283 uint8_t read = s->smb_addr & 0x01;
284 uint8_t cmd = s->smb_cmd;
285 uint8_t addr = s->smb_addr >> 1;
286 i2c_bus *bus = s->smbus;
289 printf("SMBus trans addr=0x%02x prot=0x%02x\n", addr, prot);
293 smbus_quick_command(bus, addr, read);
297 s->smb_data0 = smbus_receive_byte(bus, addr);
299 smbus_send_byte(bus, addr, cmd);
304 s->smb_data0 = smbus_read_byte(bus, addr, cmd);
306 smbus_write_byte(bus, addr, cmd, s->smb_data0);
312 val = smbus_read_word(bus, addr, cmd);
314 s->smb_data1 = val >> 8;
316 smbus_write_word(bus, addr, cmd, (s->smb_data1 << 8) | s->smb_data0);
321 s->smb_data0 = smbus_read_block(bus, addr, cmd, s->smb_data);
323 smbus_write_block(bus, addr, cmd, s->smb_data, s->smb_data0);
335 static void smb_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
337 PIIX4PMState *s = opaque;
340 printf("SMB writeb port=0x%04x val=0x%02x\n", addr, val);
365 s->smb_data[s->smb_index++] = val;
366 if (s->smb_index > 31)
374 static uint32_t smb_ioport_readb(void *opaque, uint32_t addr)
376 PIIX4PMState *s = opaque;
386 val = s->smb_ctl & 0x1f;
401 val = s->smb_data[s->smb_index++];
402 if (s->smb_index > 31)
410 printf("SMB readb port=0x%04x val=0x%02x\n", addr, val);
415 static void pm_io_space_update(PIIX4PMState *s)
419 if (s->dev.config[0x80] & 1) {
420 pm_io_base = le32_to_cpu(*(uint32_t *)(s->dev.config + 0x40));
421 pm_io_base &= 0xffc0;
423 /* XXX: need to improve memory and ioport allocation */
425 printf("PM: mapping to 0x%x\n", pm_io_base);
427 register_ioport_write(pm_io_base, 64, 2, pm_ioport_writew, s);
428 register_ioport_read(pm_io_base, 64, 2, pm_ioport_readw, s);
429 register_ioport_write(pm_io_base, 64, 4, pm_ioport_writel, s);
430 register_ioport_read(pm_io_base, 64, 4, pm_ioport_readl, s);
434 static void pm_write_config(PCIDevice *d,
435 uint32_t address, uint32_t val, int len)
437 pci_default_write_config(d, address, val, len);
439 pm_io_space_update((PIIX4PMState *)d);
442 static void pm_save(QEMUFile* f,void *opaque)
444 PIIX4PMState *s = opaque;
446 pci_device_save(&s->dev, f);
448 qemu_put_be16s(f, &s->pmsts);
449 qemu_put_be16s(f, &s->pmen);
450 qemu_put_be16s(f, &s->pmcntrl);
451 qemu_put_8s(f, &s->apmc);
452 qemu_put_8s(f, &s->apms);
453 qemu_put_timer(f, s->tmr_timer);
454 qemu_put_be64(f, s->tmr_overflow_time);
457 static int pm_load(QEMUFile* f,void* opaque,int version_id)
459 PIIX4PMState *s = opaque;
465 ret = pci_device_load(&s->dev, f);
469 qemu_get_be16s(f, &s->pmsts);
470 qemu_get_be16s(f, &s->pmen);
471 qemu_get_be16s(f, &s->pmcntrl);
472 qemu_get_8s(f, &s->apmc);
473 qemu_get_8s(f, &s->apms);
474 qemu_get_timer(f, s->tmr_timer);
475 s->tmr_overflow_time=qemu_get_be64(f);
477 pm_io_space_update(s);
482 i2c_bus *piix4_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base,
488 s = (PIIX4PMState *)pci_register_device(bus,
489 "PM", sizeof(PIIX4PMState),
490 devfn, NULL, pm_write_config);
492 pci_conf = s->dev.config;
493 pci_conf[0x00] = 0x86;
494 pci_conf[0x01] = 0x80;
495 pci_conf[0x02] = 0x13;
496 pci_conf[0x03] = 0x71;
497 pci_conf[0x06] = 0x80;
498 pci_conf[0x07] = 0x02;
499 pci_conf[0x08] = 0x03; // revision number
500 pci_conf[0x09] = 0x00;
501 pci_conf[0x0a] = 0x80; // other bridge device
502 pci_conf[0x0b] = 0x06; // bridge device
503 pci_conf[0x0e] = 0x00; // header_type
504 pci_conf[0x3d] = 0x01; // interrupt pin 1
506 pci_conf[0x40] = 0x01; /* PM io base read only bit */
508 register_ioport_write(0xb2, 2, 1, pm_smi_writeb, s);
509 register_ioport_read(0xb2, 2, 1, pm_smi_readb, s);
511 register_ioport_write(ACPI_DBG_IO_ADDR, 4, 4, acpi_dbg_writel, s);
513 /* XXX: which specification is used ? The i82731AB has different
515 pci_conf[0x5f] = (parallel_hds[0] != NULL ? 0x80 : 0) | 0x10;
516 pci_conf[0x63] = 0x60;
517 pci_conf[0x67] = (serial_hds[0] != NULL ? 0x08 : 0) |
518 (serial_hds[1] != NULL ? 0x90 : 0);
520 pci_conf[0x90] = smb_io_base | 1;
521 pci_conf[0x91] = smb_io_base >> 8;
522 pci_conf[0xd2] = 0x09;
523 register_ioport_write(smb_io_base, 64, 1, smb_ioport_writeb, s);
524 register_ioport_read(smb_io_base, 64, 1, smb_ioport_readb, s);
526 s->tmr_timer = qemu_new_timer(vm_clock, pm_tmr_timer, s);
528 register_savevm("piix4_pm", 0, 1, pm_save, pm_load, s);
530 s->smbus = i2c_init_bus();
535 #if defined(TARGET_I386)
536 void qemu_system_powerdown(void)
538 if(pm_state->pmen & PWRBTN_EN) {
539 pm_state->pmsts |= PWRBTN_EN;
540 pm_update_sci(pm_state);