2 * PowerMac MacIO device emulation
4 * Copyright (c) 2005-2007 Fabrice Bellard
5 * Copyright (c) 2007 Jocelyn Mayer
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "hw/ppc/mac.h"
27 #include "hw/pci/pci.h"
28 #include "hw/ppc/mac_dbdma.h"
29 #include "hw/char/escc.h"
31 #define TYPE_MACIO "macio"
32 #define MACIO(obj) OBJECT_CHECK(MacIOState, (obj), TYPE_MACIO)
34 typedef struct MacIOState
43 MemoryRegion
*pic_mem
;
44 MemoryRegion
*escc_mem
;
47 #define OLDWORLD_MACIO(obj) \
48 OBJECT_CHECK(OldWorldMacIOState, (obj), TYPE_OLDWORLD_MACIO)
50 typedef struct OldWorldMacIOState
{
52 MacIOState parent_obj
;
57 MacIONVRAMState nvram
;
61 #define NEWWORLD_MACIO(obj) \
62 OBJECT_CHECK(NewWorldMacIOState, (obj), TYPE_NEWWORLD_MACIO)
64 typedef struct NewWorldMacIOState
{
66 MacIOState parent_obj
;
73 * The mac-io has two interfaces to the ESCC. One is called "escc-legacy",
74 * while the other one is the normal, current ESCC interface.
76 * The magic below creates memory aliases to spawn the escc-legacy device
77 * purely by rerouting the respective registers to our escc region. This
78 * works because the only difference between the two memory regions is the
79 * register layout, not their semantics.
81 * Reference: ftp://ftp.software.ibm.com/rs6000/technology/spec/chrp/inwork/CHRP_IORef_1.0.pdf
83 static void macio_escc_legacy_setup(MacIOState
*macio_state
)
85 MemoryRegion
*escc_legacy
= g_new(MemoryRegion
, 1);
86 MemoryRegion
*bar
= &macio_state
->bar
;
88 static const int maps
[] = {
107 memory_region_init(escc_legacy
, NULL
, "escc-legacy", 256);
108 for (i
= 0; i
< ARRAY_SIZE(maps
); i
+= 2) {
109 MemoryRegion
*port
= g_new(MemoryRegion
, 1);
110 memory_region_init_alias(port
, NULL
, "escc-legacy-port",
111 macio_state
->escc_mem
, maps
[i
+1], 0x2);
112 memory_region_add_subregion(escc_legacy
, maps
[i
], port
);
115 memory_region_add_subregion(bar
, 0x12000, escc_legacy
);
118 static void macio_bar_setup(MacIOState
*macio_state
)
120 MemoryRegion
*bar
= &macio_state
->bar
;
122 if (macio_state
->escc_mem
) {
123 memory_region_add_subregion(bar
, 0x13000, macio_state
->escc_mem
);
124 macio_escc_legacy_setup(macio_state
);
128 static int macio_common_initfn(PCIDevice
*d
)
130 MacIOState
*s
= MACIO(d
);
131 SysBusDevice
*sysbus_dev
;
134 d
->config
[0x3d] = 0x01; // interrupt on pin 1
136 ret
= qdev_init(DEVICE(&s
->cuda
));
140 sysbus_dev
= SYS_BUS_DEVICE(&s
->cuda
);
141 memory_region_add_subregion(&s
->bar
, 0x16000,
142 sysbus_mmio_get_region(sysbus_dev
, 0));
145 pci_register_bar(d
, 0, PCI_BASE_ADDRESS_SPACE_MEMORY
, &s
->bar
);
150 static int macio_oldworld_initfn(PCIDevice
*d
)
152 MacIOState
*s
= MACIO(d
);
153 OldWorldMacIOState
*os
= OLDWORLD_MACIO(d
);
154 SysBusDevice
*sysbus_dev
;
155 int ret
= macio_common_initfn(d
);
160 sysbus_dev
= SYS_BUS_DEVICE(&s
->cuda
);
161 sysbus_connect_irq(sysbus_dev
, 0, os
->irqs
[0]);
163 ret
= qdev_init(DEVICE(&os
->nvram
));
167 sysbus_dev
= SYS_BUS_DEVICE(&os
->nvram
);
168 memory_region_add_subregion(&s
->bar
, 0x60000,
169 sysbus_mmio_get_region(sysbus_dev
, 0));
170 pmac_format_nvram_partition(&os
->nvram
, os
->nvram
.size
);
174 memory_region_add_subregion(&s
->bar
, 0x00000, s
->pic_mem
);
177 sysbus_dev
= SYS_BUS_DEVICE(&os
->ide
);
178 sysbus_connect_irq(sysbus_dev
, 0, os
->irqs
[1]);
179 sysbus_connect_irq(sysbus_dev
, 1, os
->irqs
[2]);
180 macio_ide_register_dma(&os
->ide
, s
->dbdma
, 0x16);
181 ret
= qdev_init(DEVICE(&os
->ide
));
189 static void macio_oldworld_init(Object
*obj
)
191 MacIOState
*s
= MACIO(obj
);
192 OldWorldMacIOState
*os
= OLDWORLD_MACIO(obj
);
195 qdev_init_gpio_out(DEVICE(obj
), os
->irqs
, ARRAY_SIZE(os
->irqs
));
197 object_initialize(&os
->nvram
, TYPE_MACIO_NVRAM
);
198 dev
= DEVICE(&os
->nvram
);
199 qdev_prop_set_uint32(dev
, "size", 0x2000);
200 qdev_prop_set_uint32(dev
, "it_shift", 4);
202 object_initialize(&os
->ide
, TYPE_MACIO_IDE
);
203 qdev_set_parent_bus(DEVICE(&os
->ide
), sysbus_get_default());
204 memory_region_add_subregion(&s
->bar
, 0x1f000 + (1 * 0x1000), &os
->ide
.mem
);
205 object_property_add_child(obj
, "ide", OBJECT(&os
->ide
), NULL
);
208 static int macio_newworld_initfn(PCIDevice
*d
)
210 MacIOState
*s
= MACIO(d
);
211 NewWorldMacIOState
*ns
= NEWWORLD_MACIO(d
);
212 SysBusDevice
*sysbus_dev
;
213 int ret
= macio_common_initfn(d
);
218 sysbus_dev
= SYS_BUS_DEVICE(&s
->cuda
);
219 sysbus_connect_irq(sysbus_dev
, 0, ns
->irqs
[0]);
223 memory_region_add_subregion(&s
->bar
, 0x40000, s
->pic_mem
);
226 sysbus_dev
= SYS_BUS_DEVICE(&ns
->ide
[0]);
227 sysbus_connect_irq(sysbus_dev
, 0, ns
->irqs
[1]);
228 sysbus_connect_irq(sysbus_dev
, 1, ns
->irqs
[2]);
229 macio_ide_register_dma(&ns
->ide
[0], s
->dbdma
, 0x16);
230 ret
= qdev_init(DEVICE(&ns
->ide
[0]));
235 sysbus_dev
= SYS_BUS_DEVICE(&ns
->ide
[1]);
236 sysbus_connect_irq(sysbus_dev
, 0, ns
->irqs
[3]);
237 sysbus_connect_irq(sysbus_dev
, 1, ns
->irqs
[4]);
238 macio_ide_register_dma(&ns
->ide
[1], s
->dbdma
, 0x1a);
239 ret
= qdev_init(DEVICE(&ns
->ide
[1]));
247 static void macio_newworld_init(Object
*obj
)
249 MacIOState
*s
= MACIO(obj
);
250 NewWorldMacIOState
*ns
= NEWWORLD_MACIO(obj
);
254 qdev_init_gpio_out(DEVICE(obj
), ns
->irqs
, ARRAY_SIZE(ns
->irqs
));
256 for (i
= 0; i
< 2; i
++) {
257 object_initialize(&ns
->ide
[i
], TYPE_MACIO_IDE
);
258 qdev_set_parent_bus(DEVICE(&ns
->ide
[i
]), sysbus_get_default());
259 memory_region_add_subregion(&s
->bar
, 0x1f000 + ((i
+ 1) * 0x1000),
261 name
= g_strdup_printf("ide[%i]", i
);
262 object_property_add_child(obj
, name
, OBJECT(&ns
->ide
[i
]), NULL
);
267 static void macio_instance_init(Object
*obj
)
269 MacIOState
*s
= MACIO(obj
);
270 MemoryRegion
*dbdma_mem
;
272 memory_region_init(&s
->bar
, NULL
, "macio", 0x80000);
274 object_initialize(&s
->cuda
, TYPE_CUDA
);
275 qdev_set_parent_bus(DEVICE(&s
->cuda
), sysbus_get_default());
276 object_property_add_child(obj
, "cuda", OBJECT(&s
->cuda
), NULL
);
278 s
->dbdma
= DBDMA_init(&dbdma_mem
);
279 memory_region_add_subregion(&s
->bar
, 0x08000, dbdma_mem
);
282 static void macio_oldworld_class_init(ObjectClass
*oc
, void *data
)
284 PCIDeviceClass
*pdc
= PCI_DEVICE_CLASS(oc
);
286 pdc
->init
= macio_oldworld_initfn
;
287 pdc
->device_id
= PCI_DEVICE_ID_APPLE_343S1201
;
290 static void macio_newworld_class_init(ObjectClass
*oc
, void *data
)
292 PCIDeviceClass
*pdc
= PCI_DEVICE_CLASS(oc
);
294 pdc
->init
= macio_newworld_initfn
;
295 pdc
->device_id
= PCI_DEVICE_ID_APPLE_UNI_N_KEYL
;
298 static void macio_class_init(ObjectClass
*klass
, void *data
)
300 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
302 k
->vendor_id
= PCI_VENDOR_ID_APPLE
;
303 k
->class_id
= PCI_CLASS_OTHERS
<< 8;
306 static const TypeInfo macio_oldworld_type_info
= {
307 .name
= TYPE_OLDWORLD_MACIO
,
308 .parent
= TYPE_MACIO
,
309 .instance_size
= sizeof(OldWorldMacIOState
),
310 .instance_init
= macio_oldworld_init
,
311 .class_init
= macio_oldworld_class_init
,
314 static const TypeInfo macio_newworld_type_info
= {
315 .name
= TYPE_NEWWORLD_MACIO
,
316 .parent
= TYPE_MACIO
,
317 .instance_size
= sizeof(NewWorldMacIOState
),
318 .instance_init
= macio_newworld_init
,
319 .class_init
= macio_newworld_class_init
,
322 static const TypeInfo macio_type_info
= {
324 .parent
= TYPE_PCI_DEVICE
,
325 .instance_size
= sizeof(MacIOState
),
326 .instance_init
= macio_instance_init
,
328 .class_init
= macio_class_init
,
331 static void macio_register_types(void)
333 type_register_static(&macio_type_info
);
334 type_register_static(&macio_oldworld_type_info
);
335 type_register_static(&macio_newworld_type_info
);
338 type_init(macio_register_types
)
340 void macio_init(PCIDevice
*d
,
341 MemoryRegion
*pic_mem
,
342 MemoryRegion
*escc_mem
)
344 MacIOState
*macio_state
= MACIO(d
);
346 macio_state
->pic_mem
= pic_mem
;
347 macio_state
->escc_mem
= escc_mem
;
348 /* Note: this code is strongly inspirated from the corresponding code
351 qdev_init_nofail(DEVICE(d
));