mips bios loading fix
[qemu] / hw / ppc_chrp.c
index 515806f..33167cd 100644 (file)
 #include "vl.h"
 
 #define BIOS_FILENAME "ppc_rom.bin"
+#define VGABIOS_FILENAME "video.x"
 #define NVRAM_SIZE        0x2000
 
 #define KERNEL_LOAD_ADDR 0x01000000
 #define INITRD_LOAD_ADDR 0x01800000
 
 /* MacIO devices (mapped inside the MacIO address space): CUDA, DBDMA,
-   NVRAM (not implemented).  */
+   NVRAM */
 
 static int dbdma_mem_index;
 static int cuda_mem_index;
@@ -38,6 +39,7 @@ static int ide0_mem_index = -1;
 static int ide1_mem_index = -1;
 static int openpic_mem_index = -1;
 static int heathrow_pic_mem_index = -1;
+static int macio_nvram_mem_index = -1;
 
 /* DBDMA: currently no op - should suffice right now */
 
@@ -82,6 +84,53 @@ static CPUReadMemoryFunc *dbdma_read[] = {
     &dbdma_readl,
 };
 
+/* macio style NVRAM device */
+typedef struct MacIONVRAMState {
+    uint8_t data[0x2000];
+} MacIONVRAMState;
+
+static void macio_nvram_writeb (void *opaque, target_phys_addr_t addr, uint32_t value)
+{
+    MacIONVRAMState *s = opaque;
+    addr = (addr >> 4) & 0x1fff;
+    s->data[addr] = value;
+    //    printf("macio_nvram_writeb %04x = %02x\n", addr, value);
+}
+
+static uint32_t macio_nvram_readb (void *opaque, target_phys_addr_t addr)
+{
+    MacIONVRAMState *s = opaque;
+    uint32_t value;
+
+    addr = (addr >> 4) & 0x1fff;
+    value = s->data[addr];
+    //    printf("macio_nvram_readb %04x = %02x\n", addr, value);
+    return value;
+}
+
+static CPUWriteMemoryFunc *macio_nvram_write[] = {
+    &macio_nvram_writeb,
+    &macio_nvram_writeb,
+    &macio_nvram_writeb,
+};
+
+static CPUReadMemoryFunc *macio_nvram_read[] = {
+    &macio_nvram_readb,
+    &macio_nvram_readb,
+    &macio_nvram_readb,
+};
+
+static MacIONVRAMState *macio_nvram_init(void)
+{
+    MacIONVRAMState *s;
+    s = qemu_mallocz(sizeof(MacIONVRAMState));
+    if (!s)
+        return NULL;
+    macio_nvram_mem_index = cpu_register_io_memory(0, macio_nvram_read, 
+                                                   macio_nvram_write, s);
+    return s;
+}
+
 static void macio_map(PCIDevice *pci_dev, int region_num, 
                       uint32_t addr, uint32_t size, int type)
 {
@@ -99,9 +148,11 @@ static void macio_map(PCIDevice *pci_dev, int region_num,
         cpu_register_physical_memory(addr + 0x40000, 0x40000, 
                                      openpic_mem_index);
     }
+    if (macio_nvram_mem_index >= 0)
+        cpu_register_physical_memory(addr + 0x60000, 0x20000, macio_nvram_mem_index);
 }
 
