Statistics
| Branch: | Revision:

root / hw / lm32_boards.c @ a1e47211

History | View | Annotate | Download (10.2 kB)

1 d821732a Michael Walle
/*
2 d821732a Michael Walle
 *  QEMU models for LatticeMico32 uclinux and evr32 boards.
3 d821732a Michael Walle
 *
4 d821732a Michael Walle
 *  Copyright (c) 2010 Michael Walle <michael@walle.cc>
5 d821732a Michael Walle
 *
6 d821732a Michael Walle
 * This library is free software; you can redistribute it and/or
7 d821732a Michael Walle
 * modify it under the terms of the GNU Lesser General Public
8 d821732a Michael Walle
 * License as published by the Free Software Foundation; either
9 d821732a Michael Walle
 * version 2 of the License, or (at your option) any later version.
10 d821732a Michael Walle
 *
11 d821732a Michael Walle
 * This library is distributed in the hope that it will be useful,
12 d821732a Michael Walle
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 d821732a Michael Walle
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 d821732a Michael Walle
 * Lesser General Public License for more details.
15 d821732a Michael Walle
 *
16 d821732a Michael Walle
 * You should have received a copy of the GNU Lesser General Public
17 d821732a Michael Walle
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 d821732a Michael Walle
 */
19 d821732a Michael Walle
20 d821732a Michael Walle
#include "sysbus.h"
21 d821732a Michael Walle
#include "hw.h"
22 d821732a Michael Walle
#include "net.h"
23 d821732a Michael Walle
#include "flash.h"
24 d821732a Michael Walle
#include "devices.h"
25 d821732a Michael Walle
#include "boards.h"
26 d821732a Michael Walle
#include "loader.h"
27 d821732a Michael Walle
#include "blockdev.h"
28 d821732a Michael Walle
#include "elf.h"
29 d821732a Michael Walle
#include "lm32_hwsetup.h"
30 d821732a Michael Walle
#include "lm32.h"
31 88fa8031 Avi Kivity
#include "exec-memory.h"
32 d821732a Michael Walle
33 d821732a Michael Walle
typedef struct {
34 b1435596 Andreas Färber
    LM32CPU *cpu;
35 d821732a Michael Walle
    target_phys_addr_t bootstrap_pc;
36 d821732a Michael Walle
    target_phys_addr_t flash_base;
37 d821732a Michael Walle
    target_phys_addr_t hwsetup_base;
38 d821732a Michael Walle
    target_phys_addr_t initrd_base;
39 d821732a Michael Walle
    size_t initrd_size;
40 d821732a Michael Walle
    target_phys_addr_t cmdline_base;
41 d821732a Michael Walle
} ResetInfo;
42 d821732a Michael Walle
43 d821732a Michael Walle
static void cpu_irq_handler(void *opaque, int irq, int level)
44 d821732a Michael Walle
{
45 93a67402 Andreas Färber
    CPULM32State *env = opaque;
46 d821732a Michael Walle
47 d821732a Michael Walle
    if (level) {
48 d821732a Michael Walle
        cpu_interrupt(env, CPU_INTERRUPT_HARD);
49 d821732a Michael Walle
    } else {
50 d821732a Michael Walle
        cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
51 d821732a Michael Walle
    }
52 d821732a Michael Walle
}
53 d821732a Michael Walle
54 d821732a Michael Walle
static void main_cpu_reset(void *opaque)
55 d821732a Michael Walle
{
56 d821732a Michael Walle
    ResetInfo *reset_info = opaque;
57 b1435596 Andreas Färber
    CPULM32State *env = &reset_info->cpu->env;
58 d821732a Michael Walle
59 b1435596 Andreas Färber
    cpu_reset(CPU(reset_info->cpu));
60 d821732a Michael Walle
61 d821732a Michael Walle
    /* init defaults */
62 d821732a Michael Walle
    env->pc = (uint32_t)reset_info->bootstrap_pc;
63 d821732a Michael Walle
    env->regs[R_R1] = (uint32_t)reset_info->hwsetup_base;
64 d821732a Michael Walle
    env->regs[R_R2] = (uint32_t)reset_info->cmdline_base;
65 d821732a Michael Walle
    env->regs[R_R3] = (uint32_t)reset_info->initrd_base;
66 d821732a Michael Walle
    env->regs[R_R4] = (uint32_t)(reset_info->initrd_base +
67 d821732a Michael Walle
        reset_info->initrd_size);
68 d821732a Michael Walle
    env->eba = reset_info->flash_base;
69 d821732a Michael Walle
    env->deba = reset_info->flash_base;
70 d821732a Michael Walle
}
71 d821732a Michael Walle
72 d821732a Michael Walle
static void lm32_evr_init(ram_addr_t ram_size_not_used,
73 d821732a Michael Walle
                          const char *boot_device,
74 d821732a Michael Walle
                          const char *kernel_filename,
75 d821732a Michael Walle
                          const char *kernel_cmdline,
76 d821732a Michael Walle
                          const char *initrd_filename, const char *cpu_model)
