main: switch qemu_set_fd_handler to g_io_add_watch
[qemu.git] / hw / petalogix_s3adsp1800_mmu.c
bloba43fb4c95c9fc9529f9e9f9be4b5f1327d9c43b6
1 /*
2 * Model of Petalogix linux reference design targeting Xilinx Spartan 3ADSP-1800
3 * boards.
5 * Copyright (c) 2009 Edgar E. Iglesias.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
26 #include "sysbus.h"
27 #include "hw.h"
28 #include "net.h"
29 #include "flash.h"
30 #include "sysemu.h"
31 #include "devices.h"
32 #include "boards.h"
33 #include "device_tree.h"
34 #include "xilinx.h"
35 #include "loader.h"
36 #include "elf.h"
37 #include "blockdev.h"
39 #include "microblaze_pic_cpu.h"
41 #define LMB_BRAM_SIZE (128 * 1024)
42 #define FLASH_SIZE (16 * 1024 * 1024)
44 static struct
46 uint32_t bootstrap_pc;
47 uint32_t cmdline;
48 uint32_t fdt;
49 } boot_info;
51 static void main_cpu_reset(void *opaque)
53 CPUState *env = opaque;
55 cpu_reset(env);
56 env->regs[5] = boot_info.cmdline;
57 env->regs[7] = boot_info.fdt;
58 env->sregs[SR_PC] = boot_info.bootstrap_pc;
61 #define BINARY_DEVICE_TREE_FILE "petalogix-s3adsp1800.dtb"
62 static int petalogix_load_device_tree(target_phys_addr_t addr,
63 uint32_t ramsize,
64 target_phys_addr_t initrd_base,
65 target_phys_addr_t initrd_size,
66 const char *kernel_cmdline)
68 char *path;
69 int fdt_size;
70 #ifdef CONFIG_FDT
71 void *fdt;
72 int r;
74 /* Try the local "mb.dtb" override. */
75 fdt = load_device_tree("mb.dtb", &fdt_size);
76 if (!fdt) {
77 path = qemu_find_file(QEMU_FILE_TYPE_BIOS, BINARY_DEVICE_TREE_FILE);
78 if (path) {
79 fdt = load_device_tree(path, &fdt_size);
80 g_free(path);
82 if (!fdt)
83 return 0;
86 r = qemu_devtree_setprop_string(fdt, "/chosen", "bootargs", kernel_cmdline);
87 if (r < 0)
88 fprintf(stderr, "couldn't set /chosen/bootargs\n");
89 cpu_physical_memory_write (addr, (void *)fdt, fdt_size);
90 #else
91 /* We lack libfdt so we cannot manipulate the fdt. Just pass on the blob
92 to the kernel. */
93 fdt_size = load_image_targphys("mb.dtb", addr, 0x10000);
94 if (fdt_size < 0) {
95 path = qemu_find_file(QEMU_FILE_TYPE_BIOS, BINARY_DEVICE_TREE_FILE);
96 if (path) {
97 fdt_size = load_image_targphys(path, addr, 0x10000);
98 g_free(path);
102 if (kernel_cmdline) {
103 fprintf(stderr,
104 "Warning: missing libfdt, cannot pass cmdline to kernel!\n");
106 #endif
107 return fdt_size;
110 static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
112 return addr - 0x30000000LL;
115 static void
116 petalogix_s3adsp1800_init(ram_addr_t ram_size,
117 const char *boot_device,
118 const char *kernel_filename,
119 const char *kernel_cmdline,
120 const char *initrd_filename, const char *cpu_model)
122 DeviceState *dev;
123 CPUState *env;
124 int kernel_size;
125 DriveInfo *dinfo;
126 int i;
127 target_phys_addr_t ddr_base = 0x90000000;
128 ram_addr_t phys_lmb_bram;
129 ram_addr_t phys_ram;
130 ram_addr_t phys_flash;
131 qemu_irq irq[32], *cpu_irq;
133 /* init CPUs */
134 if (cpu_model == NULL) {
135 cpu_model = "microblaze";
137 env = cpu_init(cpu_model);
139 env->pvr.regs[10] = 0x0c000000; /* spartan 3a dsp family. */
140 qemu_register_reset(main_cpu_reset, env);
142 /* Attach emulated BRAM through the LMB. */
143 phys_lmb_bram = qemu_ram_alloc(NULL, "petalogix_s3adsp1800.lmb_bram",
144 LMB_BRAM_SIZE);
145 cpu_register_physical_memory(0x00000000, LMB_BRAM_SIZE,
146 phys_lmb_bram | IO_MEM_RAM);
148 phys_ram = qemu_ram_alloc(NULL, "petalogix_s3adsp1800.ram", ram_size);
149 cpu_register_physical_memory(ddr_base, ram_size, phys_ram | IO_MEM_RAM);
151 phys_flash = qemu_ram_alloc(NULL, "petalogix_s3adsp1800.flash", FLASH_SIZE);
152 dinfo = drive_get(IF_PFLASH, 0, 0);
153 pflash_cfi01_register(0xa0000000, phys_flash,
154 dinfo ? dinfo->bdrv : NULL, (64 * 1024),
155 FLASH_SIZE >> 16,
156 1, 0x89, 0x18, 0x0000, 0x0, 1);
158 cpu_irq = microblaze_pic_init_cpu(env);
159 dev = xilinx_intc_create(0x81800000, cpu_irq[0], 2);
160 for (i = 0; i < 32; i++) {
161 irq[i] = qdev_get_gpio_in(dev, i);
164 sysbus_create_simple("xilinx,uartlite", 0x84000000, irq[3]);
165 /* 2 timers at irq 2 @ 62 Mhz. */
166 xilinx_timer_create(0x83c00000, irq[0], 2, 62 * 1000000);
167 xilinx_ethlite_create(&nd_table[0], 0x81000000, irq[1], 0, 0);
169 if (kernel_filename) {
170 uint64_t entry, low, high;
171 uint32_t base32;
172 int big_endian = 0;
174 #ifdef TARGET_WORDS_BIGENDIAN
175 big_endian = 1;
176 #endif
178 /* Boots a kernel elf binary. */
179 kernel_size = load_elf(kernel_filename, NULL, NULL,
180 &entry, &low, &high,
181 big_endian, ELF_MACHINE, 0);
182 base32 = entry;
183 if (base32 == 0xc0000000) {
184 kernel_size = load_elf(kernel_filename, translate_kernel_address,
185 NULL, &entry, NULL, NULL,
186 big_endian, ELF_MACHINE, 0);
188 /* Always boot into physical ram. */
189 boot_info.bootstrap_pc = ddr_base + (entry & 0x0fffffff);
191 /* If it wasn't an ELF image, try an u-boot image. */
192 if (kernel_size < 0) {
193 target_phys_addr_t uentry, loadaddr;
195 kernel_size = load_uimage(kernel_filename, &uentry, &loadaddr, 0);
196 boot_info.bootstrap_pc = uentry;
197 high = (loadaddr + kernel_size + 3) & ~3;
200 /* Not an ELF image nor an u-boot image, try a RAW image. */
201 if (kernel_size < 0) {
202 kernel_size = load_image_targphys(kernel_filename, ddr_base,
203 ram_size);
204 boot_info.bootstrap_pc = ddr_base;
205 high = (ddr_base + kernel_size + 3) & ~3;
208 boot_info.cmdline = high + 4096;
209 if (kernel_cmdline && strlen(kernel_cmdline)) {
210 pstrcpy_targphys("cmdline", boot_info.cmdline, 256, kernel_cmdline);
212 /* Provide a device-tree. */
213 boot_info.fdt = boot_info.cmdline + 4096;
214 petalogix_load_device_tree(boot_info.fdt, ram_size,
215 0, 0,
216 kernel_cmdline);
220 static QEMUMachine petalogix_s3adsp1800_machine = {
221 .name = "petalogix-s3adsp1800",
222 .desc = "PetaLogix linux refdesign for xilinx Spartan 3ADSP1800",
223 .init = petalogix_s3adsp1800_init,
224 .is_default = 1
227 static void petalogix_s3adsp1800_machine_init(void)
229 qemu_register_machine(&petalogix_s3adsp1800_machine);
232 machine_init(petalogix_s3adsp1800_machine_init);