xtfpga.c 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Copyright (c) 2011, Max Filippov, Open Source and Linux Lab.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Open Source and Linux Lab nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

28
#include "sysemu/sysemu.h"
29 30
#include "hw/boards.h"
#include "hw/loader.h"
31
#include "elf.h"
32 33
#include "exec/memory.h"
#include "exec/address-spaces.h"
P
Paolo Bonzini 已提交
34
#include "hw/char/serial.h"
P
Paolo Bonzini 已提交
35
#include "net/net.h"
36
#include "hw/sysbus.h"
P
Paolo Bonzini 已提交
37
#include "hw/block/flash.h"
38
#include "sysemu/blockdev.h"
39
#include "sysemu/char.h"
40
#include "qemu/error-report.h"
41
#include "bootparam.h"
M
Max Filippov 已提交
42 43

typedef struct LxBoardDesc {
44
    hwaddr flash_base;
M
Max Filippov 已提交
45
    size_t flash_size;
46
    size_t flash_boot_base;
M
Max Filippov 已提交
47 48 49
    size_t flash_sector_size;
    size_t sram_size;
} LxBoardDesc;
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

typedef struct Lx60FpgaState {
    MemoryRegion iomem;
    uint32_t leds;
    uint32_t switches;
} Lx60FpgaState;

static void lx60_fpga_reset(void *opaque)
{
    Lx60FpgaState *s = opaque;

    s->leds = 0;
    s->switches = 0;
}

A
Avi Kivity 已提交
65
static uint64_t lx60_fpga_read(void *opaque, hwaddr addr,
66 67 68 69 70 71
        unsigned size)
{
    Lx60FpgaState *s = opaque;

    switch (addr) {
    case 0x0: /*build date code*/
72
        return 0x09272011;
73 74 75 76 77 78 79 80 81 82 83 84 85

    case 0x4: /*processor clock frequency, Hz*/
        return 10000000;

    case 0x8: /*LEDs (off = 0, on = 1)*/
        return s->leds;

    case 0xc: /*DIP switches (off = 0, on = 1)*/
        return s->switches;
    }
    return 0;
}

A
Avi Kivity 已提交
86
static void lx60_fpga_write(void *opaque, hwaddr addr,
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        uint64_t val, unsigned size)
{
    Lx60FpgaState *s = opaque;

    switch (addr) {
    case 0x8: /*LEDs (off = 0, on = 1)*/
        s->leds = val;
        break;

    case 0x10: /*board reset*/
        if (val == 0xdead) {
            qemu_system_reset_request();
        }
        break;
    }
}

static const MemoryRegionOps lx60_fpga_ops = {
    .read = lx60_fpga_read,
    .write = lx60_fpga_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
};

static Lx60FpgaState *lx60_fpga_init(MemoryRegion *address_space,
A
Avi Kivity 已提交
111
        hwaddr base)
112 113 114
{
    Lx60FpgaState *s = g_malloc(sizeof(Lx60FpgaState));

115
    memory_region_init_io(&s->iomem, NULL, &lx60_fpga_ops, s,
116
            "lx60.fpga", 0x10000);
117 118 119 120 121 122 123
    memory_region_add_subregion(address_space, base, &s->iomem);
    lx60_fpga_reset(s);
    qemu_register_reset(lx60_fpga_reset, s);
    return s;
}

static void lx60_net_init(MemoryRegion *address_space,
A
Avi Kivity 已提交
124 125 126
        hwaddr base,
        hwaddr descriptors,
        hwaddr buffers,
127 128 129 130 131 132 133 134 135 136
        qemu_irq irq, NICInfo *nd)
{
    DeviceState *dev;
    SysBusDevice *s;
    MemoryRegion *ram;

    dev = qdev_create(NULL, "open_eth");
    qdev_set_nic_properties(dev, nd);
    qdev_init_nofail(dev);

137
    s = SYS_BUS_DEVICE(dev);
138 139 140 141 142 143 144
    sysbus_connect_irq(s, 0, irq);
    memory_region_add_subregion(address_space, base,
            sysbus_mmio_get_region(s, 0));
    memory_region_add_subregion(address_space, descriptors,
            sysbus_mmio_get_region(s, 1));

    ram = g_malloc(sizeof(*ram));
145
    memory_region_init_ram(ram, OBJECT(s), "open_eth.ram", 16384);
146
    vmstate_register_ram_global(ram);
147 148 149
    memory_region_add_subregion(address_space, buffers, ram);
}

150
static uint64_t translate_phys_addr(void *opaque, uint64_t addr)
151
{
152 153 154
    XtensaCPU *cpu = opaque;

    return cpu_get_phys_page_debug(CPU(cpu), addr);
155 156
}