-static void macio_init(PCIBus *bus)
+static void macio_init(PCIBus *bus, int device_id)
 {
     PCIDevice *d;
 
@@ -111,8 +162,8 @@ static void macio_init(PCIBus *bus)
        in PearPC */
     d->config[0x00] = 0x6b; // vendor_id
     d->config[0x01] = 0x10;
-    d->config[0x02] = 0x22;
-    d->config[0x03] = 0x00;
+    d->config[0x02] = device_id;
+    d->config[0x03] = device_id >> 8;
 
     d->config[0x0a] = 0x00; // class_sub = pci2pci
     d->config[0x0b] = 0xff; // class_base = bridge
@@ -218,6 +269,28 @@ static void pic_irq_request(void *opaque, int level)
 {
 }
 
+static uint8_t nvram_chksum(const uint8_t *buf, int n)
+{
+    int sum, i;
+    sum = 0;
+    for(i = 0; i < n; i++)
+        sum += buf[i];
+    return (sum & 0xff) + (sum >> 8);
+}
+
+/* set a free Mac OS NVRAM partition */
+void pmac_format_nvram_partition(uint8_t *buf, int len)
+{
+    char partition_name[12] = "wwwwwwwwwwww";
+    
+    buf[0] = 0x7f; /* free partition magic */
+    buf[1] = 0; /* checksum */
+    buf[2] = len >> 8;
+    buf[3] = len;
+    memcpy(buf + 4, partition_name, 12);
+    buf[1] = nvram_chksum(buf, 16);
+}    
+
 /* PowerPC CHRP hardware initialisation */
 static void ppc_chrp_init(int ram_size, int vga_ram_size, int boot_device,
                           DisplayState *ds, const char **fd_filename, 
@@ -227,35 +300,88 @@ static void ppc_chrp_init(int ram_size, int vga_ram_size, int boot_device,
                           const char *initrd_filename,
                           int is_heathrow)
 {
+    CPUState *env;
     char buf[1024];
     SetIRQFunc *set_irq;
     void *pic;
     m48t59_t *nvram;
     int PPC_io_memory, unin_memory;
-    int ret, linux_boot, i;
-    unsigned long bios_offset;
+    int linux_boot, i;
+    unsigned long bios_offset, vga_bios_offset;
     uint32_t kernel_base, kernel_size, initrd_base, initrd_size;
     ppc_def_t *def;
     PCIBus *pci_bus;
     const char *arch_name;
+    int vga_bios_size, bios_size;
 
     linux_boot = (kernel_filename != NULL);
 
+    /* init CPUs */
+    env = cpu_init();
+    register_savevm("cpu", 0, 3, cpu_save, cpu_load, env);
+
+    /* Register CPU as a 74x/75x */
+    /* XXX: CPU model (or PVR) should be provided on command line */
+    //    ppc_find_by_name("750gx", &def); // Linux boot OK
+    //    ppc_find_by_name("750fx", &def); // Linux boot OK
+    /* Linux does not boot on 750cxe (and probably other 750cx based)
+     * because it assumes it has 8 IBAT & DBAT pairs as it only have 4.
+     */
+    //    ppc_find_by_name("750cxe", &def);
+    //    ppc_find_by_name("750p", &def);
+    //    ppc_find_by_name("740p", &def);
+    ppc_find_by_name("750", &def);
+    //    ppc_find_by_name("740", &def);
+    //    ppc_find_by_name("G3", &def);
+    //    ppc_find_by_name("604r", &def);
+    //    ppc_find_by_name("604e", &def);
+    //    ppc_find_by_name("604", &def);
+    if (def == NULL) {
+        cpu_abort(env, "Unable to find PowerPC CPU definition\n");
+    }
+    cpu_ppc_register(env, def);
+
+    /* Set time-base frequency to 100 Mhz */
+    cpu_ppc_tb_init(env, 100UL * 1000UL * 1000UL);
+    
+    env->osi_call = vga_osi_call;
+
     /* allocate RAM */
     cpu_register_physical_memory(0, ram_size, IO_MEM_RAM);
 
     /* allocate and load BIOS */
     bios_offset = ram_size + vga_ram_size;
     snprintf(buf, sizeof(buf), "%s/%s", bios_dir, BIOS_FILENAME);
-    ret = load_image(buf, phys_ram_base + bios_offset);
-    if (ret != BIOS_SIZE) {
-        fprintf(stderr, "qemu: could not load PPC PREP bios '%s'\n", buf);
+    bios_size = load_image(buf, phys_ram_base + bios_offset);
+    if (bios_size < 0 || bios_size > BIOS_SIZE) {
+        fprintf(stderr, "qemu: could not load PowerPC bios '%s'\n", buf);
         exit(1);
     }
-    cpu_register_physical_memory((uint32_t)(-BIOS_SIZE), 
-                                 BIOS_SIZE, bios_offset | IO_MEM_ROM);
-    cpu_single_env->nip = 0xfffffffc;
-
+    bios_size = (bios_size + 0xfff) & ~0xfff;
+    cpu_register_physical_memory((uint32_t)(-bios_size), 
+                                 bios_size, bios_offset | IO_MEM_ROM);
+    
+    /* allocate and load VGA BIOS */
+    vga_bios_offset = bios_offset + bios_size;
+    snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_FILENAME);
+    vga_bios_size = load_image(buf, phys_ram_base + vga_bios_offset + 8);
+    if (vga_bios_size < 0) {
+        /* if no bios is present, we can still work */
+        fprintf(stderr, "qemu: warning: could not load VGA bios '%s'\n", buf);
+        vga_bios_size = 0;
+    } else {
+        /* set a specific header (XXX: find real Apple format for NDRV
+           drivers) */
+        phys_ram_base[vga_bios_offset] = 'N';
+        phys_ram_base[vga_bios_offset + 1] = 'D';
+        phys_ram_base[vga_bios_offset + 2] = 'R';
+        phys_ram_base[vga_bios_offset + 3] = 'V';
+        cpu_to_be32w((uint32_t *)(phys_ram_base + vga_bios_offset + 4), 
+                     vga_bios_size);
+        vga_bios_size += 8;
+    }
+    vga_bios_size = (vga_bios_size + 0xfff) & ~0xfff;
+    
     if (linux_boot) {
         kernel_base = KERNEL_LOAD_ADDR;
         /* now we can load the kernel */
@@ -286,31 +412,6 @@ static void ppc_chrp_init(int ram_size, int vga_ram_size, int boot_device,
         initrd_base = 0;
         initrd_size = 0;
     }
-    /* Register CPU as a 74x/75x */
-    /* XXX: CPU model (or PVR) should be provided on command line */
-    //    ppc_find_by_name("750gx", &def); // Linux boot OK
-    //    ppc_find_by_name("750fx", &def); // Linux boot OK
-    /* Linux does not boot on 750cxe (and probably other 750cx based)
-     * because it assumes it has 8 IBAT & DBAT pairs as it only have 4.
-     */
-    //    ppc_find_by_name("750cxe", &def);
-    //    ppc_find_by_name("750p", &def);
-    //    ppc_find_by_name("740p", &def);
-    ppc_find_by_name("750", &def);
-    //    ppc_find_by_name("740", &def);
-    //    ppc_find_by_name("G3", &def);
-    //    ppc_find_by_name("604r", &def);
-    //    ppc_find_by_name("604e", &def);
-    //    ppc_find_by_name("604", &def);
-    if (def == NULL) {
-        cpu_abort(cpu_single_env, "Unable to find PowerPC CPU definition\n");
-    }
-    cpu_ppc_register(cpu_single_env, def);
-
-    /* Set time-base frequency to 100 Mhz */
-    cpu_ppc_tb_init(cpu_single_env, 100UL * 1000UL * 1000UL);
-
-    cpu_single_env->osi_call = vga_osi_call;
 
     if (is_heathrow) {
         isa_mem_base = 0x80000000;
@@ -321,8 +422,9 @@ static void ppc_chrp_init(int ram_size, int vga_ram_size, int boot_device,
         cpu_register_physical_memory(0xfe000000, 0x00200000, PPC_io_memory);
         
         /* init basic PC hardware */
-        vga_initialize(pci_bus, ds, phys_ram_base + ram_size, ram_size, 
-                       vga_ram_size);
+        vga_initialize(pci_bus, ds, phys_ram_base + ram_size, 
+                       ram_size, vga_ram_size,
+                       vga_bios_offset, vga_bios_size);
         pic = heathrow_pic_init(&heathrow_pic_mem_index);
         set_irq = heathrow_pic_set_irq;
         pci_set_pic(pci_bus, set_irq, pic);
@@ -331,10 +433,12 @@ static void ppc_chrp_init(int ram_size, int vga_ram_size, int boot_device,
         isa_pic = pic_init(pic_irq_request, NULL);
         
         /* XXX: use Mac Serial port */
-        serial_init(0x3f8, 4, serial_hds[0]);
+        serial_init(&pic_set_irq_new, isa_pic, 0x3f8, 4, serial_hds[0]);
         
         for(i = 0; i < nb_nics; i++) {
-            pci_ne2000_init(pci_bus, &nd_table[i]);
+            if (!nd_table[i].model)
+                nd_table[i].model = "ne2k_pci";
+            pci_nic_init(pci_bus, &nd_table[i]);
         }
         
         pci_cmd646_ide_init(pci_bus, &bs_table[0], 0);
@@ -345,9 +449,15 @@ static void ppc_chrp_init(int ram_size, int vga_ram_size, int boot_device,
         adb_kbd_init(&adb_bus);
         adb_mouse_init(&adb_bus);
         
-        macio_init(pci_bus);
+        {
+            MacIONVRAMState *nvr;
+            nvr = macio_nvram_init();
+            pmac_format_nvram_partition(nvr->data, 0x2000);
+        }
+
+        macio_init(pci_bus, 0x0017);
         
-        nvram = m48t59_init(8, 0xFFF04000, 0x0074, NVRAM_SIZE);
+        nvram = m48t59_init(8, 0xFFF04000, 0x0074, NVRAM_SIZE, 59);
         
         arch_name = "HEATHROW";
     } else {
@@ -363,9 +473,10 @@ static void ppc_chrp_init(int ram_size, int vga_ram_size, int boot_device,
         cpu_register_physical_memory(0xf8000000, 0x00001000, unin_memory);
 
         /* init basic PC hardware */
-        vga_initialize(pci_bus, ds, phys_ram_base + ram_size, ram_size, 
-                       vga_ram_size);
-        pic = openpic_init(NULL, &openpic_mem_index, 1);
+        vga_initialize(pci_bus, ds, phys_ram_base + ram_size,
+                       ram_size, vga_ram_size,
+                       vga_bios_offset, vga_bios_size);
+        pic = openpic_init(NULL, &openpic_mem_index, 1, &env);
         set_irq = openpic_set_irq;
         pci_set_pic(pci_bus, set_irq, pic);
 
@@ -373,7 +484,7 @@ static void ppc_chrp_init(int ram_size, int vga_ram_size, int boot_device,
         isa_pic = pic_init(pic_irq_request, NULL);
         
         /* XXX: use Mac Serial port */
-        serial_init(0x3f8, 4, serial_hds[0]);
+        serial_init(&pic_set_irq_new, isa_pic, 0x3f8, 4, serial_hds[0]);
         
         for(i = 0; i < nb_nics; i++) {
             pci_ne2000_init(pci_bus, &nd_table[i]);
@@ -391,9 +502,9 @@ static void ppc_chrp_init(int ram_size, int vga_ram_size, int boot_device,
         adb_kbd_init(&adb_bus);
         adb_mouse_init(&adb_bus);
         
-        macio_init(pci_bus);
+        macio_init(pci_bus, 0x0022);
         
-        nvram = m48t59_init(8, 0xFFF04000, 0x0074, NVRAM_SIZE);
+        nvram = m48t59_init(8, 0xFFF04000, 0x0074, NVRAM_SIZE, 59);
         
         arch_name = "MAC99";
     }
@@ -441,13 +552,13 @@ static void ppc_heathrow_init(int ram_size, int vga_ram_size, int boot_device,
 }
 
 QEMUMachine core99_machine = {
-    "core99",
-    "Core99 based PowerMAC",
+    "mac99",
+    "Mac99 based PowerMAC",
     ppc_core99_init,
 };
 
 QEMUMachine heathrow_machine = {
-    "heathrow",
+    "g3bw",
     "Heathrow based PowerMAC",
     ppc_heathrow_init,
 };