lm32_boards.c 9.9 KB
Newer Older
M
Michael Walle 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 *  QEMU models for LatticeMico32 uclinux and evr32 boards.
 *
 *  Copyright (c) 2010 Michael Walle <michael@walle.cc>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

20 21 22 23 24 25
#include "hw/sysbus.h"
#include "hw/hw.h"
#include "hw/flash.h"
#include "hw/devices.h"
#include "hw/boards.h"
#include "hw/loader.h"
26
#include "sysemu/blockdev.h"
M
Michael Walle 已提交
27
#include "elf.h"
28 29
#include "hw/lm32_hwsetup.h"
#include "hw/lm32.h"
30
#include "exec/address-spaces.h"
M
Michael Walle 已提交
31 32

typedef struct {
33
    LM32CPU *cpu;
A
Avi Kivity 已提交
34 35 36 37
    hwaddr bootstrap_pc;
    hwaddr flash_base;
    hwaddr hwsetup_base;
    hwaddr initrd_base;
M
Michael Walle 已提交
38
    size_t initrd_size;
A
Avi Kivity 已提交
39
    hwaddr cmdline_base;
M
Michael Walle 已提交
40 41 42 43
} ResetInfo;

static void cpu_irq_handler(void *opaque, int irq, int level)
{
44 45 46
    LM32CPU *cpu = opaque;
    CPULM32State *env = &cpu->env;
    CPUState *cs = CPU(cpu);
M
Michael Walle 已提交
47 48 49 50

    if (level) {
        cpu_interrupt(env, CPU_INTERRUPT_HARD);
    } else {
51
        cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
M
Michael Walle 已提交
52 53 54 55 56 57
    }
}

static void main_cpu_reset(void *opaque)
{
    ResetInfo *reset_info = opaque;
58
    CPULM32State *env = &reset_info->cpu->env;
M
Michael Walle 已提交
59

60
    cpu_reset(CPU(reset_info->cpu));
M
Michael Walle 已提交
61 62 63 64 65 66 67 68 69 70 71 72

    /* init defaults */
    env->pc = (uint32_t)reset_info->bootstrap_pc;
    env->regs[R_R1] = (uint32_t)reset_info->hwsetup_base;
    env->regs[R_R2] = (uint32_t)reset_info->cmdline_base;
    env->regs[R_R3] = (uint32_t)reset_info->initrd_base;
    env->regs[R_R4] = (uint32_t)(reset_info->initrd_base +
        reset_info->initrd_size);
    env->eba = reset_info->flash_base;
    env->deba = reset_info->flash_base;
}

