xref: /openbmc/qemu/hw/isa/vt82c686.c (revision 7e01bd80)
147934d0aSPaolo Bonzini /*
247934d0aSPaolo Bonzini  * VT82C686B south bridge support
347934d0aSPaolo Bonzini  *
447934d0aSPaolo Bonzini  * Copyright (c) 2008 yajin (yajin@vm-kernel.org)
547934d0aSPaolo Bonzini  * Copyright (c) 2009 chenming (chenming@rdc.faw.com.cn)
647934d0aSPaolo Bonzini  * Copyright (c) 2010 Huacai Chen (zltjiangshi@gmail.com)
747934d0aSPaolo Bonzini  * This code is licensed under the GNU GPL v2.
847934d0aSPaolo Bonzini  *
947934d0aSPaolo Bonzini  * Contributions after 2012-01-13 are licensed under the terms of the
1047934d0aSPaolo Bonzini  * GNU GPL, version 2 or (at your option) any later version.
11f9f0c9e2SBALATON Zoltan  *
12f9f0c9e2SBALATON Zoltan  * VT8231 south bridge support and general clean up to allow it
13f9f0c9e2SBALATON Zoltan  * Copyright (c) 2018-2020 BALATON Zoltan
1447934d0aSPaolo Bonzini  */
1547934d0aSPaolo Bonzini 
160430891cSPeter Maydell #include "qemu/osdep.h"
1747934d0aSPaolo Bonzini #include "hw/isa/vt82c686.h"
1847934d0aSPaolo Bonzini #include "hw/pci/pci.h"
19a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
209eb6abbfSBernhard Beschow #include "hw/ide/pci.h"
2147934d0aSPaolo Bonzini #include "hw/isa/isa.h"
2298cf824bSPhilippe Mathieu-Daudé #include "hw/isa/superio.h"
233dc31cb8SBALATON Zoltan #include "hw/intc/i8259.h"
243dc31cb8SBALATON Zoltan #include "hw/irq.h"
253dc31cb8SBALATON Zoltan #include "hw/dma/i8257.h"
261a99ddbeSBernhard Beschow #include "hw/usb/hcd-uhci.h"
273dc31cb8SBALATON Zoltan #include "hw/timer/i8254.h"
283dc31cb8SBALATON Zoltan #include "hw/rtc/mc146818rtc.h"
29d6454270SMarkus Armbruster #include "migration/vmstate.h"
3047934d0aSPaolo Bonzini #include "hw/isa/apm.h"
3147934d0aSPaolo Bonzini #include "hw/acpi/acpi.h"
3247934d0aSPaolo Bonzini #include "hw/i2c/pm_smbus.h"
339307d06dSMarkus Armbruster #include "qapi/error.h"
342c4c556eSBALATON Zoltan #include "qemu/log.h"
350b8fa32fSMarkus Armbruster #include "qemu/module.h"
36911629e6SBALATON Zoltan #include "qemu/range.h"
3747934d0aSPaolo Bonzini #include "qemu/timer.h"
38ff413a1fSBALATON Zoltan #include "trace.h"
3947934d0aSPaolo Bonzini 
40e1a69736SBALATON Zoltan #define TYPE_VIA_PM "via-pm"
41e1a69736SBALATON Zoltan OBJECT_DECLARE_SIMPLE_TYPE(ViaPMState, VIA_PM)
4247934d0aSPaolo Bonzini 
43e1a69736SBALATON Zoltan struct ViaPMState {
4447934d0aSPaolo Bonzini     PCIDevice dev;
4547934d0aSPaolo Bonzini     MemoryRegion io;
4647934d0aSPaolo Bonzini     ACPIREGS ar;
4747934d0aSPaolo Bonzini     APMState apm;
4847934d0aSPaolo Bonzini     PMSMBus smb;
49db1015e9SEduardo Habkost };
5047934d0aSPaolo Bonzini 
51e1a69736SBALATON Zoltan static void pm_io_space_update(ViaPMState *s)
5247934d0aSPaolo Bonzini {
533ab1eea6SBALATON Zoltan     uint32_t pmbase = pci_get_long(s->dev.config + 0x48) & 0xff80UL;
5447934d0aSPaolo Bonzini 
5547934d0aSPaolo Bonzini     memory_region_transaction_begin();
563ab1eea6SBALATON Zoltan     memory_region_set_address(&s->io, pmbase);
573ab1eea6SBALATON Zoltan     memory_region_set_enabled(&s->io, s->dev.config[0x41] & BIT(7));
5847934d0aSPaolo Bonzini     memory_region_transaction_commit();
5947934d0aSPaolo Bonzini }
6047934d0aSPaolo Bonzini 
61e1a69736SBALATON Zoltan static void smb_io_space_update(ViaPMState *s)
62911629e6SBALATON Zoltan {
63911629e6SBALATON Zoltan     uint32_t smbase = pci_get_long(s->dev.config + 0x90) & 0xfff0UL;
64911629e6SBALATON Zoltan 
65911629e6SBALATON Zoltan     memory_region_transaction_begin();
66911629e6SBALATON Zoltan     memory_region_set_address(&s->smb.io, smbase);
67911629e6SBALATON Zoltan     memory_region_set_enabled(&s->smb.io, s->dev.config[0xd2] & BIT(0));
68911629e6SBALATON Zoltan     memory_region_transaction_commit();
69911629e6SBALATON Zoltan }
70911629e6SBALATON Zoltan 
7147934d0aSPaolo Bonzini static int vmstate_acpi_post_load(void *opaque, int version_id)
7247934d0aSPaolo Bonzini {
73e1a69736SBALATON Zoltan     ViaPMState *s = opaque;
7447934d0aSPaolo Bonzini 
7547934d0aSPaolo Bonzini     pm_io_space_update(s);
76911629e6SBALATON Zoltan     smb_io_space_update(s);
7747934d0aSPaolo Bonzini     return 0;
7847934d0aSPaolo Bonzini }
7947934d0aSPaolo Bonzini 
8047934d0aSPaolo Bonzini static const VMStateDescription vmstate_acpi = {
8147934d0aSPaolo Bonzini     .name = "vt82c686b_pm",
8247934d0aSPaolo Bonzini     .version_id = 1,
8347934d0aSPaolo Bonzini     .minimum_version_id = 1,
8447934d0aSPaolo Bonzini     .post_load = vmstate_acpi_post_load,
8547934d0aSPaolo Bonzini     .fields = (VMStateField[]) {
86e1a69736SBALATON Zoltan         VMSTATE_PCI_DEVICE(dev, ViaPMState),
87e1a69736SBALATON Zoltan         VMSTATE_UINT16(ar.pm1.evt.sts, ViaPMState),
88e1a69736SBALATON Zoltan         VMSTATE_UINT16(ar.pm1.evt.en, ViaPMState),
89e1a69736SBALATON Zoltan         VMSTATE_UINT16(ar.pm1.cnt.cnt, ViaPMState),
90e1a69736SBALATON Zoltan         VMSTATE_STRUCT(apm, ViaPMState, 0, vmstate_apm, APMState),
91e1a69736SBALATON Zoltan         VMSTATE_TIMER_PTR(ar.tmr.timer, ViaPMState),
92e1a69736SBALATON Zoltan         VMSTATE_INT64(ar.tmr.overflow_time, ViaPMState),
9347934d0aSPaolo Bonzini         VMSTATE_END_OF_LIST()
9447934d0aSPaolo Bonzini     }
9547934d0aSPaolo Bonzini };
9647934d0aSPaolo Bonzini 
9794349bffSBALATON Zoltan static void pm_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int len)
9894349bffSBALATON Zoltan {
99e1a69736SBALATON Zoltan     ViaPMState *s = VIA_PM(d);
100911629e6SBALATON Zoltan 
10194349bffSBALATON Zoltan     trace_via_pm_write(addr, val, len);
10294349bffSBALATON Zoltan     pci_default_write_config(d, addr, val, len);
1033ab1eea6SBALATON Zoltan     if (ranges_overlap(addr, len, 0x48, 4)) {
1043ab1eea6SBALATON Zoltan         uint32_t v = pci_get_long(s->dev.config + 0x48);
1053ab1eea6SBALATON Zoltan         pci_set_long(s->dev.config + 0x48, (v & 0xff80UL) | 1);
1063ab1eea6SBALATON Zoltan     }
1073ab1eea6SBALATON Zoltan     if (range_covers_byte(addr, len, 0x41)) {
1083ab1eea6SBALATON Zoltan         pm_io_space_update(s);
1093ab1eea6SBALATON Zoltan     }
110911629e6SBALATON Zoltan     if (ranges_overlap(addr, len, 0x90, 4)) {
111911629e6SBALATON Zoltan         uint32_t v = pci_get_long(s->dev.config + 0x90);
112911629e6SBALATON Zoltan         pci_set_long(s->dev.config + 0x90, (v & 0xfff0UL) | 1);
113911629e6SBALATON Zoltan     }
114911629e6SBALATON Zoltan     if (range_covers_byte(addr, len, 0xd2)) {
115911629e6SBALATON Zoltan         s->dev.config[0xd2] &= 0xf;
116911629e6SBALATON Zoltan         smb_io_space_update(s);
117911629e6SBALATON Zoltan     }
11894349bffSBALATON Zoltan }
11994349bffSBALATON Zoltan 
12035e360edSBALATON Zoltan static void pm_io_write(void *op, hwaddr addr, uint64_t data, unsigned size)
12135e360edSBALATON Zoltan {
12235e360edSBALATON Zoltan     trace_via_pm_io_write(addr, data, size);
12335e360edSBALATON Zoltan }
12435e360edSBALATON Zoltan 
12535e360edSBALATON Zoltan static uint64_t pm_io_read(void *op, hwaddr addr, unsigned size)
12635e360edSBALATON Zoltan {
12735e360edSBALATON Zoltan     trace_via_pm_io_read(addr, 0, size);
12835e360edSBALATON Zoltan     return 0;
12935e360edSBALATON Zoltan }
13035e360edSBALATON Zoltan 
13135e360edSBALATON Zoltan static const MemoryRegionOps pm_io_ops = {
13235e360edSBALATON Zoltan     .read = pm_io_read,
13335e360edSBALATON Zoltan     .write = pm_io_write,
13435e360edSBALATON Zoltan     .endianness = DEVICE_NATIVE_ENDIAN,
13535e360edSBALATON Zoltan     .impl = {
13635e360edSBALATON Zoltan         .min_access_size = 1,
13735e360edSBALATON Zoltan         .max_access_size = 1,
13835e360edSBALATON Zoltan     },
13935e360edSBALATON Zoltan };
14035e360edSBALATON Zoltan 
141e1a69736SBALATON Zoltan static void pm_update_sci(ViaPMState *s)
14294349bffSBALATON Zoltan {
14394349bffSBALATON Zoltan     int sci_level, pmsts;
14494349bffSBALATON Zoltan 
14594349bffSBALATON Zoltan     pmsts = acpi_pm1_evt_get_sts(&s->ar);
14694349bffSBALATON Zoltan     sci_level = (((pmsts & s->ar.pm1.evt.en) &
14794349bffSBALATON Zoltan                   (ACPI_BITMASK_RT_CLOCK_ENABLE |
14894349bffSBALATON Zoltan                    ACPI_BITMASK_POWER_BUTTON_ENABLE |
14994349bffSBALATON Zoltan                    ACPI_BITMASK_GLOBAL_LOCK_ENABLE |
15094349bffSBALATON Zoltan                    ACPI_BITMASK_TIMER_ENABLE)) != 0);
1510fae92a3SIsaku Yamahata     if (pci_get_byte(s->dev.config + PCI_INTERRUPT_PIN)) {
1520fae92a3SIsaku Yamahata         /*
1530fae92a3SIsaku Yamahata          * FIXME:
1540fae92a3SIsaku Yamahata          * Fix device model that realizes this PM device and remove
1550fae92a3SIsaku Yamahata          * this work around.
1560fae92a3SIsaku Yamahata          * The device model should wire SCI and setup
1570fae92a3SIsaku Yamahata          * PCI_INTERRUPT_PIN properly.
1580fae92a3SIsaku Yamahata          * If PIN# = 0(interrupt pin isn't used), don't raise SCI as
1590fae92a3SIsaku Yamahata          * work around.
1600fae92a3SIsaku Yamahata          */
16194349bffSBALATON Zoltan         pci_set_irq(&s->dev, sci_level);
1620fae92a3SIsaku Yamahata     }
16394349bffSBALATON Zoltan     /* schedule a timer interruption if needed */
16494349bffSBALATON Zoltan     acpi_pm_tmr_update(&s->ar, (s->ar.pm1.evt.en & ACPI_BITMASK_TIMER_ENABLE) &&
16594349bffSBALATON Zoltan                        !(pmsts & ACPI_BITMASK_TIMER_STATUS));
16694349bffSBALATON Zoltan }
16794349bffSBALATON Zoltan 
16894349bffSBALATON Zoltan static void pm_tmr_timer(ACPIREGS *ar)
16994349bffSBALATON Zoltan {
170e1a69736SBALATON Zoltan     ViaPMState *s = container_of(ar, ViaPMState, ar);
17194349bffSBALATON Zoltan     pm_update_sci(s);
17294349bffSBALATON Zoltan }
17394349bffSBALATON Zoltan 
174e1a69736SBALATON Zoltan static void via_pm_reset(DeviceState *d)
175911629e6SBALATON Zoltan {
176e1a69736SBALATON Zoltan     ViaPMState *s = VIA_PM(d);
177911629e6SBALATON Zoltan 
1789af8e529SBALATON Zoltan     memset(s->dev.config + PCI_CONFIG_HEADER_SIZE, 0,
1799af8e529SBALATON Zoltan            PCI_CONFIG_SPACE_SIZE - PCI_CONFIG_HEADER_SIZE);
1809af8e529SBALATON Zoltan     /* Power Management IO base */
1819af8e529SBALATON Zoltan     pci_set_long(s->dev.config + 0x48, 1);
182911629e6SBALATON Zoltan     /* SMBus IO base */
183911629e6SBALATON Zoltan     pci_set_long(s->dev.config + 0x90, 1);
184911629e6SBALATON Zoltan 
18544421c60SIsaku Yamahata     acpi_pm1_evt_reset(&s->ar);
18644421c60SIsaku Yamahata     acpi_pm1_cnt_reset(&s->ar);
18744421c60SIsaku Yamahata     acpi_pm_tmr_reset(&s->ar);
18844421c60SIsaku Yamahata     pm_update_sci(s);
18944421c60SIsaku Yamahata 
1903ab1eea6SBALATON Zoltan     pm_io_space_update(s);
191911629e6SBALATON Zoltan     smb_io_space_update(s);
192911629e6SBALATON Zoltan }
193911629e6SBALATON Zoltan 
194e1a69736SBALATON Zoltan static void via_pm_realize(PCIDevice *dev, Error **errp)
19547934d0aSPaolo Bonzini {
196e1a69736SBALATON Zoltan     ViaPMState *s = VIA_PM(dev);
19747934d0aSPaolo Bonzini 
1983ab1eea6SBALATON Zoltan     pci_set_word(dev->config + PCI_STATUS, PCI_STATUS_FAST_BACK |
19947934d0aSPaolo Bonzini                  PCI_STATUS_DEVSEL_MEDIUM);
20047934d0aSPaolo Bonzini 
201a30c34d2SPhilippe Mathieu-Daudé     pm_smbus_init(DEVICE(s), &s->smb, false);
202911629e6SBALATON Zoltan     memory_region_add_subregion(pci_address_space_io(dev), 0, &s->smb.io);
203911629e6SBALATON Zoltan     memory_region_set_enabled(&s->smb.io, false);
20447934d0aSPaolo Bonzini 
20547934d0aSPaolo Bonzini     apm_init(dev, &s->apm, NULL, s);
20647934d0aSPaolo Bonzini 
207e1a69736SBALATON Zoltan     memory_region_init_io(&s->io, OBJECT(dev), &pm_io_ops, s, "via-pm", 128);
20835e360edSBALATON Zoltan     memory_region_add_subregion(pci_address_space_io(dev), 0, &s->io);
20947934d0aSPaolo Bonzini     memory_region_set_enabled(&s->io, false);
21047934d0aSPaolo Bonzini 
21147934d0aSPaolo Bonzini     acpi_pm_tmr_init(&s->ar, pm_tmr_timer, &s->io);
21247934d0aSPaolo Bonzini     acpi_pm1_evt_init(&s->ar, pm_tmr_timer, &s->io);
2136be8cf56SIsaku Yamahata     acpi_pm1_cnt_init(&s->ar, &s->io, false, false, 2, false);
21447934d0aSPaolo Bonzini }
21547934d0aSPaolo Bonzini 
216e1a69736SBALATON Zoltan typedef struct via_pm_init_info {
217e1a69736SBALATON Zoltan     uint16_t device_id;
218e1a69736SBALATON Zoltan } ViaPMInitInfo;
219e1a69736SBALATON Zoltan 
22047934d0aSPaolo Bonzini static void via_pm_class_init(ObjectClass *klass, void *data)
22147934d0aSPaolo Bonzini {
22247934d0aSPaolo Bonzini     DeviceClass *dc = DEVICE_CLASS(klass);
22347934d0aSPaolo Bonzini     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
224e1a69736SBALATON Zoltan     ViaPMInitInfo *info = data;
22547934d0aSPaolo Bonzini 
226e1a69736SBALATON Zoltan     k->realize = via_pm_realize;
22747934d0aSPaolo Bonzini     k->config_write = pm_write_config;
22847934d0aSPaolo Bonzini     k->vendor_id = PCI_VENDOR_ID_VIA;
229e1a69736SBALATON Zoltan     k->device_id = info->device_id;
23047934d0aSPaolo Bonzini     k->class_id = PCI_CLASS_BRIDGE_OTHER;
23147934d0aSPaolo Bonzini     k->revision = 0x40;
232e1a69736SBALATON Zoltan     dc->reset = via_pm_reset;
233084bf4b4SBALATON Zoltan     /* Reason: part of VIA south bridge, does not exist stand alone */
234084bf4b4SBALATON Zoltan     dc->user_creatable = false;
23547934d0aSPaolo Bonzini     dc->vmsd = &vmstate_acpi;
23647934d0aSPaolo Bonzini }
23747934d0aSPaolo Bonzini 
23847934d0aSPaolo Bonzini static const TypeInfo via_pm_info = {
239e1a69736SBALATON Zoltan     .name          = TYPE_VIA_PM,
24047934d0aSPaolo Bonzini     .parent        = TYPE_PCI_DEVICE,
241e1a69736SBALATON Zoltan     .instance_size = sizeof(ViaPMState),
242e1a69736SBALATON Zoltan     .abstract      = true,
243fd3b02c8SEduardo Habkost     .interfaces = (InterfaceInfo[]) {
244fd3b02c8SEduardo Habkost         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
245fd3b02c8SEduardo Habkost         { },
246fd3b02c8SEduardo Habkost     },
24747934d0aSPaolo Bonzini };
24847934d0aSPaolo Bonzini 
249e1a69736SBALATON Zoltan static const ViaPMInitInfo vt82c686b_pm_init_info = {
250e1a69736SBALATON Zoltan     .device_id = PCI_DEVICE_ID_VIA_82C686B_PM,
251e1a69736SBALATON Zoltan };
252e1a69736SBALATON Zoltan 
253d1053772SBernhard Beschow #define TYPE_VT82C686B_PM "vt82c686b-pm"
254d1053772SBernhard Beschow 
255e1a69736SBALATON Zoltan static const TypeInfo vt82c686b_pm_info = {
256e1a69736SBALATON Zoltan     .name          = TYPE_VT82C686B_PM,
257e1a69736SBALATON Zoltan     .parent        = TYPE_VIA_PM,
258e1a69736SBALATON Zoltan     .class_init    = via_pm_class_init,
259e1a69736SBALATON Zoltan     .class_data    = (void *)&vt82c686b_pm_init_info,
260e1a69736SBALATON Zoltan };
261e1a69736SBALATON Zoltan 
262e1a69736SBALATON Zoltan static const ViaPMInitInfo vt8231_pm_init_info = {
263e1a69736SBALATON Zoltan     .device_id = PCI_DEVICE_ID_VIA_8231_PM,
264e1a69736SBALATON Zoltan };
265e1a69736SBALATON Zoltan 
266d1053772SBernhard Beschow #define TYPE_VT8231_PM "vt8231-pm"
267d1053772SBernhard Beschow 
268e1a69736SBALATON Zoltan static const TypeInfo vt8231_pm_info = {
269e1a69736SBALATON Zoltan     .name          = TYPE_VT8231_PM,
270e1a69736SBALATON Zoltan     .parent        = TYPE_VIA_PM,
271e1a69736SBALATON Zoltan     .class_init    = via_pm_class_init,
272e1a69736SBALATON Zoltan     .class_data    = (void *)&vt8231_pm_init_info,
273e1a69736SBALATON Zoltan };
274e1a69736SBALATON Zoltan 
27594349bffSBALATON Zoltan 
276f028c2deSBALATON Zoltan #define TYPE_VIA_SUPERIO "via-superio"
277f028c2deSBALATON Zoltan OBJECT_DECLARE_SIMPLE_TYPE(ViaSuperIOState, VIA_SUPERIO)
27894349bffSBALATON Zoltan 
279f028c2deSBALATON Zoltan struct ViaSuperIOState {
280f028c2deSBALATON Zoltan     ISASuperIODevice superio;
281f028c2deSBALATON Zoltan     uint8_t regs[0x100];
282f028c2deSBALATON Zoltan     const MemoryRegionOps *io_ops;
283f028c2deSBALATON Zoltan     MemoryRegion io;
284f028c2deSBALATON Zoltan };
285f028c2deSBALATON Zoltan 
286f028c2deSBALATON Zoltan static inline void via_superio_io_enable(ViaSuperIOState *s, bool enable)
28794349bffSBALATON Zoltan {
288f028c2deSBALATON Zoltan     memory_region_set_enabled(&s->io, enable);
289f028c2deSBALATON Zoltan }
290f028c2deSBALATON Zoltan 
291f028c2deSBALATON Zoltan static void via_superio_realize(DeviceState *d, Error **errp)
292f028c2deSBALATON Zoltan {
293f028c2deSBALATON Zoltan     ViaSuperIOState *s = VIA_SUPERIO(d);
294f028c2deSBALATON Zoltan     ISASuperIOClass *ic = ISA_SUPERIO_GET_CLASS(s);
295f028c2deSBALATON Zoltan     Error *local_err = NULL;
296f028c2deSBALATON Zoltan 
297f028c2deSBALATON Zoltan     assert(s->io_ops);
298f028c2deSBALATON Zoltan     ic->parent_realize(d, &local_err);
299f028c2deSBALATON Zoltan     if (local_err) {
300f028c2deSBALATON Zoltan         error_propagate(errp, local_err);
301f028c2deSBALATON Zoltan         return;
302f028c2deSBALATON Zoltan     }
303f028c2deSBALATON Zoltan     memory_region_init_io(&s->io, OBJECT(d), s->io_ops, s, "via-superio", 2);
304f028c2deSBALATON Zoltan     memory_region_set_enabled(&s->io, false);
305f028c2deSBALATON Zoltan     /* The floppy also uses 0x3f0 and 0x3f1 but this seems to work anyway */
306f028c2deSBALATON Zoltan     memory_region_add_subregion(isa_address_space_io(ISA_DEVICE(s)), 0x3f0,
307f028c2deSBALATON Zoltan                                 &s->io);
308f028c2deSBALATON Zoltan }
309f028c2deSBALATON Zoltan 
310f028c2deSBALATON Zoltan static uint64_t via_superio_cfg_read(void *opaque, hwaddr addr, unsigned size)
311f028c2deSBALATON Zoltan {
312f028c2deSBALATON Zoltan     ViaSuperIOState *sc = opaque;
313f028c2deSBALATON Zoltan     uint8_t idx = sc->regs[0];
314f028c2deSBALATON Zoltan     uint8_t val = sc->regs[idx];
315f028c2deSBALATON Zoltan 
316f028c2deSBALATON Zoltan     if (addr == 0) {
317f028c2deSBALATON Zoltan         return idx;
318f028c2deSBALATON Zoltan     }
319f028c2deSBALATON Zoltan     if (addr == 1 && idx == 0) {
320f028c2deSBALATON Zoltan         val = 0; /* reading reg 0 where we store index value */
321f028c2deSBALATON Zoltan     }
322f028c2deSBALATON Zoltan     trace_via_superio_read(idx, val);
323f028c2deSBALATON Zoltan     return val;
324f028c2deSBALATON Zoltan }
325f028c2deSBALATON Zoltan 
326f028c2deSBALATON Zoltan static void via_superio_class_init(ObjectClass *klass, void *data)
327f028c2deSBALATON Zoltan {
328f028c2deSBALATON Zoltan     DeviceClass *dc = DEVICE_CLASS(klass);
329f028c2deSBALATON Zoltan     ISASuperIOClass *sc = ISA_SUPERIO_CLASS(klass);
330f028c2deSBALATON Zoltan 
331f028c2deSBALATON Zoltan     sc->parent_realize = dc->realize;
332f028c2deSBALATON Zoltan     dc->realize = via_superio_realize;
333f028c2deSBALATON Zoltan }
334f028c2deSBALATON Zoltan 
335f028c2deSBALATON Zoltan static const TypeInfo via_superio_info = {
336f028c2deSBALATON Zoltan     .name          = TYPE_VIA_SUPERIO,
337f028c2deSBALATON Zoltan     .parent        = TYPE_ISA_SUPERIO,
338f028c2deSBALATON Zoltan     .instance_size = sizeof(ViaSuperIOState),
339f028c2deSBALATON Zoltan     .class_size    = sizeof(ISASuperIOClass),
340f028c2deSBALATON Zoltan     .class_init    = via_superio_class_init,
341f028c2deSBALATON Zoltan     .abstract      = true,
342f028c2deSBALATON Zoltan };
343f028c2deSBALATON Zoltan 
344f028c2deSBALATON Zoltan #define TYPE_VT82C686B_SUPERIO "vt82c686b-superio"
345f028c2deSBALATON Zoltan 
346f028c2deSBALATON Zoltan static void vt82c686b_superio_cfg_write(void *opaque, hwaddr addr,
347f028c2deSBALATON Zoltan                                         uint64_t data, unsigned size)
348f028c2deSBALATON Zoltan {
349f028c2deSBALATON Zoltan     ViaSuperIOState *sc = opaque;
350c953bf71SBALATON Zoltan     uint8_t idx = sc->regs[0];
35194349bffSBALATON Zoltan 
352cc2b4550SBALATON Zoltan     if (addr == 0) { /* config index register */
353cc2b4550SBALATON Zoltan         sc->regs[0] = data;
3542b98dca9SBALATON Zoltan         return;
3552b98dca9SBALATON Zoltan     }
356cc2b4550SBALATON Zoltan 
357cc2b4550SBALATON Zoltan     /* config data register */
358cc2b4550SBALATON Zoltan     trace_via_superio_write(idx, data);
359c953bf71SBALATON Zoltan     switch (idx) {
36094349bffSBALATON Zoltan     case 0x00 ... 0xdf:
36194349bffSBALATON Zoltan     case 0xe4:
36294349bffSBALATON Zoltan     case 0xe5:
36394349bffSBALATON Zoltan     case 0xe9 ... 0xed:
36494349bffSBALATON Zoltan     case 0xf3:
36594349bffSBALATON Zoltan     case 0xf5:
36694349bffSBALATON Zoltan     case 0xf7:
36794349bffSBALATON Zoltan     case 0xf9 ... 0xfb:
36894349bffSBALATON Zoltan     case 0xfd ... 0xff:
369b7741b77SBALATON Zoltan         /* ignore write to read only registers */
370b7741b77SBALATON Zoltan         return;
37194349bffSBALATON Zoltan     /* case 0xe6 ... 0xe8: Should set base port of parallel and serial */
37294349bffSBALATON Zoltan     default:
3732c4c556eSBALATON Zoltan         qemu_log_mask(LOG_UNIMP,
3742c4c556eSBALATON Zoltan                       "via_superio_cfg: unimplemented register 0x%x\n", idx);
37594349bffSBALATON Zoltan         break;
37694349bffSBALATON Zoltan     }
377cc2b4550SBALATON Zoltan     sc->regs[idx] = data;
37894349bffSBALATON Zoltan }
37994349bffSBALATON Zoltan 
380f028c2deSBALATON Zoltan static const MemoryRegionOps vt82c686b_superio_cfg_ops = {
381f028c2deSBALATON Zoltan     .read = via_superio_cfg_read,
382f028c2deSBALATON Zoltan     .write = vt82c686b_superio_cfg_write,
38394349bffSBALATON Zoltan     .endianness = DEVICE_NATIVE_ENDIAN,
38494349bffSBALATON Zoltan     .impl = {
38594349bffSBALATON Zoltan         .min_access_size = 1,
38694349bffSBALATON Zoltan         .max_access_size = 1,
38794349bffSBALATON Zoltan     },
38894349bffSBALATON Zoltan };
38994349bffSBALATON Zoltan 
390f028c2deSBALATON Zoltan static void vt82c686b_superio_reset(DeviceState *dev)
391f028c2deSBALATON Zoltan {
392f028c2deSBALATON Zoltan     ViaSuperIOState *s = VIA_SUPERIO(dev);
39394349bffSBALATON Zoltan 
394f028c2deSBALATON Zoltan     memset(s->regs, 0, sizeof(s->regs));
395f028c2deSBALATON Zoltan     /* Device ID */
396f028c2deSBALATON Zoltan     vt82c686b_superio_cfg_write(s, 0, 0xe0, 1);
397f028c2deSBALATON Zoltan     vt82c686b_superio_cfg_write(s, 1, 0x3c, 1);
398f028c2deSBALATON Zoltan     /* Function select - all disabled */
399f028c2deSBALATON Zoltan     vt82c686b_superio_cfg_write(s, 0, 0xe2, 1);
400f028c2deSBALATON Zoltan     vt82c686b_superio_cfg_write(s, 1, 0x03, 1);
401f028c2deSBALATON Zoltan     /* Floppy ctrl base addr 0x3f0-7 */
402f028c2deSBALATON Zoltan     vt82c686b_superio_cfg_write(s, 0, 0xe3, 1);
403f028c2deSBALATON Zoltan     vt82c686b_superio_cfg_write(s, 1, 0xfc, 1);
404f028c2deSBALATON Zoltan     /* Parallel port base addr 0x378-f */
405f028c2deSBALATON Zoltan     vt82c686b_superio_cfg_write(s, 0, 0xe6, 1);
406f028c2deSBALATON Zoltan     vt82c686b_superio_cfg_write(s, 1, 0xde, 1);
407f028c2deSBALATON Zoltan     /* Serial port 1 base addr 0x3f8-f */
408f028c2deSBALATON Zoltan     vt82c686b_superio_cfg_write(s, 0, 0xe7, 1);
409f028c2deSBALATON Zoltan     vt82c686b_superio_cfg_write(s, 1, 0xfe, 1);
410f028c2deSBALATON Zoltan     /* Serial port 2 base addr 0x2f8-f */
411f028c2deSBALATON Zoltan     vt82c686b_superio_cfg_write(s, 0, 0xe8, 1);
412f028c2deSBALATON Zoltan     vt82c686b_superio_cfg_write(s, 1, 0xbe, 1);
41394349bffSBALATON Zoltan 
414f028c2deSBALATON Zoltan     vt82c686b_superio_cfg_write(s, 0, 0, 1);
415f028c2deSBALATON Zoltan }
416f028c2deSBALATON Zoltan 
417f028c2deSBALATON Zoltan static void vt82c686b_superio_init(Object *obj)
418f028c2deSBALATON Zoltan {
419f028c2deSBALATON Zoltan     VIA_SUPERIO(obj)->io_ops = &vt82c686b_superio_cfg_ops;
420f028c2deSBALATON Zoltan }
421f028c2deSBALATON Zoltan 
422f028c2deSBALATON Zoltan static void vt82c686b_superio_class_init(ObjectClass *klass, void *data)
423f028c2deSBALATON Zoltan {
424f028c2deSBALATON Zoltan     DeviceClass *dc = DEVICE_CLASS(klass);
425f028c2deSBALATON Zoltan     ISASuperIOClass *sc = ISA_SUPERIO_CLASS(klass);
426f028c2deSBALATON Zoltan 
427f028c2deSBALATON Zoltan     dc->reset = vt82c686b_superio_reset;
428f028c2deSBALATON Zoltan     sc->serial.count = 2;
429f028c2deSBALATON Zoltan     sc->parallel.count = 1;
430f028c2deSBALATON Zoltan     sc->ide.count = 0; /* emulated by via-ide */
431f028c2deSBALATON Zoltan     sc->floppy.count = 1;
432f028c2deSBALATON Zoltan }
433f028c2deSBALATON Zoltan 
434f028c2deSBALATON Zoltan static const TypeInfo vt82c686b_superio_info = {
435f028c2deSBALATON Zoltan     .name          = TYPE_VT82C686B_SUPERIO,
436f028c2deSBALATON Zoltan     .parent        = TYPE_VIA_SUPERIO,
437f028c2deSBALATON Zoltan     .instance_size = sizeof(ViaSuperIOState),
438f028c2deSBALATON Zoltan     .instance_init = vt82c686b_superio_init,
439f028c2deSBALATON Zoltan     .class_size    = sizeof(ISASuperIOClass),
440f028c2deSBALATON Zoltan     .class_init    = vt82c686b_superio_class_init,
441f028c2deSBALATON Zoltan };
442f028c2deSBALATON Zoltan 
44394349bffSBALATON Zoltan 
444ab74864fSBALATON Zoltan #define TYPE_VT8231_SUPERIO "vt8231-superio"
445ab74864fSBALATON Zoltan 
446ab74864fSBALATON Zoltan static void vt8231_superio_cfg_write(void *opaque, hwaddr addr,
447ab74864fSBALATON Zoltan                                      uint64_t data, unsigned size)
448ab74864fSBALATON Zoltan {
449ab74864fSBALATON Zoltan     ViaSuperIOState *sc = opaque;
450ab74864fSBALATON Zoltan     uint8_t idx = sc->regs[0];
451ab74864fSBALATON Zoltan 
452ab74864fSBALATON Zoltan     if (addr == 0) { /* config index register */
453ab74864fSBALATON Zoltan         sc->regs[0] = data;
454ab74864fSBALATON Zoltan         return;
455ab74864fSBALATON Zoltan     }
456ab74864fSBALATON Zoltan 
457ab74864fSBALATON Zoltan     /* config data register */
458ab74864fSBALATON Zoltan     trace_via_superio_write(idx, data);
459ab74864fSBALATON Zoltan     switch (idx) {
460ab74864fSBALATON Zoltan     case 0x00 ... 0xdf:
461ab74864fSBALATON Zoltan     case 0xe7 ... 0xef:
462ab74864fSBALATON Zoltan     case 0xf0 ... 0xf1:
463ab74864fSBALATON Zoltan     case 0xf5:
464ab74864fSBALATON Zoltan     case 0xf8:
465ab74864fSBALATON Zoltan     case 0xfd:
466ab74864fSBALATON Zoltan         /* ignore write to read only registers */
467ab74864fSBALATON Zoltan         return;
468ab74864fSBALATON Zoltan     default:
469ab74864fSBALATON Zoltan         qemu_log_mask(LOG_UNIMP,
470ab74864fSBALATON Zoltan                       "via_superio_cfg: unimplemented register 0x%x\n", idx);
471ab74864fSBALATON Zoltan         break;
472ab74864fSBALATON Zoltan     }
473ab74864fSBALATON Zoltan     sc->regs[idx] = data;
474ab74864fSBALATON Zoltan }
475ab74864fSBALATON Zoltan 
476ab74864fSBALATON Zoltan static const MemoryRegionOps vt8231_superio_cfg_ops = {
477ab74864fSBALATON Zoltan     .read = via_superio_cfg_read,
478ab74864fSBALATON Zoltan     .write = vt8231_superio_cfg_write,
479ab74864fSBALATON Zoltan     .endianness = DEVICE_NATIVE_ENDIAN,
480ab74864fSBALATON Zoltan     .impl = {
481ab74864fSBALATON Zoltan         .min_access_size = 1,
482ab74864fSBALATON Zoltan         .max_access_size = 1,
483ab74864fSBALATON Zoltan     },
484ab74864fSBALATON Zoltan };
485ab74864fSBALATON Zoltan 
486ab74864fSBALATON Zoltan static void vt8231_superio_reset(DeviceState *dev)
487ab74864fSBALATON Zoltan {
488ab74864fSBALATON Zoltan     ViaSuperIOState *s = VIA_SUPERIO(dev);
489ab74864fSBALATON Zoltan 
490ab74864fSBALATON Zoltan     memset(s->regs, 0, sizeof(s->regs));
491ab74864fSBALATON Zoltan     /* Device ID */
492ab74864fSBALATON Zoltan     s->regs[0xf0] = 0x3c;
493ab74864fSBALATON Zoltan     /* Device revision */
494ab74864fSBALATON Zoltan     s->regs[0xf1] = 0x01;
495ab74864fSBALATON Zoltan     /* Function select - all disabled */
496ab74864fSBALATON Zoltan     vt8231_superio_cfg_write(s, 0, 0xf2, 1);
497ab74864fSBALATON Zoltan     vt8231_superio_cfg_write(s, 1, 0x03, 1);
498ab74864fSBALATON Zoltan     /* Serial port base addr */
499ab74864fSBALATON Zoltan     vt8231_superio_cfg_write(s, 0, 0xf4, 1);
500ab74864fSBALATON Zoltan     vt8231_superio_cfg_write(s, 1, 0xfe, 1);
501ab74864fSBALATON Zoltan     /* Parallel port base addr */
502ab74864fSBALATON Zoltan     vt8231_superio_cfg_write(s, 0, 0xf6, 1);
503ab74864fSBALATON Zoltan     vt8231_superio_cfg_write(s, 1, 0xde, 1);
504ab74864fSBALATON Zoltan     /* Floppy ctrl base addr */
505ab74864fSBALATON Zoltan     vt8231_superio_cfg_write(s, 0, 0xf7, 1);
506ab74864fSBALATON Zoltan     vt8231_superio_cfg_write(s, 1, 0xfc, 1);
507ab74864fSBALATON Zoltan 
508ab74864fSBALATON Zoltan     vt8231_superio_cfg_write(s, 0, 0, 1);
509ab74864fSBALATON Zoltan }
510ab74864fSBALATON Zoltan 
511ab74864fSBALATON Zoltan static void vt8231_superio_init(Object *obj)
512ab74864fSBALATON Zoltan {
513ab74864fSBALATON Zoltan     VIA_SUPERIO(obj)->io_ops = &vt8231_superio_cfg_ops;
514ab74864fSBALATON Zoltan }
515ab74864fSBALATON Zoltan 
516ab74864fSBALATON Zoltan static uint16_t vt8231_superio_serial_iobase(ISASuperIODevice *sio,
517ab74864fSBALATON Zoltan                                              uint8_t index)
518ab74864fSBALATON Zoltan {
519ab74864fSBALATON Zoltan         return 0x2f8; /* FIXME: This should be settable via registers f2-f4 */
520ab74864fSBALATON Zoltan }
521ab74864fSBALATON Zoltan 
522ab74864fSBALATON Zoltan static void vt8231_superio_class_init(ObjectClass *klass, void *data)
523ab74864fSBALATON Zoltan {
524ab74864fSBALATON Zoltan     DeviceClass *dc = DEVICE_CLASS(klass);
525ab74864fSBALATON Zoltan     ISASuperIOClass *sc = ISA_SUPERIO_CLASS(klass);
526ab74864fSBALATON Zoltan 
527ab74864fSBALATON Zoltan     dc->reset = vt8231_superio_reset;
528ab74864fSBALATON Zoltan     sc->serial.count = 1;
529ab74864fSBALATON Zoltan     sc->serial.get_iobase = vt8231_superio_serial_iobase;
530ab74864fSBALATON Zoltan     sc->parallel.count = 1;
531ab74864fSBALATON Zoltan     sc->ide.count = 0; /* emulated by via-ide */
532ab74864fSBALATON Zoltan     sc->floppy.count = 1;
533ab74864fSBALATON Zoltan }
534ab74864fSBALATON Zoltan 
535ab74864fSBALATON Zoltan static const TypeInfo vt8231_superio_info = {
536ab74864fSBALATON Zoltan     .name          = TYPE_VT8231_SUPERIO,
537ab74864fSBALATON Zoltan     .parent        = TYPE_VIA_SUPERIO,
538ab74864fSBALATON Zoltan     .instance_size = sizeof(ViaSuperIOState),
539ab74864fSBALATON Zoltan     .instance_init = vt8231_superio_init,
540ab74864fSBALATON Zoltan     .class_size    = sizeof(ISASuperIOClass),
541ab74864fSBALATON Zoltan     .class_init    = vt8231_superio_class_init,
542ab74864fSBALATON Zoltan };
543ab74864fSBALATON Zoltan 
544ab74864fSBALATON Zoltan 
5452e84e107SBALATON Zoltan #define TYPE_VIA_ISA "via-isa"
5462e84e107SBALATON Zoltan OBJECT_DECLARE_SIMPLE_TYPE(ViaISAState, VIA_ISA)
54794349bffSBALATON Zoltan 
5482e84e107SBALATON Zoltan struct ViaISAState {
54994349bffSBALATON Zoltan     PCIDevice dev;
5503dc31cb8SBALATON Zoltan     qemu_irq cpu_intr;
551bb98e0f5SPhilippe Mathieu-Daudé     qemu_irq *isa_irqs_in;
552*7e01bd80SBALATON Zoltan     uint16_t irq_state[ISA_NUM_IRQS];
5538e4022a8SBernhard Beschow     ViaSuperIOState via_sio;
5548df71297SPhilippe Mathieu-Daudé     MC146818RtcState rtc;
5559eb6abbfSBernhard Beschow     PCIIDEState ide;
5561a99ddbeSBernhard Beschow     UHCIState uhci[2];
557d1053772SBernhard Beschow     ViaPMState pm;
558eb604411SBALATON Zoltan     ViaAC97State ac97;
5590a8d405dSBernhard Beschow     PCIDevice mc97;
56094349bffSBALATON Zoltan };
56194349bffSBALATON Zoltan 
5622e84e107SBALATON Zoltan static const VMStateDescription vmstate_via = {
5632e84e107SBALATON Zoltan     .name = "via-isa",
5642e84e107SBALATON Zoltan     .version_id = 1,
5652e84e107SBALATON Zoltan     .minimum_version_id = 1,
5662e84e107SBALATON Zoltan     .fields = (VMStateField[]) {
5672e84e107SBALATON Zoltan         VMSTATE_PCI_DEVICE(dev, ViaISAState),
5682e84e107SBALATON Zoltan         VMSTATE_END_OF_LIST()
5692e84e107SBALATON Zoltan     }
5702e84e107SBALATON Zoltan };
5712e84e107SBALATON Zoltan 
5729eb6abbfSBernhard Beschow static void via_isa_init(Object *obj)
5739eb6abbfSBernhard Beschow {
5749eb6abbfSBernhard Beschow     ViaISAState *s = VIA_ISA(obj);
5759eb6abbfSBernhard Beschow 
5763ecb2e62SBernhard Beschow     object_initialize_child(obj, "rtc", &s->rtc, TYPE_MC146818_RTC);
5779eb6abbfSBernhard Beschow     object_initialize_child(obj, "ide", &s->ide, TYPE_VIA_IDE);
5781a99ddbeSBernhard Beschow     object_initialize_child(obj, "uhci1", &s->uhci[0], TYPE_VT82C686B_USB_UHCI);
5791a99ddbeSBernhard Beschow     object_initialize_child(obj, "uhci2", &s->uhci[1], TYPE_VT82C686B_USB_UHCI);
5800a8d405dSBernhard Beschow     object_initialize_child(obj, "ac97", &s->ac97, TYPE_VIA_AC97);
5810a8d405dSBernhard Beschow     object_initialize_child(obj, "mc97", &s->mc97, TYPE_VIA_MC97);
5829eb6abbfSBernhard Beschow }
5839eb6abbfSBernhard Beschow 
5842e84e107SBALATON Zoltan static const TypeInfo via_isa_info = {
5852e84e107SBALATON Zoltan     .name          = TYPE_VIA_ISA,
5862e84e107SBALATON Zoltan     .parent        = TYPE_PCI_DEVICE,
5872e84e107SBALATON Zoltan     .instance_size = sizeof(ViaISAState),
5889eb6abbfSBernhard Beschow     .instance_init = via_isa_init,
5892e84e107SBALATON Zoltan     .abstract      = true,
5902e84e107SBALATON Zoltan     .interfaces    = (InterfaceInfo[]) {
5912e84e107SBALATON Zoltan         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
5922e84e107SBALATON Zoltan         { },
5932e84e107SBALATON Zoltan     },
59494349bffSBALATON Zoltan };
59594349bffSBALATON Zoltan 
596*7e01bd80SBALATON Zoltan void via_isa_set_irq(PCIDevice *d, int pin, int level)
597*7e01bd80SBALATON Zoltan {
598*7e01bd80SBALATON Zoltan     ViaISAState *s = VIA_ISA(pci_get_function_0(d));
599*7e01bd80SBALATON Zoltan     uint8_t irq = d->config[PCI_INTERRUPT_LINE], max_irq = 15;
600*7e01bd80SBALATON Zoltan     int f = PCI_FUNC(d->devfn);
601*7e01bd80SBALATON Zoltan     uint16_t mask = BIT(f);
602*7e01bd80SBALATON Zoltan 
603*7e01bd80SBALATON Zoltan     switch (f) {
604*7e01bd80SBALATON Zoltan     case 2: /* USB ports 0-1 */
605*7e01bd80SBALATON Zoltan     case 3: /* USB ports 2-3 */
606*7e01bd80SBALATON Zoltan         max_irq = 14;
607*7e01bd80SBALATON Zoltan         break;
608*7e01bd80SBALATON Zoltan     }
609*7e01bd80SBALATON Zoltan 
610*7e01bd80SBALATON Zoltan     /* Keep track of the state of all sources */
611*7e01bd80SBALATON Zoltan     if (level) {
612*7e01bd80SBALATON Zoltan         s->irq_state[0] |= mask;
613*7e01bd80SBALATON Zoltan     } else {
614*7e01bd80SBALATON Zoltan         s->irq_state[0] &= ~mask;
615*7e01bd80SBALATON Zoltan     }
616*7e01bd80SBALATON Zoltan     if (irq == 0 || irq == 0xff) {
617*7e01bd80SBALATON Zoltan         return; /* disabled */
618*7e01bd80SBALATON Zoltan     }
619*7e01bd80SBALATON Zoltan     if (unlikely(irq > max_irq || irq == 2)) {
620*7e01bd80SBALATON Zoltan         qemu_log_mask(LOG_GUEST_ERROR, "Invalid ISA IRQ routing %d for %d",
621*7e01bd80SBALATON Zoltan                       irq, f);
622*7e01bd80SBALATON Zoltan         return;
623*7e01bd80SBALATON Zoltan     }
624*7e01bd80SBALATON Zoltan     /* Record source state at mapped IRQ */
625*7e01bd80SBALATON Zoltan     if (level) {
626*7e01bd80SBALATON Zoltan         s->irq_state[irq] |= mask;
627*7e01bd80SBALATON Zoltan     } else {
628*7e01bd80SBALATON Zoltan         s->irq_state[irq] &= ~mask;
629*7e01bd80SBALATON Zoltan     }
630*7e01bd80SBALATON Zoltan     /* Make sure there are no stuck bits if mapping has changed */
631*7e01bd80SBALATON Zoltan     s->irq_state[irq] &= s->irq_state[0];
632*7e01bd80SBALATON Zoltan     /* ISA IRQ level is the OR of all sources routed to it */
633*7e01bd80SBALATON Zoltan     qemu_set_irq(s->isa_irqs_in[irq], !!s->irq_state[irq]);
634*7e01bd80SBALATON Zoltan }
635*7e01bd80SBALATON Zoltan 
63638200011SBALATON Zoltan static void via_isa_request_i8259_irq(void *opaque, int irq, int level)
63738200011SBALATON Zoltan {
63838200011SBALATON Zoltan     ViaISAState *s = opaque;
63938200011SBALATON Zoltan     qemu_set_irq(s->cpu_intr, level);
64038200011SBALATON Zoltan }
64138200011SBALATON Zoltan 
6422fdadd02SBALATON Zoltan static int via_isa_get_pci_irq(const ViaISAState *s, int irq_num)
6432fdadd02SBALATON Zoltan {
6442fdadd02SBALATON Zoltan     switch (irq_num) {
6452fdadd02SBALATON Zoltan     case 0:
6462fdadd02SBALATON Zoltan         return s->dev.config[0x55] >> 4;
6472fdadd02SBALATON Zoltan     case 1:
6482fdadd02SBALATON Zoltan         return s->dev.config[0x56] & 0xf;
6492fdadd02SBALATON Zoltan     case 2:
6502fdadd02SBALATON Zoltan         return s->dev.config[0x56] >> 4;
6512fdadd02SBALATON Zoltan     case 3:
6522fdadd02SBALATON Zoltan         return s->dev.config[0x57] >> 4;
6532fdadd02SBALATON Zoltan     }
6542fdadd02SBALATON Zoltan     return 0;
6552fdadd02SBALATON Zoltan }
6562fdadd02SBALATON Zoltan 
6572fdadd02SBALATON Zoltan static void via_isa_set_pci_irq(void *opaque, int irq_num, int level)
6582fdadd02SBALATON Zoltan {
6592fdadd02SBALATON Zoltan     ViaISAState *s = opaque;
6602fdadd02SBALATON Zoltan     PCIBus *bus = pci_get_bus(&s->dev);
6612fdadd02SBALATON Zoltan     int i, pic_level, pic_irq = via_isa_get_pci_irq(s, irq_num);
6622fdadd02SBALATON Zoltan 
6632fdadd02SBALATON Zoltan     /* IRQ 0: disabled, IRQ 2,8,13: reserved */
6642fdadd02SBALATON Zoltan     if (!pic_irq) {
6652fdadd02SBALATON Zoltan         return;
6662fdadd02SBALATON Zoltan     }
6672fdadd02SBALATON Zoltan     if (unlikely(pic_irq == 2 || pic_irq == 8 || pic_irq == 13)) {
6682fdadd02SBALATON Zoltan         qemu_log_mask(LOG_GUEST_ERROR, "Invalid ISA IRQ routing");
6692fdadd02SBALATON Zoltan     }
6702fdadd02SBALATON Zoltan 
6712fdadd02SBALATON Zoltan     /* The pic level is the logical OR of all the PCI irqs mapped to it. */
6722fdadd02SBALATON Zoltan     pic_level = 0;
6732fdadd02SBALATON Zoltan     for (i = 0; i < PCI_NUM_PINS; i++) {
6742fdadd02SBALATON Zoltan         if (pic_irq == via_isa_get_pci_irq(s, i)) {
6752fdadd02SBALATON Zoltan             pic_level |= pci_bus_get_irq_level(bus, i);
6762fdadd02SBALATON Zoltan         }
6772fdadd02SBALATON Zoltan     }
6782fdadd02SBALATON Zoltan     /* Now we change the pic irq level according to the via irq mappings. */
6792fdadd02SBALATON Zoltan     qemu_set_irq(s->isa_irqs_in[pic_irq], pic_level);
6802fdadd02SBALATON Zoltan }
6812fdadd02SBALATON Zoltan 
6823a2f166fSBALATON Zoltan static void via_isa_realize(PCIDevice *d, Error **errp)
6833a2f166fSBALATON Zoltan {
6843a2f166fSBALATON Zoltan     ViaISAState *s = VIA_ISA(d);
6853a2f166fSBALATON Zoltan     DeviceState *dev = DEVICE(d);
6869eb6abbfSBernhard Beschow     PCIBus *pci_bus = pci_get_bus(d);
68738200011SBALATON Zoltan     qemu_irq *isa_irq;
68891ba92d1SBernhard Beschow     ISABus *isa_bus;
6893a2f166fSBALATON Zoltan     int i;
6903a2f166fSBALATON Zoltan 
6913a2f166fSBALATON Zoltan     qdev_init_gpio_out(dev, &s->cpu_intr, 1);
69238200011SBALATON Zoltan     isa_irq = qemu_allocate_irqs(via_isa_request_i8259_irq, s, 1);
693dd28cc87SBernhard Beschow     isa_bus = isa_bus_new(dev, pci_address_space(d), pci_address_space_io(d),
694c1561d1dSBernhard Beschow                           errp);
695c1561d1dSBernhard Beschow 
696c1561d1dSBernhard Beschow     if (!isa_bus) {
697c1561d1dSBernhard Beschow         return;
698c1561d1dSBernhard Beschow     }
699c1561d1dSBernhard Beschow 
70038200011SBALATON Zoltan     s->isa_irqs_in = i8259_init(isa_bus, *isa_irq);
7017067887eSPhilippe Mathieu-Daudé     isa_bus_register_input_irqs(isa_bus, s->isa_irqs_in);
70291ba92d1SBernhard Beschow     i8254_pit_init(isa_bus, 0x40, 0, NULL);
70391ba92d1SBernhard Beschow     i8257_dma_init(isa_bus, 0);
7043ecb2e62SBernhard Beschow 
7052fdadd02SBALATON Zoltan     qdev_init_gpio_in_named(dev, via_isa_set_pci_irq, "pirq", PCI_NUM_PINS);
7062fdadd02SBALATON Zoltan 
7073ecb2e62SBernhard Beschow     /* RTC */
7083ecb2e62SBernhard Beschow     qdev_prop_set_int32(DEVICE(&s->rtc), "base_year", 2000);
7093ecb2e62SBernhard Beschow     if (!qdev_realize(DEVICE(&s->rtc), BUS(isa_bus), errp)) {
7103ecb2e62SBernhard Beschow         return;
7113ecb2e62SBernhard Beschow     }
7123ecb2e62SBernhard Beschow     isa_connect_gpio_out(ISA_DEVICE(&s->rtc), 0, s->rtc.isairq);
7133a2f166fSBALATON Zoltan 
7143a2f166fSBALATON Zoltan     for (i = 0; i < PCI_CONFIG_HEADER_SIZE; i++) {
7153a2f166fSBALATON Zoltan         if (i < PCI_COMMAND || i >= PCI_REVISION_ID) {
7163a2f166fSBALATON Zoltan             d->wmask[i] = 0;
7173a2f166fSBALATON Zoltan         }
7183a2f166fSBALATON Zoltan     }
7198e4022a8SBernhard Beschow 
7208e4022a8SBernhard Beschow     /* Super I/O */
72191ba92d1SBernhard Beschow     if (!qdev_realize(DEVICE(&s->via_sio), BUS(isa_bus), errp)) {
7228e4022a8SBernhard Beschow         return;
7238e4022a8SBernhard Beschow     }
7249eb6abbfSBernhard Beschow 
7259eb6abbfSBernhard Beschow     /* Function 1: IDE */
7269eb6abbfSBernhard Beschow     qdev_prop_set_int32(DEVICE(&s->ide), "addr", d->devfn + 1);
7279eb6abbfSBernhard Beschow     if (!qdev_realize(DEVICE(&s->ide), BUS(pci_bus), errp)) {
7289eb6abbfSBernhard Beschow         return;
7299eb6abbfSBernhard Beschow     }
73068eadfa2SBernhard Beschow     for (i = 0; i < 2; i++) {
73168eadfa2SBernhard Beschow         qdev_connect_gpio_out_named(DEVICE(&s->ide), "isa-irq", i,
73268eadfa2SBernhard Beschow                                     s->isa_irqs_in[14 + i]);
73368eadfa2SBernhard Beschow     }
7341a99ddbeSBernhard Beschow 
7351a99ddbeSBernhard Beschow     /* Functions 2-3: USB Ports */
7361a99ddbeSBernhard Beschow     for (i = 0; i < ARRAY_SIZE(s->uhci); i++) {
7371a99ddbeSBernhard Beschow         qdev_prop_set_int32(DEVICE(&s->uhci[i]), "addr", d->devfn + 2 + i);
7381a99ddbeSBernhard Beschow         if (!qdev_realize(DEVICE(&s->uhci[i]), BUS(pci_bus), errp)) {
7391a99ddbeSBernhard Beschow             return;
7401a99ddbeSBernhard Beschow         }
7411a99ddbeSBernhard Beschow     }
742d1053772SBernhard Beschow 
743d1053772SBernhard Beschow     /* Function 4: Power Management */
744d1053772SBernhard Beschow     qdev_prop_set_int32(DEVICE(&s->pm), "addr", d->devfn + 4);
745d1053772SBernhard Beschow     if (!qdev_realize(DEVICE(&s->pm), BUS(pci_bus), errp)) {
746d1053772SBernhard Beschow         return;
747d1053772SBernhard Beschow     }
7480a8d405dSBernhard Beschow 
7490a8d405dSBernhard Beschow     /* Function 5: AC97 Audio */
7500a8d405dSBernhard Beschow     qdev_prop_set_int32(DEVICE(&s->ac97), "addr", d->devfn + 5);
7510a8d405dSBernhard Beschow     if (!qdev_realize(DEVICE(&s->ac97), BUS(pci_bus), errp)) {
7520a8d405dSBernhard Beschow         return;
7530a8d405dSBernhard Beschow     }
7540a8d405dSBernhard Beschow 
7550a8d405dSBernhard Beschow     /* Function 6: MC97 Modem */
7560a8d405dSBernhard Beschow     qdev_prop_set_int32(DEVICE(&s->mc97), "addr", d->devfn + 6);
7570a8d405dSBernhard Beschow     if (!qdev_realize(DEVICE(&s->mc97), BUS(pci_bus), errp)) {
7580a8d405dSBernhard Beschow         return;
7590a8d405dSBernhard Beschow     }
7603a2f166fSBALATON Zoltan }
7613a2f166fSBALATON Zoltan 
7622e84e107SBALATON Zoltan /* TYPE_VT82C686B_ISA */
7632e84e107SBALATON Zoltan 
76494349bffSBALATON Zoltan static void vt82c686b_write_config(PCIDevice *d, uint32_t addr,
76594349bffSBALATON Zoltan                                    uint32_t val, int len)
76694349bffSBALATON Zoltan {
7672e84e107SBALATON Zoltan     ViaISAState *s = VIA_ISA(d);
76894349bffSBALATON Zoltan 
76994349bffSBALATON Zoltan     trace_via_isa_write(addr, val, len);
77094349bffSBALATON Zoltan     pci_default_write_config(d, addr, val, len);
77194349bffSBALATON Zoltan     if (addr == 0x85) {
77294349bffSBALATON Zoltan         /* BIT(1): enable or disable superio config io ports */
7738e4022a8SBernhard Beschow         via_superio_io_enable(&s->via_sio, val & BIT(1));
77494349bffSBALATON Zoltan     }
77594349bffSBALATON Zoltan }
77694349bffSBALATON Zoltan 
77794349bffSBALATON Zoltan static void vt82c686b_isa_reset(DeviceState *dev)
77894349bffSBALATON Zoltan {
7792e84e107SBALATON Zoltan     ViaISAState *s = VIA_ISA(dev);
78094349bffSBALATON Zoltan     uint8_t *pci_conf = s->dev.config;
78194349bffSBALATON Zoltan 
78294349bffSBALATON Zoltan     pci_set_long(pci_conf + PCI_CAPABILITY_LIST, 0x000000c0);
78394349bffSBALATON Zoltan     pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
78494349bffSBALATON Zoltan                  PCI_COMMAND_MASTER | PCI_COMMAND_SPECIAL);
78594349bffSBALATON Zoltan     pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM);
78694349bffSBALATON Zoltan 
78794349bffSBALATON Zoltan     pci_conf[0x48] = 0x01; /* Miscellaneous Control 3 */
78894349bffSBALATON Zoltan     pci_conf[0x4a] = 0x04; /* IDE interrupt Routing */
78994349bffSBALATON Zoltan     pci_conf[0x4f] = 0x03; /* DMA/Master Mem Access Control 3 */
79094349bffSBALATON Zoltan     pci_conf[0x50] = 0x2d; /* PnP DMA Request Control */
79194349bffSBALATON Zoltan     pci_conf[0x59] = 0x04;
79294349bffSBALATON Zoltan     pci_conf[0x5a] = 0x04; /* KBC/RTC Control*/
79394349bffSBALATON Zoltan     pci_conf[0x5f] = 0x04;
79494349bffSBALATON Zoltan     pci_conf[0x77] = 0x10; /* GPIO Control 1/2/3/4 */
79594349bffSBALATON Zoltan }
79694349bffSBALATON Zoltan 
7978e4022a8SBernhard Beschow static void vt82c686b_init(Object *obj)
79847934d0aSPaolo Bonzini {
7998e4022a8SBernhard Beschow     ViaISAState *s = VIA_ISA(obj);
80047934d0aSPaolo Bonzini 
8018e4022a8SBernhard Beschow     object_initialize_child(obj, "sio", &s->via_sio, TYPE_VT82C686B_SUPERIO);
802d1053772SBernhard Beschow     object_initialize_child(obj, "pm", &s->pm, TYPE_VT82C686B_PM);
80347934d0aSPaolo Bonzini }
80447934d0aSPaolo Bonzini 
8052e84e107SBALATON Zoltan static void vt82c686b_class_init(ObjectClass *klass, void *data)
80647934d0aSPaolo Bonzini {
80747934d0aSPaolo Bonzini     DeviceClass *dc = DEVICE_CLASS(klass);
80847934d0aSPaolo Bonzini     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
80947934d0aSPaolo Bonzini 
8108e4022a8SBernhard Beschow     k->realize = via_isa_realize;
81147934d0aSPaolo Bonzini     k->config_write = vt82c686b_write_config;
81247934d0aSPaolo Bonzini     k->vendor_id = PCI_VENDOR_ID_VIA;
8132e84e107SBALATON Zoltan     k->device_id = PCI_DEVICE_ID_VIA_82C686B_ISA;
81447934d0aSPaolo Bonzini     k->class_id = PCI_CLASS_BRIDGE_ISA;
81547934d0aSPaolo Bonzini     k->revision = 0x40;
8169dc1a769SPhilippe Mathieu-Daudé     dc->reset = vt82c686b_isa_reset;
81747934d0aSPaolo Bonzini     dc->desc = "ISA bridge";
81847934d0aSPaolo Bonzini     dc->vmsd = &vmstate_via;
8192e84e107SBALATON Zoltan     /* Reason: part of VIA VT82C686 southbridge, needs to be wired up */
820e90f2a8cSEduardo Habkost     dc->user_creatable = false;
82147934d0aSPaolo Bonzini }
82247934d0aSPaolo Bonzini 
8232e84e107SBALATON Zoltan static const TypeInfo vt82c686b_isa_info = {
8240f798461SBALATON Zoltan     .name          = TYPE_VT82C686B_ISA,
8252e84e107SBALATON Zoltan     .parent        = TYPE_VIA_ISA,
8262e84e107SBALATON Zoltan     .instance_size = sizeof(ViaISAState),
8278e4022a8SBernhard Beschow     .instance_init = vt82c686b_init,
8282e84e107SBALATON Zoltan     .class_init    = vt82c686b_class_init,
82947934d0aSPaolo Bonzini };
83047934d0aSPaolo Bonzini 
831f9f0c9e2SBALATON Zoltan /* TYPE_VT8231_ISA */
83294349bffSBALATON Zoltan 
833f9f0c9e2SBALATON Zoltan static void vt8231_write_config(PCIDevice *d, uint32_t addr,
834f9f0c9e2SBALATON Zoltan                                 uint32_t val, int len)
83598cf824bSPhilippe Mathieu-Daudé {
836f9f0c9e2SBALATON Zoltan     ViaISAState *s = VIA_ISA(d);
83798cf824bSPhilippe Mathieu-Daudé 
838f9f0c9e2SBALATON Zoltan     trace_via_isa_write(addr, val, len);
839f9f0c9e2SBALATON Zoltan     pci_default_write_config(d, addr, val, len);
840f9f0c9e2SBALATON Zoltan     if (addr == 0x50) {
841f9f0c9e2SBALATON Zoltan         /* BIT(2): enable or disable superio config io ports */
8428e4022a8SBernhard Beschow         via_superio_io_enable(&s->via_sio, val & BIT(2));
843f9f0c9e2SBALATON Zoltan     }
84498cf824bSPhilippe Mathieu-Daudé }
84598cf824bSPhilippe Mathieu-Daudé 
846f9f0c9e2SBALATON Zoltan static void vt8231_isa_reset(DeviceState *dev)
847f9f0c9e2SBALATON Zoltan {
848f9f0c9e2SBALATON Zoltan     ViaISAState *s = VIA_ISA(dev);
849f9f0c9e2SBALATON Zoltan     uint8_t *pci_conf = s->dev.config;
850f9f0c9e2SBALATON Zoltan 
851f9f0c9e2SBALATON Zoltan     pci_set_long(pci_conf + PCI_CAPABILITY_LIST, 0x000000c0);
852f9f0c9e2SBALATON Zoltan     pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
853f9f0c9e2SBALATON Zoltan                  PCI_COMMAND_MASTER | PCI_COMMAND_SPECIAL);
854f9f0c9e2SBALATON Zoltan     pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM);
855f9f0c9e2SBALATON Zoltan 
85668eadfa2SBernhard Beschow     pci_conf[0x4c] = 0x04; /* IDE interrupt Routing */
857f9f0c9e2SBALATON Zoltan     pci_conf[0x58] = 0x40; /* Miscellaneous Control 0 */
858f9f0c9e2SBALATON Zoltan     pci_conf[0x67] = 0x08; /* Fast IR Config */
859f9f0c9e2SBALATON Zoltan     pci_conf[0x6b] = 0x01; /* Fast IR I/O Base */
860f9f0c9e2SBALATON Zoltan }
861f9f0c9e2SBALATON Zoltan 
8628e4022a8SBernhard Beschow static void vt8231_init(Object *obj)
863f9f0c9e2SBALATON Zoltan {
8648e4022a8SBernhard Beschow     ViaISAState *s = VIA_ISA(obj);
865f9f0c9e2SBALATON Zoltan 
8668e4022a8SBernhard Beschow     object_initialize_child(obj, "sio", &s->via_sio, TYPE_VT8231_SUPERIO);
867d1053772SBernhard Beschow     object_initialize_child(obj, "pm", &s->pm, TYPE_VT8231_PM);
868f9f0c9e2SBALATON Zoltan }
869f9f0c9e2SBALATON Zoltan 
870f9f0c9e2SBALATON Zoltan static void vt8231_class_init(ObjectClass *klass, void *data)
871f9f0c9e2SBALATON Zoltan {
872f9f0c9e2SBALATON Zoltan     DeviceClass *dc = DEVICE_CLASS(klass);
873f9f0c9e2SBALATON Zoltan     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
874f9f0c9e2SBALATON Zoltan 
8758e4022a8SBernhard Beschow     k->realize = via_isa_realize;
876f9f0c9e2SBALATON Zoltan     k->config_write = vt8231_write_config;
877f9f0c9e2SBALATON Zoltan     k->vendor_id = PCI_VENDOR_ID_VIA;
878f9f0c9e2SBALATON Zoltan     k->device_id = PCI_DEVICE_ID_VIA_8231_ISA;
879f9f0c9e2SBALATON Zoltan     k->class_id = PCI_CLASS_BRIDGE_ISA;
880f9f0c9e2SBALATON Zoltan     k->revision = 0x10;
881f9f0c9e2SBALATON Zoltan     dc->reset = vt8231_isa_reset;
882f9f0c9e2SBALATON Zoltan     dc->desc = "ISA bridge";
883f9f0c9e2SBALATON Zoltan     dc->vmsd = &vmstate_via;
884f9f0c9e2SBALATON Zoltan     /* Reason: part of VIA VT8231 southbridge, needs to be wired up */
885f9f0c9e2SBALATON Zoltan     dc->user_creatable = false;
886f9f0c9e2SBALATON Zoltan }
887f9f0c9e2SBALATON Zoltan 
888f9f0c9e2SBALATON Zoltan static const TypeInfo vt8231_isa_info = {
889f9f0c9e2SBALATON Zoltan     .name          = TYPE_VT8231_ISA,
890f9f0c9e2SBALATON Zoltan     .parent        = TYPE_VIA_ISA,
891f9f0c9e2SBALATON Zoltan     .instance_size = sizeof(ViaISAState),
8928e4022a8SBernhard Beschow     .instance_init = vt8231_init,
893f9f0c9e2SBALATON Zoltan     .class_init    = vt8231_class_init,
89498cf824bSPhilippe Mathieu-Daudé };
89598cf824bSPhilippe Mathieu-Daudé 
89694349bffSBALATON Zoltan 
89747934d0aSPaolo Bonzini static void vt82c686b_register_types(void)
89847934d0aSPaolo Bonzini {
89947934d0aSPaolo Bonzini     type_register_static(&via_pm_info);
900e1a69736SBALATON Zoltan     type_register_static(&vt82c686b_pm_info);
901e1a69736SBALATON Zoltan     type_register_static(&vt8231_pm_info);
90294349bffSBALATON Zoltan     type_register_static(&via_superio_info);
903f028c2deSBALATON Zoltan     type_register_static(&vt82c686b_superio_info);
904ab74864fSBALATON Zoltan     type_register_static(&vt8231_superio_info);
9052e84e107SBALATON Zoltan     type_register_static(&via_isa_info);
9062e84e107SBALATON Zoltan     type_register_static(&vt82c686b_isa_info);
907f9f0c9e2SBALATON Zoltan     type_register_static(&vt8231_isa_info);
90847934d0aSPaolo Bonzini }
90947934d0aSPaolo Bonzini 
91047934d0aSPaolo Bonzini type_init(vt82c686b_register_types)
911