q800.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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/datadir.h"
  25. #include "sysemu/sysemu.h"
  26. #include "cpu.h"
  27. #include "hw/boards.h"
  28. #include "hw/or-irq.h"
  29. #include "hw/nmi.h"
  30. #include "elf.h"
  31. #include "hw/loader.h"
  32. #include "ui/console.h"
  33. #include "hw/char/escc.h"
  34. #include "hw/sysbus.h"
  35. #include "hw/scsi/esp.h"
  36. #include "standard-headers/asm-m68k/bootinfo.h"
  37. #include "standard-headers/asm-m68k/bootinfo-mac.h"
  38. #include "bootinfo.h"
  39. #include "hw/misc/mac_via.h"
  40. #include "hw/input/adb.h"
  41. #include "hw/nubus/mac-nubus-bridge.h"
  42. #include "hw/display/macfb.h"
  43. #include "hw/block/swim.h"
  44. #include "net/net.h"
  45. #include "qapi/error.h"
  46. #include "sysemu/qtest.h"
  47. #include "sysemu/runstate.h"
  48. #include "sysemu/reset.h"
  49. #include "migration/vmstate.h"
  50. #define MACROM_ADDR 0x40800000
  51. #define MACROM_SIZE 0x00100000
  52. #define MACROM_FILENAME "MacROM.bin"
  53. #define IO_BASE 0x50000000
  54. #define IO_SLICE 0x00040000
  55. #define IO_SIZE 0x04000000
  56. #define VIA_BASE (IO_BASE + 0x00000)
  57. #define SONIC_PROM_BASE (IO_BASE + 0x08000)
  58. #define SONIC_BASE (IO_BASE + 0x0a000)
  59. #define SCC_BASE (IO_BASE + 0x0c020)
  60. #define ESP_BASE (IO_BASE + 0x10000)
  61. #define ESP_PDMA (IO_BASE + 0x10100)
  62. #define ASC_BASE (IO_BASE + 0x14000)
  63. #define SWIM_BASE (IO_BASE + 0x1E000)
  64. #define SONIC_PROM_SIZE 0x1000
  65. /*
  66. * the video base, whereas it a Nubus address,
  67. * is needed by the kernel to have early display and
  68. * thus provided by the bootloader
  69. */
  70. #define VIDEO_BASE 0xf9000000
  71. #define MAC_CLOCK 3686418
  72. /*
  73. * Slot 0x9 is reserved for use by the in-built framebuffer whilst only
  74. * slots 0xc, 0xd and 0xe physically exist on the Quadra 800
  75. */
  76. #define Q800_NUBUS_SLOTS_AVAILABLE (BIT(0x9) | BIT(0xc) | BIT(0xd) | \
  77. BIT(0xe))
  78. /*
  79. * The GLUE (General Logic Unit) is an Apple custom integrated circuit chip
  80. * that performs a variety of functions (RAM management, clock generation, ...).
  81. * The GLUE chip receives interrupt requests from various devices,
  82. * assign priority to each, and asserts one or more interrupt line to the
  83. * CPU.
  84. */
  85. #define TYPE_GLUE "q800-glue"
  86. OBJECT_DECLARE_SIMPLE_TYPE(GLUEState, GLUE)
  87. struct GLUEState {
  88. SysBusDevice parent_obj;
  89. M68kCPU *cpu;
  90. uint8_t ipr;
  91. uint8_t auxmode;
  92. qemu_irq irqs[1];
  93. QEMUTimer *nmi_release;
  94. };
  95. #define GLUE_IRQ_IN_VIA1 0
  96. #define GLUE_IRQ_IN_VIA2 1
  97. #define GLUE_IRQ_IN_SONIC 2
  98. #define GLUE_IRQ_IN_ESCC 3
  99. #define GLUE_IRQ_IN_NMI 4
  100. #define GLUE_IRQ_NUBUS_9 0
  101. /*
  102. * The GLUE logic on the Quadra 800 supports 2 different IRQ routing modes
  103. * controlled from the VIA1 auxmode GPIO (port B bit 6) which are documented
  104. * in NetBSD as follows:
  105. *
  106. * A/UX mode (Linux, NetBSD, auxmode GPIO low)
  107. *
  108. * Level 0: Spurious: ignored
  109. * Level 1: Software
  110. * Level 2: VIA2 (except ethernet, sound)
  111. * Level 3: Ethernet
  112. * Level 4: Serial (SCC)
  113. * Level 5: Sound
  114. * Level 6: VIA1
  115. * Level 7: NMIs: parity errors, RESET button, YANCC error
  116. *
  117. * Classic mode (default: used by MacOS, A/UX 3.0.1, auxmode GPIO high)
  118. *
  119. * Level 0: Spurious: ignored
  120. * Level 1: VIA1 (clock, ADB)
  121. * Level 2: VIA2 (NuBus, SCSI)
  122. * Level 3:
  123. * Level 4: Serial (SCC)
  124. * Level 5:
  125. * Level 6:
  126. * Level 7: Non-maskable: parity errors, RESET button
  127. *
  128. * Note that despite references to A/UX mode in Linux and NetBSD, at least
  129. * A/UX 3.0.1 still uses Classic mode.
  130. */
  131. static void GLUE_set_irq(void *opaque, int irq, int level)
  132. {
  133. GLUEState *s = opaque;
  134. int i;
  135. if (s->auxmode) {
  136. /* Classic mode */
  137. switch (irq) {
  138. case GLUE_IRQ_IN_VIA1:
  139. irq = 0;
  140. break;
  141. case GLUE_IRQ_IN_VIA2:
  142. irq = 1;
  143. break;
  144. case GLUE_IRQ_IN_SONIC:
  145. /* Route to VIA2 instead */
  146. qemu_set_irq(s->irqs[GLUE_IRQ_NUBUS_9], level);
  147. return;
  148. case GLUE_IRQ_IN_ESCC:
  149. irq = 3;
  150. break;
  151. case GLUE_IRQ_IN_NMI:
  152. irq = 6;
  153. break;
  154. default:
  155. g_assert_not_reached();
  156. }
  157. } else {
  158. /* A/UX mode */
  159. switch (irq) {
  160. case GLUE_IRQ_IN_VIA1:
  161. irq = 5;
  162. break;
  163. case GLUE_IRQ_IN_VIA2:
  164. irq = 1;
  165. break;
  166. case GLUE_IRQ_IN_SONIC:
  167. irq = 2;
  168. break;
  169. case GLUE_IRQ_IN_ESCC:
  170. irq = 3;
  171. break;
  172. case GLUE_IRQ_IN_NMI:
  173. irq = 6;
  174. break;
  175. default:
  176. g_assert_not_reached();
  177. }
  178. }
  179. if (level) {
  180. s->ipr |= 1 << irq;
  181. } else {
  182. s->ipr &= ~(1 << irq);
  183. }
  184. for (i = 7; i >= 0; i--) {
  185. if ((s->ipr >> i) & 1) {
  186. m68k_set_irq_level(s->cpu, i + 1, i + 25);
  187. return;
  188. }
  189. }
  190. m68k_set_irq_level(s->cpu, 0, 0);
  191. }
  192. static void glue_auxmode_set_irq(void *opaque, int irq, int level)
  193. {
  194. GLUEState *s = GLUE(opaque);
  195. s->auxmode = level;
  196. }
  197. static void glue_nmi(NMIState *n, int cpu_index, Error **errp)
  198. {
  199. GLUEState *s = GLUE(n);
  200. /* Hold NMI active for 100ms */
  201. GLUE_set_irq(s, GLUE_IRQ_IN_NMI, 1);
  202. timer_mod(s->nmi_release, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 100);
  203. }
  204. static void glue_nmi_release(void *opaque)
  205. {
  206. GLUEState *s = GLUE(opaque);
  207. GLUE_set_irq(s, GLUE_IRQ_IN_NMI, 0);
  208. }
  209. static void glue_reset(DeviceState *dev)
  210. {
  211. GLUEState *s = GLUE(dev);
  212. s->ipr = 0;
  213. s->auxmode = 0;
  214. timer_del(s->nmi_release);
  215. }
  216. static const VMStateDescription vmstate_glue = {
  217. .name = "q800-glue",
  218. .version_id = 0,
  219. .minimum_version_id = 0,
  220. .fields = (VMStateField[]) {
  221. VMSTATE_UINT8(ipr, GLUEState),
  222. VMSTATE_UINT8(auxmode, GLUEState),
  223. VMSTATE_TIMER_PTR(nmi_release, GLUEState),
  224. VMSTATE_END_OF_LIST(),
  225. },
  226. };
  227. /*
  228. * If the m68k CPU implemented its inbound irq lines as GPIO lines
  229. * rather than via the m68k_set_irq_level() function we would not need
  230. * this cpu link property and could instead provide outbound IRQ lines
  231. * that the board could wire up to the CPU.
  232. */
  233. static Property glue_properties[] = {
  234. DEFINE_PROP_LINK("cpu", GLUEState, cpu, TYPE_M68K_CPU, M68kCPU *),
  235. DEFINE_PROP_END_OF_LIST(),
  236. };
  237. static void glue_finalize(Object *obj)
  238. {
  239. GLUEState *s = GLUE(obj);
  240. timer_free(s->nmi_release);
  241. }
  242. static void glue_init(Object *obj)
  243. {
  244. DeviceState *dev = DEVICE(obj);
  245. GLUEState *s = GLUE(dev);
  246. qdev_init_gpio_in(dev, GLUE_set_irq, 8);
  247. qdev_init_gpio_in_named(dev, glue_auxmode_set_irq, "auxmode", 1);
  248. qdev_init_gpio_out(dev, s->irqs, 1);
  249. /* NMI release timer */
  250. s->nmi_release = timer_new_ms(QEMU_CLOCK_VIRTUAL, glue_nmi_release, s);
  251. }
  252. static void glue_class_init(ObjectClass *klass, void *data)
  253. {
  254. DeviceClass *dc = DEVICE_CLASS(klass);
  255. NMIClass *nc = NMI_CLASS(klass);
  256. dc->vmsd = &vmstate_glue;
  257. dc->reset = glue_reset;
  258. device_class_set_props(dc, glue_properties);
  259. nc->nmi_monitor_handler = glue_nmi;
  260. }
  261. static const TypeInfo glue_info = {
  262. .name = TYPE_GLUE,
  263. .parent = TYPE_SYS_BUS_DEVICE,
  264. .instance_size = sizeof(GLUEState),
  265. .instance_init = glue_init,
  266. .instance_finalize = glue_finalize,
  267. .class_init = glue_class_init,
  268. .interfaces = (InterfaceInfo[]) {
  269. { TYPE_NMI },
  270. { }
  271. },
  272. };
  273. static void main_cpu_reset(void *opaque)
  274. {
  275. M68kCPU *cpu = opaque;
  276. CPUState *cs = CPU(cpu);
  277. cpu_reset(cs);
  278. cpu->env.aregs[7] = ldl_phys(cs->as, 0);
  279. cpu->env.pc = ldl_phys(cs->as, 4);
  280. }
  281. static uint8_t fake_mac_rom[] = {
  282. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  283. /* offset: 0xa - mac_reset */
  284. /* via2[vDirB] |= VIA2B_vPower */
  285. 0x20, 0x7C, 0x50, 0xF0, 0x24, 0x00, /* moveal VIA2_BASE+vDirB,%a0 */
  286. 0x10, 0x10, /* moveb %a0@,%d0 */
  287. 0x00, 0x00, 0x00, 0x04, /* orib #4,%d0 */
  288. 0x10, 0x80, /* moveb %d0,%a0@ */
  289. /* via2[vBufB] &= ~VIA2B_vPower */
  290. 0x20, 0x7C, 0x50, 0xF0, 0x20, 0x00, /* moveal VIA2_BASE+vBufB,%a0 */
  291. 0x10, 0x10, /* moveb %a0@,%d0 */
  292. 0x02, 0x00, 0xFF, 0xFB, /* andib #-5,%d0 */
  293. 0x10, 0x80, /* moveb %d0,%a0@ */
  294. /* while (true) ; */
  295. 0x60, 0xFE /* bras [self] */
  296. };
  297. static void q800_init(MachineState *machine)
  298. {
  299. M68kCPU *cpu = NULL;
  300. int linux_boot;
  301. int32_t kernel_size;
  302. uint64_t elf_entry;
  303. char *filename;
  304. int bios_size;
  305. ram_addr_t initrd_base;
  306. int32_t initrd_size;
  307. MemoryRegion *rom;
  308. MemoryRegion *io;
  309. MemoryRegion *dp8393x_prom = g_new(MemoryRegion, 1);
  310. uint8_t *prom;
  311. const int io_slice_nb = (IO_SIZE / IO_SLICE) - 1;
  312. int i, checksum;
  313. MacFbMode *macfb_mode;
  314. ram_addr_t ram_size = machine->ram_size;
  315. const char *kernel_filename = machine->kernel_filename;
  316. const char *initrd_filename = machine->initrd_filename;
  317. const char *kernel_cmdline = machine->kernel_cmdline;
  318. const char *bios_name = machine->firmware ?: MACROM_FILENAME;
  319. hwaddr parameters_base;
  320. CPUState *cs;
  321. DeviceState *dev;
  322. DeviceState *via1_dev, *via2_dev;
  323. DeviceState *escc_orgate;
  324. SysBusESPState *sysbus_esp;
  325. ESPState *esp;
  326. SysBusDevice *sysbus;
  327. BusState *adb_bus;
  328. NubusBus *nubus;
  329. DeviceState *glue;
  330. DriveInfo *dinfo;
  331. linux_boot = (kernel_filename != NULL);
  332. if (ram_size > 1 * GiB) {
  333. error_report("Too much memory for this machine: %" PRId64 " MiB, "
  334. "maximum 1024 MiB", ram_size / MiB);
  335. exit(1);
  336. }
  337. /* init CPUs */
  338. cpu = M68K_CPU(cpu_create(machine->cpu_type));
  339. qemu_register_reset(main_cpu_reset, cpu);
  340. /* RAM */
  341. memory_region_add_subregion(get_system_memory(), 0, machine->ram);
  342. /*
  343. * Memory from IO_BASE to IO_BASE + IO_SLICE is repeated
  344. * from IO_BASE + IO_SLICE to IO_BASE + IO_SIZE
  345. */
  346. io = g_new(MemoryRegion, io_slice_nb);
  347. for (i = 0; i < io_slice_nb; i++) {
  348. char *name = g_strdup_printf("mac_m68k.io[%d]", i + 1);
  349. memory_region_init_alias(&io[i], NULL, name, get_system_memory(),
  350. IO_BASE, IO_SLICE);
  351. memory_region_add_subregion(get_system_memory(),
  352. IO_BASE + (i + 1) * IO_SLICE, &io[i]);
  353. g_free(name);
  354. }
  355. /* IRQ Glue */
  356. glue = qdev_new(TYPE_GLUE);
  357. object_property_set_link(OBJECT(glue), "cpu", OBJECT(cpu), &error_abort);
  358. sysbus_realize_and_unref(SYS_BUS_DEVICE(glue), &error_fatal);
  359. /* VIA 1 */
  360. via1_dev = qdev_new(TYPE_MOS6522_Q800_VIA1);
  361. dinfo = drive_get(IF_MTD, 0, 0);
  362. if (dinfo) {
  363. qdev_prop_set_drive(via1_dev, "drive", blk_by_legacy_dinfo(dinfo));
  364. }
  365. sysbus = SYS_BUS_DEVICE(via1_dev);
  366. sysbus_realize_and_unref(sysbus, &error_fatal);
  367. sysbus_mmio_map(sysbus, 1, VIA_BASE);
  368. sysbus_connect_irq(sysbus, 0, qdev_get_gpio_in(glue, GLUE_IRQ_IN_VIA1));
  369. /* A/UX mode */
  370. qdev_connect_gpio_out(via1_dev, 0,
  371. qdev_get_gpio_in_named(glue, "auxmode", 0));
  372. adb_bus = qdev_get_child_bus(via1_dev, "adb.0");
  373. dev = qdev_new(TYPE_ADB_KEYBOARD);
  374. qdev_realize_and_unref(dev, adb_bus, &error_fatal);
  375. dev = qdev_new(TYPE_ADB_MOUSE);
  376. qdev_realize_and_unref(dev, adb_bus, &error_fatal);
  377. /* VIA 2 */
  378. via2_dev = qdev_new(TYPE_MOS6522_Q800_VIA2);
  379. sysbus = SYS_BUS_DEVICE(via2_dev);
  380. sysbus_realize_and_unref(sysbus, &error_fatal);
  381. sysbus_mmio_map(sysbus, 1, VIA_BASE + VIA_SIZE);
  382. sysbus_connect_irq(sysbus, 0, qdev_get_gpio_in(glue, GLUE_IRQ_IN_VIA2));
  383. /* MACSONIC */
  384. if (nb_nics > 1) {
  385. error_report("q800 can only have one ethernet interface");
  386. exit(1);
  387. }
  388. qemu_check_nic_model(&nd_table[0], "dp83932");
  389. /*
  390. * MacSonic driver needs an Apple MAC address
  391. * Valid prefix are:
  392. * 00:05:02 Apple
  393. * 00:80:19 Dayna Communications, Inc.
  394. * 00:A0:40 Apple
  395. * 08:00:07 Apple
  396. * (Q800 use the last one)
  397. */
  398. nd_table[0].macaddr.a[0] = 0x08;
  399. nd_table[0].macaddr.a[1] = 0x00;
  400. nd_table[0].macaddr.a[2] = 0x07;
  401. dev = qdev_new("dp8393x");
  402. qdev_set_nic_properties(dev, &nd_table[0]);
  403. qdev_prop_set_uint8(dev, "it_shift", 2);
  404. qdev_prop_set_bit(dev, "big_endian", true);
  405. object_property_set_link(OBJECT(dev), "dma_mr",
  406. OBJECT(get_system_memory()), &error_abort);
  407. sysbus = SYS_BUS_DEVICE(dev);
  408. sysbus_realize_and_unref(sysbus, &error_fatal);
  409. sysbus_mmio_map(sysbus, 0, SONIC_BASE);
  410. sysbus_connect_irq(sysbus, 0, qdev_get_gpio_in(glue, GLUE_IRQ_IN_SONIC));
  411. memory_region_init_rom(dp8393x_prom, NULL, "dp8393x-q800.prom",
  412. SONIC_PROM_SIZE, &error_fatal);
  413. memory_region_add_subregion(get_system_memory(), SONIC_PROM_BASE,
  414. dp8393x_prom);
  415. /* Add MAC address with valid checksum to PROM */
  416. prom = memory_region_get_ram_ptr(dp8393x_prom);
  417. checksum = 0;
  418. for (i = 0; i < 6; i++) {
  419. prom[i] = revbit8(nd_table[0].macaddr.a[i]);
  420. checksum ^= prom[i];
  421. }
  422. prom[7] = 0xff - checksum;
  423. /* SCC */
  424. dev = qdev_new(TYPE_ESCC);
  425. qdev_prop_set_uint32(dev, "disabled", 0);
  426. qdev_prop_set_uint32(dev, "frequency", MAC_CLOCK);
  427. qdev_prop_set_uint32(dev, "it_shift", 1);
  428. qdev_prop_set_bit(dev, "bit_swap", true);
  429. qdev_prop_set_chr(dev, "chrA", serial_hd(0));
  430. qdev_prop_set_chr(dev, "chrB", serial_hd(1));
  431. qdev_prop_set_uint32(dev, "chnBtype", 0);
  432. qdev_prop_set_uint32(dev, "chnAtype", 0);
  433. sysbus = SYS_BUS_DEVICE(dev);
  434. sysbus_realize_and_unref(sysbus, &error_fatal);
  435. /* Logically OR both its IRQs together */
  436. escc_orgate = DEVICE(object_new(TYPE_OR_IRQ));
  437. object_property_set_int(OBJECT(escc_orgate), "num-lines", 2, &error_fatal);
  438. qdev_realize_and_unref(escc_orgate, NULL, &error_fatal);
  439. sysbus_connect_irq(sysbus, 0, qdev_get_gpio_in(escc_orgate, 0));
  440. sysbus_connect_irq(sysbus, 1, qdev_get_gpio_in(escc_orgate, 1));
  441. qdev_connect_gpio_out(DEVICE(escc_orgate), 0,
  442. qdev_get_gpio_in(glue, GLUE_IRQ_IN_ESCC));
  443. sysbus_mmio_map(sysbus, 0, SCC_BASE);
  444. /* SCSI */
  445. dev = qdev_new(TYPE_SYSBUS_ESP);
  446. sysbus_esp = SYSBUS_ESP(dev);
  447. esp = &sysbus_esp->esp;
  448. esp->dma_memory_read = NULL;
  449. esp->dma_memory_write = NULL;
  450. esp->dma_opaque = NULL;
  451. sysbus_esp->it_shift = 4;
  452. esp->dma_enabled = 1;
  453. sysbus = SYS_BUS_DEVICE(dev);
  454. sysbus_realize_and_unref(sysbus, &error_fatal);
  455. /* SCSI and SCSI data IRQs are negative edge triggered */
  456. sysbus_connect_irq(sysbus, 0, qemu_irq_invert(qdev_get_gpio_in(via2_dev,
  457. VIA2_IRQ_SCSI_BIT)));
  458. sysbus_connect_irq(sysbus, 1, qemu_irq_invert(qdev_get_gpio_in(via2_dev,
  459. VIA2_IRQ_SCSI_DATA_BIT)));
  460. sysbus_mmio_map(sysbus, 0, ESP_BASE);
  461. sysbus_mmio_map(sysbus, 1, ESP_PDMA);
  462. scsi_bus_legacy_handle_cmdline(&esp->bus);
  463. /* SWIM floppy controller */
  464. dev = qdev_new(TYPE_SWIM);
  465. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  466. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, SWIM_BASE);
  467. /* NuBus */
  468. dev = qdev_new(TYPE_MAC_NUBUS_BRIDGE);
  469. qdev_prop_set_uint32(dev, "slot-available-mask",
  470. Q800_NUBUS_SLOTS_AVAILABLE);
  471. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  472. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0,
  473. MAC_NUBUS_FIRST_SLOT * NUBUS_SUPER_SLOT_SIZE);
  474. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 1, NUBUS_SLOT_BASE +
  475. MAC_NUBUS_FIRST_SLOT * NUBUS_SLOT_SIZE);
  476. qdev_connect_gpio_out(dev, 9,
  477. qdev_get_gpio_in_named(via2_dev, "nubus-irq",
  478. VIA2_NUBUS_IRQ_INTVIDEO));
  479. for (i = 1; i < VIA2_NUBUS_IRQ_NB; i++) {
  480. qdev_connect_gpio_out(dev, 9 + i,
  481. qdev_get_gpio_in_named(via2_dev, "nubus-irq",
  482. VIA2_NUBUS_IRQ_9 + i));
  483. }
  484. /*
  485. * Since the framebuffer in slot 0x9 uses a separate IRQ, wire the unused
  486. * IRQ via GLUE for use by SONIC Ethernet in classic mode
  487. */
  488. qdev_connect_gpio_out(glue, GLUE_IRQ_NUBUS_9,
  489. qdev_get_gpio_in_named(via2_dev, "nubus-irq",
  490. VIA2_NUBUS_IRQ_9));
  491. nubus = &NUBUS_BRIDGE(dev)->bus;
  492. /* framebuffer in nubus slot #9 */
  493. dev = qdev_new(TYPE_NUBUS_MACFB);
  494. qdev_prop_set_uint32(dev, "slot", 9);
  495. qdev_prop_set_uint32(dev, "width", graphic_width);
  496. qdev_prop_set_uint32(dev, "height", graphic_height);
  497. qdev_prop_set_uint8(dev, "depth", graphic_depth);
  498. if (graphic_width == 1152 && graphic_height == 870) {
  499. qdev_prop_set_uint8(dev, "display", MACFB_DISPLAY_APPLE_21_COLOR);
  500. } else {
  501. qdev_prop_set_uint8(dev, "display", MACFB_DISPLAY_VGA);
  502. }
  503. qdev_realize_and_unref(dev, BUS(nubus), &error_fatal);
  504. macfb_mode = (NUBUS_MACFB(dev)->macfb).mode;
  505. cs = CPU(cpu);
  506. if (linux_boot) {
  507. uint64_t high;
  508. kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
  509. &elf_entry, NULL, &high, NULL, 1,
  510. EM_68K, 0, 0);
  511. if (kernel_size < 0) {
  512. error_report("could not load kernel '%s'", kernel_filename);
  513. exit(1);
  514. }
  515. stl_phys(cs->as, 4, elf_entry); /* reset initial PC */
  516. parameters_base = (high + 1) & ~1;
  517. BOOTINFO1(cs->as, parameters_base, BI_MACHTYPE, MACH_MAC);
  518. BOOTINFO1(cs->as, parameters_base, BI_FPUTYPE, FPU_68040);
  519. BOOTINFO1(cs->as, parameters_base, BI_MMUTYPE, MMU_68040);
  520. BOOTINFO1(cs->as, parameters_base, BI_CPUTYPE, CPU_68040);
  521. BOOTINFO1(cs->as, parameters_base, BI_MAC_CPUID, CPUB_68040);
  522. BOOTINFO1(cs->as, parameters_base, BI_MAC_MODEL, MAC_MODEL_Q800);
  523. BOOTINFO1(cs->as, parameters_base,
  524. BI_MAC_MEMSIZE, ram_size >> 20); /* in MB */
  525. BOOTINFO2(cs->as, parameters_base, BI_MEMCHUNK, 0, ram_size);
  526. BOOTINFO1(cs->as, parameters_base, BI_MAC_VADDR,
  527. VIDEO_BASE + macfb_mode->offset);
  528. BOOTINFO1(cs->as, parameters_base, BI_MAC_VDEPTH, graphic_depth);
  529. BOOTINFO1(cs->as, parameters_base, BI_MAC_VDIM,
  530. (graphic_height << 16) | graphic_width);
  531. BOOTINFO1(cs->as, parameters_base, BI_MAC_VROW, macfb_mode->stride);
  532. BOOTINFO1(cs->as, parameters_base, BI_MAC_SCCBASE, SCC_BASE);
  533. rom = g_malloc(sizeof(*rom));
  534. memory_region_init_ram_ptr(rom, NULL, "m68k_fake_mac.rom",
  535. sizeof(fake_mac_rom), fake_mac_rom);
  536. memory_region_set_readonly(rom, true);
  537. memory_region_add_subregion(get_system_memory(), MACROM_ADDR, rom);
  538. if (kernel_cmdline) {
  539. BOOTINFOSTR(cs->as, parameters_base, BI_COMMAND_LINE,
  540. kernel_cmdline);
  541. }
  542. /* load initrd */
  543. if (initrd_filename) {
  544. initrd_size = get_image_size(initrd_filename);
  545. if (initrd_size < 0) {
  546. error_report("could not load initial ram disk '%s'",
  547. initrd_filename);
  548. exit(1);
  549. }
  550. initrd_base = (ram_size - initrd_size) & TARGET_PAGE_MASK;
  551. load_image_targphys(initrd_filename, initrd_base,
  552. ram_size - initrd_base);
  553. BOOTINFO2(cs->as, parameters_base, BI_RAMDISK, initrd_base,
  554. initrd_size);
  555. } else {
  556. initrd_base = 0;
  557. initrd_size = 0;
  558. }
  559. BOOTINFO0(cs->as, parameters_base, BI_LAST);
  560. } else {
  561. uint8_t *ptr;
  562. /* allocate and load BIOS */
  563. rom = g_malloc(sizeof(*rom));
  564. memory_region_init_rom(rom, NULL, "m68k_mac.rom", MACROM_SIZE,
  565. &error_abort);
  566. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  567. memory_region_add_subregion(get_system_memory(), MACROM_ADDR, rom);
  568. /* Load MacROM binary */
  569. if (filename) {
  570. bios_size = load_image_targphys(filename, MACROM_ADDR, MACROM_SIZE);
  571. g_free(filename);
  572. } else {
  573. bios_size = -1;
  574. }
  575. /* Remove qtest_enabled() check once firmware files are in the tree */
  576. if (!qtest_enabled()) {
  577. if (bios_size <= 0 || bios_size > MACROM_SIZE) {
  578. error_report("could not load MacROM '%s'", bios_name);
  579. exit(1);
  580. }
  581. ptr = rom_ptr(MACROM_ADDR, bios_size);
  582. assert(ptr != NULL);
  583. stl_phys(cs->as, 0, ldl_p(ptr)); /* reset initial SP */
  584. stl_phys(cs->as, 4,
  585. MACROM_ADDR + ldl_p(ptr + 4)); /* reset initial PC */
  586. }
  587. }
  588. }
  589. static void q800_machine_class_init(ObjectClass *oc, void *data)
  590. {
  591. MachineClass *mc = MACHINE_CLASS(oc);
  592. mc->desc = "Macintosh Quadra 800";
  593. mc->init = q800_init;
  594. mc->default_cpu_type = M68K_CPU_TYPE_NAME("m68040");
  595. mc->max_cpus = 1;
  596. mc->block_default_type = IF_SCSI;
  597. mc->default_ram_id = "m68k_mac.ram";
  598. }
  599. static const TypeInfo q800_machine_typeinfo = {
  600. .name = MACHINE_TYPE_NAME("q800"),
  601. .parent = TYPE_MACHINE,
  602. .class_init = q800_machine_class_init,
  603. };
  604. static void q800_machine_register_types(void)
  605. {
  606. type_register_static(&q800_machine_typeinfo);
  607. type_register_static(&glue_info);
  608. }
  609. type_init(q800_machine_register_types)