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.
26 #include "qemu-timer.h"
28 typedef uint32_t pci_addr_t
;
30 //#define DEBUG_VT82C686B
32 #ifdef DEBUG_VT82C686B
33 #define DPRINTF(fmt, ...) fprintf(stderr, "%s: " fmt, __FUNCTION__, ##__VA_ARGS__)
35 #define DPRINTF(fmt, ...)
38 typedef struct SuperIOConfig
45 typedef struct VT82C686BState
{
47 SuperIOConfig superio_conf
;
50 static void superio_ioport_writeb(void *opaque
, uint32_t addr
, uint32_t data
)
53 SuperIOConfig
*superio_conf
= opaque
;
55 DPRINTF("superio_ioport_writeb address 0x%x val 0x%x\n", addr
, data
);
57 superio_conf
->index
= data
& 0xff;
60 switch (superio_conf
->index
) {
76 switch (superio_conf
->index
) {
78 if ((data
& 0xff) != 0xfe) {
79 DPRINTF("chage uart 1 base. unsupported yet\n");
83 if ((data
& 0xff) != 0xbe) {
84 DPRINTF("chage uart 2 base. unsupported yet\n");
89 superio_conf
->config
[superio_conf
->index
] = data
& 0xff;
93 superio_conf
->config
[superio_conf
->index
] = data
& 0xff;
97 static uint32_t superio_ioport_readb(void *opaque
, uint32_t addr
)
99 SuperIOConfig
*superio_conf
= opaque
;
101 DPRINTF("superio_ioport_readb address 0x%x\n", addr
);
102 return (superio_conf
->config
[superio_conf
->index
]);
105 static void vt82c686b_reset(void * opaque
)
107 PCIDevice
*d
= opaque
;
108 uint8_t *pci_conf
= d
->config
;
109 VT82C686BState
*vt82c
= DO_UPCAST(VT82C686BState
, dev
, d
);
111 pci_set_long(pci_conf
+ PCI_CAPABILITY_LIST
, 0x000000c0);
112 pci_set_word(pci_conf
+ PCI_COMMAND
, PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
|
113 PCI_COMMAND_MASTER
| PCI_COMMAND_SPECIAL
);
114 pci_set_word(pci_conf
+ PCI_STATUS
, PCI_STATUS_DEVSEL_MEDIUM
);
116 pci_conf
[0x48] = 0x01; /* Miscellaneous Control 3 */
117 pci_conf
[0x4a] = 0x04; /* IDE interrupt Routing */
118 pci_conf
[0x4f] = 0x03; /* DMA/Master Mem Access Control 3 */
119 pci_conf
[0x50] = 0x2d; /* PnP DMA Request Control */
120 pci_conf
[0x59] = 0x04;
121 pci_conf
[0x5a] = 0x04; /* KBC/RTC Control*/
122 pci_conf
[0x5f] = 0x04;
123 pci_conf
[0x77] = 0x10; /* GPIO Control 1/2/3/4 */
125 vt82c
->superio_conf
.config
[0xe0] = 0x3c;
126 vt82c
->superio_conf
.config
[0xe2] = 0x03;
127 vt82c
->superio_conf
.config
[0xe3] = 0xfc;
128 vt82c
->superio_conf
.config
[0xe6] = 0xde;
129 vt82c
->superio_conf
.config
[0xe7] = 0xfe;
130 vt82c
->superio_conf
.config
[0xe8] = 0xbe;
133 /* write config pci function0 registers. PCI-ISA bridge */
134 static void vt82c686b_write_config(PCIDevice
* d
, uint32_t address
,
135 uint32_t val
, int len
)
137 VT82C686BState
*vt686
= DO_UPCAST(VT82C686BState
, dev
, d
);
139 DPRINTF("vt82c686b_write_config address 0x%x val 0x%x len 0x%x\n",
142 pci_default_write_config(d
, address
, val
, len
);
143 if (address
== 0x85) { /* enable or disable super IO configure */
145 /* floppy also uses 0x3f0 and 0x3f1.
146 * But we do not emulate flopy,so just set it here. */
147 isa_unassign_ioport(0x3f0, 2);
148 register_ioport_read(0x3f0, 2, 1, superio_ioport_readb
,
149 &vt686
->superio_conf
);
150 register_ioport_write(0x3f0, 2, 1, superio_ioport_writeb
,
151 &vt686
->superio_conf
);
153 isa_unassign_ioport(0x3f0, 2);
158 #define ACPI_DBG_IO_ADDR 0xb044
160 typedef struct VT686PMState
{
167 uint32_t smb_io_base
;
170 typedef struct VT686AC97State
{
174 typedef struct VT686MC97State
{
178 static void pm_update_sci(VT686PMState
*s
)
180 int sci_level
, pmsts
;
182 pmsts
= acpi_pm1_evt_get_sts(&s
->pm1a
, s
->tmr
.overflow_time
);
183 sci_level
= (((pmsts
& s
->pm1a
.en
) &
184 (ACPI_BITMASK_RT_CLOCK_ENABLE
|
185 ACPI_BITMASK_POWER_BUTTON_ENABLE
|
186 ACPI_BITMASK_GLOBAL_LOCK_ENABLE
|
187 ACPI_BITMASK_TIMER_ENABLE
)) != 0);
188 qemu_set_irq(s
->dev
.irq
[0], sci_level
);
189 /* schedule a timer interruption if needed */
190 acpi_pm_tmr_update(&s
->tmr
, (s
->pm1a
.en
& ACPI_BITMASK_TIMER_ENABLE
) &&
191 !(pmsts
& ACPI_BITMASK_TIMER_STATUS
));
194 static void pm_tmr_timer(ACPIPMTimer
*tmr
)
196 VT686PMState
*s
= container_of(tmr
, VT686PMState
, tmr
);
200 static void pm_ioport_writew(void *opaque
, uint32_t addr
, uint32_t val
)
202 VT686PMState
*s
= opaque
;
207 acpi_pm1_evt_write_sts(&s
->pm1a
, &s
->tmr
, val
);
215 acpi_pm1_cnt_write(&s
->pm1a
, &s
->pm1_cnt
, val
);
220 DPRINTF("PM writew port=0x%04x val=0x%02x\n", addr
, val
);
223 static uint32_t pm_ioport_readw(void *opaque
, uint32_t addr
)
225 VT686PMState
*s
= opaque
;
231 val
= acpi_pm1_evt_get_sts(&s
->pm1a
, s
->tmr
.overflow_time
);
237 val
= s
->pm1_cnt
.cnt
;
243 DPRINTF("PM readw port=0x%04x val=0x%02x\n", addr
, val
);
247 static void pm_ioport_writel(void *opaque
, uint32_t addr
, uint32_t val
)
250 DPRINTF("PM writel port=0x%04x val=0x%08x\n", addr
, val
);
253 static uint32_t pm_ioport_readl(void *opaque
, uint32_t addr
)
255 VT686PMState
*s
= opaque
;
261 val
= acpi_pm_tmr_get(&s
->tmr
);
267 DPRINTF("PM readl port=0x%04x val=0x%08x\n", addr
, val
);
271 static void pm_io_space_update(VT686PMState
*s
)
275 if (s
->dev
.config
[0x80] & 1) {
276 pm_io_base
= pci_get_long(s
->dev
.config
+ 0x40);
277 pm_io_base
&= 0xffc0;
279 /* XXX: need to improve memory and ioport allocation */
280 DPRINTF("PM: mapping to 0x%x\n", pm_io_base
);
281 register_ioport_write(pm_io_base
, 64, 2, pm_ioport_writew
, s
);
282 register_ioport_read(pm_io_base
, 64, 2, pm_ioport_readw
, s
);
283 register_ioport_write(pm_io_base
, 64, 4, pm_ioport_writel
, s
);
284 register_ioport_read(pm_io_base
, 64, 4, pm_ioport_readl
, s
);
288 static void pm_write_config(PCIDevice
*d
,
289 uint32_t address
, uint32_t val
, int len
)
291 DPRINTF("pm_write_config address 0x%x val 0x%x len 0x%x\n",
293 pci_default_write_config(d
, address
, val
, len
);
296 static int vmstate_acpi_post_load(void *opaque
, int version_id
)
298 VT686PMState
*s
= opaque
;
300 pm_io_space_update(s
);
304 static const VMStateDescription vmstate_acpi
= {
305 .name
= "vt82c686b_pm",
307 .minimum_version_id
= 1,
308 .minimum_version_id_old
= 1,
309 .post_load
= vmstate_acpi_post_load
,
310 .fields
= (VMStateField
[]) {
311 VMSTATE_PCI_DEVICE(dev
, VT686PMState
),
312 VMSTATE_UINT16(pm1a
.sts
, VT686PMState
),
313 VMSTATE_UINT16(pm1a
.en
, VT686PMState
),
314 VMSTATE_UINT16(pm1_cnt
.cnt
, VT686PMState
),
315 VMSTATE_STRUCT(apm
, VT686PMState
, 0, vmstate_apm
, APMState
),
316 VMSTATE_TIMER(tmr
.timer
, VT686PMState
),
317 VMSTATE_INT64(tmr
.overflow_time
, VT686PMState
),
318 VMSTATE_END_OF_LIST()
323 * TODO: vt82c686b_ac97_init() and vt82c686b_mc97_init()
324 * just register a PCI device now, functionalities will be implemented later.
327 static int vt82c686b_ac97_initfn(PCIDevice
*dev
)
329 VT686AC97State
*s
= DO_UPCAST(VT686AC97State
, dev
, dev
);
330 uint8_t *pci_conf
= s
->dev
.config
;
332 pci_set_word(pci_conf
+ PCI_COMMAND
, PCI_COMMAND_INVALIDATE
|
334 pci_set_word(pci_conf
+ PCI_STATUS
, PCI_STATUS_CAP_LIST
|
335 PCI_STATUS_DEVSEL_MEDIUM
);
336 pci_set_long(pci_conf
+ PCI_INTERRUPT_PIN
, 0x03);
341 void vt82c686b_ac97_init(PCIBus
*bus
, int devfn
)
345 dev
= pci_create(bus
, devfn
, "VT82C686B_AC97");
346 qdev_init_nofail(&dev
->qdev
);
349 static void via_ac97_class_init(ObjectClass
*klass
, void *data
)
351 DeviceClass
*dc
= DEVICE_CLASS(klass
);
352 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
354 k
->init
= vt82c686b_ac97_initfn
;
355 k
->vendor_id
= PCI_VENDOR_ID_VIA
;
356 k
->device_id
= PCI_DEVICE_ID_VIA_AC97
;
358 k
->class_id
= PCI_CLASS_MULTIMEDIA_AUDIO
;
362 static TypeInfo via_ac97_info
= {
363 .name
= "VT82C686B_AC97",
364 .parent
= TYPE_PCI_DEVICE
,
365 .instance_size
= sizeof(VT686AC97State
),
366 .class_init
= via_ac97_class_init
,
369 static int vt82c686b_mc97_initfn(PCIDevice
*dev
)
371 VT686MC97State
*s
= DO_UPCAST(VT686MC97State
, dev
, dev
);
372 uint8_t *pci_conf
= s
->dev
.config
;
374 pci_set_word(pci_conf
+ PCI_COMMAND
, PCI_COMMAND_INVALIDATE
|
375 PCI_COMMAND_VGA_PALETTE
);
376 pci_set_word(pci_conf
+ PCI_STATUS
, PCI_STATUS_DEVSEL_MEDIUM
);
377 pci_set_long(pci_conf
+ PCI_INTERRUPT_PIN
, 0x03);
382 void vt82c686b_mc97_init(PCIBus
*bus
, int devfn
)
386 dev
= pci_create(bus
, devfn
, "VT82C686B_MC97");
387 qdev_init_nofail(&dev
->qdev
);
390 static void via_mc97_class_init(ObjectClass
*klass
, void *data
)
392 DeviceClass
*dc
= DEVICE_CLASS(klass
);
393 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
395 k
->init
= vt82c686b_mc97_initfn
;
396 k
->vendor_id
= PCI_VENDOR_ID_VIA
;
397 k
->device_id
= PCI_DEVICE_ID_VIA_MC97
;
398 k
->class_id
= PCI_CLASS_COMMUNICATION_OTHER
;
403 static TypeInfo via_mc97_info
= {
404 .name
= "VT82C686B_MC97",
405 .parent
= TYPE_PCI_DEVICE
,
406 .instance_size
= sizeof(VT686MC97State
),
407 .class_init
= via_mc97_class_init
,
410 /* vt82c686 pm init */
411 static int vt82c686b_pm_initfn(PCIDevice
*dev
)
413 VT686PMState
*s
= DO_UPCAST(VT686PMState
, dev
, dev
);
416 pci_conf
= s
->dev
.config
;
417 pci_set_word(pci_conf
+ PCI_COMMAND
, 0);
418 pci_set_word(pci_conf
+ PCI_STATUS
, PCI_STATUS_FAST_BACK
|
419 PCI_STATUS_DEVSEL_MEDIUM
);
421 /* 0x48-0x4B is Power Management I/O Base */
422 pci_set_long(pci_conf
+ 0x48, 0x00000001);
424 /* SMB ports:0xeee0~0xeeef */
425 s
->smb_io_base
=((s
->smb_io_base
& 0xfff0) + 0x0);
426 pci_conf
[0x90] = s
->smb_io_base
| 1;
427 pci_conf
[0x91] = s
->smb_io_base
>> 8;
428 pci_conf
[0xd2] = 0x90;
429 register_ioport_write(s
->smb_io_base
, 0xf, 1, smb_ioport_writeb
, &s
->smb
);
430 register_ioport_read(s
->smb_io_base
, 0xf, 1, smb_ioport_readb
, &s
->smb
);
432 apm_init(&s
->apm
, NULL
, s
);
434 acpi_pm_tmr_init(&s
->tmr
, pm_tmr_timer
);
435 acpi_pm1_cnt_init(&s
->pm1_cnt
, NULL
);
437 pm_smbus_init(&s
->dev
.qdev
, &s
->smb
);
442 i2c_bus
*vt82c686b_pm_init(PCIBus
*bus
, int devfn
, uint32_t smb_io_base
,
448 dev
= pci_create(bus
, devfn
, "VT82C686B_PM");
449 qdev_prop_set_uint32(&dev
->qdev
, "smb_io_base", smb_io_base
);
451 s
= DO_UPCAST(VT686PMState
, dev
, dev
);
453 qdev_init_nofail(&dev
->qdev
);
458 static Property via_pm_properties
[] = {
459 DEFINE_PROP_UINT32("smb_io_base", VT686PMState
, smb_io_base
, 0),
460 DEFINE_PROP_END_OF_LIST(),
463 static void via_pm_class_init(ObjectClass
*klass
, void *data
)
465 DeviceClass
*dc
= DEVICE_CLASS(klass
);
466 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
468 k
->init
= vt82c686b_pm_initfn
;
469 k
->config_write
= pm_write_config
;
470 k
->vendor_id
= PCI_VENDOR_ID_VIA
;
471 k
->device_id
= PCI_DEVICE_ID_VIA_ACPI
;
472 k
->class_id
= PCI_CLASS_BRIDGE_OTHER
;
475 dc
->vmsd
= &vmstate_acpi
;
476 dc
->props
= via_pm_properties
;
479 static TypeInfo via_pm_info
= {
480 .name
= "VT82C686B_PM",
481 .parent
= TYPE_PCI_DEVICE
,
482 .instance_size
= sizeof(VT686PMState
),
483 .class_init
= via_pm_class_init
,
486 static const VMStateDescription vmstate_via
= {
489 .minimum_version_id
= 1,
490 .minimum_version_id_old
= 1,
491 .fields
= (VMStateField
[]) {
492 VMSTATE_PCI_DEVICE(dev
, VT82C686BState
),
493 VMSTATE_END_OF_LIST()
497 /* init the PCI-to-ISA bridge */
498 static int vt82c686b_initfn(PCIDevice
*d
)
504 isa_bus_new(&d
->qdev
, pci_address_space_io(d
));
506 pci_conf
= d
->config
;
507 pci_config_set_prog_interface(pci_conf
, 0x0);
510 for (i
= 0x00; i
< 0xff; i
++) {
511 if (i
<=0x03 || (i
>=0x08 && i
<=0x3f)) {
516 qemu_register_reset(vt82c686b_reset
, d
);
521 ISABus
*vt82c686b_init(PCIBus
*bus
, int devfn
)
525 d
= pci_create_simple_multifunction(bus
, devfn
, true, "VT82C686B");
527 return DO_UPCAST(ISABus
, qbus
, qdev_get_child_bus(&d
->qdev
, "isa.0"));
530 static void via_class_init(ObjectClass
*klass
, void *data
)
532 DeviceClass
*dc
= DEVICE_CLASS(klass
);
533 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
535 k
->init
= vt82c686b_initfn
;
536 k
->config_write
= vt82c686b_write_config
;
537 k
->vendor_id
= PCI_VENDOR_ID_VIA
;
538 k
->device_id
= PCI_DEVICE_ID_VIA_ISA_BRIDGE
;
539 k
->class_id
= PCI_CLASS_BRIDGE_ISA
;
541 dc
->desc
= "ISA bridge";
543 dc
->vmsd
= &vmstate_via
;
546 static TypeInfo via_info
= {
548 .parent
= TYPE_PCI_DEVICE
,
549 .instance_size
= sizeof(VT82C686BState
),
550 .class_init
= via_class_init
,
553 static void vt82c686b_register_types(void)
555 type_register_static(&via_ac97_info
);
556 type_register_static(&via_mc97_info
);
557 type_register_static(&via_pm_info
);
558 type_register_static(&via_info
);
561 type_init(vt82c686b_register_types
)