157
static void lx60_reset(void *opaque)
158
{
159
    XtensaCPU *cpu = opaque;
160

161
    cpu_reset(CPU(cpu));
162 163
}

164
static void lx_init(const LxBoardDesc *board, MachineState *machine)
165 166 167 168 169 170 171
{
#ifdef TARGET_WORDS_BIGENDIAN
    int be = 1;
#else
    int be = 0;
#endif
    MemoryRegion *system_memory = get_system_memory();
172
    XtensaCPU *cpu = NULL;
A
Andreas Färber 已提交
173
    CPUXtensaState *env = NULL;
174
    MemoryRegion *ram, *rom, *system_io;
M
Max Filippov 已提交
175 176
    DriveInfo *dinfo;
    pflash_t *flash = NULL;
177
    QemuOpts *machine_opts = qemu_get_machine_opts();
178
    const char *cpu_model = machine->cpu_model;
179 180
    const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
    const char *kernel_cmdline = qemu_opt_get(machine_opts, "append");
181 182
    int n;

M
Max Filippov 已提交
183
    if (!cpu_model) {
184
        cpu_model = XTENSA_DEFAULT_CPU_MODEL;
M
Max Filippov 已提交
185 186
    }

187
    for (n = 0; n < smp_cpus; n++) {
188 189
        cpu = cpu_xtensa_init(cpu_model);
        if (cpu == NULL) {
190 191 192
            error_report("unable to find CPU definition '%s'\n",
                         cpu_model);
            exit(EXIT_FAILURE);
193
        }
194 195
        env = &cpu->env;

196
        env->sregs[PRID] = n;
197
        qemu_register_reset(lx60_reset, cpu);
198 199 200
        /* Need MMU initialized prior to ELF loading,
         * so that ELF gets loaded into virtual addresses
         */
201
        cpu_reset(CPU(cpu));
202 203 204
    }

    ram = g_malloc(sizeof(*ram));
205
    memory_region_init_ram(ram, NULL, "lx60.dram", machine->ram_size);
206
    vmstate_register_ram_global(ram);
207 208 209
    memory_region_add_subregion(system_memory, 0, ram);

    system_io = g_malloc(sizeof(*system_io));
210
    memory_region_init(system_io, NULL, "lx60.io", 224 * 1024 * 1024);
211 212
    memory_region_add_subregion(system_memory, 0xf0000000, system_io);
    lx60_fpga_init(system_io, 0x0d020000);
S
Stefan Hajnoczi 已提交
213
    if (nd_table[0].used) {
214 215 216 217 218 219 220 221 222 223 224
        lx60_net_init(system_io, 0x0d030000, 0x0d030400, 0x0d800000,
                xtensa_get_extint(env, 1), nd_table);
    }

    if (!serial_hds[0]) {
        serial_hds[0] = qemu_chr_new("serial0", "null", NULL);
    }

    serial_mm_init(system_io, 0x0d050020, 2, xtensa_get_extint(env, 0),
            115200, serial_hds[0], DEVICE_NATIVE_ENDIAN);

M
Max Filippov 已提交
225 226
    dinfo = drive_get(IF_PFLASH, 0, 0);
    if (dinfo) {
227
        flash = pflash_cfi01_register(board->flash_base,
M
Max Filippov 已提交
228 229 230 231 232
                NULL, "lx60.io.flash", board->flash_size,
                dinfo->bdrv, board->flash_sector_size,
                board->flash_size / board->flash_sector_size,
                4, 0x0000, 0x0000, 0x0000, 0x0000, be);
        if (flash == NULL) {
233 234
            error_report("unable to mount pflash\n");
            exit(EXIT_FAILURE);
M
Max Filippov 已提交
235 236 237 238
        }
    }

    /* Use presence of kernel file name as 'boot from SRAM' switch. */
239
    if (kernel_filename) {
240
        rom = g_malloc(sizeof(*rom));
241
        memory_region_init_ram(rom, NULL, "lx60.sram", board->sram_size);
242
        vmstate_register_ram_global(rom);
243 244 245 246 247 248 249 250 251 252
        memory_region_add_subregion(system_memory, 0xfe000000, rom);

        /* Put kernel bootparameters to the end of that SRAM */
        if (kernel_cmdline) {
            size_t cmdline_size = strlen(kernel_cmdline) + 1;
            size_t bp_size = sizeof(BpTag[4]) + cmdline_size;
            uint32_t tagptr = (0xfe000000 + board->sram_size - bp_size) & ~0xff;

            env->regs[2] = tagptr;

253
            tagptr = put_tag(tagptr, BP_TAG_FIRST, 0, NULL);
254
            if (cmdline_size > 1) {
255
                tagptr = put_tag(tagptr, BP_TAG_COMMAND_LINE,
256 257
                        cmdline_size, kernel_cmdline);
            }
258
            tagptr = put_tag(tagptr, BP_TAG_LAST, 0, NULL);
259
        }
260 261
        uint64_t elf_entry;
        uint64_t elf_lowaddr;
262
        int success = load_elf(kernel_filename, translate_phys_addr, cpu,
263 264 265 266
                &elf_entry, &elf_lowaddr, NULL, be, ELF_MACHINE, 0);
        if (success > 0) {
            env->pc = elf_entry;
        }
M
Max Filippov 已提交
267 268 269 270 271
    } else {
        if (flash) {
            MemoryRegion *flash_mr = pflash_cfi01_get_memory(flash);
            MemoryRegion *flash_io = g_malloc(sizeof(*flash_io));

272
            memory_region_init_alias(flash_io, NULL, "lx60.flash",
273 274 275
                    flash_mr, board->flash_boot_base,
                    board->flash_size - board->flash_boot_base < 0x02000000 ?
                    board->flash_size - board->flash_boot_base : 0x02000000);
M
Max Filippov 已提交
276 277 278
            memory_region_add_subregion(system_memory, 0xfe000000,
                    flash_io);
        }
279 280 281
    }
}

