80097df8183492fc76c75c912b127f0e1ea3966f
[qemu] / hw / petalogix_s3adsp1800_mmu.c
1 /*
2  * Model of Petalogix linux reference design targeting Xilinx Spartan 3ADSP-1800
3  * boards.
4  *
5  * Copyright (c) 2009 Edgar E. Iglesias.
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
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.
24  */
25
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
36 #define LMB_BRAM_SIZE  (128 * 1024)
37 #define FLASH_SIZE     (16 * 1024 * 1024)
38
39 static uint32_t bootstrap_pc;
40
41 static void main_cpu_reset(void *opaque)
42 {
43     CPUState *env = opaque;
44     cpu_reset(env);
45     env->sregs[SR_PC] = bootstrap_pc;
46 }
47
48 #define BINARY_DEVICE_TREE_FILE "petalogix-s3adsp1800.dtb"
49 static int petalogix_load_device_tree(target_phys_addr_t addr,
50                                       uint32_t ramsize,
51                                       target_phys_addr_t initrd_base,
52                                       target_phys_addr_t initrd_size,
53                                       const char *kernel_cmdline)
54 {
55 #ifdef HAVE_FDT
56     void *fdt;
57     char *path = NULL;
58     int pathlen;
59     int r;
60 #endif
61     int fdt_size;
62
63 #ifdef HAVE_FDT
64     /* Try the local "mb.dtb" override.  */
65     fdt = load_device_tree("mb.dtb", &fdt_size);
66     if (!fdt) {
67         pathlen = snprintf(NULL, 0, "%s/%s",
68                            bios_dir, BINARY_DEVICE_TREE_FILE) + 1;
69         path = qemu_malloc(pathlen);
70         snprintf(path, pathlen, "%s/%s", bios_dir, BINARY_DEVICE_TREE_FILE);
71         fdt = load_device_tree(BINARY_DEVICE_TREE_FILE, &fdt_size);
72         free(path);
73         if (!fdt)
74             return 0;
75     }
76
77     r = qemu_devtree_setprop_string(fdt, "/chosen", "bootargs", kernel_cmdline);
78     if (r < 0)
79         fprintf(stderr, "couldn't set /chosen/bootargs\n");
80     cpu_physical_memory_write (addr, (void *)fdt, fdt_size);
81 #else
82     /* We lack libfdt so we cannot manipulate the fdt. Just pass on the blob
83        to the kernel.  */
84     fdt_size = load_image_targphys("mb.dtb", addr, 0x10000);
85     if (fdt_size < 0) {
86         fdt_size = load_image_targphys(BINARY_DEVICE_TREE_FILE, addr, 0x10000);
87     }
88
89     if (kernel_cmdline) {
90         fprintf(stderr,
91                 "Warning: missing libfdt, cannot pass cmdline to kernel!\n");
92     }
93 #endif
94     return fdt_size;
95 }
96
97 static void
98 petalogix_s3adsp1800_init(ram_addr_t ram_size,
99                           const char *boot_device,
100                           const char *kernel_filename,
101                           const char *kernel_cmdline,
102                           const char *initrd_filename, const char *cpu_model)
103 {
104     DeviceState *dev;
105     CPUState *env;
106     int kernel_size;
107     int i;
108     target_phys_addr_t ddr_base = 0x90000000;
109     ram_addr_t phys_lmb_bram;
110     ram_addr_t phys_ram;
111     ram_addr_t phys_flash;
112     qemu_irq irq[32], *cpu_irq;
113
114     /* init CPUs */
115     if (cpu_model == NULL) {
116         cpu_model = "microblaze";
117     }
118     env = cpu_init(cpu_model);
119
120     env->pvr.regs[10] = 0x0c000000; /* spartan 3a dsp family.  */
121     qemu_register_reset(main_cpu_reset, 0, env);
122
123     /* Attach emulated BRAM through the LMB.  */
124     phys_lmb_bram = qemu_ram_alloc(LMB_BRAM_SIZE);
125     cpu_register_physical_memory(0x00000000, LMB_BRAM_SIZE,
126                                  phys_lmb_bram | IO_MEM_RAM);
127
128     phys_ram = qemu_ram_alloc(ram_size);
129     cpu_register_physical_memory(ddr_base, ram_size, phys_ram | IO_MEM_RAM);
130
131     phys_flash = qemu_ram_alloc(FLASH_SIZE);
132     i = drive_get_index(IF_PFLASH, 0, 0);
133     pflash_cfi02_register(0xa0000000, phys_flash,
134                           i != -1 ? drives_table[i].bdrv : NULL, (64 * 1024),
135                           FLASH_SIZE >> 16,
136                           1, 1, 0x0000, 0x0000, 0x0000, 0x0000,
137                           0x555, 0x2aa);
138
139     cpu_irq = microblaze_pic_init_cpu(env);
140     dev = xilinx_intc_create(0x81800000, cpu_irq[0], 2);
141     for (i = 0; i < 32; i++) {
142         irq[i] = qdev_get_gpio_in(dev, i);
143     }
144
145     sysbus_create_simple("xilinx,uartlite", 0x84000000, irq[3]);
146     /* 2 timers at irq 2 @ 62 Mhz.  */
147     xilinx_timer_create(0x83c00000, irq[0], 2, 62 * 1000000);
148     xilinx_ethlite_create(&nd_table[0], 0x81000000, irq[1], 0, 0);
149
150     if (kernel_filename) {
151         uint64_t entry, low, high;
152         int kcmdline_len;
153         uint32_t base32;
154
155         /* Boots a kernel elf binary.  */
156         kernel_size = load_elf(kernel_filename, 0,
157                                &entry, &low, &high);
158         base32 = entry;
159         if (base32 == 0xc0000000) {
160             kernel_size = load_elf(kernel_filename, -0x30000000LL,
161                                    &entry, NULL, NULL);
162         }
163         /* Always boot into physical ram.  */
164         bootstrap_pc = ddr_base + (entry & 0x0fffffff);
165         if (kernel_size < 0) {
166             /* If we failed loading ELF's try a raw image.  */
167             kernel_size = load_image_targphys(kernel_filename, ddr_base,
168                                               ram_size);
169             bootstrap_pc = ddr_base;
170         }
171
172         env->regs[5] = ddr_base + kernel_size;
173         if (kernel_cmdline && (kcmdline_len = strlen(kernel_cmdline))) {
174             pstrcpy_targphys(env->regs[5], 256, kernel_cmdline);
175         }
176         env->regs[6] = 0;
177         /* Provide a device-tree.  */
178         env->regs[7] = ddr_base + kernel_size + 256;
179         petalogix_load_device_tree(env->regs[7], ram_size,
180                                    env->regs[6], 0,
181                                    kernel_cmdline);
182     }
183
184     env->sregs[SR_PC] = bootstrap_pc;
185 }
186
187 static QEMUMachine petalogix_s3adsp1800_machine = {
188     .name = "petalogix-s3adsp1800",
189     .desc = "Petalogix linux refdesign for xilinx Spartan 3ADSP1800",
190     .init = petalogix_s3adsp1800_init,
191     .is_default = 1
192 };
193
194 static void petalogix_s3adsp1800_machine_init(void)
195 {
196     qemu_register_machine(&petalogix_s3adsp1800_machine);
197 }
198
199 machine_init(petalogix_s3adsp1800_machine_init);