Merge commit 'd34e8f6e9d3a396c3327aa9807c83f9e1f4a7bd7' into upstream-merge
[qemu-kvm.git] / hw / i82378.c
blob99b453abdc51ee9c4d51ee33676e14622585a0f7
1 /*
2 * QEMU Intel i82378 emulation (PCI to ISA bridge)
4 * Copyright (c) 2010-2011 Hervé Poussineau
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "pci.h"
21 #include "pc.h"
23 //#define DEBUG_I82378
25 #ifdef DEBUG_I82378
26 #define DPRINTF(fmt, ...) \
27 do { fprintf(stderr, "i82378: " fmt , ## __VA_ARGS__); } while (0)
28 #else
29 #define DPRINTF(fmt, ...) \
30 do {} while (0)
31 #endif
33 #define BADF(fmt, ...) \
34 do { fprintf(stderr, "i82378 ERROR: " fmt , ## __VA_ARGS__); } while (0)
36 typedef struct I82378State {
37 qemu_irq out[2];
38 qemu_irq *i8259;
39 MemoryRegion io;
40 MemoryRegion mem;
41 } I82378State;
43 typedef struct PCIi82378State {
44 PCIDevice pci_dev;
45 uint32_t isa_io_base;
46 uint32_t isa_mem_base;
47 I82378State state;
48 } PCIi82378State;
50 static const VMStateDescription vmstate_pci_i82378 = {
51 .name = "pci-i82378",
52 .version_id = 0,
53 .minimum_version_id = 0,
54 .fields = (VMStateField[]) {
55 VMSTATE_PCI_DEVICE(pci_dev, PCIi82378State),
56 VMSTATE_END_OF_LIST()
60 static void i82378_io_write(void *opaque, target_phys_addr_t addr,
61 uint64_t value, unsigned int size)
63 switch (size) {
64 case 1:
65 DPRINTF("%s: " TARGET_FMT_plx "=%02" PRIx64 "\n", __func__,
66 addr, value);
67 cpu_outb(addr, value);
68 break;
69 case 2:
70 DPRINTF("%s: " TARGET_FMT_plx "=%04" PRIx64 "\n", __func__,
71 addr, value);
72 cpu_outw(addr, value);
73 break;
74 case 4:
75 DPRINTF("%s: " TARGET_FMT_plx "=%08" PRIx64 "\n", __func__,
76 addr, value);
77 cpu_outl(addr, value);
78 break;
79 default:
80 abort();
84 static uint64_t i82378_io_read(void *opaque, target_phys_addr_t addr,
85 unsigned int size)
87 DPRINTF("%s: " TARGET_FMT_plx "\n", __func__, addr);
88 switch (size) {
89 case 1:
90 return cpu_inb(addr);
91 case 2:
92 return cpu_inw(addr);
93 case 4:
94 return cpu_inl(addr);
95 default:
96 abort();
100 static const MemoryRegionOps i82378_io_ops = {
101 .read = i82378_io_read,
102 .write = i82378_io_write,
103 .endianness = DEVICE_LITTLE_ENDIAN,
106 static void i82378_mem_write(void *opaque, target_phys_addr_t addr,
107 uint64_t value, unsigned int size)
109 switch (size) {
110 case 1:
111 DPRINTF("%s: " TARGET_FMT_plx "=%02" PRIx64 "\n", __func__,
112 addr, value);
113 cpu_outb(addr, value);
114 break;
115 case 2:
116 DPRINTF("%s: " TARGET_FMT_plx "=%04" PRIx64 "\n", __func__,
117 addr, value);
118 cpu_outw(addr, value);
119 break;
120 case 4:
121 DPRINTF("%s: " TARGET_FMT_plx "=%08" PRIx64 "\n", __func__,
122 addr, value);
123 cpu_outl(addr, value);
124 break;
125 default:
126 abort();
130 static uint64_t i82378_mem_read(void *opaque, target_phys_addr_t addr,
131 unsigned int size)
133 DPRINTF("%s: " TARGET_FMT_plx "\n", __func__, addr);
134 switch (size) {
135 case 1:
136 return cpu_inb(addr);
137 case 2:
138 return cpu_inw(addr);
139 case 4:
140 return cpu_inl(addr);
141 default:
142 abort();
146 static const MemoryRegionOps i82378_mem_ops = {
147 .read = i82378_mem_read,
148 .write = i82378_mem_write,
149 .endianness = DEVICE_LITTLE_ENDIAN,
152 static void i82378_request_out0_irq(void *opaque, int irq, int level)
154 I82378State *s = opaque;
155 qemu_set_irq(s->out[0], level);
158 static void i82378_request_pic_irq(void *opaque, int irq, int level)
160 DeviceState *dev = opaque;
161 PCIDevice *pci = DO_UPCAST(PCIDevice, qdev, dev);
162 PCIi82378State *s = DO_UPCAST(PCIi82378State, pci_dev, pci);
164 qemu_set_irq(s->state.i8259[irq], level);
167 static void i82378_init(DeviceState *dev, I82378State *s)
169 ISABus *isabus = DO_UPCAST(ISABus, qbus, qdev_get_child_bus(dev, "isa.0"));
170 ISADevice *pit;
171 qemu_irq *out0_irq;
173 /* This device has:
174 2 82C59 (irq)
175 1 82C54 (pit)
176 2 82C37 (dma)
178 Utility Bus Support Registers
180 All devices accept byte access only, except timer
183 qdev_init_gpio_out(dev, s->out, 2);
184 qdev_init_gpio_in(dev, i82378_request_pic_irq, 16);
186 /* Workaround the fact that i8259 is not qdev'ified... */
187 out0_irq = qemu_allocate_irqs(i82378_request_out0_irq, s, 1);
189 /* 2 82C59 (irq) */
190 s->i8259 = i8259_init(isabus, *out0_irq);
191 isa_bus_irqs(isabus, s->i8259);
193 /* 1 82C54 (pit) */
194 pit = pit_init(isabus, 0x40, 0);
196 /* speaker */
197 pcspk_init(pit);
199 /* 2 82C37 (dma) */
200 DMA_init(1, &s->out[1]);
201 isa_create_simple(isabus, "i82374");
203 /* timer */
204 isa_create_simple(isabus, "mc146818rtc");
207 static int pci_i82378_init(PCIDevice *dev)
209 PCIi82378State *pci = DO_UPCAST(PCIi82378State, pci_dev, dev);
210 I82378State *s = &pci->state;
211 uint8_t *pci_conf;
213 pci_conf = dev->config;
214 pci_set_word(pci_conf + PCI_COMMAND,
215 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
216 pci_set_word(pci_conf + PCI_STATUS,
217 PCI_STATUS_DEVSEL_MEDIUM);
219 pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin 0 */
221 memory_region_init_io(&s->io, &i82378_io_ops, s, "i82378-io", 0x00010000);
222 pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->io);
224 memory_region_init_io(&s->mem, &i82378_mem_ops, s, "i82378-mem", 0x01000000);
225 memory_region_set_coalescing(&s->mem);
226 pci_register_bar(dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->mem);
228 /* Make I/O address read only */
229 pci_set_word(dev->wmask + PCI_COMMAND, PCI_COMMAND_SPECIAL);
230 pci_set_long(dev->wmask + PCI_BASE_ADDRESS_0, 0);
231 pci_set_long(pci_conf + PCI_BASE_ADDRESS_0, pci->isa_io_base);
233 isa_mem_base = pci->isa_mem_base;
234 isa_bus_new(&dev->qdev, pci_address_space_io(dev));
236 i82378_init(&dev->qdev, s);
238 return 0;
241 static void pci_i82378_class_init(ObjectClass *klass, void *data)
243 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
245 k->init = pci_i82378_init;
246 k->vendor_id = PCI_VENDOR_ID_INTEL;
247 k->device_id = PCI_DEVICE_ID_INTEL_82378;
248 k->revision = 0x03;
249 k->class_id = PCI_CLASS_BRIDGE_ISA;
250 k->subsystem_vendor_id = 0x0;
251 k->subsystem_id = 0x0;
254 static DeviceInfo pci_i82378_info = {
255 .name = "i82378",
256 .size = sizeof(PCIi82378State),
257 .vmsd = &vmstate_pci_i82378,
258 .class_init = pci_i82378_class_init,
259 .props = (Property[]) {
260 DEFINE_PROP_HEX32("iobase", PCIi82378State, isa_io_base, 0x80000000),
261 DEFINE_PROP_HEX32("membase", PCIi82378State, isa_mem_base, 0xc0000000),
262 DEFINE_PROP_END_OF_LIST()
266 static void i82378_register_devices(void)
268 pci_qdev_register(&pci_i82378_info);
271 device_init(i82378_register_devices)