77 d821732a Michael Walle
{
78 47dc4fa2 Andreas Färber
    LM32CPU *cpu;
79 93a67402 Andreas Färber
    CPULM32State *env;
80 d821732a Michael Walle
    DriveInfo *dinfo;
81 88fa8031 Avi Kivity
    MemoryRegion *address_space_mem =  get_system_memory();
82 88fa8031 Avi Kivity
    MemoryRegion *phys_ram = g_new(MemoryRegion, 1);
83 d821732a Michael Walle
    qemu_irq *cpu_irq, irq[32];
84 d821732a Michael Walle
    ResetInfo *reset_info;
85 d821732a Michael Walle
    int i;
86 d821732a Michael Walle
87 d821732a Michael Walle
    /* memory map */
88 d821732a Michael Walle
    target_phys_addr_t flash_base  = 0x04000000;
89 d821732a Michael Walle
    size_t flash_sector_size       = 256 * 1024;
90 d821732a Michael Walle
    size_t flash_size              = 32 * 1024 * 1024;
91 d821732a Michael Walle
    target_phys_addr_t ram_base    = 0x08000000;
92 d821732a Michael Walle
    size_t ram_size                = 64 * 1024 * 1024;
93 d821732a Michael Walle
    target_phys_addr_t timer0_base = 0x80002000;
94 d821732a Michael Walle
    target_phys_addr_t uart0_base  = 0x80006000;
95 d821732a Michael Walle
    target_phys_addr_t timer1_base = 0x8000a000;
96 d821732a Michael Walle
    int uart0_irq                  = 0;
97 d821732a Michael Walle
    int timer0_irq                 = 1;
98 d821732a Michael Walle
    int timer1_irq                 = 3;
99 d821732a Michael Walle
100 7267c094 Anthony Liguori
    reset_info = g_malloc0(sizeof(ResetInfo));
101 d821732a Michael Walle
102 d821732a Michael Walle
    if (cpu_model == NULL) {
103 d821732a Michael Walle
        cpu_model = "lm32-full";
104 d821732a Michael Walle
    }
105 47dc4fa2 Andreas Färber
    cpu = cpu_lm32_init(cpu_model);
106 47dc4fa2 Andreas Färber
    env = &cpu->env;
107 b1435596 Andreas Färber
    reset_info->cpu = cpu;
108 d821732a Michael Walle
109 d821732a Michael Walle
    reset_info->flash_base = flash_base;
110 d821732a Michael Walle
111 c5705a77 Avi Kivity
    memory_region_init_ram(phys_ram, "lm32_evr.sdram", ram_size);
112 c5705a77 Avi Kivity
    vmstate_register_ram_global(phys_ram);
113 88fa8031 Avi Kivity
    memory_region_add_subregion(address_space_mem, ram_base, phys_ram);
114 d821732a Michael Walle
115 d821732a Michael Walle
    dinfo = drive_get(IF_PFLASH, 0, 0);
116 d821732a Michael Walle
    /* Spansion S29NS128P */
117 cfe5f011 Avi Kivity
    pflash_cfi02_register(flash_base, NULL, "lm32_evr.flash", flash_size,
118 d821732a Michael Walle
                          dinfo ? dinfo->bdrv : NULL, flash_sector_size,
119 d821732a Michael Walle
                          flash_size / flash_sector_size, 1, 2,
120 01e0451a Anthony Liguori
                          0x01, 0x7e, 0x43, 0x00, 0x555, 0x2aa, 1);
121 d821732a Michael Walle
122 d821732a Michael Walle
    /* create irq lines */
123 d821732a Michael Walle
    cpu_irq = qemu_allocate_irqs(cpu_irq_handler, env, 1);
124 d821732a Michael Walle
    env->pic_state = lm32_pic_init(*cpu_irq);
125 d821732a Michael Walle
    for (i = 0; i < 32; i++) {
126 d821732a Michael Walle
        irq[i] = qdev_get_gpio_in(env->pic_state, i);
127 d821732a Michael Walle
    }
128 d821732a Michael Walle
129 d821732a Michael Walle
    sysbus_create_simple("lm32-uart", uart0_base, irq[uart0_irq]);
130 d821732a Michael Walle
    sysbus_create_simple("lm32-timer", timer0_base, irq[timer0_irq]);
131 d821732a Michael Walle
    sysbus_create_simple("lm32-timer", timer1_base, irq[timer1_irq]);
132 d821732a Michael Walle
133 d821732a Michael Walle
    /* make sure juart isn't the first chardev */
134 d821732a Michael Walle
    env->juart_state = lm32_juart_init();
135 d821732a Michael Walle
136 d821732a Michael Walle
    reset_info->bootstrap_pc = flash_base;
137 d821732a Michael Walle
138 d821732a Michael Walle
    if (kernel_filename) {
139 d821732a Michael Walle
        uint64_t entry;
140 d821732a Michael Walle
        int kernel_size;
141 d821732a Michael Walle
142 d821732a Michael Walle
        kernel_size = load_elf(kernel_filename, NULL, NULL, &entry, NULL, NULL,
143 d821732a Michael Walle
                               1, ELF_MACHINE, 0);
144 d821732a Michael Walle
        reset_info->bootstrap_pc = entry;
145 d821732a Michael Walle
146 d821732a Michael Walle
        if (kernel_size < 0) {
147 d821732a Michael Walle
            kernel_size = load_image_targphys(kernel_filename, ram_base,
148 d821732a Michael Walle
                                              ram_size);
149 d821732a Michael Walle
            reset_info->bootstrap_pc = ram_base;
150 d821732a Michael Walle
        }
151 d821732a Michael Walle
152 d821732a Michael Walle
        if (kernel_size < 0) {
153 d821732a Michael Walle
            fprintf(stderr, "qemu: could not load kernel '%s'\n",
154 d821732a Michael Walle
                    kernel_filename);
155 d821732a Michael Walle
            exit(1);
156 d821732a Michael Walle
        }
157 d821732a Michael Walle
    }
158 d821732a Michael Walle
159 d821732a Michael Walle
    qemu_register_reset(main_cpu_reset, reset_info);
160 d821732a Michael Walle
}
161 d821732a Michael Walle
162 d821732a Michael Walle
static void lm32_uclinux_init(ram_addr_t ram_size_not_used,
163 d821732a Michael Walle
                          const char *boot_device,
164 d821732a Michael Walle
                          const char *kernel_filename,
165 d821732a Michael Walle
                          const char *kernel_cmdline,
166 d821732a Michael Walle
                          const char *initrd_filename, const char *cpu_model)
