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"
25 #include "virtio-blk.h"
30 #include "device_tree.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
51 static int mpc8544_copy_soc_cell(void *fdt
, const char *node
, const char *prop
)
56 ret
= kvmppc_read_host_property(node
, prop
, &cell
, sizeof(cell
));
58 fprintf(stderr
, "couldn't read host %s/%s\n", node
, prop
);
62 ret
= qemu_devtree_setprop_cell(fdt
, "/cpus/PowerPC,8544@0",
65 fprintf(stderr
, "couldn't set guest /cpus/PowerPC,8544@0/%s\n", prop
);
74 static void *mpc8544_load_device_tree(target_phys_addr_t addr
,
76 target_phys_addr_t initrd_base
,
77 target_phys_addr_t initrd_size
,
78 const char *kernel_cmdline
)
82 uint32_t mem_reg_property
[] = {0, ramsize
};
88 pathlen
= snprintf(NULL
, 0, "%s/%s", bios_dir
, BINARY_DEVICE_TREE_FILE
) + 1;
89 path
= qemu_malloc(pathlen
);
91 snprintf(path
, pathlen
, "%s/%s", bios_dir
, BINARY_DEVICE_TREE_FILE
);
93 fdt
= load_device_tree(path
, &fdt_size
);
98 /* Manipulate device tree in memory. */
99 ret
= qemu_devtree_setprop(fdt
, "/memory", "reg", mem_reg_property
,
100 sizeof(mem_reg_property
));
102 fprintf(stderr
, "couldn't set /memory/reg\n");
104 ret
= qemu_devtree_setprop_cell(fdt
, "/chosen", "linux,initrd-start",
107 fprintf(stderr
, "couldn't set /chosen/linux,initrd-start\n");
109 ret
= qemu_devtree_setprop_cell(fdt
, "/chosen", "linux,initrd-end",
110 (initrd_base
+ initrd_size
));
112 fprintf(stderr
, "couldn't set /chosen/linux,initrd-end\n");
114 ret
= qemu_devtree_setprop_string(fdt
, "/chosen", "bootargs",
117 fprintf(stderr
, "couldn't set /chosen/bootargs\n");
124 if ((dp
= opendir("/proc/device-tree/cpus/")) == NULL
) {
125 printf("Can't open directory /proc/device-tree/cpus/\n");
130 while ((dirp
= readdir(dp
)) != NULL
) {
131 if (strncmp(dirp
->d_name
, "PowerPC", 7) == 0) {
132 snprintf(buf
, 128, "/cpus/%s", dirp
->d_name
);
137 if (buf
[0] == '\0') {
138 printf("Unknow host!\n");
142 mpc8544_copy_soc_cell(fdt
, buf
, "clock-frequency");
143 mpc8544_copy_soc_cell(fdt
, buf
, "timebase-frequency");
146 cpu_physical_memory_write (addr
, (void *)fdt
, fdt_size
);
154 static void mpc8544ds_init(ram_addr_t ram_size
, int vga_ram_size
,
155 const char *boot_device
,
156 const char *kernel_filename
,
157 const char *kernel_cmdline
,
158 const char *initrd_filename
,
159 const char *cpu_model
)
164 uint64_t elf_lowaddr
;
165 target_ulong entry
=0;
166 target_ulong loadaddr
=UIMAGE_LOAD_BASE
;
167 target_long kernel_size
=0;
168 target_ulong dt_base
=DTB_LOAD_BASE
;
169 target_ulong initrd_base
=INITRD_LOAD_BASE
;
170 target_long initrd_size
=0;
173 unsigned int pci_irq_nrs
[4] = {1, 2, 3, 4};
174 qemu_irq
*irqs
, *mpic
, *pci_irqs
;
175 SerialState
* serial
[2];
178 env
= cpu_ppc_init("e500v2_v30");
180 fprintf(stderr
, "Unable to initialize CPU!\n");
184 /* Fixup Memory size on a alignment boundary */
185 ram_size
&= ~(RAM_SIZES_ALIGN
- 1);
187 /* Register Memory */
188 cpu_register_physical_memory(0, ram_size
, qemu_ram_alloc(ram_size
));
191 irqs
= qemu_mallocz(sizeof(qemu_irq
) * OPENPIC_OUTPUT_NB
);
192 irqs
[OPENPIC_OUTPUT_INT
] = ((qemu_irq
*)env
->irq_inputs
)[PPCE500_INPUT_INT
];
193 irqs
[OPENPIC_OUTPUT_CINT
] = ((qemu_irq
*)env
->irq_inputs
)[PPCE500_INPUT_CINT
];
194 mpic
= mpic_init(MPC8544_MPIC_REGS_BASE
, 1, &irqs
, NULL
);
198 serial
[0] = serial_mm_init(MPC8544_SERIAL0_REGS_BASE
,
199 0, mpic
[12+26], 399193,
203 serial
[0] = serial_mm_init(MPC8544_SERIAL1_REGS_BASE
,
204 0, mpic
[12+26], 399193,
208 pci_irqs
= qemu_malloc(sizeof(qemu_irq
) * 4);
209 pci_irqs
[0] = mpic
[pci_irq_nrs
[0]];
210 pci_irqs
[1] = mpic
[pci_irq_nrs
[1]];
211 pci_irqs
[2] = mpic
[pci_irq_nrs
[2]];
212 pci_irqs
[3] = mpic
[pci_irq_nrs
[3]];
213 pci_bus
= ppce500_pci_init(pci_irqs
, MPC8544_PCI_REGS_BASE
);
215 printf("couldn't create PCI controller!\n");
217 isa_mmio_init(MPC8544_PCI_IO
, MPC8544_PCI_IOLEN
);
222 /* Add virtio block devices. */
223 while ((i
= drive_get_index(IF_VIRTIO
, 0, unit_id
)) != -1) {
224 virtio_blk_init(pci_bus
, drives_table
[i
].bdrv
);
228 /* Register network interfaces. */
229 for (i
= 0; i
< nb_nics
; i
++) {
230 pci_nic_init(pci_bus
, &nd_table
[i
], -1, "virtio");
235 if (kernel_filename
) {
236 kernel_size
= load_uimage(kernel_filename
, &entry
, &loadaddr
, NULL
);
237 if (kernel_size
< 0) {
238 kernel_size
= load_elf(kernel_filename
, 0, &elf_entry
, &elf_lowaddr
,
241 loadaddr
= elf_lowaddr
;
243 /* XXX try again as binary */
244 if (kernel_size
< 0) {
245 fprintf(stderr
, "qemu: could not load kernel '%s'\n",
252 if (initrd_filename
) {
253 initrd_size
= load_image_targphys(initrd_filename
, initrd_base
,
254 ram_size
- initrd_base
);
256 if (initrd_size
< 0) {
257 fprintf(stderr
, "qemu: could not load initial ram disk '%s'\n",
263 /* If we're loading a kernel directly, we must load the device tree too. */
264 if (kernel_filename
) {
265 fdt
= mpc8544_load_device_tree(dt_base
, ram_size
,
266 initrd_base
, initrd_size
, kernel_cmdline
);
268 fprintf(stderr
, "couldn't load device tree\n");
272 /* Set initial guest state. */
273 env
->gpr
[1] = (16<<20) - 8;
274 env
->gpr
[3] = dt_base
;
276 /* XXX we currently depend on KVM to create some initial TLB entries. */
285 QEMUMachine mpc8544ds_machine
= {
288 .init
= mpc8544ds_init
,