OSDN Git Service

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