73
static void lm32_evr_init(QEMUMachineInitArgs *args)
M
Michael Walle 已提交
74
{
75 76
    const char *cpu_model = args->cpu_model;
    const char *kernel_filename = args->kernel_filename;
77
    LM32CPU *cpu;
A
Andreas Färber 已提交
78
    CPULM32State *env;
M
Michael Walle 已提交
79
    DriveInfo *dinfo;
A
Avi Kivity 已提交
80 81
    MemoryRegion *address_space_mem =  get_system_memory();
    MemoryRegion *phys_ram = g_new(MemoryRegion, 1);
M
Michael Walle 已提交
82 83 84 85 86
    qemu_irq *cpu_irq, irq[32];
    ResetInfo *reset_info;
    int i;

    /* memory map */
A
Avi Kivity 已提交
87
    hwaddr flash_base  = 0x04000000;
M
Michael Walle 已提交
88 89
    size_t flash_sector_size       = 256 * 1024;
    size_t flash_size              = 32 * 1024 * 1024;
A
Avi Kivity 已提交
90
    hwaddr ram_base    = 0x08000000;
M
Michael Walle 已提交
91
    size_t ram_size                = 64 * 1024 * 1024;
A
Avi Kivity 已提交
92 93 94
    hwaddr timer0_base = 0x80002000;
    hwaddr uart0_base  = 0x80006000;
    hwaddr timer1_base = 0x8000a000;
M
Michael Walle 已提交
95 96 97 98
    int uart0_irq                  = 0;
    int timer0_irq                 = 1;
    int timer1_irq                 = 3;

99
    reset_info = g_malloc0(sizeof(ResetInfo));
M
Michael Walle 已提交
100 101 102 103

    if (cpu_model == NULL) {
        cpu_model = "lm32-full";
    }
104 105
    cpu = cpu_lm32_init(cpu_model);
    env = &cpu->env;
106
    reset_info->cpu = cpu;
M
Michael Walle 已提交
107 108 109

    reset_info->flash_base = flash_base;

110 111
    memory_region_init_ram(phys_ram, "lm32_evr.sdram", ram_size);
    vmstate_register_ram_global(phys_ram);
A
Avi Kivity 已提交
112
    memory_region_add_subregion(address_space_mem, ram_base, phys_ram);
M
Michael Walle 已提交
113 114 115

    dinfo = drive_get(IF_PFLASH, 0, 0);
    /* Spansion S29NS128P */
116
    pflash_cfi02_register(flash_base, NULL, "lm32_evr.flash", flash_size,
M
Michael Walle 已提交
117 118
                          dinfo ? dinfo->bdrv : NULL, flash_sector_size,
                          flash_size / flash_sector_size, 1, 2,
119
                          0x01, 0x7e, 0x43, 0x00, 0x555, 0x2aa, 1);
M
Michael Walle 已提交
120 121

    /* create irq lines */
122
    cpu_irq = qemu_allocate_irqs(cpu_irq_handler, cpu, 1);
M
Michael Walle 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    env->pic_state = lm32_pic_init(*cpu_irq);
    for (i = 0; i < 32; i++) {
        irq[i] = qdev_get_gpio_in(env->pic_state, i);
    }

    sysbus_create_simple("lm32-uart", uart0_base, irq[uart0_irq]);
    sysbus_create_simple("lm32-timer", timer0_base, irq[timer0_irq]);
    sysbus_create_simple("lm32-timer", timer1_base, irq[timer1_irq]);

    /* make sure juart isn't the first chardev */
    env->juart_state = lm32_juart_init();

    reset_info->bootstrap_pc = flash_base;

    if (kernel_filename) {
        uint64_t entry;
        int kernel_size;

        kernel_size = load_elf(kernel_filename, NULL, NULL, &entry, NULL, NULL,
                               1, ELF_MACHINE, 0);
        reset_info->bootstrap_pc = entry;

        if (kernel_size < 0) {
            kernel_size = load_image_targphys(kernel_filename, ram_base,
                                              ram_size);
            reset_info->bootstrap_pc = ram_base;
        }

        if (kernel_size < 0) {
            fprintf(stderr, "qemu: could not load kernel '%s'\n",
                    kernel_filename);
            exit(1);
        }
    }

    qemu_register_reset(main_cpu_reset, reset_info);
}