167 d821732a Michael Walle
{
168 47dc4fa2 Andreas Färber
    LM32CPU *cpu;
169 93a67402 Andreas Färber
    CPULM32State *env;
170 d821732a Michael Walle
    DriveInfo *dinfo;
171 88fa8031 Avi Kivity
    MemoryRegion *address_space_mem =  get_system_memory();
172 88fa8031 Avi Kivity
    MemoryRegion *phys_ram = g_new(MemoryRegion, 1);
173 d821732a Michael Walle
    qemu_irq *cpu_irq, irq[32];
174 d821732a Michael Walle
    HWSetup *hw;
175 d821732a Michael Walle
    ResetInfo *reset_info;
176 d821732a Michael Walle
    int i;
177 d821732a Michael Walle
178 d821732a Michael Walle
    /* memory map */
179 d821732a Michael Walle
    target_phys_addr_t flash_base   = 0x04000000;
180 d821732a Michael Walle
    size_t flash_sector_size        = 256 * 1024;
181 d821732a Michael Walle
    size_t flash_size               = 32 * 1024 * 1024;
182 d821732a Michael Walle
    target_phys_addr_t ram_base     = 0x08000000;
183 d821732a Michael Walle
    size_t ram_size                 = 64 * 1024 * 1024;
184 d821732a Michael Walle
    target_phys_addr_t uart0_base   = 0x80000000;
185 d821732a Michael Walle
    target_phys_addr_t timer0_base  = 0x80002000;
186 d821732a Michael Walle
    target_phys_addr_t timer1_base  = 0x80010000;
187 d821732a Michael Walle
    target_phys_addr_t timer2_base  = 0x80012000;
188 d821732a Michael Walle
    int uart0_irq                   = 0;
189 d821732a Michael Walle
    int timer0_irq                  = 1;
190 d821732a Michael Walle
    int timer1_irq                  = 20;
191 d821732a Michael Walle
    int timer2_irq                  = 21;
192 d821732a Michael Walle
    target_phys_addr_t hwsetup_base = 0x0bffe000;
193 d821732a Michael Walle
    target_phys_addr_t cmdline_base = 0x0bfff000;
194 d821732a Michael Walle
    target_phys_addr_t initrd_base  = 0x08400000;
195 d821732a Michael Walle
    size_t initrd_max               = 0x01000000;
196 d821732a Michael Walle
197 7267c094 Anthony Liguori
    reset_info = g_malloc0(sizeof(ResetInfo));
198 d821732a Michael Walle
199 d821732a Michael Walle
    if (cpu_model == NULL) {
200 d821732a Michael Walle
        cpu_model = "lm32-full";
201 d821732a Michael Walle
    }
202 47dc4fa2 Andreas Färber
    cpu = cpu_lm32_init(cpu_model);
203 47dc4fa2 Andreas Färber
    env = &cpu->env;
204 b1435596 Andreas Färber
    reset_info->cpu = cpu;
205 d821732a Michael Walle
206 d821732a Michael Walle
    reset_info->flash_base = flash_base;
207 d821732a Michael Walle
208 c5705a77 Avi Kivity
    memory_region_init_ram(phys_ram, "lm32_uclinux.sdram", ram_size);
209 c5705a77 Avi Kivity
    vmstate_register_ram_global(phys_ram);
210 88fa8031 Avi Kivity
    memory_region_add_subregion(address_space_mem, ram_base, phys_ram);
211 d821732a Michael Walle
212 d821732a Michael Walle
    dinfo = drive_get(IF_PFLASH, 0, 0);
213 d821732a Michael Walle
    /* Spansion S29NS128P */
214 cfe5f011 Avi Kivity
    pflash_cfi02_register(flash_base, NULL, "lm32_uclinux.flash", flash_size,
215 d821732a Michael Walle
                          dinfo ? dinfo->bdrv : NULL, flash_sector_size,
216 d821732a Michael Walle
                          flash_size / flash_sector_size, 1, 2,
217 01e0451a Anthony Liguori
                          0x01, 0x7e, 0x43, 0x00, 0x555, 0x2aa, 1);
218 d821732a Michael Walle
219 d821732a Michael Walle
    /* create irq lines */
220 d821732a Michael Walle
    cpu_irq = qemu_allocate_irqs(cpu_irq_handler, env, 1);
221 d821732a Michael Walle
    env->pic_state = lm32_pic_init(*cpu_irq);
222 d821732a Michael Walle
    for (i = 0; i < 32; i++) {
223 d821732a Michael Walle
        irq[i] = qdev_get_gpio_in(env->pic_state, i);
224 d821732a Michael Walle
    }
225 d821732a Michael Walle
226 d821732a Michael Walle
    sysbus_create_simple("lm32-uart", uart0_base, irq[uart0_irq]);
227 d821732a Michael Walle
    sysbus_create_simple("lm32-timer", timer0_base, irq[timer0_irq]);
228 d821732a Michael Walle
    sysbus_create_simple("lm32-timer", timer1_base, irq[timer1_irq]);
229 d821732a Michael Walle
    sysbus_create_simple("lm32-timer", timer2_base, irq[timer2_irq]);
230 d821732a Michael Walle
231 d821732a Michael Walle
    /* make sure juart isn't the first chardev */
232 d821732a Michael Walle
    env->juart_state = lm32_juart_init();
233 d821732a Michael Walle
234 d821732a Michael Walle
    reset_info->bootstrap_pc = flash_base;
235 d821732a Michael Walle
236 d821732a Michael Walle
    if (kernel_filename) {
237 d821732a Michael Walle
        uint64_t entry;
238 d821732a Michael Walle
        int kernel_size;
239 d821732a Michael Walle
240 d821732a Michael Walle
        kernel_size = load_elf(kernel_filename, NULL, NULL, &entry, NULL, NULL,
241 d821732a Michael Walle
                               1, ELF_MACHINE, 0);
242 d821732a Michael Walle
        reset_info->bootstrap_pc = entry;
243 d821732a Michael Walle
244 d821732a Michael Walle
        if (kernel_size < 0) {
245 d821732a Michael Walle
            kernel_size = load_image_targphys(kernel_filename, ram_base,
246 d821732a Michael Walle
                                              ram_size);
247 d821732a Michael Walle
            reset_info->bootstrap_pc = ram_base;
248 d821732a Michael Walle
        }
249 d821732a Michael Walle
250 d821732a Michael Walle
        if (kernel_size < 0) {
251 d821732a Michael Walle
            fprintf(stderr, "qemu: could not load kernel '%s'\n",
252 d821732a Michael Walle
                    kernel_filename);
253 d821732a Michael Walle
            exit(1);
254 d821732a Michael Walle
        }
255 d821732a Michael Walle
    }
256 d821732a Michael Walle
257 d821732a Michael Walle
    /* generate a rom with the hardware description */
258 d821732a Michael Walle
    hw = hwsetup_init();
259 d821732a Michael Walle
    hwsetup_add_cpu(hw, "LM32", 75000000);
260 d821732a Michael Walle
    hwsetup_add_flash(hw, "flash", flash_base, flash_size);
261 d821732a Michael Walle
    hwsetup_add_ddr_sdram(hw, "ddr_sdram", ram_base, ram_size);
262 d821732a Michael Walle
    hwsetup_add_timer(hw, "timer0", timer0_base, timer0_irq);
263 d821732a Michael Walle
    hwsetup_add_timer(hw, "timer1_dev_only", timer1_base, timer1_irq);
264 d821732a Michael Walle
    hwsetup_add_timer(hw, "timer2_dev_only", timer2_base, timer2_irq);
265 d821732a Michael Walle
    hwsetup_add_uart(hw, "uart", uart0_base, uart0_irq);
266 d821732a Michael Walle
    hwsetup_add_trailer(hw);
267 d821732a Michael Walle
    hwsetup_create_rom(hw, hwsetup_base);
268 d821732a Michael Walle
    hwsetup_free(hw);
269 d821732a Michael Walle
270 d821732a Michael Walle
    reset_info->hwsetup_base = hwsetup_base;
271 d821732a Michael Walle
272 d821732a Michael Walle
    if (kernel_cmdline && strlen(kernel_cmdline)) {
273 d821732a Michael Walle
        pstrcpy_targphys("cmdline", cmdline_base, TARGET_PAGE_SIZE,
274 d821732a Michael Walle
                kernel_cmdline);
275 d821732a Michael Walle
        reset_info->cmdline_base = cmdline_base;
276 d821732a Michael Walle
    }
277 d821732a Michael Walle
278 d821732a Michael Walle
    if (initrd_filename) {
279 d821732a Michael Walle
        size_t initrd_size;
280 d821732a Michael Walle
        initrd_size = load_image_targphys(initrd_filename, initrd_base,
281 d821732a Michael Walle
                initrd_max);
282 d821732a Michael Walle
        reset_info->initrd_base = initrd_base;
283 d821732a Michael Walle
        reset_info->initrd_size = initrd_size;
284 d821732a Michael Walle
    }
285 d821732a Michael Walle
286 d821732a Michael Walle
    qemu_register_reset(main_cpu_reset, reset_info);
287 d821732a Michael Walle
}
288 d821732a Michael Walle
289 d821732a Michael Walle
static QEMUMachine lm32_evr_machine = {
290 d821732a Michael Walle
    .name = "lm32-evr",
291 d821732a Michael Walle
    .desc = "LatticeMico32 EVR32 eval system",
292 d821732a Michael Walle
    .init = lm32_evr_init,
293 d821732a Michael Walle
    .is_default = 1
294 d821732a Michael Walle
};
295 d821732a Michael Walle
296 d821732a Michael Walle
static QEMUMachine lm32_uclinux_machine = {
297 d821732a Michael Walle
    .name = "lm32-uclinux",
298 d821732a Michael Walle
    .desc = "lm32 platform for uClinux and u-boot by Theobroma Systems",
299 d821732a Michael Walle
    .init = lm32_uclinux_init,
300 d821732a Michael Walle
    .is_default = 0
301 d821732a Michael Walle
};
302 d821732a Michael Walle
303 d821732a Michael Walle
static void lm32_machine_init(void)
304 d821732a Michael Walle
{
305 d821732a Michael Walle
    qemu_register_machine(&lm32_uclinux_machine);
306 d821732a Michael Walle
    qemu_register_machine(&lm32_evr_machine);
307 d821732a Michael Walle
}
308 d821732a Michael Walle
309 d821732a Michael Walle
machine_init(lm32_machine_init);