Temporary workaround for ppc on ppc
[qemu/mini2440.git] / hw / ppce500_mpc8544ds.c
blob3d73f8a79705a6971f462b2c65b220674d9a9c31
1 /*
2 * Qemu PowerPC MPC8544DS board emualtion
4 * Copyright (C) 2009 Freescale Semiconductor, Inc. All rights reserved.
6 * Author: Yu Liu, <yu.liu@freescale.com>
8 * This file is derived from hw/ppc440_bamboo.c,
9 * the copyright for that material belongs to the original owners.
11 * This is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
17 #include <dirent.h>
19 #include "config.h"
20 #include "qemu-common.h"
21 #include "net.h"
22 #include "hw.h"
23 #include "pc.h"
24 #include "pci.h"
25 #include "virtio-blk.h"
26 #include "boards.h"
27 #include "sysemu.h"
28 #include "kvm.h"
29 #include "kvm_ppc.h"
30 #include "device_tree.h"
31 #include "openpic.h"
32 #include "ppce500.h"
34 #define BINARY_DEVICE_TREE_FILE "mpc8544ds.dtb"
35 #define UIMAGE_LOAD_BASE 0
36 #define DTB_LOAD_BASE 0x600000
37 #define INITRD_LOAD_BASE 0x2000000
39 #define RAM_SIZES_ALIGN (64UL << 20)
41 #define MPC8544_CCSRBAR_BASE 0xE0000000
42 #define MPC8544_MPIC_REGS_BASE (MPC8544_CCSRBAR_BASE + 0x40000)
43 #define MPC8544_SERIAL0_REGS_BASE (MPC8544_CCSRBAR_BASE + 0x4500)
44 #define MPC8544_SERIAL1_REGS_BASE (MPC8544_CCSRBAR_BASE + 0x4600)
45 #define MPC8544_PCI_REGS_BASE (MPC8544_CCSRBAR_BASE + 0x8000)
46 #define MPC8544_PCI_REGS_SIZE 0x1000
47 #define MPC8544_PCI_IO 0xE1000000
48 #define MPC8544_PCI_IOLEN 0x10000
50 #ifdef HAVE_FDT
51 static int mpc8544_copy_soc_cell(void *fdt, const char *node, const char *prop)
53 uint32_t cell;
54 int ret;
56 ret = kvmppc_read_host_property(node, prop, &cell, sizeof(cell));
57 if (ret < 0) {
58 fprintf(stderr, "couldn't read host %s/%s\n", node, prop);
59 goto out;
62 ret = qemu_devtree_setprop_cell(fdt, "/cpus/PowerPC,8544@0",
63 prop, cell);
64 if (ret < 0) {
65 fprintf(stderr, "couldn't set guest /cpus/PowerPC,8544@0/%s\n", prop);
66 goto out;
69 out:
70 return ret;
72 #endif
74 static void *mpc8544_load_device_tree(void *addr,
75 uint32_t ramsize,
76 target_phys_addr_t initrd_base,
77 target_phys_addr_t initrd_size,
78 const char *kernel_cmdline)
80 void *fdt = NULL;
81 #ifdef HAVE_FDT
82 uint32_t mem_reg_property[] = {0, ramsize};
83 char *path;
84 int pathlen;
85 int ret;
87 pathlen = snprintf(NULL, 0, "%s/%s", bios_dir, BINARY_DEVICE_TREE_FILE) + 1;
88 path = qemu_malloc(pathlen);
90 snprintf(path, pathlen, "%s/%s", bios_dir, BINARY_DEVICE_TREE_FILE);
92 fdt = load_device_tree(path, addr);
93 qemu_free(path);
94 if (fdt == NULL)
95 goto out;
97 /* Manipulate device tree in memory. */
98 ret = qemu_devtree_setprop(fdt, "/memory", "reg", mem_reg_property,
99 sizeof(mem_reg_property));
100 if (ret < 0)
101 fprintf(stderr, "couldn't set /memory/reg\n");
103 ret = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-start",
104 initrd_base);
105 if (ret < 0)
106 fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
108 ret = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-end",
109 (initrd_base + initrd_size));
110 if (ret < 0)
111 fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
113 ret = qemu_devtree_setprop_string(fdt, "/chosen", "bootargs",
114 kernel_cmdline);
115 if (ret < 0)
116 fprintf(stderr, "couldn't set /chosen/bootargs\n");
118 if (kvm_enabled()) {
119 struct dirent *dirp;
120 DIR *dp;
121 char buf[128];
123 if ((dp = opendir("/proc/device-tree/cpus/")) == NULL) {
124 printf("Can't open directory /proc/device-tree/cpus/\n");
125 goto out;
128 buf[0] = '\0';
129 while ((dirp = readdir(dp)) != NULL) {
130 if (strncmp(dirp->d_name, "PowerPC", 7) == 0) {
131 snprintf(buf, 128, "/cpus/%s", dirp->d_name);
132 break;
135 closedir(dp);
136 if (buf[0] == '\0') {
137 printf("Unknow host!\n");
138 goto out;
141 mpc8544_copy_soc_cell(fdt, buf, "clock-frequency");
142 mpc8544_copy_soc_cell(fdt, buf, "timebase-frequency");
145 out:
146 #endif
148 return fdt;
151 static void mpc8544ds_init(ram_addr_t ram_size, int vga_ram_size,
152 const char *boot_device,
153 const char *kernel_filename,
154 const char *kernel_cmdline,
155 const char *initrd_filename,
156 const char *cpu_model)
158 PCIBus *pci_bus;
159 CPUState *env;
160 uint64_t elf_entry;
161 uint64_t elf_lowaddr;
162 target_ulong entry=0;
163 target_ulong loadaddr=UIMAGE_LOAD_BASE;
164 target_long kernel_size=0;
165 target_ulong dt_base=DTB_LOAD_BASE;
166 target_ulong initrd_base=INITRD_LOAD_BASE;
167 target_long initrd_size=0;
168 void *fdt;
169 int i=0;
170 unsigned int pci_irq_nrs[4] = {1, 2, 3, 4};
171 qemu_irq *irqs, *mpic, *pci_irqs;
172 SerialState * serial[2];
174 /* Setup CPU */
175 env = cpu_ppc_init("e500v2_v30");
176 if (!env) {
177 fprintf(stderr, "Unable to initialize CPU!\n");
178 exit(1);
181 /* Fixup Memory size on a alignment boundary */
182 ram_size &= ~(RAM_SIZES_ALIGN - 1);
184 /* Register Memory */
185 cpu_register_physical_memory(0, ram_size, 0);
187 /* MPIC */
188 irqs = qemu_mallocz(sizeof(qemu_irq) * OPENPIC_OUTPUT_NB);
189 irqs[OPENPIC_OUTPUT_INT] = ((qemu_irq *)env->irq_inputs)[PPCE500_INPUT_INT];
190 irqs[OPENPIC_OUTPUT_CINT] = ((qemu_irq *)env->irq_inputs)[PPCE500_INPUT_CINT];
191 mpic = mpic_init(MPC8544_MPIC_REGS_BASE, 1, &irqs, NULL);
193 /* Serial */
194 if (serial_hds[0])
195 serial[0] = serial_mm_init(MPC8544_SERIAL0_REGS_BASE,
196 0, mpic[12+26], 399193,
197 serial_hds[0], 1);
199 if (serial_hds[1])
200 serial[0] = serial_mm_init(MPC8544_SERIAL1_REGS_BASE,
201 0, mpic[12+26], 399193,
202 serial_hds[0], 1);
204 /* PCI */
205 pci_irqs = qemu_malloc(sizeof(qemu_irq) * 4);
206 pci_irqs[0] = mpic[pci_irq_nrs[0]];
207 pci_irqs[1] = mpic[pci_irq_nrs[1]];
208 pci_irqs[2] = mpic[pci_irq_nrs[2]];
209 pci_irqs[3] = mpic[pci_irq_nrs[3]];
210 pci_bus = ppce500_pci_init(pci_irqs, MPC8544_PCI_REGS_BASE);
211 if (!pci_bus)
212 printf("couldn't create PCI controller!\n");
214 isa_mmio_init(MPC8544_PCI_IO, MPC8544_PCI_IOLEN);
216 if (pci_bus) {
217 int unit_id = 0;
219 /* Add virtio block devices. */
220 while ((i = drive_get_index(IF_VIRTIO, 0, unit_id)) != -1) {
221 virtio_blk_init(pci_bus, drives_table[i].bdrv);
222 unit_id++;
225 /* Register network interfaces. */
226 for (i = 0; i < nb_nics; i++) {
227 pci_nic_init(pci_bus, &nd_table[i], -1, "virtio");
231 /* Load kernel. */
232 if (kernel_filename) {
233 kernel_size = load_uimage(kernel_filename, &entry, &loadaddr, NULL);
234 if (kernel_size < 0) {
235 kernel_size = load_elf(kernel_filename, 0, &elf_entry, &elf_lowaddr,
236 NULL);
237 entry = elf_entry;
238 loadaddr = elf_lowaddr;
240 /* XXX try again as binary */
241 if (kernel_size < 0) {
242 fprintf(stderr, "qemu: could not load kernel '%s'\n",
243 kernel_filename);
244 exit(1);
248 /* Load initrd. */
249 if (initrd_filename) {
250 initrd_size = load_image(initrd_filename, phys_ram_base + initrd_base);
252 if (initrd_size < 0) {
253 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
254 initrd_filename);
255 exit(1);
259 /* If we're loading a kernel directly, we must load the device tree too. */
260 if (kernel_filename) {
261 fdt = mpc8544_load_device_tree(phys_ram_base + dt_base, ram_size,
262 initrd_base, initrd_size, kernel_cmdline);
263 if (fdt == NULL) {
264 fprintf(stderr, "couldn't load device tree\n");
265 exit(1);
268 /* Set initial guest state. */
269 env->gpr[1] = (16<<20) - 8;
270 env->gpr[3] = dt_base;
271 env->nip = entry;
272 /* XXX we currently depend on KVM to create some initial TLB entries. */
275 if (kvm_enabled())
276 kvmppc_init();
278 return;
281 QEMUMachine mpc8544ds_machine = {
282 .name = "mpc8544ds",
283 .desc = "mpc8544ds",
284 .init = mpc8544ds_init,
285 .ram_require = RAM_SIZES_ALIGN | RAMSIZE_FIXED,