]> git.ipfire.org Git - thirdparty/qemu.git/blame - hw/isa/vt82c686.c
vt82c686: Move creation of ISA devices to the ISA bridge
[thirdparty/qemu.git] / hw / isa / vt82c686.c
CommitLineData
edf79e66
HC
1/*
2 * VT82C686B south bridge support
3 *
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.
6b620ca3
PB
8 *
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.
edf79e66
HC
11 */
12
0430891c 13#include "qemu/osdep.h"
0d09e41a 14#include "hw/isa/vt82c686.h"
83c9f4ca 15#include "hw/pci/pci.h"
a27bd6c7 16#include "hw/qdev-properties.h"
0d09e41a 17#include "hw/isa/isa.h"
98cf824b 18#include "hw/isa/superio.h"
3dc31cb8
BZ
19#include "hw/intc/i8259.h"
20#include "hw/irq.h"
21#include "hw/dma/i8257.h"
22#include "hw/timer/i8254.h"
23#include "hw/rtc/mc146818rtc.h"
d6454270 24#include "migration/vmstate.h"
0d09e41a
PB
25#include "hw/isa/apm.h"
26#include "hw/acpi/acpi.h"
27#include "hw/i2c/pm_smbus.h"
9307d06d 28#include "qapi/error.h"
0b8fa32f 29#include "qemu/module.h"
911629e6 30#include "qemu/range.h"
1de7afc9 31#include "qemu/timer.h"
022c62cb 32#include "exec/address-spaces.h"
ff413a1f 33#include "trace.h"
edf79e66 34
e1a69736
BZ
35#define TYPE_VIA_PM "via-pm"
36OBJECT_DECLARE_SIMPLE_TYPE(ViaPMState, VIA_PM)
edf79e66 37
e1a69736 38struct ViaPMState {
edf79e66 39 PCIDevice dev;
a2902821 40 MemoryRegion io;
355bf2e5 41 ACPIREGS ar;
edf79e66 42 APMState apm;
edf79e66 43 PMSMBus smb;
db1015e9 44};
edf79e66 45
e1a69736 46static void pm_io_space_update(ViaPMState *s)
edf79e66 47{
3ab1eea6 48 uint32_t pmbase = pci_get_long(s->dev.config + 0x48) & 0xff80UL;
edf79e66 49
a2902821 50 memory_region_transaction_begin();
3ab1eea6
BZ
51 memory_region_set_address(&s->io, pmbase);
52 memory_region_set_enabled(&s->io, s->dev.config[0x41] & BIT(7));
a2902821 53 memory_region_transaction_commit();
edf79e66
HC
54}
55
e1a69736 56static void smb_io_space_update(ViaPMState *s)
911629e6
BZ
57{
58 uint32_t smbase = pci_get_long(s->dev.config + 0x90) & 0xfff0UL;
59
60 memory_region_transaction_begin();
61 memory_region_set_address(&s->smb.io, smbase);
62 memory_region_set_enabled(&s->smb.io, s->dev.config[0xd2] & BIT(0));
63 memory_region_transaction_commit();
64}
65
edf79e66
HC
66static int vmstate_acpi_post_load(void *opaque, int version_id)
67{
e1a69736 68 ViaPMState *s = opaque;
edf79e66
HC
69
70 pm_io_space_update(s);
911629e6 71 smb_io_space_update(s);
edf79e66
HC
72 return 0;
73}
74
75static const VMStateDescription vmstate_acpi = {
76 .name = "vt82c686b_pm",
77 .version_id = 1,
78 .minimum_version_id = 1,
edf79e66 79 .post_load = vmstate_acpi_post_load,
d49805ae 80 .fields = (VMStateField[]) {
e1a69736
BZ
81 VMSTATE_PCI_DEVICE(dev, ViaPMState),
82 VMSTATE_UINT16(ar.pm1.evt.sts, ViaPMState),
83 VMSTATE_UINT16(ar.pm1.evt.en, ViaPMState),
84 VMSTATE_UINT16(ar.pm1.cnt.cnt, ViaPMState),
85 VMSTATE_STRUCT(apm, ViaPMState, 0, vmstate_apm, APMState),
86 VMSTATE_TIMER_PTR(ar.tmr.timer, ViaPMState),
87 VMSTATE_INT64(ar.tmr.overflow_time, ViaPMState),
edf79e66
HC
88 VMSTATE_END_OF_LIST()
89 }
90};
91
94349bff
BZ
92static void pm_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int len)
93{
e1a69736 94 ViaPMState *s = VIA_PM(d);
911629e6 95
94349bff
BZ
96 trace_via_pm_write(addr, val, len);
97 pci_default_write_config(d, addr, val, len);
3ab1eea6
BZ
98 if (ranges_overlap(addr, len, 0x48, 4)) {
99 uint32_t v = pci_get_long(s->dev.config + 0x48);
100 pci_set_long(s->dev.config + 0x48, (v & 0xff80UL) | 1);
101 }
102 if (range_covers_byte(addr, len, 0x41)) {
103 pm_io_space_update(s);
104 }
911629e6
BZ
105 if (ranges_overlap(addr, len, 0x90, 4)) {
106 uint32_t v = pci_get_long(s->dev.config + 0x90);
107 pci_set_long(s->dev.config + 0x90, (v & 0xfff0UL) | 1);
108 }
109 if (range_covers_byte(addr, len, 0xd2)) {
110 s->dev.config[0xd2] &= 0xf;
111 smb_io_space_update(s);
112 }
94349bff
BZ
113}
114
35e360ed
BZ
115static void pm_io_write(void *op, hwaddr addr, uint64_t data, unsigned size)
116{
117 trace_via_pm_io_write(addr, data, size);
118}
119
120static uint64_t pm_io_read(void *op, hwaddr addr, unsigned size)
121{
122 trace_via_pm_io_read(addr, 0, size);
123 return 0;
124}
125
126static const MemoryRegionOps pm_io_ops = {
127 .read = pm_io_read,
128 .write = pm_io_write,
129 .endianness = DEVICE_NATIVE_ENDIAN,
130 .impl = {
131 .min_access_size = 1,
132 .max_access_size = 1,
133 },
134};
135
e1a69736 136static void pm_update_sci(ViaPMState *s)
94349bff
BZ
137{
138 int sci_level, pmsts;
139
140 pmsts = acpi_pm1_evt_get_sts(&s->ar);
141 sci_level = (((pmsts & s->ar.pm1.evt.en) &
142 (ACPI_BITMASK_RT_CLOCK_ENABLE |
143 ACPI_BITMASK_POWER_BUTTON_ENABLE |
144 ACPI_BITMASK_GLOBAL_LOCK_ENABLE |
145 ACPI_BITMASK_TIMER_ENABLE)) != 0);
146 pci_set_irq(&s->dev, sci_level);
147 /* schedule a timer interruption if needed */
148 acpi_pm_tmr_update(&s->ar, (s->ar.pm1.evt.en & ACPI_BITMASK_TIMER_ENABLE) &&
149 !(pmsts & ACPI_BITMASK_TIMER_STATUS));
150}
151
152static void pm_tmr_timer(ACPIREGS *ar)
153{
e1a69736 154 ViaPMState *s = container_of(ar, ViaPMState, ar);
94349bff
BZ
155 pm_update_sci(s);
156}
157
e1a69736 158static void via_pm_reset(DeviceState *d)
911629e6 159{
e1a69736 160 ViaPMState *s = VIA_PM(d);
911629e6 161
9af8e529
BZ
162 memset(s->dev.config + PCI_CONFIG_HEADER_SIZE, 0,
163 PCI_CONFIG_SPACE_SIZE - PCI_CONFIG_HEADER_SIZE);
164 /* Power Management IO base */
165 pci_set_long(s->dev.config + 0x48, 1);
911629e6
BZ
166 /* SMBus IO base */
167 pci_set_long(s->dev.config + 0x90, 1);
911629e6 168
3ab1eea6 169 pm_io_space_update(s);
911629e6
BZ
170 smb_io_space_update(s);
171}
172
e1a69736 173static void via_pm_realize(PCIDevice *dev, Error **errp)
edf79e66 174{
e1a69736 175 ViaPMState *s = VIA_PM(dev);
edf79e66 176
3ab1eea6 177 pci_set_word(dev->config + PCI_STATUS, PCI_STATUS_FAST_BACK |
edf79e66
HC
178 PCI_STATUS_DEVSEL_MEDIUM);
179
a30c34d2 180 pm_smbus_init(DEVICE(s), &s->smb, false);
911629e6
BZ
181 memory_region_add_subregion(pci_address_space_io(dev), 0, &s->smb.io);
182 memory_region_set_enabled(&s->smb.io, false);
edf79e66 183
42d8a3cf 184 apm_init(dev, &s->apm, NULL, s);
edf79e66 185
e1a69736 186 memory_region_init_io(&s->io, OBJECT(dev), &pm_io_ops, s, "via-pm", 128);
35e360ed 187 memory_region_add_subregion(pci_address_space_io(dev), 0, &s->io);
a2902821 188 memory_region_set_enabled(&s->io, false);
edf79e66 189
77d58b1e 190 acpi_pm_tmr_init(&s->ar, pm_tmr_timer, &s->io);
b5a7c024 191 acpi_pm1_evt_init(&s->ar, pm_tmr_timer, &s->io);
9a10bbb4 192 acpi_pm1_cnt_init(&s->ar, &s->io, false, false, 2);
edf79e66
HC
193}
194
e1a69736
BZ
195typedef struct via_pm_init_info {
196 uint16_t device_id;
197} ViaPMInitInfo;
198
40021f08
AL
199static void via_pm_class_init(ObjectClass *klass, void *data)
200{
39bffca2 201 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08 202 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
e1a69736 203 ViaPMInitInfo *info = data;
40021f08 204
e1a69736 205 k->realize = via_pm_realize;
40021f08
AL
206 k->config_write = pm_write_config;
207 k->vendor_id = PCI_VENDOR_ID_VIA;
e1a69736 208 k->device_id = info->device_id;
40021f08
AL
209 k->class_id = PCI_CLASS_BRIDGE_OTHER;
210 k->revision = 0x40;
e1a69736 211 dc->reset = via_pm_reset;
084bf4b4
BZ
212 /* Reason: part of VIA south bridge, does not exist stand alone */
213 dc->user_creatable = false;
39bffca2 214 dc->vmsd = &vmstate_acpi;
40021f08
AL
215}
216
8c43a6f0 217static const TypeInfo via_pm_info = {
e1a69736 218 .name = TYPE_VIA_PM,
39bffca2 219 .parent = TYPE_PCI_DEVICE,
e1a69736
BZ
220 .instance_size = sizeof(ViaPMState),
221 .abstract = true,
fd3b02c8
EH
222 .interfaces = (InterfaceInfo[]) {
223 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
224 { },
225 },
edf79e66
HC
226};
227
e1a69736
BZ
228static const ViaPMInitInfo vt82c686b_pm_init_info = {
229 .device_id = PCI_DEVICE_ID_VIA_82C686B_PM,
230};
231
232static const TypeInfo vt82c686b_pm_info = {
233 .name = TYPE_VT82C686B_PM,
234 .parent = TYPE_VIA_PM,
235 .class_init = via_pm_class_init,
236 .class_data = (void *)&vt82c686b_pm_init_info,
237};
238
239static const ViaPMInitInfo vt8231_pm_init_info = {
240 .device_id = PCI_DEVICE_ID_VIA_8231_PM,
241};
242
243static const TypeInfo vt8231_pm_info = {
244 .name = TYPE_VT8231_PM,
245 .parent = TYPE_VIA_PM,
246 .class_init = via_pm_class_init,
247 .class_data = (void *)&vt8231_pm_init_info,
248};
249
94349bff
BZ
250
251typedef struct SuperIOConfig {
252 uint8_t regs[0x100];
253 uint8_t index;
254 MemoryRegion io;
255} SuperIOConfig;
256
257static void superio_cfg_write(void *opaque, hwaddr addr, uint64_t data,
258 unsigned size)
259{
260 SuperIOConfig *sc = opaque;
261
262 if (addr == 0x3f0) { /* config index register */
263 sc->index = data & 0xff;
264 } else {
265 bool can_write = true;
266 /* 0x3f1, config data register */
267 trace_via_superio_write(sc->index, data & 0xff);
268 switch (sc->index) {
269 case 0x00 ... 0xdf:
270 case 0xe4:
271 case 0xe5:
272 case 0xe9 ... 0xed:
273 case 0xf3:
274 case 0xf5:
275 case 0xf7:
276 case 0xf9 ... 0xfb:
277 case 0xfd ... 0xff:
278 can_write = false;
279 break;
280 /* case 0xe6 ... 0xe8: Should set base port of parallel and serial */
281 default:
282 break;
283
284 }
285 if (can_write) {
286 sc->regs[sc->index] = data & 0xff;
287 }
288 }
289}
290
291static uint64_t superio_cfg_read(void *opaque, hwaddr addr, unsigned size)
292{
293 SuperIOConfig *sc = opaque;
294 uint8_t val = sc->regs[sc->index];
295
296 trace_via_superio_read(sc->index, val);
297 return val;
298}
299
300static const MemoryRegionOps superio_cfg_ops = {
301 .read = superio_cfg_read,
302 .write = superio_cfg_write,
303 .endianness = DEVICE_NATIVE_ENDIAN,
304 .impl = {
305 .min_access_size = 1,
306 .max_access_size = 1,
307 },
308};
309
310
311OBJECT_DECLARE_SIMPLE_TYPE(VT82C686BISAState, VT82C686B_ISA)
312
313struct VT82C686BISAState {
314 PCIDevice dev;
3dc31cb8 315 qemu_irq cpu_intr;
94349bff
BZ
316 SuperIOConfig superio_cfg;
317};
318
3dc31cb8
BZ
319static void via_isa_request_i8259_irq(void *opaque, int irq, int level)
320{
321 VT82C686BISAState *s = opaque;
322 qemu_set_irq(s->cpu_intr, level);
323}
324
94349bff
BZ
325static void vt82c686b_write_config(PCIDevice *d, uint32_t addr,
326 uint32_t val, int len)
327{
328 VT82C686BISAState *s = VT82C686B_ISA(d);
329
330 trace_via_isa_write(addr, val, len);
331 pci_default_write_config(d, addr, val, len);
332 if (addr == 0x85) {
333 /* BIT(1): enable or disable superio config io ports */
334 memory_region_set_enabled(&s->superio_cfg.io, val & BIT(1));
335 }
336}
337
edf79e66
HC
338static const VMStateDescription vmstate_via = {
339 .name = "vt82c686b",
340 .version_id = 1,
341 .minimum_version_id = 1,
d49805ae 342 .fields = (VMStateField[]) {
0f798461 343 VMSTATE_PCI_DEVICE(dev, VT82C686BISAState),
edf79e66
HC
344 VMSTATE_END_OF_LIST()
345 }
346};
347
94349bff
BZ
348static void vt82c686b_isa_reset(DeviceState *dev)
349{
350 VT82C686BISAState *s = VT82C686B_ISA(dev);
351 uint8_t *pci_conf = s->dev.config;
352
353 pci_set_long(pci_conf + PCI_CAPABILITY_LIST, 0x000000c0);
354 pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
355 PCI_COMMAND_MASTER | PCI_COMMAND_SPECIAL);
356 pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM);
357
358 pci_conf[0x48] = 0x01; /* Miscellaneous Control 3 */
359 pci_conf[0x4a] = 0x04; /* IDE interrupt Routing */
360 pci_conf[0x4f] = 0x03; /* DMA/Master Mem Access Control 3 */
361 pci_conf[0x50] = 0x2d; /* PnP DMA Request Control */
362 pci_conf[0x59] = 0x04;
363 pci_conf[0x5a] = 0x04; /* KBC/RTC Control*/
364 pci_conf[0x5f] = 0x04;
365 pci_conf[0x77] = 0x10; /* GPIO Control 1/2/3/4 */
366
367 s->superio_cfg.regs[0xe0] = 0x3c; /* Device ID */
368 s->superio_cfg.regs[0xe2] = 0x03; /* Function select */
369 s->superio_cfg.regs[0xe3] = 0xfc; /* Floppy ctrl base addr */
370 s->superio_cfg.regs[0xe6] = 0xde; /* Parallel port base addr */
371 s->superio_cfg.regs[0xe7] = 0xfe; /* Serial port 1 base addr */
372 s->superio_cfg.regs[0xe8] = 0xbe; /* Serial port 2 base addr */
373}
374
9af21dbe 375static void vt82c686b_realize(PCIDevice *d, Error **errp)
edf79e66 376{
007b3103 377 VT82C686BISAState *s = VT82C686B_ISA(d);
9859ad1c 378 DeviceState *dev = DEVICE(d);
bcc37e24 379 ISABus *isa_bus;
3dc31cb8 380 qemu_irq *isa_irq;
edf79e66
HC
381 int i;
382
3dc31cb8
BZ
383 qdev_init_gpio_out(dev, &s->cpu_intr, 1);
384 isa_irq = qemu_allocate_irqs(via_isa_request_i8259_irq, s, 1);
9859ad1c
BZ
385 isa_bus = isa_bus_new(dev, get_system_memory(), pci_address_space_io(d),
386 &error_fatal);
3dc31cb8
BZ
387 isa_bus_irqs(isa_bus, i8259_init(isa_bus, *isa_irq));
388 i8254_pit_init(isa_bus, 0x40, 0, NULL);
389 i8257_dma_init(isa_bus, 0);
390 isa_create_simple(isa_bus, TYPE_VT82C686B_SUPERIO);
391 mc146818_rtc_init(isa_bus, 2000, NULL);
edf79e66 392
9859ad1c
BZ
393 for (i = 0; i < PCI_CONFIG_HEADER_SIZE; i++) {
394 if (i < PCI_COMMAND || i >= PCI_REVISION_ID) {
395 d->wmask[i] = 0;
f3db354c 396 }
edf79e66
HC
397 }
398
6be6e4bc
BZ
399 memory_region_init_io(&s->superio_cfg.io, OBJECT(d), &superio_cfg_ops,
400 &s->superio_cfg, "superio_cfg", 2);
401 memory_region_set_enabled(&s->superio_cfg.io, false);
f3db354c
FB
402 /*
403 * The floppy also uses 0x3f0 and 0x3f1.
404 * But we do not emulate a floppy, so just set it here.
405 */
bcc37e24 406 memory_region_add_subregion(isa_bus->address_space_io, 0x3f0,
6be6e4bc 407 &s->superio_cfg.io);
edf79e66
HC
408}
409
40021f08
AL
410static void via_class_init(ObjectClass *klass, void *data)
411{
39bffca2 412 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08
AL
413 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
414
9af21dbe 415 k->realize = vt82c686b_realize;
40021f08
AL
416 k->config_write = vt82c686b_write_config;
417 k->vendor_id = PCI_VENDOR_ID_VIA;
418 k->device_id = PCI_DEVICE_ID_VIA_ISA_BRIDGE;
419 k->class_id = PCI_CLASS_BRIDGE_ISA;
420 k->revision = 0x40;
9dc1a769 421 dc->reset = vt82c686b_isa_reset;
39bffca2 422 dc->desc = "ISA bridge";
39bffca2 423 dc->vmsd = &vmstate_via;
04916ee9
MA
424 /*
425 * Reason: part of VIA VT82C686 southbridge, needs to be wired up,
c3a09ff6 426 * e.g. by mips_fuloong2e_init()
04916ee9 427 */
e90f2a8c 428 dc->user_creatable = false;
40021f08
AL
429}
430
8c43a6f0 431static const TypeInfo via_info = {
0f798461 432 .name = TYPE_VT82C686B_ISA,
39bffca2 433 .parent = TYPE_PCI_DEVICE,
0f798461 434 .instance_size = sizeof(VT82C686BISAState),
39bffca2 435 .class_init = via_class_init,
fd3b02c8
EH
436 .interfaces = (InterfaceInfo[]) {
437 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
438 { },
439 },
edf79e66
HC
440};
441
94349bff 442
98cf824b
PMD
443static void vt82c686b_superio_class_init(ObjectClass *klass, void *data)
444{
445 ISASuperIOClass *sc = ISA_SUPERIO_CLASS(klass);
446
447 sc->serial.count = 2;
448 sc->parallel.count = 1;
449 sc->ide.count = 0;
450 sc->floppy.count = 1;
451}
452
453static const TypeInfo via_superio_info = {
454 .name = TYPE_VT82C686B_SUPERIO,
455 .parent = TYPE_ISA_SUPERIO,
456 .instance_size = sizeof(ISASuperIODevice),
457 .class_size = sizeof(ISASuperIOClass),
458 .class_init = vt82c686b_superio_class_init,
459};
460
94349bff 461
83f7d43a 462static void vt82c686b_register_types(void)
edf79e66 463{
83f7d43a 464 type_register_static(&via_pm_info);
e1a69736
BZ
465 type_register_static(&vt82c686b_pm_info);
466 type_register_static(&vt8231_pm_info);
39bffca2 467 type_register_static(&via_info);
94349bff 468 type_register_static(&via_superio_info);
edf79e66 469}
83f7d43a
AF
470
471type_init(vt82c686b_register_types)