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"
27 #include "exec-memory.h"
29 typedef uint32_t pci_addr_t
;
31 //#define DEBUG_VT82C686B
33 #ifdef DEBUG_VT82C686B
34 #define DPRINTF(fmt, ...) fprintf(stderr, "%s: " fmt, __FUNCTION__, ##__VA_ARGS__)
36 #define DPRINTF(fmt, ...)
39 typedef struct SuperIOConfig
46 typedef struct VT82C686BState
{
48 SuperIOConfig superio_conf
;
51 static void superio_ioport_writeb(void *opaque
, uint32_t addr
, uint32_t data
)
54 SuperIOConfig
*superio_conf
= opaque
;
56 DPRINTF("superio_ioport_writeb address 0x%x val 0x%x\n", addr
, data
);
58 superio_conf
->index
= data
& 0xff;
61 switch (superio_conf
->index
) {
77 switch (superio_conf
->index
) {
79 if ((data
& 0xff) != 0xfe) {
80 DPRINTF("chage uart 1 base. unsupported yet\n");
84 if ((data
& 0xff) != 0xbe) {
85 DPRINTF("chage uart 2 base. unsupported yet\n");
90 superio_conf
->config
[superio_conf
->index
] = data
& 0xff;
94 superio_conf
->config
[superio_conf
->index
] = data
& 0xff;
98 static uint32_t superio_ioport_readb(void *opaque
, uint32_t addr
)
100 SuperIOConfig
*superio_conf
= opaque
;
102 DPRINTF("superio_ioport_readb address 0x%x\n", addr
);
103 return (superio_conf
->config
[superio_conf
->index
]);
106 static void vt82c686b_reset(void * opaque
)
108 PCIDevice
*d
= opaque
;
109 uint8_t *pci_conf
= d
->config
;
110 VT82C686BState
*vt82c
= DO_UPCAST(VT82C686BState
, dev
, d
);
112 pci_set_long(pci_conf
+ PCI_CAPABILITY_LIST
, 0x000000c0);
113 pci_set_word(pci_conf
+ PCI_COMMAND
, PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
|
114 PCI_COMMAND_MASTER
| PCI_COMMAND_SPECIAL
);
115 pci_set_word(pci_conf
+ PCI_STATUS
, PCI_STATUS_DEVSEL_MEDIUM
);
117 pci_conf
[0x48] = 0x01; /* Miscellaneous Control 3 */
118 pci_conf
[0x4a] = 0x04; /* IDE interrupt Routing */
119 pci_conf
[0x4f] = 0x03; /* DMA/Master Mem Access Control 3 */
120 pci_conf
[0x50] = 0x2d; /* PnP DMA Request Control */
121 pci_conf
[0x59] = 0x04;
122 pci_conf
[0x5a] = 0x04; /* KBC/RTC Control*/
123 pci_conf
[0x5f] = 0x04;
124 pci_conf
[0x77] = 0x10; /* GPIO Control 1/2/3/4 */
126 vt82c
->superio_conf
.config
[0xe0] = 0x3c;
127 vt82c
->superio_conf
.config
[0xe2] = 0x03;
128 vt82c
->superio_conf
.config
[0xe3] = 0xfc;
129 vt82c
->superio_conf
.config
[0xe6] = 0xde;
130 vt82c
->superio_conf
.config
[0xe7] = 0xfe;
131 vt82c
->superio_conf
.config
[0xe8] = 0xbe;
134 /* write config pci function0 registers. PCI-ISA bridge */
135 static void vt82c686b_write_config(PCIDevice
* d
, uint32_t address
,
136 uint32_t val
, int len
)
138 VT82C686BState
*vt686
= DO_UPCAST(VT82C686BState
, dev
, d
);
140 DPRINTF("vt82c686b_write_config address 0x%x val 0x%x len 0x%x\n",
143 pci_default_write_config(d
, address
, val
, len
);
144 if (address
== 0x85) { /* enable or disable super IO configure */
146 /* floppy also uses 0x3f0 and 0x3f1.
147 * But we do not emulate flopy,so just set it here. */
148 isa_unassign_ioport(0x3f0, 2);
149 register_ioport_read(0x3f0, 2, 1, superio_ioport_readb
,
150 &vt686
->superio_conf
);
151 register_ioport_write(0x3f0, 2, 1, superio_ioport_writeb
,
152 &vt686
->superio_conf
);
154 isa_unassign_ioport(0x3f0, 2);
159 #define ACPI_DBG_IO_ADDR 0xb044
161 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
->ar
);
183 sci_level
= (((pmsts
& s
->ar
.pm1
.evt
.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
->ar
, (s
->ar
.pm1
.evt
.en
& ACPI_BITMASK_TIMER_ENABLE
) &&
191 !(pmsts
& ACPI_BITMASK_TIMER_STATUS
));
194 static void pm_tmr_timer(ACPIREGS
*ar
)
196 VT686PMState
*s
= container_of(ar
, VT686PMState
, ar
);
200 static void pm_ioport_writew(void *opaque
, uint32_t addr
, uint32_t val
)
207 DPRINTF("PM writew port=0x%04x val=0x%02x\n", addr
, val
);
210 static uint32_t pm_ioport_readw(void *opaque
, uint32_t addr
)
220 DPRINTF("PM readw port=0x%04x val=0x%02x\n", addr
, val
);
224 static void pm_ioport_writel(void *opaque
, uint32_t addr
, uint32_t val
)
227 DPRINTF("PM writel port=0x%04x val=0x%08x\n", addr
, val
);
230 static uint32_t pm_ioport_readl(void *opaque
, uint32_t addr
)
240 DPRINTF("PM readl port=0x%04x val=0x%08x\n", addr
, val
);
244 static const MemoryRegionOps pm_io_ops
= {
245 .old_portio
= (MemoryRegionPortio
[]) {
246 { .offset
= 0, .len
= 64, .size
= 2,
247 .read
= pm_ioport_readw
, .write
= pm_ioport_writew
},
248 { .offset
= 0, .len
= 64, .size
= 4,
249 .read
= pm_ioport_readl
, .write
= pm_ioport_writel
},
250 PORTIO_END_OF_LIST(),
252 .valid
.min_access_size
= 1,
253 .valid
.max_access_size
= 4,
254 .impl
.min_access_size
= 1,
255 .impl
.max_access_size
= 4,
256 .endianness
= DEVICE_LITTLE_ENDIAN
,
259 static void pm_io_space_update(VT686PMState
*s
)
263 pm_io_base
= pci_get_long(s
->dev
.config
+ 0x40);
264 pm_io_base
&= 0xffc0;
266 memory_region_transaction_begin();
267 memory_region_set_enabled(&s
->io
, s
->dev
.config
[0x80] & 1);
268 memory_region_set_address(&s
->io
, pm_io_base
);
269 memory_region_transaction_commit();
272 static void pm_write_config(PCIDevice
*d
,
273 uint32_t address
, uint32_t val
, int len
)
275 DPRINTF("pm_write_config address 0x%x val 0x%x len 0x%x\n",
277 pci_default_write_config(d
, address
, val
, len
);
280 static int vmstate_acpi_post_load(void *opaque
, int version_id
)
282 VT686PMState
*s
= opaque
;
284 pm_io_space_update(s
);
288 static const VMStateDescription vmstate_acpi
= {
289 .name
= "vt82c686b_pm",
291 .minimum_version_id
= 1,
292 .minimum_version_id_old
= 1,
293 .post_load
= vmstate_acpi_post_load
,
294 .fields
= (VMStateField
[]) {
295 VMSTATE_PCI_DEVICE(dev
, VT686PMState
),
296 VMSTATE_UINT16(ar
.pm1
.evt
.sts
, VT686PMState
),
297 VMSTATE_UINT16(ar
.pm1
.evt
.en
, VT686PMState
),
298 VMSTATE_UINT16(ar
.pm1
.cnt
.cnt
, VT686PMState
),
299 VMSTATE_STRUCT(apm
, VT686PMState
, 0, vmstate_apm
, APMState
),
300 VMSTATE_TIMER(ar
.tmr
.timer
, VT686PMState
),
301 VMSTATE_INT64(ar
.tmr
.overflow_time
, VT686PMState
),
302 VMSTATE_END_OF_LIST()
307 * TODO: vt82c686b_ac97_init() and vt82c686b_mc97_init()
308 * just register a PCI device now, functionalities will be implemented later.
311 static int vt82c686b_ac97_initfn(PCIDevice
*dev
)
313 VT686AC97State
*s
= DO_UPCAST(VT686AC97State
, dev
, dev
);
314 uint8_t *pci_conf
= s
->dev
.config
;
316 pci_set_word(pci_conf
+ PCI_COMMAND
, PCI_COMMAND_INVALIDATE
|
318 pci_set_word(pci_conf
+ PCI_STATUS
, PCI_STATUS_CAP_LIST
|
319 PCI_STATUS_DEVSEL_MEDIUM
);
320 pci_set_long(pci_conf
+ PCI_INTERRUPT_PIN
, 0x03);
325 void vt82c686b_ac97_init(PCIBus
*bus
, int devfn
)
329 dev
= pci_create(bus
, devfn
, "VT82C686B_AC97");
330 qdev_init_nofail(&dev
->qdev
);
333 static void via_ac97_class_init(ObjectClass
*klass
, void *data
)
335 DeviceClass
*dc
= DEVICE_CLASS(klass
);
336 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
338 k
->init
= vt82c686b_ac97_initfn
;
339 k
->vendor_id
= PCI_VENDOR_ID_VIA
;
340 k
->device_id
= PCI_DEVICE_ID_VIA_AC97
;
342 k
->class_id
= PCI_CLASS_MULTIMEDIA_AUDIO
;
346 static TypeInfo via_ac97_info
= {
347 .name
= "VT82C686B_AC97",
348 .parent
= TYPE_PCI_DEVICE
,
349 .instance_size
= sizeof(VT686AC97State
),
350 .class_init
= via_ac97_class_init
,
353 static int vt82c686b_mc97_initfn(PCIDevice
*dev
)
355 VT686MC97State
*s
= DO_UPCAST(VT686MC97State
, dev
, dev
);
356 uint8_t *pci_conf
= s
->dev
.config
;
358 pci_set_word(pci_conf
+ PCI_COMMAND
, PCI_COMMAND_INVALIDATE
|
359 PCI_COMMAND_VGA_PALETTE
);
360 pci_set_word(pci_conf
+ PCI_STATUS
, PCI_STATUS_DEVSEL_MEDIUM
);
361 pci_set_long(pci_conf
+ PCI_INTERRUPT_PIN
, 0x03);
366 void vt82c686b_mc97_init(PCIBus
*bus
, int devfn
)
370 dev
= pci_create(bus
, devfn
, "VT82C686B_MC97");
371 qdev_init_nofail(&dev
->qdev
);
374 static void via_mc97_class_init(ObjectClass
*klass
, void *data
)
376 DeviceClass
*dc
= DEVICE_CLASS(klass
);
377 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
379 k
->init
= vt82c686b_mc97_initfn
;
380 k
->vendor_id
= PCI_VENDOR_ID_VIA
;
381 k
->device_id
= PCI_DEVICE_ID_VIA_MC97
;
382 k
->class_id
= PCI_CLASS_COMMUNICATION_OTHER
;
387 static TypeInfo via_mc97_info
= {
388 .name
= "VT82C686B_MC97",
389 .parent
= TYPE_PCI_DEVICE
,
390 .instance_size
= sizeof(VT686MC97State
),
391 .class_init
= via_mc97_class_init
,
394 /* vt82c686 pm init */
395 static int vt82c686b_pm_initfn(PCIDevice
*dev
)
397 VT686PMState
*s
= DO_UPCAST(VT686PMState
, dev
, dev
);
400 pci_conf
= s
->dev
.config
;
401 pci_set_word(pci_conf
+ PCI_COMMAND
, 0);
402 pci_set_word(pci_conf
+ PCI_STATUS
, PCI_STATUS_FAST_BACK
|
403 PCI_STATUS_DEVSEL_MEDIUM
);
405 /* 0x48-0x4B is Power Management I/O Base */
406 pci_set_long(pci_conf
+ 0x48, 0x00000001);
408 /* SMB ports:0xeee0~0xeeef */
409 s
->smb_io_base
=((s
->smb_io_base
& 0xfff0) + 0x0);
410 pci_conf
[0x90] = s
->smb_io_base
| 1;
411 pci_conf
[0x91] = s
->smb_io_base
>> 8;
412 pci_conf
[0xd2] = 0x90;
413 register_ioport_write(s
->smb_io_base
, 0xf, 1, smb_ioport_writeb
, &s
->smb
);
414 register_ioport_read(s
->smb_io_base
, 0xf, 1, smb_ioport_readb
, &s
->smb
);
416 apm_init(&s
->apm
, NULL
, s
);
418 memory_region_init_io(&s
->io
, &pm_io_ops
, s
, "vt82c686-pm", 64);
419 memory_region_set_enabled(&s
->io
, false);
420 memory_region_add_subregion(get_system_io(), 0, &s
->io
);
422 acpi_pm_tmr_init(&s
->ar
, pm_tmr_timer
, &s
->io
);
423 acpi_pm1_evt_init(&s
->ar
, pm_tmr_timer
, &s
->io
);
424 acpi_pm1_cnt_init(&s
->ar
, &s
->io
);
426 pm_smbus_init(&s
->dev
.qdev
, &s
->smb
);
431 i2c_bus
*vt82c686b_pm_init(PCIBus
*bus
, int devfn
, uint32_t smb_io_base
,
437 dev
= pci_create(bus
, devfn
, "VT82C686B_PM");
438 qdev_prop_set_uint32(&dev
->qdev
, "smb_io_base", smb_io_base
);
440 s
= DO_UPCAST(VT686PMState
, dev
, dev
);
442 qdev_init_nofail(&dev
->qdev
);
447 static Property via_pm_properties
[] = {
448 DEFINE_PROP_UINT32("smb_io_base", VT686PMState
, smb_io_base
, 0),
449 DEFINE_PROP_END_OF_LIST(),
452 static void via_pm_class_init(ObjectClass
*klass
, void *data
)
454 DeviceClass
*dc
= DEVICE_CLASS(klass
);
455 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
457 k
->init
= vt82c686b_pm_initfn
;
458 k
->config_write
= pm_write_config
;
459 k
->vendor_id
= PCI_VENDOR_ID_VIA
;
460 k
->device_id
= PCI_DEVICE_ID_VIA_ACPI
;
461 k
->class_id
= PCI_CLASS_BRIDGE_OTHER
;
464 dc
->vmsd
= &vmstate_acpi
;
465 dc
->props
= via_pm_properties
;
468 static TypeInfo via_pm_info
= {
469 .name
= "VT82C686B_PM",
470 .parent
= TYPE_PCI_DEVICE
,
471 .instance_size
= sizeof(VT686PMState
),
472 .class_init
= via_pm_class_init
,
475 static const VMStateDescription vmstate_via
= {
478 .minimum_version_id
= 1,
479 .minimum_version_id_old
= 1,
480 .fields
= (VMStateField
[]) {
481 VMSTATE_PCI_DEVICE(dev
, VT82C686BState
),
482 VMSTATE_END_OF_LIST()
486 /* init the PCI-to-ISA bridge */
487 static int vt82c686b_initfn(PCIDevice
*d
)
493 isa_bus_new(&d
->qdev
, pci_address_space_io(d
));
495 pci_conf
= d
->config
;
496 pci_config_set_prog_interface(pci_conf
, 0x0);
499 for (i
= 0x00; i
< 0xff; i
++) {
500 if (i
<=0x03 || (i
>=0x08 && i
<=0x3f)) {
505 qemu_register_reset(vt82c686b_reset
, d
);
510 ISABus
*vt82c686b_init(PCIBus
*bus
, int devfn
)
514 d
= pci_create_simple_multifunction(bus
, devfn
, true, "VT82C686B");
516 return DO_UPCAST(ISABus
, qbus
, qdev_get_child_bus(&d
->qdev
, "isa.0"));
519 static void via_class_init(ObjectClass
*klass
, void *data
)
521 DeviceClass
*dc
= DEVICE_CLASS(klass
);
522 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
524 k
->init
= vt82c686b_initfn
;
525 k
->config_write
= vt82c686b_write_config
;
526 k
->vendor_id
= PCI_VENDOR_ID_VIA
;
527 k
->device_id
= PCI_DEVICE_ID_VIA_ISA_BRIDGE
;
528 k
->class_id
= PCI_CLASS_BRIDGE_ISA
;
530 dc
->desc
= "ISA bridge";
532 dc
->vmsd
= &vmstate_via
;
535 static TypeInfo via_info
= {
537 .parent
= TYPE_PCI_DEVICE
,
538 .instance_size
= sizeof(VT82C686BState
),
539 .class_init
= via_class_init
,
542 static void vt82c686b_register_types(void)
544 type_register_static(&via_ac97_info
);
545 type_register_static(&via_mc97_info
);
546 type_register_static(&via_pm_info
);
547 type_register_static(&via_info
);
550 type_init(vt82c686b_register_types
)