282
static void xtensa_lx60_init(MachineState *machine)
283
{
M
Max Filippov 已提交
284
    static const LxBoardDesc lx60_board = {
285 286
        .flash_base = 0xf8000000,
        .flash_size = 0x00400000,
M
Max Filippov 已提交
287 288 289
        .flash_sector_size = 0x10000,
        .sram_size = 0x20000,
    };
290
    lx_init(&lx60_board, machine);
M
Max Filippov 已提交
291 292
}

293
static void xtensa_lx200_init(MachineState *machine)
M
Max Filippov 已提交
294 295
{
    static const LxBoardDesc lx200_board = {
296 297
        .flash_base = 0xf8000000,
        .flash_size = 0x01000000,
M
Max Filippov 已提交
298 299 300
        .flash_sector_size = 0x20000,
        .sram_size = 0x2000000,
    };
301
    lx_init(&lx200_board, machine);
302 303
}

304
static void xtensa_ml605_init(MachineState *machine)
305 306 307 308 309 310 311
{
    static const LxBoardDesc ml605_board = {
        .flash_base = 0xf8000000,
        .flash_size = 0x02000000,
        .flash_sector_size = 0x20000,
        .sram_size = 0x2000000,
    };
312
    lx_init(&ml605_board, machine);
313 314
}

315
static void xtensa_kc705_init(MachineState *machine)
316 317 318 319
{
    static const LxBoardDesc kc705_board = {
        .flash_base = 0xf0000000,
        .flash_size = 0x08000000,
320
        .flash_boot_base = 0x06000000,
321 322 323
        .flash_sector_size = 0x20000,
        .sram_size = 0x2000000,
    };
324
    lx_init(&kc705_board, machine);
325 326
}

327 328
static QEMUMachine xtensa_lx60_machine = {
    .name = "lx60",
329
    .desc = "lx60 EVB (" XTENSA_DEFAULT_CPU_MODEL ")",
330 331 332 333
    .init = xtensa_lx60_init,
    .max_cpus = 4,
};

M
Max Filippov 已提交
334 335
static QEMUMachine xtensa_lx200_machine = {
    .name = "lx200",
336
    .desc = "lx200 EVB (" XTENSA_DEFAULT_CPU_MODEL ")",
M
Max Filippov 已提交
337 338 339 340
    .init = xtensa_lx200_init,
    .max_cpus = 4,
};

341 342 343 344 345 346 347 348 349 350 351 352 353 354
static QEMUMachine xtensa_ml605_machine = {
    .name = "ml605",
    .desc = "ml605 EVB (" XTENSA_DEFAULT_CPU_MODEL ")",
    .init = xtensa_ml605_init,
    .max_cpus = 4,
};

static QEMUMachine xtensa_kc705_machine = {
    .name = "kc705",
    .desc = "kc705 EVB (" XTENSA_DEFAULT_CPU_MODEL ")",
    .init = xtensa_kc705_init,
    .max_cpus = 4,
};

M
Max Filippov 已提交
355
static void xtensa_lx_machines_init(void)
356 357
{
    qemu_register_machine(&xtensa_lx60_machine);
M
Max Filippov 已提交
358
    qemu_register_machine(&xtensa_lx200_machine);
359 360
    qemu_register_machine(&xtensa_ml605_machine);
    qemu_register_machine(&xtensa_kc705_machine);
361 362
}

M
Max Filippov 已提交
363
machine_init(xtensa_lx_machines_init);