xref: /qemu/hw/isa/vt82c686.c (revision db1015e92e04835c9eb50c29625fe566d1202dbd)
1edf79e66SHuacai Chen /*
2edf79e66SHuacai Chen  * VT82C686B south bridge support
3edf79e66SHuacai Chen  *
4edf79e66SHuacai Chen  * Copyright (c) 2008 yajin (yajin@vm-kernel.org)
5edf79e66SHuacai Chen  * Copyright (c) 2009 chenming (chenming@rdc.faw.com.cn)
6edf79e66SHuacai Chen  * Copyright (c) 2010 Huacai Chen (zltjiangshi@gmail.com)
7edf79e66SHuacai Chen  * This code is licensed under the GNU GPL v2.
86b620ca3SPaolo Bonzini  *
96b620ca3SPaolo Bonzini  * Contributions after 2012-01-13 are licensed under the terms of the
106b620ca3SPaolo Bonzini  * GNU GPL, version 2 or (at your option) any later version.
11edf79e66SHuacai Chen  */
12edf79e66SHuacai Chen 
130430891cSPeter Maydell #include "qemu/osdep.h"
140d09e41aSPaolo Bonzini #include "hw/isa/vt82c686.h"
150d09e41aSPaolo Bonzini #include "hw/i2c/i2c.h"
1683c9f4caSPaolo Bonzini #include "hw/pci/pci.h"
17a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
180d09e41aSPaolo Bonzini #include "hw/isa/isa.h"
1998cf824bSPhilippe Mathieu-Daudé #include "hw/isa/superio.h"
2083c9f4caSPaolo Bonzini #include "hw/sysbus.h"
21d6454270SMarkus Armbruster #include "migration/vmstate.h"
220d09e41aSPaolo Bonzini #include "hw/mips/mips.h"
230d09e41aSPaolo Bonzini #include "hw/isa/apm.h"
240d09e41aSPaolo Bonzini #include "hw/acpi/acpi.h"
250d09e41aSPaolo Bonzini #include "hw/i2c/pm_smbus.h"
269307d06dSMarkus Armbruster #include "qapi/error.h"
270b8fa32fSMarkus Armbruster #include "qemu/module.h"
281de7afc9SPaolo Bonzini #include "qemu/timer.h"
29022c62cbSPaolo Bonzini #include "exec/address-spaces.h"
30*db1015e9SEduardo Habkost #include "qom/object.h"
31edf79e66SHuacai Chen 
32f3db354cSFilip Bozuta /* #define DEBUG_VT82C686B */
33edf79e66SHuacai Chen 
34edf79e66SHuacai Chen #ifdef DEBUG_VT82C686B
35a89f364aSAlistair Francis #define DPRINTF(fmt, ...) fprintf(stderr, "%s: " fmt, __func__, ##__VA_ARGS__)
36edf79e66SHuacai Chen #else
37edf79e66SHuacai Chen #define DPRINTF(fmt, ...)
38edf79e66SHuacai Chen #endif
39edf79e66SHuacai Chen 
40f3db354cSFilip Bozuta typedef struct SuperIOConfig {
419feb8adeSPaolo Bonzini     uint8_t config[0x100];
42edf79e66SHuacai Chen     uint8_t index;
43edf79e66SHuacai Chen     uint8_t data;
44edf79e66SHuacai Chen } SuperIOConfig;
45edf79e66SHuacai Chen 
46*db1015e9SEduardo Habkost struct VT82C686BState {
47edf79e66SHuacai Chen     PCIDevice dev;
48bcc37e24SJan Kiszka     MemoryRegion superio;
49edf79e66SHuacai Chen     SuperIOConfig superio_conf;
50*db1015e9SEduardo Habkost };
51*db1015e9SEduardo Habkost typedef struct VT82C686BState VT82C686BState;
52edf79e66SHuacai Chen 
53417349e6SGonglei #define TYPE_VT82C686B_DEVICE "VT82C686B"
54417349e6SGonglei #define VT82C686B_DEVICE(obj) \
55417349e6SGonglei     OBJECT_CHECK(VT82C686BState, (obj), TYPE_VT82C686B_DEVICE)
56417349e6SGonglei 
57bcc37e24SJan Kiszka static void superio_ioport_writeb(void *opaque, hwaddr addr, uint64_t data,
58bcc37e24SJan Kiszka                                   unsigned size)
59edf79e66SHuacai Chen {
60edf79e66SHuacai Chen     SuperIOConfig *superio_conf = opaque;
61edf79e66SHuacai Chen 
62edf79e66SHuacai Chen     DPRINTF("superio_ioport_writeb  address 0x%x  val 0x%x\n", addr, data);
63edf79e66SHuacai Chen     if (addr == 0x3f0) {
64edf79e66SHuacai Chen         superio_conf->index = data & 0xff;
65edf79e66SHuacai Chen     } else {
66b196d969Szhanghailiang         bool can_write = true;
67edf79e66SHuacai Chen         /* 0x3f1 */
68edf79e66SHuacai Chen         switch (superio_conf->index) {
69edf79e66SHuacai Chen         case 0x00 ... 0xdf:
70edf79e66SHuacai Chen         case 0xe4:
71edf79e66SHuacai Chen         case 0xe5:
72edf79e66SHuacai Chen         case 0xe9 ... 0xed:
73edf79e66SHuacai Chen         case 0xf3:
74edf79e66SHuacai Chen         case 0xf5:
75edf79e66SHuacai Chen         case 0xf7:
76edf79e66SHuacai Chen         case 0xf9 ... 0xfb:
77edf79e66SHuacai Chen         case 0xfd ... 0xff:
78b196d969Szhanghailiang             can_write = false;
79edf79e66SHuacai Chen             break;
80edf79e66SHuacai Chen         case 0xe7:
81edf79e66SHuacai Chen             if ((data & 0xff) != 0xfe) {
82b196d969Szhanghailiang                 DPRINTF("change uart 1 base. unsupported yet\n");
83b196d969Szhanghailiang                 can_write = false;
84edf79e66SHuacai Chen             }
85edf79e66SHuacai Chen             break;
86edf79e66SHuacai Chen         case 0xe8:
87edf79e66SHuacai Chen             if ((data & 0xff) != 0xbe) {
88b196d969Szhanghailiang                 DPRINTF("change uart 2 base. unsupported yet\n");
89b196d969Szhanghailiang                 can_write = false;
90edf79e66SHuacai Chen             }
91edf79e66SHuacai Chen             break;
92edf79e66SHuacai Chen         default:
93b196d969Szhanghailiang             break;
94b196d969Szhanghailiang 
95b196d969Szhanghailiang         }
96b196d969Szhanghailiang         if (can_write) {
97edf79e66SHuacai Chen             superio_conf->config[superio_conf->index] = data & 0xff;
98edf79e66SHuacai Chen         }
99edf79e66SHuacai Chen     }
100edf79e66SHuacai Chen }
101edf79e66SHuacai Chen 
102bcc37e24SJan Kiszka static uint64_t superio_ioport_readb(void *opaque, hwaddr addr, unsigned size)
103edf79e66SHuacai Chen {
104edf79e66SHuacai Chen     SuperIOConfig *superio_conf = opaque;
105edf79e66SHuacai Chen 
106edf79e66SHuacai Chen     DPRINTF("superio_ioport_readb  address 0x%x\n", addr);
107f3db354cSFilip Bozuta     return superio_conf->config[superio_conf->index];
108edf79e66SHuacai Chen }
109edf79e66SHuacai Chen 
110bcc37e24SJan Kiszka static const MemoryRegionOps superio_ops = {
111bcc37e24SJan Kiszka     .read = superio_ioport_readb,
112bcc37e24SJan Kiszka     .write = superio_ioport_writeb,
113bcc37e24SJan Kiszka     .endianness = DEVICE_NATIVE_ENDIAN,
114bcc37e24SJan Kiszka     .impl = {
115bcc37e24SJan Kiszka         .min_access_size = 1,
116bcc37e24SJan Kiszka         .max_access_size = 1,
117bcc37e24SJan Kiszka     },
118bcc37e24SJan Kiszka };
119bcc37e24SJan Kiszka 
1209dc1a769SPhilippe Mathieu-Daudé static void vt82c686b_isa_reset(DeviceState *dev)
121edf79e66SHuacai Chen {
1229dc1a769SPhilippe Mathieu-Daudé     VT82C686BState *vt82c = VT82C686B_DEVICE(dev);
1239dc1a769SPhilippe Mathieu-Daudé     uint8_t *pci_conf = vt82c->dev.config;
124edf79e66SHuacai Chen 
125edf79e66SHuacai Chen     pci_set_long(pci_conf + PCI_CAPABILITY_LIST, 0x000000c0);
126edf79e66SHuacai Chen     pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
127edf79e66SHuacai Chen                  PCI_COMMAND_MASTER | PCI_COMMAND_SPECIAL);
128edf79e66SHuacai Chen     pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM);
129edf79e66SHuacai Chen 
130edf79e66SHuacai Chen     pci_conf[0x48] = 0x01; /* Miscellaneous Control 3 */
131edf79e66SHuacai Chen     pci_conf[0x4a] = 0x04; /* IDE interrupt Routing */
132edf79e66SHuacai Chen     pci_conf[0x4f] = 0x03; /* DMA/Master Mem Access Control 3 */
133edf79e66SHuacai Chen     pci_conf[0x50] = 0x2d; /* PnP DMA Request Control */
134edf79e66SHuacai Chen     pci_conf[0x59] = 0x04;
135edf79e66SHuacai Chen     pci_conf[0x5a] = 0x04; /* KBC/RTC Control*/
136edf79e66SHuacai Chen     pci_conf[0x5f] = 0x04;
137edf79e66SHuacai Chen     pci_conf[0x77] = 0x10; /* GPIO Control 1/2/3/4 */
138edf79e66SHuacai Chen 
139edf79e66SHuacai Chen     vt82c->superio_conf.config[0xe0] = 0x3c;
140edf79e66SHuacai Chen     vt82c->superio_conf.config[0xe2] = 0x03;
141edf79e66SHuacai Chen     vt82c->superio_conf.config[0xe3] = 0xfc;
142edf79e66SHuacai Chen     vt82c->superio_conf.config[0xe6] = 0xde;
143edf79e66SHuacai Chen     vt82c->superio_conf.config[0xe7] = 0xfe;
144edf79e66SHuacai Chen     vt82c->superio_conf.config[0xe8] = 0xbe;
145edf79e66SHuacai Chen }
146edf79e66SHuacai Chen 
147edf79e66SHuacai Chen /* write config pci function0 registers. PCI-ISA bridge */
148edf79e66SHuacai Chen static void vt82c686b_write_config(PCIDevice *d, uint32_t address,
149edf79e66SHuacai Chen                                    uint32_t val, int len)
150edf79e66SHuacai Chen {
151417349e6SGonglei     VT82C686BState *vt686 = VT82C686B_DEVICE(d);
152edf79e66SHuacai Chen 
153edf79e66SHuacai Chen     DPRINTF("vt82c686b_write_config  address 0x%x  val 0x%x len 0x%x\n",
154edf79e66SHuacai Chen            address, val, len);
155edf79e66SHuacai Chen 
156edf79e66SHuacai Chen     pci_default_write_config(d, address, val, len);
157edf79e66SHuacai Chen     if (address == 0x85) {  /* enable or disable super IO configure */
158bcc37e24SJan Kiszka         memory_region_set_enabled(&vt686->superio, val & 0x2);
159edf79e66SHuacai Chen     }
160edf79e66SHuacai Chen }
161edf79e66SHuacai Chen 
162edf79e66SHuacai Chen #define ACPI_DBG_IO_ADDR  0xb044
163edf79e66SHuacai Chen 
164*db1015e9SEduardo Habkost struct VT686PMState {
165edf79e66SHuacai Chen     PCIDevice dev;
166a2902821SGerd Hoffmann     MemoryRegion io;
167355bf2e5SGerd Hoffmann     ACPIREGS ar;
168edf79e66SHuacai Chen     APMState apm;
169edf79e66SHuacai Chen     PMSMBus smb;
170edf79e66SHuacai Chen     uint32_t smb_io_base;
171*db1015e9SEduardo Habkost };
172*db1015e9SEduardo Habkost typedef struct VT686PMState VT686PMState;
173edf79e66SHuacai Chen 
174*db1015e9SEduardo Habkost struct VT686AC97State {
175edf79e66SHuacai Chen     PCIDevice dev;
176*db1015e9SEduardo Habkost };
177*db1015e9SEduardo Habkost typedef struct VT686AC97State VT686AC97State;
178edf79e66SHuacai Chen 
179*db1015e9SEduardo Habkost struct VT686MC97State {
180edf79e66SHuacai Chen     PCIDevice dev;
181*db1015e9SEduardo Habkost };
182*db1015e9SEduardo Habkost typedef struct VT686MC97State VT686MC97State;
183edf79e66SHuacai Chen 
184417349e6SGonglei #define TYPE_VT82C686B_PM_DEVICE "VT82C686B_PM"
185417349e6SGonglei #define VT82C686B_PM_DEVICE(obj) \
186417349e6SGonglei     OBJECT_CHECK(VT686PMState, (obj), TYPE_VT82C686B_PM_DEVICE)
187417349e6SGonglei 
188417349e6SGonglei #define TYPE_VT82C686B_MC97_DEVICE "VT82C686B_MC97"
189417349e6SGonglei #define VT82C686B_MC97_DEVICE(obj) \
190417349e6SGonglei     OBJECT_CHECK(VT686MC97State, (obj), TYPE_VT82C686B_MC97_DEVICE)
191417349e6SGonglei 
192417349e6SGonglei #define TYPE_VT82C686B_AC97_DEVICE "VT82C686B_AC97"
193417349e6SGonglei #define VT82C686B_AC97_DEVICE(obj) \
194417349e6SGonglei     OBJECT_CHECK(VT686AC97State, (obj), TYPE_VT82C686B_AC97_DEVICE)
195417349e6SGonglei 
196edf79e66SHuacai Chen static void pm_update_sci(VT686PMState *s)
197edf79e66SHuacai Chen {
198edf79e66SHuacai Chen     int sci_level, pmsts;
199edf79e66SHuacai Chen 
2002886be1bSGerd Hoffmann     pmsts = acpi_pm1_evt_get_sts(&s->ar);
201355bf2e5SGerd Hoffmann     sci_level = (((pmsts & s->ar.pm1.evt.en) &
20204dc308fSIsaku Yamahata                   (ACPI_BITMASK_RT_CLOCK_ENABLE |
20304dc308fSIsaku Yamahata                    ACPI_BITMASK_POWER_BUTTON_ENABLE |
20404dc308fSIsaku Yamahata                    ACPI_BITMASK_GLOBAL_LOCK_ENABLE |
20504dc308fSIsaku Yamahata                    ACPI_BITMASK_TIMER_ENABLE)) != 0);
2069e64f8a3SMarcel Apfelbaum     pci_set_irq(&s->dev, sci_level);
207edf79e66SHuacai Chen     /* schedule a timer interruption if needed */
208355bf2e5SGerd Hoffmann     acpi_pm_tmr_update(&s->ar, (s->ar.pm1.evt.en & ACPI_BITMASK_TIMER_ENABLE) &&
209a54d41a8SIsaku Yamahata                        !(pmsts & ACPI_BITMASK_TIMER_STATUS));
210edf79e66SHuacai Chen }
211edf79e66SHuacai Chen 
212355bf2e5SGerd Hoffmann static void pm_tmr_timer(ACPIREGS *ar)
213edf79e66SHuacai Chen {
214355bf2e5SGerd Hoffmann     VT686PMState *s = container_of(ar, VT686PMState, ar);
215edf79e66SHuacai Chen     pm_update_sci(s);
216edf79e66SHuacai Chen }
217edf79e66SHuacai Chen 
218edf79e66SHuacai Chen static void pm_io_space_update(VT686PMState *s)
219edf79e66SHuacai Chen {
220edf79e66SHuacai Chen     uint32_t pm_io_base;
221edf79e66SHuacai Chen 
222edf79e66SHuacai Chen     pm_io_base = pci_get_long(s->dev.config + 0x40);
223edf79e66SHuacai Chen     pm_io_base &= 0xffc0;
224edf79e66SHuacai Chen 
225a2902821SGerd Hoffmann     memory_region_transaction_begin();
226a2902821SGerd Hoffmann     memory_region_set_enabled(&s->io, s->dev.config[0x80] & 1);
227a2902821SGerd Hoffmann     memory_region_set_address(&s->io, pm_io_base);
228a2902821SGerd Hoffmann     memory_region_transaction_commit();
229edf79e66SHuacai Chen }
230edf79e66SHuacai Chen 
231edf79e66SHuacai Chen static void pm_write_config(PCIDevice *d,
232edf79e66SHuacai Chen                             uint32_t address, uint32_t val, int len)
233edf79e66SHuacai Chen {
234edf79e66SHuacai Chen     DPRINTF("pm_write_config  address 0x%x  val 0x%x len 0x%x\n",
235edf79e66SHuacai Chen            address, val, len);
236edf79e66SHuacai Chen     pci_default_write_config(d, address, val, len);
237edf79e66SHuacai Chen }
238edf79e66SHuacai Chen 
239edf79e66SHuacai Chen static int vmstate_acpi_post_load(void *opaque, int version_id)
240edf79e66SHuacai Chen {
241edf79e66SHuacai Chen     VT686PMState *s = opaque;
242edf79e66SHuacai Chen 
243edf79e66SHuacai Chen     pm_io_space_update(s);
244edf79e66SHuacai Chen     return 0;
245edf79e66SHuacai Chen }
246edf79e66SHuacai Chen 
247edf79e66SHuacai Chen static const VMStateDescription vmstate_acpi = {
248edf79e66SHuacai Chen     .name = "vt82c686b_pm",
249edf79e66SHuacai Chen     .version_id = 1,
250edf79e66SHuacai Chen     .minimum_version_id = 1,
251edf79e66SHuacai Chen     .post_load = vmstate_acpi_post_load,
252edf79e66SHuacai Chen     .fields = (VMStateField[]) {
253edf79e66SHuacai Chen         VMSTATE_PCI_DEVICE(dev, VT686PMState),
254355bf2e5SGerd Hoffmann         VMSTATE_UINT16(ar.pm1.evt.sts, VT686PMState),
255355bf2e5SGerd Hoffmann         VMSTATE_UINT16(ar.pm1.evt.en, VT686PMState),
256355bf2e5SGerd Hoffmann         VMSTATE_UINT16(ar.pm1.cnt.cnt, VT686PMState),
257edf79e66SHuacai Chen         VMSTATE_STRUCT(apm, VT686PMState, 0, vmstate_apm, APMState),
258e720677eSPaolo Bonzini         VMSTATE_TIMER_PTR(ar.tmr.timer, VT686PMState),
259355bf2e5SGerd Hoffmann         VMSTATE_INT64(ar.tmr.overflow_time, VT686PMState),
260edf79e66SHuacai Chen         VMSTATE_END_OF_LIST()
261edf79e66SHuacai Chen     }
262edf79e66SHuacai Chen };
263edf79e66SHuacai Chen 
264edf79e66SHuacai Chen /*
265edf79e66SHuacai Chen  * TODO: vt82c686b_ac97_init() and vt82c686b_mc97_init()
266edf79e66SHuacai Chen  * just register a PCI device now, functionalities will be implemented later.
267edf79e66SHuacai Chen  */
268edf79e66SHuacai Chen 
2699af21dbeSMarkus Armbruster static void vt82c686b_ac97_realize(PCIDevice *dev, Error **errp)
270edf79e66SHuacai Chen {
271417349e6SGonglei     VT686AC97State *s = VT82C686B_AC97_DEVICE(dev);
272edf79e66SHuacai Chen     uint8_t *pci_conf = s->dev.config;
273edf79e66SHuacai Chen 
274edf79e66SHuacai Chen     pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_INVALIDATE |
275edf79e66SHuacai Chen                  PCI_COMMAND_PARITY);
276edf79e66SHuacai Chen     pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_CAP_LIST |
277edf79e66SHuacai Chen                  PCI_STATUS_DEVSEL_MEDIUM);
278edf79e66SHuacai Chen     pci_set_long(pci_conf + PCI_INTERRUPT_PIN, 0x03);
279edf79e66SHuacai Chen }
280edf79e66SHuacai Chen 
281edf79e66SHuacai Chen void vt82c686b_ac97_init(PCIBus *bus, int devfn)
282edf79e66SHuacai Chen {
283edf79e66SHuacai Chen     PCIDevice *dev;
284edf79e66SHuacai Chen 
2859307d06dSMarkus Armbruster     dev = pci_new(devfn, TYPE_VT82C686B_AC97_DEVICE);
2869307d06dSMarkus Armbruster     pci_realize_and_unref(dev, bus, &error_fatal);
287edf79e66SHuacai Chen }
288edf79e66SHuacai Chen 
28940021f08SAnthony Liguori static void via_ac97_class_init(ObjectClass *klass, void *data)
29040021f08SAnthony Liguori {
29139bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
29240021f08SAnthony Liguori     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
29340021f08SAnthony Liguori 
2949af21dbeSMarkus Armbruster     k->realize = vt82c686b_ac97_realize;
29540021f08SAnthony Liguori     k->vendor_id = PCI_VENDOR_ID_VIA;
29640021f08SAnthony Liguori     k->device_id = PCI_DEVICE_ID_VIA_AC97;
29740021f08SAnthony Liguori     k->revision = 0x50;
29840021f08SAnthony Liguori     k->class_id = PCI_CLASS_MULTIMEDIA_AUDIO;
299125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
30039bffca2SAnthony Liguori     dc->desc = "AC97";
30140021f08SAnthony Liguori }
30240021f08SAnthony Liguori 
3038c43a6f0SAndreas Färber static const TypeInfo via_ac97_info = {
304417349e6SGonglei     .name          = TYPE_VT82C686B_AC97_DEVICE,
30539bffca2SAnthony Liguori     .parent        = TYPE_PCI_DEVICE,
30639bffca2SAnthony Liguori     .instance_size = sizeof(VT686AC97State),
30740021f08SAnthony Liguori     .class_init    = via_ac97_class_init,
308fd3b02c8SEduardo Habkost     .interfaces = (InterfaceInfo[]) {
309fd3b02c8SEduardo Habkost         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
310fd3b02c8SEduardo Habkost         { },
311fd3b02c8SEduardo Habkost     },
312edf79e66SHuacai Chen };
313edf79e66SHuacai Chen 
3149af21dbeSMarkus Armbruster static void vt82c686b_mc97_realize(PCIDevice *dev, Error **errp)
315edf79e66SHuacai Chen {
316417349e6SGonglei     VT686MC97State *s = VT82C686B_MC97_DEVICE(dev);
317edf79e66SHuacai Chen     uint8_t *pci_conf = s->dev.config;
318edf79e66SHuacai Chen 
319edf79e66SHuacai Chen     pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_INVALIDATE |
320edf79e66SHuacai Chen                  PCI_COMMAND_VGA_PALETTE);
321edf79e66SHuacai Chen     pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM);
322edf79e66SHuacai Chen     pci_set_long(pci_conf + PCI_INTERRUPT_PIN, 0x03);
323edf79e66SHuacai Chen }
324edf79e66SHuacai Chen 
325edf79e66SHuacai Chen void vt82c686b_mc97_init(PCIBus *bus, int devfn)
326edf79e66SHuacai Chen {
327edf79e66SHuacai Chen     PCIDevice *dev;
328edf79e66SHuacai Chen 
3299307d06dSMarkus Armbruster     dev = pci_new(devfn, TYPE_VT82C686B_MC97_DEVICE);
3309307d06dSMarkus Armbruster     pci_realize_and_unref(dev, bus, &error_fatal);
331edf79e66SHuacai Chen }
332edf79e66SHuacai Chen 
33340021f08SAnthony Liguori static void via_mc97_class_init(ObjectClass *klass, void *data)
33440021f08SAnthony Liguori {
33539bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
33640021f08SAnthony Liguori     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
33740021f08SAnthony Liguori 
3389af21dbeSMarkus Armbruster     k->realize = vt82c686b_mc97_realize;
33940021f08SAnthony Liguori     k->vendor_id = PCI_VENDOR_ID_VIA;
34040021f08SAnthony Liguori     k->device_id = PCI_DEVICE_ID_VIA_MC97;
34140021f08SAnthony Liguori     k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
34240021f08SAnthony Liguori     k->revision = 0x30;
343125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
34439bffca2SAnthony Liguori     dc->desc = "MC97";
34540021f08SAnthony Liguori }
34640021f08SAnthony Liguori 
3478c43a6f0SAndreas Färber static const TypeInfo via_mc97_info = {
348417349e6SGonglei     .name          = TYPE_VT82C686B_MC97_DEVICE,
34939bffca2SAnthony Liguori     .parent        = TYPE_PCI_DEVICE,
35039bffca2SAnthony Liguori     .instance_size = sizeof(VT686MC97State),
35140021f08SAnthony Liguori     .class_init    = via_mc97_class_init,
352fd3b02c8SEduardo Habkost     .interfaces = (InterfaceInfo[]) {
353fd3b02c8SEduardo Habkost         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
354fd3b02c8SEduardo Habkost         { },
355fd3b02c8SEduardo Habkost     },
356edf79e66SHuacai Chen };
357edf79e66SHuacai Chen 
358edf79e66SHuacai Chen /* vt82c686 pm init */
3599af21dbeSMarkus Armbruster static void vt82c686b_pm_realize(PCIDevice *dev, Error **errp)
360edf79e66SHuacai Chen {
361417349e6SGonglei     VT686PMState *s = VT82C686B_PM_DEVICE(dev);
362edf79e66SHuacai Chen     uint8_t *pci_conf;
363edf79e66SHuacai Chen 
364edf79e66SHuacai Chen     pci_conf = s->dev.config;
365edf79e66SHuacai Chen     pci_set_word(pci_conf + PCI_COMMAND, 0);
366edf79e66SHuacai Chen     pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_FAST_BACK |
367edf79e66SHuacai Chen                  PCI_STATUS_DEVSEL_MEDIUM);
368edf79e66SHuacai Chen 
369edf79e66SHuacai Chen     /* 0x48-0x4B is Power Management I/O Base */
370edf79e66SHuacai Chen     pci_set_long(pci_conf + 0x48, 0x00000001);
371edf79e66SHuacai Chen 
372edf79e66SHuacai Chen     /* SMB ports:0xeee0~0xeeef */
373edf79e66SHuacai Chen     s->smb_io_base = ((s->smb_io_base & 0xfff0) + 0x0);
374edf79e66SHuacai Chen     pci_conf[0x90] = s->smb_io_base | 1;
375edf79e66SHuacai Chen     pci_conf[0x91] = s->smb_io_base >> 8;
376edf79e66SHuacai Chen     pci_conf[0xd2] = 0x90;
377a30c34d2SPhilippe Mathieu-Daudé     pm_smbus_init(DEVICE(s), &s->smb, false);
378798512e5SGerd Hoffmann     memory_region_add_subregion(get_system_io(), s->smb_io_base, &s->smb.io);
379edf79e66SHuacai Chen 
38042d8a3cfSJulien Grall     apm_init(dev, &s->apm, NULL, s);
381edf79e66SHuacai Chen 
3821437c94bSPaolo Bonzini     memory_region_init(&s->io, OBJECT(dev), "vt82c686-pm", 64);
383a2902821SGerd Hoffmann     memory_region_set_enabled(&s->io, false);
384a2902821SGerd Hoffmann     memory_region_add_subregion(get_system_io(), 0, &s->io);
385edf79e66SHuacai Chen 
38677d58b1eSGerd Hoffmann     acpi_pm_tmr_init(&s->ar, pm_tmr_timer, &s->io);
387b5a7c024SGerd Hoffmann     acpi_pm1_evt_init(&s->ar, pm_tmr_timer, &s->io);
3889a10bbb4SLaszlo Ersek     acpi_pm1_cnt_init(&s->ar, &s->io, false, false, 2);
389edf79e66SHuacai Chen }
390edf79e66SHuacai Chen 
391a5c82852SAndreas Färber I2CBus *vt82c686b_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base,
392edf79e66SHuacai Chen                           qemu_irq sci_irq)
393edf79e66SHuacai Chen {
394edf79e66SHuacai Chen     PCIDevice *dev;
395edf79e66SHuacai Chen     VT686PMState *s;
396edf79e66SHuacai Chen 
3979307d06dSMarkus Armbruster     dev = pci_new(devfn, TYPE_VT82C686B_PM_DEVICE);
398edf79e66SHuacai Chen     qdev_prop_set_uint32(&dev->qdev, "smb_io_base", smb_io_base);
399edf79e66SHuacai Chen 
400417349e6SGonglei     s = VT82C686B_PM_DEVICE(dev);
401edf79e66SHuacai Chen 
4029307d06dSMarkus Armbruster     pci_realize_and_unref(dev, bus, &error_fatal);
403edf79e66SHuacai Chen 
404edf79e66SHuacai Chen     return s->smb.smbus;
405edf79e66SHuacai Chen }
406edf79e66SHuacai Chen 
40740021f08SAnthony Liguori static Property via_pm_properties[] = {
408edf79e66SHuacai Chen     DEFINE_PROP_UINT32("smb_io_base", VT686PMState, smb_io_base, 0),
409edf79e66SHuacai Chen     DEFINE_PROP_END_OF_LIST(),
41040021f08SAnthony Liguori };
41140021f08SAnthony Liguori 
41240021f08SAnthony Liguori static void via_pm_class_init(ObjectClass *klass, void *data)
41340021f08SAnthony Liguori {
41439bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
41540021f08SAnthony Liguori     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
41640021f08SAnthony Liguori 
4179af21dbeSMarkus Armbruster     k->realize = vt82c686b_pm_realize;
41840021f08SAnthony Liguori     k->config_write = pm_write_config;
41940021f08SAnthony Liguori     k->vendor_id = PCI_VENDOR_ID_VIA;
42040021f08SAnthony Liguori     k->device_id = PCI_DEVICE_ID_VIA_ACPI;
42140021f08SAnthony Liguori     k->class_id = PCI_CLASS_BRIDGE_OTHER;
42240021f08SAnthony Liguori     k->revision = 0x40;
42339bffca2SAnthony Liguori     dc->desc = "PM";
42439bffca2SAnthony Liguori     dc->vmsd = &vmstate_acpi;
425125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
4264f67d30bSMarc-André Lureau     device_class_set_props(dc, via_pm_properties);
427edf79e66SHuacai Chen }
42840021f08SAnthony Liguori 
4298c43a6f0SAndreas Färber static const TypeInfo via_pm_info = {
430417349e6SGonglei     .name          = TYPE_VT82C686B_PM_DEVICE,
43139bffca2SAnthony Liguori     .parent        = TYPE_PCI_DEVICE,
43239bffca2SAnthony Liguori     .instance_size = sizeof(VT686PMState),
43340021f08SAnthony Liguori     .class_init    = via_pm_class_init,
434fd3b02c8SEduardo Habkost     .interfaces = (InterfaceInfo[]) {
435fd3b02c8SEduardo Habkost         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
436fd3b02c8SEduardo Habkost         { },
437fd3b02c8SEduardo Habkost     },
438edf79e66SHuacai Chen };
439edf79e66SHuacai Chen 
440edf79e66SHuacai Chen static const VMStateDescription vmstate_via = {
441edf79e66SHuacai Chen     .name = "vt82c686b",
442edf79e66SHuacai Chen     .version_id = 1,
443edf79e66SHuacai Chen     .minimum_version_id = 1,
444edf79e66SHuacai Chen     .fields = (VMStateField[]) {
445edf79e66SHuacai Chen         VMSTATE_PCI_DEVICE(dev, VT82C686BState),
446edf79e66SHuacai Chen         VMSTATE_END_OF_LIST()
447edf79e66SHuacai Chen     }
448edf79e66SHuacai Chen };
449edf79e66SHuacai Chen 
450edf79e66SHuacai Chen /* init the PCI-to-ISA bridge */
4519af21dbeSMarkus Armbruster static void vt82c686b_realize(PCIDevice *d, Error **errp)
452edf79e66SHuacai Chen {
453417349e6SGonglei     VT82C686BState *vt82c = VT82C686B_DEVICE(d);
454edf79e66SHuacai Chen     uint8_t *pci_conf;
455bcc37e24SJan Kiszka     ISABus *isa_bus;
456edf79e66SHuacai Chen     uint8_t *wmask;
457edf79e66SHuacai Chen     int i;
458edf79e66SHuacai Chen 
459bb2ed009SHervé Poussineau     isa_bus = isa_bus_new(DEVICE(d), get_system_memory(),
460d10e5432SMarkus Armbruster                           pci_address_space_io(d), errp);
461d10e5432SMarkus Armbruster     if (!isa_bus) {
462d10e5432SMarkus Armbruster         return;
463d10e5432SMarkus Armbruster     }
464edf79e66SHuacai Chen 
465edf79e66SHuacai Chen     pci_conf = d->config;
466edf79e66SHuacai Chen     pci_config_set_prog_interface(pci_conf, 0x0);
467edf79e66SHuacai Chen 
468edf79e66SHuacai Chen     wmask = d->wmask;
469edf79e66SHuacai Chen     for (i = 0x00; i < 0xff; i++) {
470edf79e66SHuacai Chen         if (i <= 0x03 || (i >= 0x08 && i <= 0x3f)) {
471edf79e66SHuacai Chen             wmask[i] = 0x00;
472edf79e66SHuacai Chen         }
473edf79e66SHuacai Chen     }
474edf79e66SHuacai Chen 
475db10ca90SPaolo Bonzini     memory_region_init_io(&vt82c->superio, OBJECT(d), &superio_ops,
4762c9b15caSPaolo Bonzini                           &vt82c->superio_conf, "superio", 2);
477bcc37e24SJan Kiszka     memory_region_set_enabled(&vt82c->superio, false);
478f3db354cSFilip Bozuta     /*
479f3db354cSFilip Bozuta      * The floppy also uses 0x3f0 and 0x3f1.
480f3db354cSFilip Bozuta      * But we do not emulate a floppy, so just set it here.
481f3db354cSFilip Bozuta      */
482bcc37e24SJan Kiszka     memory_region_add_subregion(isa_bus->address_space_io, 0x3f0,
483bcc37e24SJan Kiszka                                 &vt82c->superio);
484edf79e66SHuacai Chen }
485edf79e66SHuacai Chen 
486728d8910SPhilippe Mathieu-Daudé ISABus *vt82c686b_isa_init(PCIBus *bus, int devfn)
487edf79e66SHuacai Chen {
488edf79e66SHuacai Chen     PCIDevice *d;
489edf79e66SHuacai Chen 
490417349e6SGonglei     d = pci_create_simple_multifunction(bus, devfn, true,
491417349e6SGonglei                                         TYPE_VT82C686B_DEVICE);
492edf79e66SHuacai Chen 
4932ae0e48dSAndreas Färber     return ISA_BUS(qdev_get_child_bus(DEVICE(d), "isa.0"));
494edf79e66SHuacai Chen }
495edf79e66SHuacai Chen 
49640021f08SAnthony Liguori static void via_class_init(ObjectClass *klass, void *data)
49740021f08SAnthony Liguori {
49839bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
49940021f08SAnthony Liguori     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
50040021f08SAnthony Liguori 
5019af21dbeSMarkus Armbruster     k->realize = vt82c686b_realize;
50240021f08SAnthony Liguori     k->config_write = vt82c686b_write_config;
50340021f08SAnthony Liguori     k->vendor_id = PCI_VENDOR_ID_VIA;
50440021f08SAnthony Liguori     k->device_id = PCI_DEVICE_ID_VIA_ISA_BRIDGE;
50540021f08SAnthony Liguori     k->class_id = PCI_CLASS_BRIDGE_ISA;
50640021f08SAnthony Liguori     k->revision = 0x40;
5079dc1a769SPhilippe Mathieu-Daudé     dc->reset = vt82c686b_isa_reset;
50839bffca2SAnthony Liguori     dc->desc = "ISA bridge";
50939bffca2SAnthony Liguori     dc->vmsd = &vmstate_via;
51004916ee9SMarkus Armbruster     /*
51104916ee9SMarkus Armbruster      * Reason: part of VIA VT82C686 southbridge, needs to be wired up,
512c3a09ff6SPhilippe Mathieu-Daudé      * e.g. by mips_fuloong2e_init()
51304916ee9SMarkus Armbruster      */
514e90f2a8cSEduardo Habkost     dc->user_creatable = false;
51540021f08SAnthony Liguori }
51640021f08SAnthony Liguori 
5178c43a6f0SAndreas Färber static const TypeInfo via_info = {
518417349e6SGonglei     .name          = TYPE_VT82C686B_DEVICE,
51939bffca2SAnthony Liguori     .parent        = TYPE_PCI_DEVICE,
52039bffca2SAnthony Liguori     .instance_size = sizeof(VT82C686BState),
52140021f08SAnthony Liguori     .class_init    = via_class_init,
522fd3b02c8SEduardo Habkost     .interfaces = (InterfaceInfo[]) {
523fd3b02c8SEduardo Habkost         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
524fd3b02c8SEduardo Habkost         { },
525fd3b02c8SEduardo Habkost     },
526edf79e66SHuacai Chen };
527edf79e66SHuacai Chen 
52898cf824bSPhilippe Mathieu-Daudé static void vt82c686b_superio_class_init(ObjectClass *klass, void *data)
52998cf824bSPhilippe Mathieu-Daudé {
53098cf824bSPhilippe Mathieu-Daudé     ISASuperIOClass *sc = ISA_SUPERIO_CLASS(klass);
53198cf824bSPhilippe Mathieu-Daudé 
53298cf824bSPhilippe Mathieu-Daudé     sc->serial.count = 2;
53398cf824bSPhilippe Mathieu-Daudé     sc->parallel.count = 1;
53498cf824bSPhilippe Mathieu-Daudé     sc->ide.count = 0;
53598cf824bSPhilippe Mathieu-Daudé     sc->floppy.count = 1;
53698cf824bSPhilippe Mathieu-Daudé }
53798cf824bSPhilippe Mathieu-Daudé 
53898cf824bSPhilippe Mathieu-Daudé static const TypeInfo via_superio_info = {
53998cf824bSPhilippe Mathieu-Daudé     .name          = TYPE_VT82C686B_SUPERIO,
54098cf824bSPhilippe Mathieu-Daudé     .parent        = TYPE_ISA_SUPERIO,
54198cf824bSPhilippe Mathieu-Daudé     .instance_size = sizeof(ISASuperIODevice),
54298cf824bSPhilippe Mathieu-Daudé     .class_size    = sizeof(ISASuperIOClass),
54398cf824bSPhilippe Mathieu-Daudé     .class_init    = vt82c686b_superio_class_init,
54498cf824bSPhilippe Mathieu-Daudé };
54598cf824bSPhilippe Mathieu-Daudé 
54683f7d43aSAndreas Färber static void vt82c686b_register_types(void)
547edf79e66SHuacai Chen {
54883f7d43aSAndreas Färber     type_register_static(&via_ac97_info);
54983f7d43aSAndreas Färber     type_register_static(&via_mc97_info);
55083f7d43aSAndreas Färber     type_register_static(&via_pm_info);
55198cf824bSPhilippe Mathieu-Daudé     type_register_static(&via_superio_info);
55239bffca2SAnthony Liguori     type_register_static(&via_info);
553edf79e66SHuacai Chen }
55483f7d43aSAndreas Färber 
55583f7d43aSAndreas Färber type_init(vt82c686b_register_types)
556