2 * QEMU Motorla 680x0 Macintosh hardware System Emulator
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 #include "qemu/osdep.h"
24 #include "qemu/units.h"
25 #include "qemu-common.h"
26 #include "qemu/datadir.h"
27 #include "sysemu/sysemu.h"
29 #include "hw/boards.h"
31 #include "hw/or-irq.h"
33 #include "hw/loader.h"
34 #include "ui/console.h"
35 #include "exec/address-spaces.h"
36 #include "hw/char/escc.h"
37 #include "hw/sysbus.h"
38 #include "hw/scsi/esp.h"
39 #include "standard-headers/asm-m68k/bootinfo.h"
40 #include "standard-headers/asm-m68k/bootinfo-mac.h"
42 #include "hw/misc/mac_via.h"
43 #include "hw/input/adb.h"
44 #include "hw/nubus/mac-nubus-bridge.h"
45 #include "hw/display/macfb.h"
46 #include "hw/block/swim.h"
48 #include "qapi/error.h"
49 #include "sysemu/qtest.h"
50 #include "sysemu/runstate.h"
51 #include "sysemu/reset.h"
52 #include "migration/vmstate.h"
54 #define MACROM_ADDR 0x40800000
55 #define MACROM_SIZE 0x00100000
57 #define MACROM_FILENAME "MacROM.bin"
59 #define IO_BASE 0x50000000
60 #define IO_SLICE 0x00040000
61 #define IO_SIZE 0x04000000
63 #define VIA_BASE (IO_BASE + 0x00000)
64 #define SONIC_PROM_BASE (IO_BASE + 0x08000)
65 #define SONIC_BASE (IO_BASE + 0x0a000)
66 #define SCC_BASE (IO_BASE + 0x0c020)
67 #define ESP_BASE (IO_BASE + 0x10000)
68 #define ESP_PDMA (IO_BASE + 0x10100)
69 #define ASC_BASE (IO_BASE + 0x14000)
70 #define SWIM_BASE (IO_BASE + 0x1E000)
72 #define NUBUS_SUPER_SLOT_BASE 0x60000000
73 #define NUBUS_SLOT_BASE 0xf0000000
76 * the video base, whereas it a Nubus address,
77 * is needed by the kernel to have early display and
78 * thus provided by the bootloader
80 #define VIDEO_BASE 0xf9001000
82 #define MAC_CLOCK 3686418
85 * The GLUE (General Logic Unit) is an Apple custom integrated circuit chip
86 * that performs a variety of functions (RAM management, clock generation, ...).
87 * The GLUE chip receives interrupt requests from various devices,
88 * assign priority to each, and asserts one or more interrupt line to the
92 #define TYPE_GLUE "q800-glue"
93 OBJECT_DECLARE_SIMPLE_TYPE(GLUEState
, GLUE
)
96 SysBusDevice parent_obj
;
101 static void GLUE_set_irq(void *opaque
, int irq
, int level
)
103 GLUEState
*s
= opaque
;
109 s
->ipr
&= ~(1 << irq
);
112 for (i
= 7; i
>= 0; i
--) {
113 if ((s
->ipr
>> i
) & 1) {
114 m68k_set_irq_level(s
->cpu
, i
+ 1, i
+ 25);
118 m68k_set_irq_level(s
->cpu
, 0, 0);
121 static void glue_reset(DeviceState
*dev
)
123 GLUEState
*s
= GLUE(dev
);
128 static const VMStateDescription vmstate_glue
= {
131 .minimum_version_id
= 0,
132 .fields
= (VMStateField
[]) {
133 VMSTATE_UINT8(ipr
, GLUEState
),
134 VMSTATE_END_OF_LIST(),
139 * If the m68k CPU implemented its inbound irq lines as GPIO lines
140 * rather than via the m68k_set_irq_level() function we would not need
141 * this cpu link property and could instead provide outbound IRQ lines
142 * that the board could wire up to the CPU.
144 static Property glue_properties
[] = {
145 DEFINE_PROP_LINK("cpu", GLUEState
, cpu
, TYPE_M68K_CPU
, M68kCPU
*),
146 DEFINE_PROP_END_OF_LIST(),
149 static void glue_init(Object
*obj
)
151 DeviceState
*dev
= DEVICE(obj
);
153 qdev_init_gpio_in(dev
, GLUE_set_irq
, 8);
156 static void glue_class_init(ObjectClass
*klass
, void *data
)
158 DeviceClass
*dc
= DEVICE_CLASS(klass
);
160 dc
->vmsd
= &vmstate_glue
;
161 dc
->reset
= glue_reset
;
162 device_class_set_props(dc
, glue_properties
);
165 static const TypeInfo glue_info
= {
167 .parent
= TYPE_SYS_BUS_DEVICE
,
168 .instance_size
= sizeof(GLUEState
),
169 .instance_init
= glue_init
,
170 .class_init
= glue_class_init
,
173 static void main_cpu_reset(void *opaque
)
175 M68kCPU
*cpu
= opaque
;
176 CPUState
*cs
= CPU(cpu
);
179 cpu
->env
.aregs
[7] = ldl_phys(cs
->as
, 0);
180 cpu
->env
.pc
= ldl_phys(cs
->as
, 4);
183 static uint8_t fake_mac_rom
[] = {
184 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186 /* offset: 0xa - mac_reset */
188 /* via2[vDirB] |= VIA2B_vPower */
189 0x20, 0x7C, 0x50, 0xF0, 0x24, 0x00, /* moveal VIA2_BASE+vDirB,%a0 */
190 0x10, 0x10, /* moveb %a0@,%d0 */
191 0x00, 0x00, 0x00, 0x04, /* orib #4,%d0 */
192 0x10, 0x80, /* moveb %d0,%a0@ */
194 /* via2[vBufB] &= ~VIA2B_vPower */
195 0x20, 0x7C, 0x50, 0xF0, 0x20, 0x00, /* moveal VIA2_BASE+vBufB,%a0 */
196 0x10, 0x10, /* moveb %a0@,%d0 */
197 0x02, 0x00, 0xFF, 0xFB, /* andib #-5,%d0 */
198 0x10, 0x80, /* moveb %d0,%a0@ */
201 0x60, 0xFE /* bras [self] */
204 static void q800_init(MachineState
*machine
)
212 ram_addr_t initrd_base
;
216 const int io_slice_nb
= (IO_SIZE
/ IO_SLICE
) - 1;
218 ram_addr_t ram_size
= machine
->ram_size
;
219 const char *kernel_filename
= machine
->kernel_filename
;
220 const char *initrd_filename
= machine
->initrd_filename
;
221 const char *kernel_cmdline
= machine
->kernel_cmdline
;
222 const char *bios_name
= machine
->firmware
?: MACROM_FILENAME
;
223 hwaddr parameters_base
;
226 DeviceState
*via_dev
;
227 DeviceState
*escc_orgate
;
228 SysBusESPState
*sysbus_esp
;
230 SysBusDevice
*sysbus
;
236 linux_boot
= (kernel_filename
!= NULL
);
238 if (ram_size
> 1 * GiB
) {
239 error_report("Too much memory for this machine: %" PRId64
" MiB, "
240 "maximum 1024 MiB", ram_size
/ MiB
);
245 cpu
= M68K_CPU(cpu_create(machine
->cpu_type
));
246 qemu_register_reset(main_cpu_reset
, cpu
);
249 memory_region_add_subregion(get_system_memory(), 0, machine
->ram
);
252 * Memory from IO_BASE to IO_BASE + IO_SLICE is repeated
253 * from IO_BASE + IO_SLICE to IO_BASE + IO_SIZE
255 io
= g_new(MemoryRegion
, io_slice_nb
);
256 for (i
= 0; i
< io_slice_nb
; i
++) {
257 char *name
= g_strdup_printf("mac_m68k.io[%d]", i
+ 1);
259 memory_region_init_alias(&io
[i
], NULL
, name
, get_system_memory(),
261 memory_region_add_subregion(get_system_memory(),
262 IO_BASE
+ (i
+ 1) * IO_SLICE
, &io
[i
]);
267 glue
= qdev_new(TYPE_GLUE
);
268 object_property_set_link(OBJECT(glue
), "cpu", OBJECT(cpu
), &error_abort
);
269 sysbus_realize_and_unref(SYS_BUS_DEVICE(glue
), &error_fatal
);
273 via_dev
= qdev_new(TYPE_MAC_VIA
);
274 dinfo
= drive_get(IF_MTD
, 0, 0);
276 qdev_prop_set_drive(via_dev
, "drive", blk_by_legacy_dinfo(dinfo
));
278 sysbus
= SYS_BUS_DEVICE(via_dev
);
279 sysbus_realize_and_unref(sysbus
, &error_fatal
);
280 sysbus_mmio_map(sysbus
, 0, VIA_BASE
);
281 qdev_connect_gpio_out_named(DEVICE(sysbus
), "irq", 0,
282 qdev_get_gpio_in(glue
, 0));
283 qdev_connect_gpio_out_named(DEVICE(sysbus
), "irq", 1,
284 qdev_get_gpio_in(glue
, 1));
287 adb_bus
= qdev_get_child_bus(via_dev
, "adb.0");
288 dev
= qdev_new(TYPE_ADB_KEYBOARD
);
289 qdev_realize_and_unref(dev
, adb_bus
, &error_fatal
);
290 dev
= qdev_new(TYPE_ADB_MOUSE
);
291 qdev_realize_and_unref(dev
, adb_bus
, &error_fatal
);
296 error_report("q800 can only have one ethernet interface");
300 qemu_check_nic_model(&nd_table
[0], "dp83932");
303 * MacSonic driver needs an Apple MAC address
306 * 00:80:19 Dayna Communications, Inc.
309 * (Q800 use the last one)
311 nd_table
[0].macaddr
.a
[0] = 0x08;
312 nd_table
[0].macaddr
.a
[1] = 0x00;
313 nd_table
[0].macaddr
.a
[2] = 0x07;
315 dev
= qdev_new("dp8393x");
316 qdev_set_nic_properties(dev
, &nd_table
[0]);
317 qdev_prop_set_uint8(dev
, "it_shift", 2);
318 qdev_prop_set_bit(dev
, "big_endian", true);
319 object_property_set_link(OBJECT(dev
), "dma_mr",
320 OBJECT(get_system_memory()), &error_abort
);
321 sysbus
= SYS_BUS_DEVICE(dev
);
322 sysbus_realize_and_unref(sysbus
, &error_fatal
);
323 sysbus_mmio_map(sysbus
, 0, SONIC_BASE
);
324 sysbus_mmio_map(sysbus
, 1, SONIC_PROM_BASE
);
325 sysbus_connect_irq(sysbus
, 0, qdev_get_gpio_in(glue
, 2));
329 dev
= qdev_new(TYPE_ESCC
);
330 qdev_prop_set_uint32(dev
, "disabled", 0);
331 qdev_prop_set_uint32(dev
, "frequency", MAC_CLOCK
);
332 qdev_prop_set_uint32(dev
, "it_shift", 1);
333 qdev_prop_set_bit(dev
, "bit_swap", true);
334 qdev_prop_set_chr(dev
, "chrA", serial_hd(0));
335 qdev_prop_set_chr(dev
, "chrB", serial_hd(1));
336 qdev_prop_set_uint32(dev
, "chnBtype", 0);
337 qdev_prop_set_uint32(dev
, "chnAtype", 0);
338 sysbus
= SYS_BUS_DEVICE(dev
);
339 sysbus_realize_and_unref(sysbus
, &error_fatal
);
341 /* Logically OR both its IRQs together */
342 escc_orgate
= DEVICE(object_new(TYPE_OR_IRQ
));
343 object_property_set_int(OBJECT(escc_orgate
), "num-lines", 2, &error_fatal
);
344 qdev_realize_and_unref(escc_orgate
, NULL
, &error_fatal
);
345 sysbus_connect_irq(sysbus
, 0, qdev_get_gpio_in(escc_orgate
, 0));
346 sysbus_connect_irq(sysbus
, 1, qdev_get_gpio_in(escc_orgate
, 1));
347 qdev_connect_gpio_out(DEVICE(escc_orgate
), 0, qdev_get_gpio_in(glue
, 3));
348 sysbus_mmio_map(sysbus
, 0, SCC_BASE
);
352 dev
= qdev_new(TYPE_SYSBUS_ESP
);
353 sysbus_esp
= SYSBUS_ESP(dev
);
354 esp
= &sysbus_esp
->esp
;
355 esp
->dma_memory_read
= NULL
;
356 esp
->dma_memory_write
= NULL
;
357 esp
->dma_opaque
= NULL
;
358 sysbus_esp
->it_shift
= 4;
359 esp
->dma_enabled
= 1;
361 sysbus
= SYS_BUS_DEVICE(dev
);
362 sysbus_realize_and_unref(sysbus
, &error_fatal
);
363 sysbus_connect_irq(sysbus
, 0, qdev_get_gpio_in_named(via_dev
,
366 sysbus_connect_irq(sysbus
, 1,
367 qdev_get_gpio_in_named(via_dev
, "via2-irq",
368 VIA2_IRQ_SCSI_DATA_BIT
));
369 sysbus_mmio_map(sysbus
, 0, ESP_BASE
);
370 sysbus_mmio_map(sysbus
, 1, ESP_PDMA
);
372 scsi_bus_legacy_handle_cmdline(&esp
->bus
);
374 /* SWIM floppy controller */
376 dev
= qdev_new(TYPE_SWIM
);
377 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev
), &error_fatal
);
378 sysbus_mmio_map(SYS_BUS_DEVICE(dev
), 0, SWIM_BASE
);
382 dev
= qdev_new(TYPE_MAC_NUBUS_BRIDGE
);
383 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev
), &error_fatal
);
384 sysbus_mmio_map(SYS_BUS_DEVICE(dev
), 0, NUBUS_SUPER_SLOT_BASE
);
385 sysbus_mmio_map(SYS_BUS_DEVICE(dev
), 1, NUBUS_SLOT_BASE
);
387 nubus
= MAC_NUBUS_BRIDGE(dev
)->bus
;
389 /* framebuffer in nubus slot #9 */
391 dev
= qdev_new(TYPE_NUBUS_MACFB
);
392 qdev_prop_set_uint32(dev
, "width", graphic_width
);
393 qdev_prop_set_uint32(dev
, "height", graphic_height
);
394 qdev_prop_set_uint8(dev
, "depth", graphic_depth
);
395 qdev_realize_and_unref(dev
, BUS(nubus
), &error_fatal
);
400 kernel_size
= load_elf(kernel_filename
, NULL
, NULL
, NULL
,
401 &elf_entry
, NULL
, &high
, NULL
, 1,
403 if (kernel_size
< 0) {
404 error_report("could not load kernel '%s'", kernel_filename
);
407 stl_phys(cs
->as
, 4, elf_entry
); /* reset initial PC */
408 parameters_base
= (high
+ 1) & ~1;
410 BOOTINFO1(cs
->as
, parameters_base
, BI_MACHTYPE
, MACH_MAC
);
411 BOOTINFO1(cs
->as
, parameters_base
, BI_FPUTYPE
, FPU_68040
);
412 BOOTINFO1(cs
->as
, parameters_base
, BI_MMUTYPE
, MMU_68040
);
413 BOOTINFO1(cs
->as
, parameters_base
, BI_CPUTYPE
, CPU_68040
);
414 BOOTINFO1(cs
->as
, parameters_base
, BI_MAC_CPUID
, CPUB_68040
);
415 BOOTINFO1(cs
->as
, parameters_base
, BI_MAC_MODEL
, MAC_MODEL_Q800
);
416 BOOTINFO1(cs
->as
, parameters_base
,
417 BI_MAC_MEMSIZE
, ram_size
>> 20); /* in MB */
418 BOOTINFO2(cs
->as
, parameters_base
, BI_MEMCHUNK
, 0, ram_size
);
419 BOOTINFO1(cs
->as
, parameters_base
, BI_MAC_VADDR
, VIDEO_BASE
);
420 BOOTINFO1(cs
->as
, parameters_base
, BI_MAC_VDEPTH
, graphic_depth
);
421 BOOTINFO1(cs
->as
, parameters_base
, BI_MAC_VDIM
,
422 (graphic_height
<< 16) | graphic_width
);
423 BOOTINFO1(cs
->as
, parameters_base
, BI_MAC_VROW
,
424 (graphic_width
* graphic_depth
+ 7) / 8);
425 BOOTINFO1(cs
->as
, parameters_base
, BI_MAC_SCCBASE
, SCC_BASE
);
427 rom
= g_malloc(sizeof(*rom
));
428 memory_region_init_ram_ptr(rom
, NULL
, "m68k_fake_mac.rom",
429 sizeof(fake_mac_rom
), fake_mac_rom
);
430 memory_region_set_readonly(rom
, true);
431 memory_region_add_subregion(get_system_memory(), MACROM_ADDR
, rom
);
433 if (kernel_cmdline
) {
434 BOOTINFOSTR(cs
->as
, parameters_base
, BI_COMMAND_LINE
,
439 if (initrd_filename
) {
440 initrd_size
= get_image_size(initrd_filename
);
441 if (initrd_size
< 0) {
442 error_report("could not load initial ram disk '%s'",
447 initrd_base
= (ram_size
- initrd_size
) & TARGET_PAGE_MASK
;
448 load_image_targphys(initrd_filename
, initrd_base
,
449 ram_size
- initrd_base
);
450 BOOTINFO2(cs
->as
, parameters_base
, BI_RAMDISK
, initrd_base
,
456 BOOTINFO0(cs
->as
, parameters_base
, BI_LAST
);
459 /* allocate and load BIOS */
460 rom
= g_malloc(sizeof(*rom
));
461 memory_region_init_rom(rom
, NULL
, "m68k_mac.rom", MACROM_SIZE
,
463 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
464 memory_region_add_subregion(get_system_memory(), MACROM_ADDR
, rom
);
466 /* Load MacROM binary */
468 bios_size
= load_image_targphys(filename
, MACROM_ADDR
, MACROM_SIZE
);
474 /* Remove qtest_enabled() check once firmware files are in the tree */
475 if (!qtest_enabled()) {
476 if (bios_size
< 0 || bios_size
> MACROM_SIZE
) {
477 error_report("could not load MacROM '%s'", bios_name
);
481 ptr
= rom_ptr(MACROM_ADDR
, MACROM_SIZE
);
482 stl_phys(cs
->as
, 0, ldl_p(ptr
)); /* reset initial SP */
484 MACROM_ADDR
+ ldl_p(ptr
+ 4)); /* reset initial PC */
489 static void q800_machine_class_init(ObjectClass
*oc
, void *data
)
491 MachineClass
*mc
= MACHINE_CLASS(oc
);
492 mc
->desc
= "Macintosh Quadra 800";
493 mc
->init
= q800_init
;
494 mc
->default_cpu_type
= M68K_CPU_TYPE_NAME("m68040");
496 mc
->block_default_type
= IF_SCSI
;
497 mc
->default_ram_id
= "m68k_mac.ram";
500 static const TypeInfo q800_machine_typeinfo
= {
501 .name
= MACHINE_TYPE_NAME("q800"),
502 .parent
= TYPE_MACHINE
,
503 .class_init
= q800_machine_class_init
,
506 static void q800_machine_register_types(void)
508 type_register_static(&q800_machine_typeinfo
);
509 type_register_static(&glue_info
);
512 type_init(q800_machine_register_types
)