2 * VT82C686B south bridge support
4 * Copyright (c) 2008 yajin (yajin@vm-kernel.org)
5 * Copyright (c) 2009 chenming (chenming@rdc.faw.com.cn)
6 * Copyright (c) 2010 Huacai Chen (zltjiangshi@gmail.com)
7 * This code is licensed under the GNU GPL v2.
9 * Contributions after 2012-01-13 are licensed under the terms of the
10 * GNU GPL, version 2 or (at your option) any later version.
12 * VT8231 south bridge support and general clean up to allow it
13 * Copyright (c) 2018-2020 BALATON Zoltan
16 #include "qemu/osdep.h"
17 #include "hw/isa/vt82c686.h"
18 #include "hw/block/fdc.h"
19 #include "hw/char/parallel-isa.h"
20 #include "hw/char/serial.h"
21 #include "hw/pci/pci.h"
22 #include "hw/qdev-properties.h"
23 #include "hw/ide/pci.h"
24 #include "hw/isa/isa.h"
25 #include "hw/isa/superio.h"
26 #include "hw/intc/i8259.h"
28 #include "hw/dma/i8257.h"
29 #include "hw/usb/hcd-uhci.h"
30 #include "hw/timer/i8254.h"
31 #include "hw/rtc/mc146818rtc.h"
32 #include "migration/vmstate.h"
33 #include "hw/isa/apm.h"
34 #include "hw/acpi/acpi.h"
35 #include "hw/i2c/pm_smbus.h"
36 #include "qapi/error.h"
38 #include "qemu/module.h"
39 #include "qemu/range.h"
40 #include "qemu/timer.h"
43 #define TYPE_VIA_PM "via-pm"
44 OBJECT_DECLARE_SIMPLE_TYPE(ViaPMState
, VIA_PM
)
54 static void pm_io_space_update(ViaPMState
*s
)
56 uint32_t pmbase
= pci_get_long(s
->dev
.config
+ 0x48) & 0xff80UL
;
58 memory_region_transaction_begin();
59 memory_region_set_address(&s
->io
, pmbase
);
60 memory_region_set_enabled(&s
->io
, s
->dev
.config
[0x41] & BIT(7));
61 memory_region_transaction_commit();
64 static void smb_io_space_update(ViaPMState
*s
)
66 uint32_t smbase
= pci_get_long(s
->dev
.config
+ 0x90) & 0xfff0UL
;
68 memory_region_transaction_begin();
69 memory_region_set_address(&s
->smb
.io
, smbase
);
70 memory_region_set_enabled(&s
->smb
.io
, s
->dev
.config
[0xd2] & BIT(0));
71 memory_region_transaction_commit();
74 static int vmstate_acpi_post_load(void *opaque
, int version_id
)
76 ViaPMState
*s
= opaque
;
78 pm_io_space_update(s
);
79 smb_io_space_update(s
);
83 static const VMStateDescription vmstate_acpi
= {
84 .name
= "vt82c686b_pm",
86 .minimum_version_id
= 1,
87 .post_load
= vmstate_acpi_post_load
,
88 .fields
= (const VMStateField
[]) {
89 VMSTATE_PCI_DEVICE(dev
, ViaPMState
),
90 VMSTATE_UINT16(ar
.pm1
.evt
.sts
, ViaPMState
),
91 VMSTATE_UINT16(ar
.pm1
.evt
.en
, ViaPMState
),
92 VMSTATE_UINT16(ar
.pm1
.cnt
.cnt
, ViaPMState
),
93 VMSTATE_STRUCT(apm
, ViaPMState
, 0, vmstate_apm
, APMState
),
94 VMSTATE_TIMER_PTR(ar
.tmr
.timer
, ViaPMState
),
95 VMSTATE_INT64(ar
.tmr
.overflow_time
, ViaPMState
),
100 static void pm_write_config(PCIDevice
*d
, uint32_t addr
, uint32_t val
, int len
)
102 ViaPMState
*s
= VIA_PM(d
);
104 trace_via_pm_write(addr
, val
, len
);
105 pci_default_write_config(d
, addr
, val
, len
);
106 if (ranges_overlap(addr
, len
, 0x48, 4)) {
107 uint32_t v
= pci_get_long(s
->dev
.config
+ 0x48);
108 pci_set_long(s
->dev
.config
+ 0x48, (v
& 0xff80UL
) | 1);
110 if (range_covers_byte(addr
, len
, 0x41)) {
111 pm_io_space_update(s
);
113 if (ranges_overlap(addr
, len
, 0x90, 4)) {
114 uint32_t v
= pci_get_long(s
->dev
.config
+ 0x90);
115 pci_set_long(s
->dev
.config
+ 0x90, (v
& 0xfff0UL
) | 1);
117 if (range_covers_byte(addr
, len
, 0xd2)) {
118 s
->dev
.config
[0xd2] &= 0xf;
119 smb_io_space_update(s
);
123 static void pm_io_write(void *op
, hwaddr addr
, uint64_t data
, unsigned size
)
125 trace_via_pm_io_write(addr
, data
, size
);
128 static uint64_t pm_io_read(void *op
, hwaddr addr
, unsigned size
)
130 trace_via_pm_io_read(addr
, 0, size
);
134 static const MemoryRegionOps pm_io_ops
= {
136 .write
= pm_io_write
,
137 .endianness
= DEVICE_NATIVE_ENDIAN
,
139 .min_access_size
= 1,
140 .max_access_size
= 1,
144 static void pm_update_sci(ViaPMState
*s
)
146 int sci_level
, pmsts
;
148 pmsts
= acpi_pm1_evt_get_sts(&s
->ar
);
149 sci_level
= (((pmsts
& s
->ar
.pm1
.evt
.en
) &
150 (ACPI_BITMASK_RT_CLOCK_ENABLE
|
151 ACPI_BITMASK_POWER_BUTTON_ENABLE
|
152 ACPI_BITMASK_GLOBAL_LOCK_ENABLE
|
153 ACPI_BITMASK_TIMER_ENABLE
)) != 0);
154 if (pci_get_byte(s
->dev
.config
+ PCI_INTERRUPT_PIN
)) {
157 * Fix device model that realizes this PM device and remove
159 * The device model should wire SCI and setup
160 * PCI_INTERRUPT_PIN properly.
161 * If PIN# = 0(interrupt pin isn't used), don't raise SCI as
164 pci_set_irq(&s
->dev
, sci_level
);
166 /* schedule a timer interruption if needed */
167 acpi_pm_tmr_update(&s
->ar
, (s
->ar
.pm1
.evt
.en
& ACPI_BITMASK_TIMER_ENABLE
) &&
168 !(pmsts
& ACPI_BITMASK_TIMER_STATUS
));
171 static void pm_tmr_timer(ACPIREGS
*ar
)
173 ViaPMState
*s
= container_of(ar
, ViaPMState
, ar
);
177 static void via_pm_reset(DeviceState
*d
)
179 ViaPMState
*s
= VIA_PM(d
);
181 memset(s
->dev
.config
+ PCI_CONFIG_HEADER_SIZE
, 0,
182 PCI_CONFIG_SPACE_SIZE
- PCI_CONFIG_HEADER_SIZE
);
183 /* Power Management IO base */
184 pci_set_long(s
->dev
.config
+ 0x48, 1);
186 pci_set_long(s
->dev
.config
+ 0x90, 1);
188 acpi_pm1_evt_reset(&s
->ar
);
189 acpi_pm1_cnt_reset(&s
->ar
);
190 acpi_pm_tmr_reset(&s
->ar
);
193 pm_io_space_update(s
);
194 smb_io_space_update(s
);
197 static void via_pm_realize(PCIDevice
*dev
, Error
**errp
)
199 ViaPMState
*s
= VIA_PM(dev
);
201 pci_set_word(dev
->config
+ PCI_STATUS
, PCI_STATUS_FAST_BACK
|
202 PCI_STATUS_DEVSEL_MEDIUM
);
204 pm_smbus_init(DEVICE(s
), &s
->smb
, false);
205 memory_region_add_subregion(pci_address_space_io(dev
), 0, &s
->smb
.io
);
206 memory_region_set_enabled(&s
->smb
.io
, false);
208 apm_init(dev
, &s
->apm
, NULL
, s
);
210 memory_region_init_io(&s
->io
, OBJECT(dev
), &pm_io_ops
, s
, "via-pm", 128);
211 memory_region_add_subregion(pci_address_space_io(dev
), 0, &s
->io
);
212 memory_region_set_enabled(&s
->io
, false);
214 acpi_pm_tmr_init(&s
->ar
, pm_tmr_timer
, &s
->io
);
215 acpi_pm1_evt_init(&s
->ar
, pm_tmr_timer
, &s
->io
);
216 acpi_pm1_cnt_init(&s
->ar
, &s
->io
, false, false, 2, false);
219 typedef struct via_pm_init_info
{
223 static void via_pm_class_init(ObjectClass
*klass
, void *data
)
225 DeviceClass
*dc
= DEVICE_CLASS(klass
);
226 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
227 ViaPMInitInfo
*info
= data
;
229 k
->realize
= via_pm_realize
;
230 k
->config_write
= pm_write_config
;
231 k
->vendor_id
= PCI_VENDOR_ID_VIA
;
232 k
->device_id
= info
->device_id
;
233 k
->class_id
= PCI_CLASS_BRIDGE_OTHER
;
235 dc
->reset
= via_pm_reset
;
236 /* Reason: part of VIA south bridge, does not exist stand alone */
237 dc
->user_creatable
= false;
238 dc
->vmsd
= &vmstate_acpi
;
241 static const TypeInfo via_pm_info
= {
243 .parent
= TYPE_PCI_DEVICE
,
244 .instance_size
= sizeof(ViaPMState
),
246 .interfaces
= (InterfaceInfo
[]) {
247 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
252 static const ViaPMInitInfo vt82c686b_pm_init_info
= {
253 .device_id
= PCI_DEVICE_ID_VIA_82C686B_PM
,
256 #define TYPE_VT82C686B_PM "vt82c686b-pm"
258 static const TypeInfo vt82c686b_pm_info
= {
259 .name
= TYPE_VT82C686B_PM
,
260 .parent
= TYPE_VIA_PM
,
261 .class_init
= via_pm_class_init
,
262 .class_data
= (void *)&vt82c686b_pm_init_info
,
265 static const ViaPMInitInfo vt8231_pm_init_info
= {
266 .device_id
= PCI_DEVICE_ID_VIA_8231_PM
,
269 #define TYPE_VT8231_PM "vt8231-pm"
271 static const TypeInfo vt8231_pm_info
= {
272 .name
= TYPE_VT8231_PM
,
273 .parent
= TYPE_VIA_PM
,
274 .class_init
= via_pm_class_init
,
275 .class_data
= (void *)&vt8231_pm_init_info
,
279 #define TYPE_VIA_SUPERIO "via-superio"
280 OBJECT_DECLARE_SIMPLE_TYPE(ViaSuperIOState
, VIA_SUPERIO
)
282 struct ViaSuperIOState
{
283 ISASuperIODevice superio
;
285 const MemoryRegionOps
*io_ops
;
289 static inline void via_superio_io_enable(ViaSuperIOState
*s
, bool enable
)
291 memory_region_set_enabled(&s
->io
, enable
);
294 static void via_superio_realize(DeviceState
*d
, Error
**errp
)
296 ViaSuperIOState
*s
= VIA_SUPERIO(d
);
297 ISASuperIOClass
*ic
= ISA_SUPERIO_GET_CLASS(s
);
298 Error
*local_err
= NULL
;
301 ic
->parent_realize(d
, &local_err
);
303 error_propagate(errp
, local_err
);
306 memory_region_init_io(&s
->io
, OBJECT(d
), s
->io_ops
, s
, "via-superio", 2);
307 memory_region_set_enabled(&s
->io
, false);
308 /* The floppy also uses 0x3f0 and 0x3f1 but this seems to work anyway */
309 memory_region_add_subregion(isa_address_space_io(ISA_DEVICE(s
)), 0x3f0,
313 static uint64_t via_superio_cfg_read(void *opaque
, hwaddr addr
, unsigned size
)
315 ViaSuperIOState
*sc
= opaque
;
316 uint8_t idx
= sc
->regs
[0];
317 uint8_t val
= sc
->regs
[idx
];
322 if (addr
== 1 && idx
== 0) {
323 val
= 0; /* reading reg 0 where we store index value */
325 trace_via_superio_read(idx
, val
);
329 static void via_superio_devices_enable(ViaSuperIOState
*s
, uint8_t data
)
331 ISASuperIOClass
*ic
= ISA_SUPERIO_GET_CLASS(s
);
333 isa_parallel_set_enabled(s
->superio
.parallel
[0], (data
& 0x3) != 3);
334 for (int i
= 0; i
< ic
->serial
.count
; i
++) {
335 isa_serial_set_enabled(s
->superio
.serial
[i
], data
& BIT(i
+ 2));
337 isa_fdc_set_enabled(s
->superio
.floppy
, data
& BIT(4));
340 static void via_superio_class_init(ObjectClass
*klass
, void *data
)
342 DeviceClass
*dc
= DEVICE_CLASS(klass
);
343 ISASuperIOClass
*sc
= ISA_SUPERIO_CLASS(klass
);
345 device_class_set_parent_realize(dc
, via_superio_realize
,
346 &sc
->parent_realize
);
349 static const TypeInfo via_superio_info
= {
350 .name
= TYPE_VIA_SUPERIO
,
351 .parent
= TYPE_ISA_SUPERIO
,
352 .instance_size
= sizeof(ViaSuperIOState
),
353 .class_size
= sizeof(ISASuperIOClass
),
354 .class_init
= via_superio_class_init
,
358 #define TYPE_VT82C686B_SUPERIO "vt82c686b-superio"
360 static void vt82c686b_superio_cfg_write(void *opaque
, hwaddr addr
,
361 uint64_t data
, unsigned size
)
363 ViaSuperIOState
*sc
= opaque
;
364 uint8_t idx
= sc
->regs
[0];
366 if (addr
== 0) { /* config index register */
371 /* config data register */
372 trace_via_superio_write(idx
, data
);
383 /* ignore write to read only registers */
387 via_superio_devices_enable(sc
, data
);
391 isa_fdc_set_iobase(sc
->superio
.floppy
, data
<< 2);
394 isa_parallel_set_iobase(sc
->superio
.parallel
[0], data
<< 2);
398 isa_serial_set_iobase(sc
->superio
.serial
[0], data
<< 2);
402 isa_serial_set_iobase(sc
->superio
.serial
[1], data
<< 2);
405 qemu_log_mask(LOG_UNIMP
,
406 "via_superio_cfg: unimplemented register 0x%x\n", idx
);
409 sc
->regs
[idx
] = data
;
412 static const MemoryRegionOps vt82c686b_superio_cfg_ops
= {
413 .read
= via_superio_cfg_read
,
414 .write
= vt82c686b_superio_cfg_write
,
415 .endianness
= DEVICE_NATIVE_ENDIAN
,
417 .min_access_size
= 1,
418 .max_access_size
= 1,
422 static void vt82c686b_superio_reset(DeviceState
*dev
)
424 ViaSuperIOState
*s
= VIA_SUPERIO(dev
);
426 memset(s
->regs
, 0, sizeof(s
->regs
));
428 vt82c686b_superio_cfg_write(s
, 0, 0xe0, 1);
429 vt82c686b_superio_cfg_write(s
, 1, 0x3c, 1);
431 * Function select - only serial enabled
432 * Fuloong 2e's rescue-yl prints to the serial console w/o enabling it. This
433 * suggests that the serial ports are enabled by default, so override the
436 vt82c686b_superio_cfg_write(s
, 0, 0xe2, 1);
437 vt82c686b_superio_cfg_write(s
, 1, 0x0f, 1);
438 /* Floppy ctrl base addr 0x3f0-7 */
439 vt82c686b_superio_cfg_write(s
, 0, 0xe3, 1);
440 vt82c686b_superio_cfg_write(s
, 1, 0xfc, 1);
441 /* Parallel port base addr 0x378-f */
442 vt82c686b_superio_cfg_write(s
, 0, 0xe6, 1);
443 vt82c686b_superio_cfg_write(s
, 1, 0xde, 1);
444 /* Serial port 1 base addr 0x3f8-f */
445 vt82c686b_superio_cfg_write(s
, 0, 0xe7, 1);
446 vt82c686b_superio_cfg_write(s
, 1, 0xfe, 1);
447 /* Serial port 2 base addr 0x2f8-f */
448 vt82c686b_superio_cfg_write(s
, 0, 0xe8, 1);
449 vt82c686b_superio_cfg_write(s
, 1, 0xbe, 1);
451 vt82c686b_superio_cfg_write(s
, 0, 0, 1);
454 static void vt82c686b_superio_init(Object
*obj
)
456 VIA_SUPERIO(obj
)->io_ops
= &vt82c686b_superio_cfg_ops
;
459 static void vt82c686b_superio_class_init(ObjectClass
*klass
, void *data
)
461 DeviceClass
*dc
= DEVICE_CLASS(klass
);
462 ISASuperIOClass
*sc
= ISA_SUPERIO_CLASS(klass
);
464 dc
->reset
= vt82c686b_superio_reset
;
465 sc
->serial
.count
= 2;
466 sc
->parallel
.count
= 1;
467 sc
->ide
.count
= 0; /* emulated by via-ide */
468 sc
->floppy
.count
= 1;
471 static const TypeInfo vt82c686b_superio_info
= {
472 .name
= TYPE_VT82C686B_SUPERIO
,
473 .parent
= TYPE_VIA_SUPERIO
,
474 .instance_size
= sizeof(ViaSuperIOState
),
475 .instance_init
= vt82c686b_superio_init
,
476 .class_size
= sizeof(ISASuperIOClass
),
477 .class_init
= vt82c686b_superio_class_init
,
481 #define TYPE_VT8231_SUPERIO "vt8231-superio"
483 static void vt8231_superio_cfg_write(void *opaque
, hwaddr addr
,
484 uint64_t data
, unsigned size
)
486 ViaSuperIOState
*sc
= opaque
;
487 uint8_t idx
= sc
->regs
[0];
489 if (addr
== 0) { /* config index register */
494 /* config data register */
495 trace_via_superio_write(idx
, data
);
503 /* ignore write to read only registers */
507 via_superio_devices_enable(sc
, data
);
511 isa_serial_set_iobase(sc
->superio
.serial
[0], data
<< 2);
514 isa_parallel_set_iobase(sc
->superio
.parallel
[0], data
<< 2);
518 isa_fdc_set_iobase(sc
->superio
.floppy
, data
<< 2);
521 qemu_log_mask(LOG_UNIMP
,
522 "via_superio_cfg: unimplemented register 0x%x\n", idx
);
525 sc
->regs
[idx
] = data
;
528 static const MemoryRegionOps vt8231_superio_cfg_ops
= {
529 .read
= via_superio_cfg_read
,
530 .write
= vt8231_superio_cfg_write
,
531 .endianness
= DEVICE_NATIVE_ENDIAN
,
533 .min_access_size
= 1,
534 .max_access_size
= 1,
538 static void vt8231_superio_reset(DeviceState
*dev
)
540 ViaSuperIOState
*s
= VIA_SUPERIO(dev
);
542 memset(s
->regs
, 0, sizeof(s
->regs
));
544 s
->regs
[0xf0] = 0x3c;
545 /* Device revision */
546 s
->regs
[0xf1] = 0x01;
547 /* Function select - all disabled */
548 vt8231_superio_cfg_write(s
, 0, 0xf2, 1);
549 vt8231_superio_cfg_write(s
, 1, 0x03, 1);
550 /* Serial port base addr */
551 vt8231_superio_cfg_write(s
, 0, 0xf4, 1);
552 vt8231_superio_cfg_write(s
, 1, 0xfe, 1);
553 /* Parallel port base addr */
554 vt8231_superio_cfg_write(s
, 0, 0xf6, 1);
555 vt8231_superio_cfg_write(s
, 1, 0xde, 1);
556 /* Floppy ctrl base addr */
557 vt8231_superio_cfg_write(s
, 0, 0xf7, 1);
558 vt8231_superio_cfg_write(s
, 1, 0xfc, 1);
560 vt8231_superio_cfg_write(s
, 0, 0, 1);
563 static void vt8231_superio_init(Object
*obj
)
565 VIA_SUPERIO(obj
)->io_ops
= &vt8231_superio_cfg_ops
;
568 static void vt8231_superio_class_init(ObjectClass
*klass
, void *data
)
570 DeviceClass
*dc
= DEVICE_CLASS(klass
);
571 ISASuperIOClass
*sc
= ISA_SUPERIO_CLASS(klass
);
573 dc
->reset
= vt8231_superio_reset
;
574 sc
->serial
.count
= 1;
575 sc
->parallel
.count
= 1;
576 sc
->ide
.count
= 0; /* emulated by via-ide */
577 sc
->floppy
.count
= 1;
580 static const TypeInfo vt8231_superio_info
= {
581 .name
= TYPE_VT8231_SUPERIO
,
582 .parent
= TYPE_VIA_SUPERIO
,
583 .instance_size
= sizeof(ViaSuperIOState
),
584 .instance_init
= vt8231_superio_init
,
585 .class_size
= sizeof(ISASuperIOClass
),
586 .class_init
= vt8231_superio_class_init
,
590 #define TYPE_VIA_ISA "via-isa"
591 OBJECT_DECLARE_SIMPLE_TYPE(ViaISAState
, VIA_ISA
)
596 qemu_irq
*isa_irqs_in
;
597 uint16_t irq_state
[ISA_NUM_IRQS
];
598 ViaSuperIOState via_sio
;
599 MC146818RtcState rtc
;
607 static const VMStateDescription vmstate_via
= {
610 .minimum_version_id
= 1,
611 .fields
= (const VMStateField
[]) {
612 VMSTATE_PCI_DEVICE(dev
, ViaISAState
),
613 VMSTATE_END_OF_LIST()
617 static void via_isa_init(Object
*obj
)
619 ViaISAState
*s
= VIA_ISA(obj
);
621 object_initialize_child(obj
, "rtc", &s
->rtc
, TYPE_MC146818_RTC
);
622 object_initialize_child(obj
, "ide", &s
->ide
, TYPE_VIA_IDE
);
623 object_initialize_child(obj
, "uhci1", &s
->uhci
[0], TYPE_VT82C686B_USB_UHCI
);
624 object_initialize_child(obj
, "uhci2", &s
->uhci
[1], TYPE_VT82C686B_USB_UHCI
);
625 object_initialize_child(obj
, "ac97", &s
->ac97
, TYPE_VIA_AC97
);
626 object_initialize_child(obj
, "mc97", &s
->mc97
, TYPE_VIA_MC97
);
629 static const TypeInfo via_isa_info
= {
630 .name
= TYPE_VIA_ISA
,
631 .parent
= TYPE_PCI_DEVICE
,
632 .instance_size
= sizeof(ViaISAState
),
633 .instance_init
= via_isa_init
,
635 .interfaces
= (InterfaceInfo
[]) {
636 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
641 static int via_isa_get_pci_irq(const ViaISAState
*s
, int pin
)
645 return s
->dev
.config
[0x55] >> 4;
647 return s
->dev
.config
[0x56] & 0xf;
649 return s
->dev
.config
[0x56] >> 4;
651 return s
->dev
.config
[0x57] >> 4;
656 void via_isa_set_irq(PCIDevice
*d
, int pin
, int level
)
658 ViaISAState
*s
= VIA_ISA(pci_get_function_0(d
));
659 uint8_t irq
= d
->config
[PCI_INTERRUPT_LINE
], max_irq
= 15;
660 int f
= PCI_FUNC(d
->devfn
);
664 case 0: /* PIRQ/PINT inputs */
665 irq
= via_isa_get_pci_irq(s
, pin
);
666 f
= 8 + pin
; /* Use function 8-11 for PCI interrupt inputs */
668 case 2: /* USB ports 0-1 */
669 case 3: /* USB ports 2-3 */
670 case 5: /* AC97 audio */
675 /* Keep track of the state of all sources */
678 s
->irq_state
[0] |= mask
;
680 s
->irq_state
[0] &= ~mask
;
682 if (irq
== 0 || irq
== 0xff) {
683 return; /* disabled */
685 if (unlikely(irq
> max_irq
|| irq
== 2)) {
686 qemu_log_mask(LOG_GUEST_ERROR
, "Invalid ISA IRQ routing %d for %d",
690 /* Record source state at mapped IRQ */
692 s
->irq_state
[irq
] |= mask
;
694 s
->irq_state
[irq
] &= ~mask
;
696 /* Make sure there are no stuck bits if mapping has changed */
697 s
->irq_state
[irq
] &= s
->irq_state
[0];
698 /* ISA IRQ level is the OR of all sources routed to it */
699 qemu_set_irq(s
->isa_irqs_in
[irq
], !!s
->irq_state
[irq
]);
702 static void via_isa_pirq(void *opaque
, int pin
, int level
)
704 via_isa_set_irq(opaque
, pin
, level
);
707 static void via_isa_request_i8259_irq(void *opaque
, int irq
, int level
)
709 ViaISAState
*s
= opaque
;
710 qemu_set_irq(s
->cpu_intr
, level
);
713 static void via_isa_realize(PCIDevice
*d
, Error
**errp
)
715 ViaISAState
*s
= VIA_ISA(d
);
716 DeviceState
*dev
= DEVICE(d
);
717 PCIBus
*pci_bus
= pci_get_bus(d
);
722 qdev_init_gpio_out_named(dev
, &s
->cpu_intr
, "intr", 1);
723 qdev_init_gpio_in_named(dev
, via_isa_pirq
, "pirq", PCI_NUM_PINS
);
724 isa_irq
= qemu_allocate_irqs(via_isa_request_i8259_irq
, s
, 1);
725 isa_bus
= isa_bus_new(dev
, pci_address_space(d
), pci_address_space_io(d
),
732 s
->isa_irqs_in
= i8259_init(isa_bus
, *isa_irq
);
733 isa_bus_register_input_irqs(isa_bus
, s
->isa_irqs_in
);
734 i8254_pit_init(isa_bus
, 0x40, 0, NULL
);
735 i8257_dma_init(OBJECT(d
), isa_bus
, 0);
738 qdev_prop_set_int32(DEVICE(&s
->rtc
), "base_year", 2000);
739 if (!qdev_realize(DEVICE(&s
->rtc
), BUS(isa_bus
), errp
)) {
742 isa_connect_gpio_out(ISA_DEVICE(&s
->rtc
), 0, s
->rtc
.isairq
);
744 for (i
= 0; i
< PCI_CONFIG_HEADER_SIZE
; i
++) {
745 if (i
< PCI_COMMAND
|| i
>= PCI_REVISION_ID
) {
751 if (!qdev_realize(DEVICE(&s
->via_sio
), BUS(isa_bus
), errp
)) {
755 /* Function 1: IDE */
756 qdev_prop_set_int32(DEVICE(&s
->ide
), "addr", d
->devfn
+ 1);
757 if (!qdev_realize(DEVICE(&s
->ide
), BUS(pci_bus
), errp
)) {
760 for (i
= 0; i
< 2; i
++) {
761 qdev_connect_gpio_out_named(DEVICE(&s
->ide
), "isa-irq", i
,
762 s
->isa_irqs_in
[14 + i
]);
765 /* Functions 2-3: USB Ports */
766 for (i
= 0; i
< ARRAY_SIZE(s
->uhci
); i
++) {
767 qdev_prop_set_int32(DEVICE(&s
->uhci
[i
]), "addr", d
->devfn
+ 2 + i
);
768 if (!qdev_realize(DEVICE(&s
->uhci
[i
]), BUS(pci_bus
), errp
)) {
773 /* Function 4: Power Management */
774 qdev_prop_set_int32(DEVICE(&s
->pm
), "addr", d
->devfn
+ 4);
775 if (!qdev_realize(DEVICE(&s
->pm
), BUS(pci_bus
), errp
)) {
779 /* Function 5: AC97 Audio */
780 qdev_prop_set_int32(DEVICE(&s
->ac97
), "addr", d
->devfn
+ 5);
781 if (!qdev_realize(DEVICE(&s
->ac97
), BUS(pci_bus
), errp
)) {
785 /* Function 6: MC97 Modem */
786 qdev_prop_set_int32(DEVICE(&s
->mc97
), "addr", d
->devfn
+ 6);
787 if (!qdev_realize(DEVICE(&s
->mc97
), BUS(pci_bus
), errp
)) {
792 /* TYPE_VT82C686B_ISA */
794 static void vt82c686b_write_config(PCIDevice
*d
, uint32_t addr
,
795 uint32_t val
, int len
)
797 ViaISAState
*s
= VIA_ISA(d
);
799 trace_via_isa_write(addr
, val
, len
);
800 pci_default_write_config(d
, addr
, val
, len
);
802 /* BIT(1): enable or disable superio config io ports */
803 via_superio_io_enable(&s
->via_sio
, val
& BIT(1));
807 static void vt82c686b_isa_reset(DeviceState
*dev
)
809 ViaISAState
*s
= VIA_ISA(dev
);
810 uint8_t *pci_conf
= s
->dev
.config
;
812 pci_set_long(pci_conf
+ PCI_CAPABILITY_LIST
, 0x000000c0);
813 pci_set_word(pci_conf
+ PCI_COMMAND
, PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
|
814 PCI_COMMAND_MASTER
| PCI_COMMAND_SPECIAL
);
815 pci_set_word(pci_conf
+ PCI_STATUS
, PCI_STATUS_DEVSEL_MEDIUM
);
817 pci_conf
[0x48] = 0x01; /* Miscellaneous Control 3 */
818 pci_conf
[0x4a] = 0x04; /* IDE interrupt Routing */
819 pci_conf
[0x4f] = 0x03; /* DMA/Master Mem Access Control 3 */
820 pci_conf
[0x50] = 0x2d; /* PnP DMA Request Control */
821 pci_conf
[0x59] = 0x04;
822 pci_conf
[0x5a] = 0x04; /* KBC/RTC Control*/
823 pci_conf
[0x5f] = 0x04;
824 pci_conf
[0x77] = 0x10; /* GPIO Control 1/2/3/4 */
827 static void vt82c686b_init(Object
*obj
)
829 ViaISAState
*s
= VIA_ISA(obj
);
831 object_initialize_child(obj
, "sio", &s
->via_sio
, TYPE_VT82C686B_SUPERIO
);
832 object_initialize_child(obj
, "pm", &s
->pm
, TYPE_VT82C686B_PM
);
835 static void vt82c686b_class_init(ObjectClass
*klass
, void *data
)
837 DeviceClass
*dc
= DEVICE_CLASS(klass
);
838 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
840 k
->realize
= via_isa_realize
;
841 k
->config_write
= vt82c686b_write_config
;
842 k
->vendor_id
= PCI_VENDOR_ID_VIA
;
843 k
->device_id
= PCI_DEVICE_ID_VIA_82C686B_ISA
;
844 k
->class_id
= PCI_CLASS_BRIDGE_ISA
;
846 dc
->reset
= vt82c686b_isa_reset
;
847 dc
->desc
= "ISA bridge";
848 dc
->vmsd
= &vmstate_via
;
849 /* Reason: part of VIA VT82C686 southbridge, needs to be wired up */
850 dc
->user_creatable
= false;
853 static const TypeInfo vt82c686b_isa_info
= {
854 .name
= TYPE_VT82C686B_ISA
,
855 .parent
= TYPE_VIA_ISA
,
856 .instance_size
= sizeof(ViaISAState
),
857 .instance_init
= vt82c686b_init
,
858 .class_init
= vt82c686b_class_init
,
861 /* TYPE_VT8231_ISA */
863 static void vt8231_write_config(PCIDevice
*d
, uint32_t addr
,
864 uint32_t val
, int len
)
866 ViaISAState
*s
= VIA_ISA(d
);
868 trace_via_isa_write(addr
, val
, len
);
869 pci_default_write_config(d
, addr
, val
, len
);
871 /* BIT(2): enable or disable superio config io ports */
872 via_superio_io_enable(&s
->via_sio
, val
& BIT(2));
876 static void vt8231_isa_reset(DeviceState
*dev
)
878 ViaISAState
*s
= VIA_ISA(dev
);
879 uint8_t *pci_conf
= s
->dev
.config
;
881 pci_set_long(pci_conf
+ PCI_CAPABILITY_LIST
, 0x000000c0);
882 pci_set_word(pci_conf
+ PCI_COMMAND
, PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
|
883 PCI_COMMAND_MASTER
| PCI_COMMAND_SPECIAL
);
884 pci_set_word(pci_conf
+ PCI_STATUS
, PCI_STATUS_DEVSEL_MEDIUM
);
886 pci_conf
[0x4c] = 0x04; /* IDE interrupt Routing */
887 pci_conf
[0x58] = 0x40; /* Miscellaneous Control 0 */
888 pci_conf
[0x67] = 0x08; /* Fast IR Config */
889 pci_conf
[0x6b] = 0x01; /* Fast IR I/O Base */
892 static void vt8231_init(Object
*obj
)
894 ViaISAState
*s
= VIA_ISA(obj
);
896 object_initialize_child(obj
, "sio", &s
->via_sio
, TYPE_VT8231_SUPERIO
);
897 object_initialize_child(obj
, "pm", &s
->pm
, TYPE_VT8231_PM
);
900 static void vt8231_class_init(ObjectClass
*klass
, void *data
)
902 DeviceClass
*dc
= DEVICE_CLASS(klass
);
903 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
905 k
->realize
= via_isa_realize
;
906 k
->config_write
= vt8231_write_config
;
907 k
->vendor_id
= PCI_VENDOR_ID_VIA
;
908 k
->device_id
= PCI_DEVICE_ID_VIA_8231_ISA
;
909 k
->class_id
= PCI_CLASS_BRIDGE_ISA
;
911 dc
->reset
= vt8231_isa_reset
;
912 dc
->desc
= "ISA bridge";
913 dc
->vmsd
= &vmstate_via
;
914 /* Reason: part of VIA VT8231 southbridge, needs to be wired up */
915 dc
->user_creatable
= false;
918 static const TypeInfo vt8231_isa_info
= {
919 .name
= TYPE_VT8231_ISA
,
920 .parent
= TYPE_VIA_ISA
,
921 .instance_size
= sizeof(ViaISAState
),
922 .instance_init
= vt8231_init
,
923 .class_init
= vt8231_class_init
,
927 static void vt82c686b_register_types(void)
929 type_register_static(&via_pm_info
);
930 type_register_static(&vt82c686b_pm_info
);
931 type_register_static(&vt8231_pm_info
);
932 type_register_static(&via_superio_info
);
933 type_register_static(&vt82c686b_superio_info
);
934 type_register_static(&vt8231_superio_info
);
935 type_register_static(&via_isa_info
);
936 type_register_static(&vt82c686b_isa_info
);
937 type_register_static(&vt8231_isa_info
);
940 type_init(vt82c686b_register_types
)