161
static void lm32_uclinux_init(QEMUMachineInitArgs *args)
M
Michael Walle 已提交
162
{
163 164 165 166
    const char *cpu_model = args->cpu_model;
    const char *kernel_filename = args->kernel_filename;
    const char *kernel_cmdline = args->kernel_cmdline;
    const char *initrd_filename = args->initrd_filename;
167
    LM32CPU *cpu;
A
Andreas Färber 已提交
168
    CPULM32State *env;
M
Michael Walle 已提交
169
    DriveInfo *dinfo;
A
Avi Kivity 已提交
170 171
    MemoryRegion *address_space_mem =  get_system_memory();
    MemoryRegion *phys_ram = g_new(MemoryRegion, 1);
M
Michael Walle 已提交
172 173 174 175 176 177
    qemu_irq *cpu_irq, irq[32];
    HWSetup *hw;
    ResetInfo *reset_info;
    int i;

    /* memory map */
A
Avi Kivity 已提交
178
    hwaddr flash_base   = 0x04000000;
M
Michael Walle 已提交
179 180
    size_t flash_sector_size        = 256 * 1024;
    size_t flash_size               = 32 * 1024 * 1024;
A
Avi Kivity 已提交
181
    hwaddr ram_base     = 0x08000000;
M
Michael Walle 已提交
182
    size_t ram_size                 = 64 * 1024 * 1024;
A
Avi Kivity 已提交
183 184 185 186
    hwaddr uart0_base   = 0x80000000;
    hwaddr timer0_base  = 0x80002000;
    hwaddr timer1_base  = 0x80010000;
    hwaddr timer2_base  = 0x80012000;
M
Michael Walle 已提交
187 188 189 190
    int uart0_irq                   = 0;
    int timer0_irq                  = 1;
    int timer1_irq                  = 20;
    int timer2_irq                  = 21;
A
Avi Kivity 已提交
191 192 193
    hwaddr hwsetup_base = 0x0bffe000;
    hwaddr cmdline_base = 0x0bfff000;
    hwaddr initrd_base  = 0x08400000;
M
Michael Walle 已提交
194 195
    size_t initrd_max               = 0x01000000;

196
    reset_info = g_malloc0(sizeof(ResetInfo));
M
Michael Walle 已提交
197 198 199 200

    if (cpu_model == NULL) {
        cpu_model = "lm32-full";
    }
201 202
    cpu = cpu_lm32_init(cpu_model);
    env = &cpu->env;
203
    reset_info->cpu = cpu;
M
Michael Walle 已提交
204 205 206

    reset_info->flash_base = flash_base;

207 208
    memory_region_init_ram(phys_ram, "lm32_uclinux.sdram", ram_size);
    vmstate_register_ram_global(phys_ram);
A
Avi Kivity 已提交
209
    memory_region_add_subregion(address_space_mem, ram_base, phys_ram);
M
Michael Walle 已提交
210 211 212

    dinfo = drive_get(IF_PFLASH, 0, 0);
    /* Spansion S29NS128P */
213
    pflash_cfi02_register(flash_base, NULL, "lm32_uclinux.flash", flash_size,
M
Michael Walle 已提交
214 215
                          dinfo ? dinfo->bdrv : NULL, flash_sector_size,
                          flash_size / flash_sector_size, 1, 2,
216
                          0x01, 0x7e, 0x43, 0x00, 0x555, 0x2aa, 1);
M
Michael Walle 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291

    /* create irq lines */
    cpu_irq = qemu_allocate_irqs(cpu_irq_handler, env, 1);
    env->pic_state = lm32_pic_init(*cpu_irq);
    for (i = 0; i < 32; i++) {
        irq[i] = qdev_get_gpio_in(env->pic_state, i);
    }

    sysbus_create_simple("lm32-uart", uart0_base, irq[uart0_irq]);
    sysbus_create_simple("lm32-timer", timer0_base, irq[timer0_irq]);
    sysbus_create_simple("lm32-timer", timer1_base, irq[timer1_irq]);
    sysbus_create_simple("lm32-timer", timer2_base, irq[timer2_irq]);

    /* make sure juart isn't the first chardev */
    env->juart_state = lm32_juart_init();

    reset_info->bootstrap_pc = flash_base;

    if (kernel_filename) {
        uint64_t entry;
        int kernel_size;

        kernel_size = load_elf(kernel_filename, NULL, NULL, &entry, NULL, NULL,
                               1, ELF_MACHINE, 0);
        reset_info->bootstrap_pc = entry;

        if (kernel_size < 0) {
            kernel_size = load_image_targphys(kernel_filename, ram_base,
                                              ram_size);
            reset_info->bootstrap_pc = ram_base;
        }

        if (kernel_size < 0) {
            fprintf(stderr, "qemu: could not load kernel '%s'\n",
                    kernel_filename);
            exit(1);
        }
    }

    /* generate a rom with the hardware description */
    hw = hwsetup_init();
    hwsetup_add_cpu(hw, "LM32", 75000000);
    hwsetup_add_flash(hw, "flash", flash_base, flash_size);
    hwsetup_add_ddr_sdram(hw, "ddr_sdram", ram_base, ram_size);
    hwsetup_add_timer(hw, "timer0", timer0_base, timer0_irq);
    hwsetup_add_timer(hw, "timer1_dev_only", timer1_base, timer1_irq);
    hwsetup_add_timer(hw, "timer2_dev_only", timer2_base, timer2_irq);
    hwsetup_add_uart(hw, "uart", uart0_base, uart0_irq);
    hwsetup_add_trailer(hw);
    hwsetup_create_rom(hw, hwsetup_base);
    hwsetup_free(hw);

    reset_info->hwsetup_base = hwsetup_base;

    if (kernel_cmdline && strlen(kernel_cmdline)) {
        pstrcpy_targphys("cmdline", cmdline_base, TARGET_PAGE_SIZE,
                kernel_cmdline);
        reset_info->cmdline_base = cmdline_base;
    }

    if (initrd_filename) {
        size_t initrd_size;
        initrd_size = load_image_targphys(initrd_filename, initrd_base,
                initrd_max);
        reset_info->initrd_base = initrd_base;
        reset_info->initrd_size = initrd_size;
    }

    qemu_register_reset(main_cpu_reset, reset_info);
}

static QEMUMachine lm32_evr_machine = {
    .name = "lm32-evr",
    .desc = "LatticeMico32 EVR32 eval system",
    .init = lm32_evr_init,
292 293
    .is_default = 1,
    DEFAULT_MACHINE_OPTIONS,
M
Michael Walle 已提交
294 295 296 297 298 299
};

static QEMUMachine lm32_uclinux_machine = {
    .name = "lm32-uclinux",
    .desc = "lm32 platform for uClinux and u-boot by Theobroma Systems",
    .init = lm32_uclinux_init,
300 301
    .is_default = 0,
    DEFAULT_MACHINE_OPTIONS,
M
Michael Walle 已提交
302 303 304 305 306 307 308 309 310
};

static void lm32_machine_init(void)
{
    qemu_register_machine(&lm32_uclinux_machine);
    qemu_register_machine(&lm32_evr_machine);
}

machine_init(lm32_machine_init);