2 * VT82C686B south bridge support
4 * Copyright (c) 2008 yajin (yajin@vm-kernel.org)
5 * Copyright (c) 2009 chenming (chenming@rdc.faw.com.cn)
6 * Copyright (c) 2010 Huacai Chen (zltjiangshi@gmail.com)
7 * This code is licensed under the GNU GPL v2.
9 * Contributions after 2012-01-13 are licensed under the terms of the
10 * GNU GPL, version 2 or (at your option) any later version.
13 #include "qemu/osdep.h"
14 #include "hw/isa/vt82c686.h"
15 #include "hw/pci/pci.h"
16 #include "hw/qdev-properties.h"
17 #include "hw/isa/isa.h"
18 #include "hw/isa/superio.h"
19 #include "migration/vmstate.h"
20 #include "hw/isa/apm.h"
21 #include "hw/acpi/acpi.h"
22 #include "hw/i2c/pm_smbus.h"
23 #include "qapi/error.h"
24 #include "qemu/module.h"
25 #include "qemu/range.h"
26 #include "qemu/timer.h"
27 #include "exec/address-spaces.h"
30 OBJECT_DECLARE_SIMPLE_TYPE(VT686PMState, VT82C686B_PM)
40 static void pm_io_space_update(VT686PMState *s)
42 uint32_t pmbase = pci_get_long(s->dev.config + 0x48) & 0xff80UL;
44 memory_region_transaction_begin();
45 memory_region_set_address(&s->io, pmbase);
46 memory_region_set_enabled(&s->io, s->dev.config[0x41] & BIT(7));
47 memory_region_transaction_commit();
50 static void smb_io_space_update(VT686PMState *s)
52 uint32_t smbase = pci_get_long(s->dev.config + 0x90) & 0xfff0UL;
54 memory_region_transaction_begin();
55 memory_region_set_address(&s->smb.io, smbase);
56 memory_region_set_enabled(&s->smb.io, s->dev.config[0xd2] & BIT(0));
57 memory_region_transaction_commit();
60 static int vmstate_acpi_post_load(void *opaque, int version_id)
62 VT686PMState *s = opaque;
64 pm_io_space_update(s);
65 smb_io_space_update(s);
69 static const VMStateDescription vmstate_acpi = {
70 .name = "vt82c686b_pm",
72 .minimum_version_id = 1,
73 .post_load = vmstate_acpi_post_load,
74 .fields = (VMStateField[]) {
75 VMSTATE_PCI_DEVICE(dev, VT686PMState),
76 VMSTATE_UINT16(ar.pm1.evt.sts, VT686PMState),
77 VMSTATE_UINT16(ar.pm1.evt.en, VT686PMState),
78 VMSTATE_UINT16(ar.pm1.cnt.cnt, VT686PMState),
79 VMSTATE_STRUCT(apm, VT686PMState, 0, vmstate_apm, APMState),
80 VMSTATE_TIMER_PTR(ar.tmr.timer, VT686PMState),
81 VMSTATE_INT64(ar.tmr.overflow_time, VT686PMState),
86 static void pm_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int len)
88 VT686PMState *s = VT82C686B_PM(d);
90 trace_via_pm_write(addr, val, len);
91 pci_default_write_config(d, addr, val, len);
92 if (ranges_overlap(addr, len, 0x48, 4)) {
93 uint32_t v = pci_get_long(s->dev.config + 0x48);
94 pci_set_long(s->dev.config + 0x48, (v & 0xff80UL) | 1);
96 if (range_covers_byte(addr, len, 0x41)) {
97 pm_io_space_update(s);
99 if (ranges_overlap(addr, len, 0x90, 4)) {
100 uint32_t v = pci_get_long(s->dev.config + 0x90);
101 pci_set_long(s->dev.config + 0x90, (v & 0xfff0UL) | 1);
103 if (range_covers_byte(addr, len, 0xd2)) {
104 s->dev.config[0xd2] &= 0xf;
105 smb_io_space_update(s);
109 static void pm_io_write(void *op, hwaddr addr, uint64_t data, unsigned size)
111 trace_via_pm_io_write(addr, data, size);
114 static uint64_t pm_io_read(void *op, hwaddr addr, unsigned size)
116 trace_via_pm_io_read(addr, 0, size);
120 static const MemoryRegionOps pm_io_ops = {
122 .write = pm_io_write,
123 .endianness = DEVICE_NATIVE_ENDIAN,
125 .min_access_size = 1,
126 .max_access_size = 1,
130 static void pm_update_sci(VT686PMState *s)
132 int sci_level, pmsts;
134 pmsts = acpi_pm1_evt_get_sts(&s->ar);
135 sci_level = (((pmsts & s->ar.pm1.evt.en) &
136 (ACPI_BITMASK_RT_CLOCK_ENABLE |
137 ACPI_BITMASK_POWER_BUTTON_ENABLE |
138 ACPI_BITMASK_GLOBAL_LOCK_ENABLE |
139 ACPI_BITMASK_TIMER_ENABLE)) != 0);
140 pci_set_irq(&s->dev, sci_level);
141 /* schedule a timer interruption if needed */
142 acpi_pm_tmr_update(&s->ar, (s->ar.pm1.evt.en & ACPI_BITMASK_TIMER_ENABLE) &&
143 !(pmsts & ACPI_BITMASK_TIMER_STATUS));
146 static void pm_tmr_timer(ACPIREGS *ar)
148 VT686PMState *s = container_of(ar, VT686PMState, ar);
152 static void vt82c686b_pm_reset(DeviceState *d)
154 VT686PMState *s = VT82C686B_PM(d);
156 memset(s->dev.config + PCI_CONFIG_HEADER_SIZE, 0,
157 PCI_CONFIG_SPACE_SIZE - PCI_CONFIG_HEADER_SIZE);
158 /* Power Management IO base */
159 pci_set_long(s->dev.config + 0x48, 1);
161 pci_set_long(s->dev.config + 0x90, 1);
163 pm_io_space_update(s);
164 smb_io_space_update(s);
167 static void vt82c686b_pm_realize(PCIDevice *dev, Error **errp)
169 VT686PMState *s = VT82C686B_PM(dev);
171 pci_set_word(dev->config + PCI_STATUS, PCI_STATUS_FAST_BACK |
172 PCI_STATUS_DEVSEL_MEDIUM);
174 pm_smbus_init(DEVICE(s), &s->smb, false);
175 memory_region_add_subregion(pci_address_space_io(dev), 0, &s->smb.io);
176 memory_region_set_enabled(&s->smb.io, false);
178 apm_init(dev, &s->apm, NULL, s);
180 memory_region_init_io(&s->io, OBJECT(dev), &pm_io_ops, s,
182 memory_region_add_subregion(pci_address_space_io(dev), 0, &s->io);
183 memory_region_set_enabled(&s->io, false);
185 acpi_pm_tmr_init(&s->ar, pm_tmr_timer, &s->io);
186 acpi_pm1_evt_init(&s->ar, pm_tmr_timer, &s->io);
187 acpi_pm1_cnt_init(&s->ar, &s->io, false, false, 2);
190 static void via_pm_class_init(ObjectClass *klass, void *data)
192 DeviceClass *dc = DEVICE_CLASS(klass);
193 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
195 k->realize = vt82c686b_pm_realize;
196 k->config_write = pm_write_config;
197 k->vendor_id = PCI_VENDOR_ID_VIA;
198 k->device_id = PCI_DEVICE_ID_VIA_ACPI;
199 k->class_id = PCI_CLASS_BRIDGE_OTHER;
201 dc->reset = vt82c686b_pm_reset;
203 dc->vmsd = &vmstate_acpi;
204 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
207 static const TypeInfo via_pm_info = {
208 .name = TYPE_VT82C686B_PM,
209 .parent = TYPE_PCI_DEVICE,
210 .instance_size = sizeof(VT686PMState),
211 .class_init = via_pm_class_init,
212 .interfaces = (InterfaceInfo[]) {
213 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
219 typedef struct SuperIOConfig {
225 static void superio_cfg_write(void *opaque, hwaddr addr, uint64_t data,
228 SuperIOConfig *sc = opaque;
230 if (addr == 0x3f0) { /* config index register */
231 sc->index = data & 0xff;
233 bool can_write = true;
234 /* 0x3f1, config data register */
235 trace_via_superio_write(sc->index, data & 0xff);
248 /* case 0xe6 ... 0xe8: Should set base port of parallel and serial */
254 sc->regs[sc->index] = data & 0xff;
259 static uint64_t superio_cfg_read(void *opaque, hwaddr addr, unsigned size)
261 SuperIOConfig *sc = opaque;
262 uint8_t val = sc->regs[sc->index];
264 trace_via_superio_read(sc->index, val);
268 static const MemoryRegionOps superio_cfg_ops = {
269 .read = superio_cfg_read,
270 .write = superio_cfg_write,
271 .endianness = DEVICE_NATIVE_ENDIAN,
273 .min_access_size = 1,
274 .max_access_size = 1,
279 OBJECT_DECLARE_SIMPLE_TYPE(VT82C686BISAState, VT82C686B_ISA)
281 struct VT82C686BISAState {
283 SuperIOConfig superio_cfg;
286 static void vt82c686b_write_config(PCIDevice *d, uint32_t addr,
287 uint32_t val, int len)
289 VT82C686BISAState *s = VT82C686B_ISA(d);
291 trace_via_isa_write(addr, val, len);
292 pci_default_write_config(d, addr, val, len);
294 /* BIT(1): enable or disable superio config io ports */
295 memory_region_set_enabled(&s->superio_cfg.io, val & BIT(1));
299 static const VMStateDescription vmstate_via = {
302 .minimum_version_id = 1,
303 .fields = (VMStateField[]) {
304 VMSTATE_PCI_DEVICE(dev, VT82C686BISAState),
305 VMSTATE_END_OF_LIST()
309 static void vt82c686b_isa_reset(DeviceState *dev)
311 VT82C686BISAState *s = VT82C686B_ISA(dev);
312 uint8_t *pci_conf = s->dev.config;
314 pci_set_long(pci_conf + PCI_CAPABILITY_LIST, 0x000000c0);
315 pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
316 PCI_COMMAND_MASTER | PCI_COMMAND_SPECIAL);
317 pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM);
319 pci_conf[0x48] = 0x01; /* Miscellaneous Control 3 */
320 pci_conf[0x4a] = 0x04; /* IDE interrupt Routing */
321 pci_conf[0x4f] = 0x03; /* DMA/Master Mem Access Control 3 */
322 pci_conf[0x50] = 0x2d; /* PnP DMA Request Control */
323 pci_conf[0x59] = 0x04;
324 pci_conf[0x5a] = 0x04; /* KBC/RTC Control*/
325 pci_conf[0x5f] = 0x04;
326 pci_conf[0x77] = 0x10; /* GPIO Control 1/2/3/4 */
328 s->superio_cfg.regs[0xe0] = 0x3c; /* Device ID */
329 s->superio_cfg.regs[0xe2] = 0x03; /* Function select */
330 s->superio_cfg.regs[0xe3] = 0xfc; /* Floppy ctrl base addr */
331 s->superio_cfg.regs[0xe6] = 0xde; /* Parallel port base addr */
332 s->superio_cfg.regs[0xe7] = 0xfe; /* Serial port 1 base addr */
333 s->superio_cfg.regs[0xe8] = 0xbe; /* Serial port 2 base addr */
336 static void vt82c686b_realize(PCIDevice *d, Error **errp)
338 VT82C686BISAState *s = VT82C686B_ISA(d);
344 isa_bus = isa_bus_new(DEVICE(d), get_system_memory(),
345 pci_address_space_io(d), errp);
350 pci_conf = d->config;
351 pci_config_set_prog_interface(pci_conf, 0x0);
354 for (i = 0x00; i < 0xff; i++) {
355 if (i <= 0x03 || (i >= 0x08 && i <= 0x3f)) {
360 memory_region_init_io(&s->superio_cfg.io, OBJECT(d), &superio_cfg_ops,
361 &s->superio_cfg, "superio_cfg", 2);
362 memory_region_set_enabled(&s->superio_cfg.io, false);
364 * The floppy also uses 0x3f0 and 0x3f1.
365 * But we do not emulate a floppy, so just set it here.
367 memory_region_add_subregion(isa_bus->address_space_io, 0x3f0,
371 static void via_class_init(ObjectClass *klass, void *data)
373 DeviceClass *dc = DEVICE_CLASS(klass);
374 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
376 k->realize = vt82c686b_realize;
377 k->config_write = vt82c686b_write_config;
378 k->vendor_id = PCI_VENDOR_ID_VIA;
379 k->device_id = PCI_DEVICE_ID_VIA_ISA_BRIDGE;
380 k->class_id = PCI_CLASS_BRIDGE_ISA;
382 dc->reset = vt82c686b_isa_reset;
383 dc->desc = "ISA bridge";
384 dc->vmsd = &vmstate_via;
386 * Reason: part of VIA VT82C686 southbridge, needs to be wired up,
387 * e.g. by mips_fuloong2e_init()
389 dc->user_creatable = false;
392 static const TypeInfo via_info = {
393 .name = TYPE_VT82C686B_ISA,
394 .parent = TYPE_PCI_DEVICE,
395 .instance_size = sizeof(VT82C686BISAState),
396 .class_init = via_class_init,
397 .interfaces = (InterfaceInfo[]) {
398 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
404 static void vt82c686b_superio_class_init(ObjectClass *klass, void *data)
406 ISASuperIOClass *sc = ISA_SUPERIO_CLASS(klass);
408 sc->serial.count = 2;
409 sc->parallel.count = 1;
411 sc->floppy.count = 1;
414 static const TypeInfo via_superio_info = {
415 .name = TYPE_VT82C686B_SUPERIO,
416 .parent = TYPE_ISA_SUPERIO,
417 .instance_size = sizeof(ISASuperIODevice),
418 .class_size = sizeof(ISASuperIOClass),
419 .class_init = vt82c686b_superio_class_init,
423 static void vt82c686b_register_types(void)
425 type_register_static(&via_pm_info);
426 type_register_static(&via_info);
427 type_register_static(&via_superio_info);
430 type_init(vt82c686b_register_types)