q800.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * QEMU Motorla 680x0 Macintosh hardware System Emulator
  3. *
  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:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  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
  20. * THE SOFTWARE.
  21. */
  22. #include "qemu/osdep.h"
  23. #include "qemu/units.h"
  24. #include "qemu-common.h"
  25. #include "sysemu/sysemu.h"
  26. #include "cpu.h"
  27. #include "hw/hw.h"
  28. #include "hw/boards.h"
  29. #include "hw/irq.h"
  30. #include "elf.h"
  31. #include "hw/loader.h"
  32. #include "ui/console.h"
  33. #include "exec/address-spaces.h"
  34. #include "hw/char/escc.h"
  35. #include "hw/sysbus.h"
  36. #include "hw/scsi/esp.h"
  37. #include "bootinfo.h"
  38. #include "hw/misc/mac_via.h"
  39. #include "hw/input/adb.h"
  40. #include "hw/nubus/mac-nubus-bridge.h"
  41. #include "hw/display/macfb.h"
  42. #include "hw/block/swim.h"
  43. #include "net/net.h"
  44. #include "qapi/error.h"
  45. #include "sysemu/qtest.h"
  46. #include "sysemu/runstate.h"
  47. #include "sysemu/reset.h"
  48. #define MACROM_ADDR 0x40800000
  49. #define MACROM_SIZE 0x00100000
  50. #define MACROM_FILENAME "MacROM.bin"
  51. #define Q800_MACHINE_ID 35
  52. #define Q800_CPU_ID (1 << 2)
  53. #define Q800_FPU_ID (1 << 2)
  54. #define Q800_MMU_ID (1 << 2)
  55. #define MACH_MAC 3
  56. #define Q800_MAC_CPU_ID 2
  57. #define IO_BASE 0x50000000
  58. #define IO_SLICE 0x00040000
  59. #define IO_SIZE 0x04000000
  60. #define VIA_BASE (IO_BASE + 0x00000)
  61. #define SONIC_PROM_BASE (IO_BASE + 0x08000)
  62. #define SONIC_BASE (IO_BASE + 0x0a000)
  63. #define SCC_BASE (IO_BASE + 0x0c020)
  64. #define ESP_BASE (IO_BASE + 0x10000)
  65. #define ESP_PDMA (IO_BASE + 0x10100)
  66. #define ASC_BASE (IO_BASE + 0x14000)
  67. #define SWIM_BASE (IO_BASE + 0x1E000)
  68. #define NUBUS_SUPER_SLOT_BASE 0x60000000
  69. #define NUBUS_SLOT_BASE 0xf0000000
  70. /*
  71. * the video base, whereas it a Nubus address,
  72. * is needed by the kernel to have early display and
  73. * thus provided by the bootloader
  74. */
  75. #define VIDEO_BASE 0xf9001000
  76. #define MAC_CLOCK 3686418
  77. /*
  78. * The GLUE (General Logic Unit) is an Apple custom integrated circuit chip
  79. * that performs a variety of functions (RAM management, clock generation, ...).
  80. * The GLUE chip receives interrupt requests from various devices,
  81. * assign priority to each, and asserts one or more interrupt line to the
  82. * CPU.
  83. */
  84. typedef struct {
  85. M68kCPU *cpu;
  86. uint8_t ipr;
  87. } GLUEState;
  88. static void GLUE_set_irq(void *opaque, int irq, int level)
  89. {
  90. GLUEState *s = opaque;
  91. int i;
  92. if (level) {
  93. s->ipr |= 1 << irq;
  94. } else {
  95. s->ipr &= ~(1 << irq);
  96. }
  97. for (i = 7; i >= 0; i--) {
  98. if ((s->ipr >> i) & 1) {
  99. m68k_set_irq_level(s->cpu, i + 1, i + 25);
  100. return;
  101. }
  102. }
  103. m68k_set_irq_level(s->cpu, 0, 0);
  104. }
  105. static void main_cpu_reset(void *opaque)
  106. {
  107. M68kCPU *cpu = opaque;
  108. CPUState *cs = CPU(cpu);
  109. cpu_reset(cs);
  110. cpu->env.aregs[7] = ldl_phys(cs->as, 0);
  111. cpu->env.pc = ldl_phys(cs->as, 4);
  112. }
  113. static uint8_t fake_mac_rom[] = {
  114. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  115. /* offset: 0xa - mac_reset */
  116. /* via2[vDirB] |= VIA2B_vPower */
  117. 0x20, 0x7C, 0x50, 0xF0, 0x24, 0x00, /* moveal VIA2_BASE+vDirB,%a0 */
  118. 0x10, 0x10, /* moveb %a0@,%d0 */
  119. 0x00, 0x00, 0x00, 0x04, /* orib #4,%d0 */
  120. 0x10, 0x80, /* moveb %d0,%a0@ */
  121. /* via2[vBufB] &= ~VIA2B_vPower */
  122. 0x20, 0x7C, 0x50, 0xF0, 0x20, 0x00, /* moveal VIA2_BASE+vBufB,%a0 */
  123. 0x10, 0x10, /* moveb %a0@,%d0 */
  124. 0x02, 0x00, 0xFF, 0xFB, /* andib #-5,%d0 */
  125. 0x10, 0x80, /* moveb %d0,%a0@ */
  126. /* while (true) ; */
  127. 0x60, 0xFE /* bras [self] */
  128. };
  129. static void q800_init(MachineState *machine)
  130. {
  131. M68kCPU *cpu = NULL;
  132. int linux_boot;
  133. int32_t kernel_size;
  134. uint64_t elf_entry;
  135. char *filename;
  136. int bios_size;
  137. ram_addr_t initrd_base;
  138. int32_t initrd_size;
  139. MemoryRegion *rom;
  140. MemoryRegion *io;
  141. const int io_slice_nb = (IO_SIZE / IO_SLICE) - 1;
  142. int i;
  143. ram_addr_t ram_size = machine->ram_size;
  144. const char *kernel_filename = machine->kernel_filename;
  145. const char *initrd_filename = machine->initrd_filename;
  146. const char *kernel_cmdline = machine->kernel_cmdline;
  147. hwaddr parameters_base;
  148. CPUState *cs;
  149. DeviceState *dev;
  150. DeviceState *via_dev;
  151. SysBusESPState *sysbus_esp;
  152. ESPState *esp;
  153. SysBusDevice *sysbus;
  154. BusState *adb_bus;
  155. NubusBus *nubus;
  156. GLUEState *irq;
  157. qemu_irq *pic;
  158. DriveInfo *dinfo;
  159. linux_boot = (kernel_filename != NULL);
  160. if (ram_size > 1 * GiB) {
  161. error_report("Too much memory for this machine: %" PRId64 " MiB, "
  162. "maximum 1024 MiB", ram_size / MiB);
  163. exit(1);
  164. }
  165. /* init CPUs */
  166. cpu = M68K_CPU(cpu_create(machine->cpu_type));
  167. qemu_register_reset(main_cpu_reset, cpu);
  168. /* RAM */
  169. memory_region_add_subregion(get_system_memory(), 0, machine->ram);
  170. /*
  171. * Memory from IO_BASE to IO_BASE + IO_SLICE is repeated
  172. * from IO_BASE + IO_SLICE to IO_BASE + IO_SIZE
  173. */
  174. io = g_new(MemoryRegion, io_slice_nb);
  175. for (i = 0; i < io_slice_nb; i++) {
  176. char *name = g_strdup_printf("mac_m68k.io[%d]", i + 1);
  177. memory_region_init_alias(&io[i], NULL, name, get_system_memory(),
  178. IO_BASE, IO_SLICE);
  179. memory_region_add_subregion(get_system_memory(),
  180. IO_BASE + (i + 1) * IO_SLICE, &io[i]);
  181. g_free(name);
  182. }
  183. /* IRQ Glue */
  184. irq = g_new0(GLUEState, 1);
  185. irq->cpu = cpu;
  186. pic = qemu_allocate_irqs(GLUE_set_irq, irq, 8);
  187. /* VIA */
  188. via_dev = qdev_new(TYPE_MAC_VIA);
  189. dinfo = drive_get(IF_MTD, 0, 0);
  190. if (dinfo) {
  191. qdev_prop_set_drive(via_dev, "drive", blk_by_legacy_dinfo(dinfo));
  192. }
  193. sysbus = SYS_BUS_DEVICE(via_dev);
  194. sysbus_realize_and_unref(sysbus, &error_fatal);
  195. sysbus_mmio_map(sysbus, 0, VIA_BASE);
  196. qdev_connect_gpio_out_named(DEVICE(sysbus), "irq", 0, pic[0]);
  197. qdev_connect_gpio_out_named(DEVICE(sysbus), "irq", 1, pic[1]);
  198. adb_bus = qdev_get_child_bus(via_dev, "adb.0");
  199. dev = qdev_new(TYPE_ADB_KEYBOARD);
  200. qdev_realize_and_unref(dev, adb_bus, &error_fatal);
  201. dev = qdev_new(TYPE_ADB_MOUSE);
  202. qdev_realize_and_unref(dev, adb_bus, &error_fatal);
  203. /* MACSONIC */
  204. if (nb_nics > 1) {
  205. error_report("q800 can only have one ethernet interface");
  206. exit(1);
  207. }
  208. qemu_check_nic_model(&nd_table[0], "dp83932");
  209. /*
  210. * MacSonic driver needs an Apple MAC address
  211. * Valid prefix are:
  212. * 00:05:02 Apple
  213. * 00:80:19 Dayna Communications, Inc.
  214. * 00:A0:40 Apple
  215. * 08:00:07 Apple
  216. * (Q800 use the last one)
  217. */
  218. nd_table[0].macaddr.a[0] = 0x08;
  219. nd_table[0].macaddr.a[1] = 0x00;
  220. nd_table[0].macaddr.a[2] = 0x07;
  221. dev = qdev_new("dp8393x");
  222. qdev_set_nic_properties(dev, &nd_table[0]);
  223. qdev_prop_set_uint8(dev, "it_shift", 2);
  224. qdev_prop_set_bit(dev, "big_endian", true);
  225. object_property_set_link(OBJECT(dev), "dma_mr",
  226. OBJECT(get_system_memory()), &error_abort);
  227. sysbus = SYS_BUS_DEVICE(dev);
  228. sysbus_realize_and_unref(sysbus, &error_fatal);
  229. sysbus_mmio_map(sysbus, 0, SONIC_BASE);
  230. sysbus_mmio_map(sysbus, 1, SONIC_PROM_BASE);
  231. sysbus_connect_irq(sysbus, 0, pic[2]);
  232. /* SCC */
  233. dev = qdev_new(TYPE_ESCC);
  234. qdev_prop_set_uint32(dev, "disabled", 0);
  235. qdev_prop_set_uint32(dev, "frequency", MAC_CLOCK);
  236. qdev_prop_set_uint32(dev, "it_shift", 1);
  237. qdev_prop_set_bit(dev, "bit_swap", true);
  238. qdev_prop_set_chr(dev, "chrA", serial_hd(0));
  239. qdev_prop_set_chr(dev, "chrB", serial_hd(1));
  240. qdev_prop_set_uint32(dev, "chnBtype", 0);
  241. qdev_prop_set_uint32(dev, "chnAtype", 0);
  242. sysbus = SYS_BUS_DEVICE(dev);
  243. sysbus_realize_and_unref(sysbus, &error_fatal);
  244. sysbus_connect_irq(sysbus, 0, pic[3]);
  245. sysbus_connect_irq(sysbus, 1, pic[3]);
  246. sysbus_mmio_map(sysbus, 0, SCC_BASE);
  247. /* SCSI */
  248. dev = qdev_new(TYPE_ESP);
  249. sysbus_esp = ESP_STATE(dev);
  250. esp = &sysbus_esp->esp;
  251. esp->dma_memory_read = NULL;
  252. esp->dma_memory_write = NULL;
  253. esp->dma_opaque = NULL;
  254. sysbus_esp->it_shift = 4;
  255. esp->dma_enabled = 1;
  256. sysbus = SYS_BUS_DEVICE(dev);
  257. sysbus_realize_and_unref(sysbus, &error_fatal);
  258. sysbus_connect_irq(sysbus, 0, qdev_get_gpio_in_named(via_dev,
  259. "via2-irq",
  260. VIA2_IRQ_SCSI_BIT));
  261. sysbus_connect_irq(sysbus, 1,
  262. qdev_get_gpio_in_named(via_dev, "via2-irq",
  263. VIA2_IRQ_SCSI_DATA_BIT));
  264. sysbus_mmio_map(sysbus, 0, ESP_BASE);
  265. sysbus_mmio_map(sysbus, 1, ESP_PDMA);
  266. scsi_bus_legacy_handle_cmdline(&esp->bus);
  267. /* SWIM floppy controller */
  268. dev = qdev_new(TYPE_SWIM);
  269. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  270. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, SWIM_BASE);
  271. /* NuBus */
  272. dev = qdev_new(TYPE_MAC_NUBUS_BRIDGE);
  273. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  274. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, NUBUS_SUPER_SLOT_BASE);
  275. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 1, NUBUS_SLOT_BASE);
  276. nubus = MAC_NUBUS_BRIDGE(dev)->bus;
  277. /* framebuffer in nubus slot #9 */
  278. dev = qdev_new(TYPE_NUBUS_MACFB);
  279. qdev_prop_set_uint32(dev, "width", graphic_width);
  280. qdev_prop_set_uint32(dev, "height", graphic_height);
  281. qdev_prop_set_uint8(dev, "depth", graphic_depth);
  282. qdev_realize_and_unref(dev, BUS(nubus), &error_fatal);
  283. cs = CPU(cpu);
  284. if (linux_boot) {
  285. uint64_t high;
  286. kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
  287. &elf_entry, NULL, &high, NULL, 1,
  288. EM_68K, 0, 0);
  289. if (kernel_size < 0) {
  290. error_report("could not load kernel '%s'", kernel_filename);
  291. exit(1);
  292. }
  293. stl_phys(cs->as, 4, elf_entry); /* reset initial PC */
  294. parameters_base = (high + 1) & ~1;
  295. BOOTINFO1(cs->as, parameters_base, BI_MACHTYPE, MACH_MAC);
  296. BOOTINFO1(cs->as, parameters_base, BI_FPUTYPE, Q800_FPU_ID);
  297. BOOTINFO1(cs->as, parameters_base, BI_MMUTYPE, Q800_MMU_ID);
  298. BOOTINFO1(cs->as, parameters_base, BI_CPUTYPE, Q800_CPU_ID);
  299. BOOTINFO1(cs->as, parameters_base, BI_MAC_CPUID, Q800_MAC_CPU_ID);
  300. BOOTINFO1(cs->as, parameters_base, BI_MAC_MODEL, Q800_MACHINE_ID);
  301. BOOTINFO1(cs->as, parameters_base,
  302. BI_MAC_MEMSIZE, ram_size >> 20); /* in MB */
  303. BOOTINFO2(cs->as, parameters_base, BI_MEMCHUNK, 0, ram_size);
  304. BOOTINFO1(cs->as, parameters_base, BI_MAC_VADDR, VIDEO_BASE);
  305. BOOTINFO1(cs->as, parameters_base, BI_MAC_VDEPTH, graphic_depth);
  306. BOOTINFO1(cs->as, parameters_base, BI_MAC_VDIM,
  307. (graphic_height << 16) | graphic_width);
  308. BOOTINFO1(cs->as, parameters_base, BI_MAC_VROW,
  309. (graphic_width * graphic_depth + 7) / 8);
  310. BOOTINFO1(cs->as, parameters_base, BI_MAC_SCCBASE, SCC_BASE);
  311. rom = g_malloc(sizeof(*rom));
  312. memory_region_init_ram_ptr(rom, NULL, "m68k_fake_mac.rom",
  313. sizeof(fake_mac_rom), fake_mac_rom);
  314. memory_region_set_readonly(rom, true);
  315. memory_region_add_subregion(get_system_memory(), MACROM_ADDR, rom);
  316. if (kernel_cmdline) {
  317. BOOTINFOSTR(cs->as, parameters_base, BI_COMMAND_LINE,
  318. kernel_cmdline);
  319. }
  320. /* load initrd */
  321. if (initrd_filename) {
  322. initrd_size = get_image_size(initrd_filename);
  323. if (initrd_size < 0) {
  324. error_report("could not load initial ram disk '%s'",
  325. initrd_filename);
  326. exit(1);
  327. }
  328. initrd_base = (ram_size - initrd_size) & TARGET_PAGE_MASK;
  329. load_image_targphys(initrd_filename, initrd_base,
  330. ram_size - initrd_base);
  331. BOOTINFO2(cs->as, parameters_base, BI_RAMDISK, initrd_base,
  332. initrd_size);
  333. } else {
  334. initrd_base = 0;
  335. initrd_size = 0;
  336. }
  337. BOOTINFO0(cs->as, parameters_base, BI_LAST);
  338. } else {
  339. uint8_t *ptr;
  340. /* allocate and load BIOS */
  341. rom = g_malloc(sizeof(*rom));
  342. memory_region_init_rom(rom, NULL, "m68k_mac.rom", MACROM_SIZE,
  343. &error_abort);
  344. if (bios_name == NULL) {
  345. bios_name = MACROM_FILENAME;
  346. }
  347. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  348. memory_region_add_subregion(get_system_memory(), MACROM_ADDR, rom);
  349. /* Load MacROM binary */
  350. if (filename) {
  351. bios_size = load_image_targphys(filename, MACROM_ADDR, MACROM_SIZE);
  352. g_free(filename);
  353. } else {
  354. bios_size = -1;
  355. }
  356. /* Remove qtest_enabled() check once firmware files are in the tree */
  357. if (!qtest_enabled()) {
  358. if (bios_size < 0 || bios_size > MACROM_SIZE) {
  359. error_report("could not load MacROM '%s'", bios_name);
  360. exit(1);
  361. }
  362. ptr = rom_ptr(MACROM_ADDR, MACROM_SIZE);
  363. stl_phys(cs->as, 0, ldl_p(ptr)); /* reset initial SP */
  364. stl_phys(cs->as, 4,
  365. MACROM_ADDR + ldl_p(ptr + 4)); /* reset initial PC */
  366. }
  367. }
  368. }
  369. static void q800_machine_class_init(ObjectClass *oc, void *data)
  370. {
  371. MachineClass *mc = MACHINE_CLASS(oc);
  372. mc->desc = "Macintosh Quadra 800";
  373. mc->init = q800_init;
  374. mc->default_cpu_type = M68K_CPU_TYPE_NAME("m68040");
  375. mc->max_cpus = 1;
  376. mc->block_default_type = IF_SCSI;
  377. mc->default_ram_id = "m68k_mac.ram";
  378. }
  379. static const TypeInfo q800_machine_typeinfo = {
  380. .name = MACHINE_TYPE_NAME("q800"),
  381. .parent = TYPE_MACHINE,
  382. .class_init = q800_machine_class_init,
  383. };
  384. static void q800_machine_register_types(void)
  385. {
  386. type_register_static(&q800_machine_typeinfo);
  387. }
  388. type_init(q800_machine_register_types)