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.
20 #include "qemu-common.h"
29 #include "device_tree.h"
33 #define BINARY_DEVICE_TREE_FILE "mpc8544ds.dtb"
34 #define UIMAGE_LOAD_BASE 0
35 #define DTB_LOAD_BASE 0x600000
36 #define INITRD_LOAD_BASE 0x2000000
38 #define RAM_SIZES_ALIGN (64UL << 20)
40 #define MPC8544_CCSRBAR_BASE 0xE0000000
41 #define MPC8544_MPIC_REGS_BASE (MPC8544_CCSRBAR_BASE + 0x40000)
42 #define MPC8544_SERIAL0_REGS_BASE (MPC8544_CCSRBAR_BASE + 0x4500)
43 #define MPC8544_SERIAL1_REGS_BASE (MPC8544_CCSRBAR_BASE + 0x4600)
44 #define MPC8544_PCI_REGS_BASE (MPC8544_CCSRBAR_BASE + 0x8000)
45 #define MPC8544_PCI_REGS_SIZE 0x1000
46 #define MPC8544_PCI_IO 0xE1000000
47 #define MPC8544_PCI_IOLEN 0x10000
50 static int mpc8544_copy_soc_cell(void *fdt
, const char *node
, const char *prop
)
55 ret
= kvmppc_read_host_property(node
, prop
, &cell
, sizeof(cell
));
57 fprintf(stderr
, "couldn't read host %s/%s\n", node
, prop
);
61 ret
= qemu_devtree_setprop_cell(fdt
, "/cpus/PowerPC,8544@0",
64 fprintf(stderr
, "couldn't set guest /cpus/PowerPC,8544@0/%s\n", prop
);
73 static void *mpc8544_load_device_tree(target_phys_addr_t addr
,
75 target_phys_addr_t initrd_base
,
76 target_phys_addr_t initrd_size
,
77 const char *kernel_cmdline
)
81 uint32_t mem_reg_property
[] = {0, ramsize
};
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
, &fdt_size
);
97 /* Manipulate device tree in memory. */
98 ret
= qemu_devtree_setprop(fdt
, "/memory", "reg", mem_reg_property
,
99 sizeof(mem_reg_property
));
101 fprintf(stderr
, "couldn't set /memory/reg\n");
103 ret
= qemu_devtree_setprop_cell(fdt
, "/chosen", "linux,initrd-start",
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
));
111 fprintf(stderr
, "couldn't set /chosen/linux,initrd-end\n");
113 ret
= qemu_devtree_setprop_string(fdt
, "/chosen", "bootargs",
116 fprintf(stderr
, "couldn't set /chosen/bootargs\n");
123 if ((dp
= opendir("/proc/device-tree/cpus/")) == NULL
) {
124 printf("Can't open directory /proc/device-tree/cpus/\n");
129 while ((dirp
= readdir(dp
)) != NULL
) {
130 if (strncmp(dirp
->d_name
, "PowerPC", 7) == 0) {
131 snprintf(buf
, 128, "/cpus/%s", dirp
->d_name
);
136 if (buf
[0] == '\0') {
137 printf("Unknow host!\n");
141 mpc8544_copy_soc_cell(fdt
, buf
, "clock-frequency");
142 mpc8544_copy_soc_cell(fdt
, buf
, "timebase-frequency");
145 cpu_physical_memory_write (addr
, (void *)fdt
, fdt_size
);
153 static void mpc8544ds_init(ram_addr_t ram_size
,
154 const char *boot_device
,
155 const char *kernel_filename
,
156 const char *kernel_cmdline
,
157 const char *initrd_filename
,
158 const char *cpu_model
)
163 uint64_t elf_lowaddr
;
164 target_ulong entry
=0;
165 target_ulong loadaddr
=UIMAGE_LOAD_BASE
;
166 target_long kernel_size
=0;
167 target_ulong dt_base
=DTB_LOAD_BASE
;
168 target_ulong initrd_base
=INITRD_LOAD_BASE
;
169 target_long initrd_size
=0;
172 unsigned int pci_irq_nrs
[4] = {1, 2, 3, 4};
173 qemu_irq
*irqs
, *mpic
, *pci_irqs
;
174 SerialState
* serial
[2];
177 env
= cpu_ppc_init("e500v2_v30");
179 fprintf(stderr
, "Unable to initialize CPU!\n");
183 /* Fixup Memory size on a alignment boundary */
184 ram_size
&= ~(RAM_SIZES_ALIGN
- 1);
186 /* Register Memory */
187 cpu_register_physical_memory(0, ram_size
, qemu_ram_alloc(ram_size
));
190 irqs
= qemu_mallocz(sizeof(qemu_irq
) * OPENPIC_OUTPUT_NB
);
191 irqs
[OPENPIC_OUTPUT_INT
] = ((qemu_irq
*)env
->irq_inputs
)[PPCE500_INPUT_INT
];
192 irqs
[OPENPIC_OUTPUT_CINT
] = ((qemu_irq
*)env
->irq_inputs
)[PPCE500_INPUT_CINT
];
193 mpic
= mpic_init(MPC8544_MPIC_REGS_BASE
, 1, &irqs
, NULL
);
197 serial
[0] = serial_mm_init(MPC8544_SERIAL0_REGS_BASE
,
198 0, mpic
[12+26], 399193,
202 serial
[0] = serial_mm_init(MPC8544_SERIAL1_REGS_BASE
,
203 0, mpic
[12+26], 399193,
207 pci_irqs
= qemu_malloc(sizeof(qemu_irq
) * 4);
208 pci_irqs
[0] = mpic
[pci_irq_nrs
[0]];
209 pci_irqs
[1] = mpic
[pci_irq_nrs
[1]];
210 pci_irqs
[2] = mpic
[pci_irq_nrs
[2]];
211 pci_irqs
[3] = mpic
[pci_irq_nrs
[3]];
212 pci_bus
= ppce500_pci_init(pci_irqs
, MPC8544_PCI_REGS_BASE
);
214 printf("couldn't create PCI controller!\n");
216 isa_mmio_init(MPC8544_PCI_IO
, MPC8544_PCI_IOLEN
);
221 /* Add virtio block devices. */
222 while ((i
= drive_get_index(IF_VIRTIO
, 0, unit_id
)) != -1) {
223 pci_create_simple(pci_bus
, -1, "virtio-blk-pci");
227 /* Register network interfaces. */
228 for (i
= 0; i
< nb_nics
; i
++) {
229 pci_nic_init(pci_bus
, &nd_table
[i
], -1, "virtio");
234 if (kernel_filename
) {
235 kernel_size
= load_uimage(kernel_filename
, &entry
, &loadaddr
, NULL
);
236 if (kernel_size
< 0) {
237 kernel_size
= load_elf(kernel_filename
, 0, &elf_entry
, &elf_lowaddr
,
240 loadaddr
= elf_lowaddr
;
242 /* XXX try again as binary */
243 if (kernel_size
< 0) {
244 fprintf(stderr
, "qemu: could not load kernel '%s'\n",
251 if (initrd_filename
) {
252 initrd_size
= load_image_targphys(initrd_filename
, initrd_base
,
253 ram_size
- initrd_base
);
255 if (initrd_size
< 0) {
256 fprintf(stderr
, "qemu: could not load initial ram disk '%s'\n",
262 /* If we're loading a kernel directly, we must load the device tree too. */
263 if (kernel_filename
) {
264 fdt
= mpc8544_load_device_tree(dt_base
, ram_size
,
265 initrd_base
, initrd_size
, kernel_cmdline
);
267 fprintf(stderr
, "couldn't load device tree\n");
271 /* Set initial guest state. */
272 env
->gpr
[1] = (16<<20) - 8;
273 env
->gpr
[3] = dt_base
;
275 /* XXX we currently depend on KVM to create some initial TLB entries. */
284 static QEMUMachine mpc8544ds_machine
= {
287 .init
= mpc8544ds_init
,
290 static void mpc8544ds_machine_init(void)
292 qemu_register_machine(&mpc8544ds_machine
);
295 machine_init(mpc8544ds_machine_init
);