xtfpga.c 15.5 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/block-backend.h"
39
#include "sysemu/char.h"
40
#include "sysemu/device_tree.h"
41
#include "qemu/error-report.h"
42
#include "bootparam.h"
M
Max Filippov 已提交
43 44

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

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 已提交
66
static uint64_t lx60_fpga_read(void *opaque, hwaddr addr,
67 68 69 70 71 72
        unsigned size)
{
    Lx60FpgaState *s = opaque;

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

    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 已提交
87
static void lx60_fpga_write(void *opaque, hwaddr addr,
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
        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 已提交
112
        hwaddr base)
113 114 115
{
    Lx60FpgaState *s = g_malloc(sizeof(Lx60FpgaState));

116
    memory_region_init_io(&s->iomem, NULL, &lx60_fpga_ops, s,
117
            "lx60.fpga", 0x10000);
118 119 120 121 122 123 124
    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 已提交
125 126 127
        hwaddr base,
        hwaddr descriptors,
        hwaddr buffers,
128 129 130 131 132 133 134 135 136 137
        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);

138
    s = SYS_BUS_DEVICE(dev);
139 140 141 142 143 144 145
    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));
146 147
    memory_region_init_ram(ram, OBJECT(s), "open_eth.ram", 16384,
                           &error_fatal);
148
    vmstate_register_ram_global(ram);
149 150 151
    memory_region_add_subregion(address_space, buffers, ram);
}

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

    return cpu_get_phys_page_debug(CPU(cpu), addr);
157 158
}

159
static void lx60_reset(void *opaque)
160
{
161
    XtensaCPU *cpu = opaque;
162

163
    cpu_reset(CPU(cpu));
164 165
}

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
static uint64_t lx60_io_read(void *opaque, hwaddr addr,
        unsigned size)
{
    return 0;
}

static void lx60_io_write(void *opaque, hwaddr addr,
        uint64_t val, unsigned size)
{
}

static const MemoryRegionOps lx60_io_ops = {
    .read = lx60_io_read,
    .write = lx60_io_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
};

183
static void lx_init(const LxBoardDesc *board, MachineState *machine)
184 185 186 187 188 189 190
{
#ifdef TARGET_WORDS_BIGENDIAN
    int be = 1;
#else
    int be = 0;
#endif
    MemoryRegion *system_memory = get_system_memory();
191
    XtensaCPU *cpu = NULL;
A
Andreas Färber 已提交
192
    CPUXtensaState *env = NULL;
193
    MemoryRegion *ram, *rom, *system_io;
M
Max Filippov 已提交
194 195
    DriveInfo *dinfo;
    pflash_t *flash = NULL;
196
    QemuOpts *machine_opts = qemu_get_machine_opts();
197
    const char *cpu_model = machine->cpu_model;
198 199
    const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
    const char *kernel_cmdline = qemu_opt_get(machine_opts, "append");
200
    const char *dtb_filename = qemu_opt_get(machine_opts, "dtb");
201
    const char *initrd_filename = qemu_opt_get(machine_opts, "initrd");
202 203
    int n;

M
Max Filippov 已提交
204
    if (!cpu_model) {
205
        cpu_model = XTENSA_DEFAULT_CPU_MODEL;
M
Max Filippov 已提交
206 207
    }

208
    for (n = 0; n < smp_cpus; n++) {
209 210
        cpu = cpu_xtensa_init(cpu_model);
        if (cpu == NULL) {
211
            error_report("unable to find CPU definition '%s'",
212 213
                         cpu_model);
            exit(EXIT_FAILURE);
214
        }
215 216
        env = &cpu->env;

217
        env->sregs[PRID] = n;
218
        qemu_register_reset(lx60_reset, cpu);
219 220 221
        /* Need MMU initialized prior to ELF loading,
         * so that ELF gets loaded into virtual addresses
         */
222
        cpu_reset(CPU(cpu));
223 224 225
    }

    ram = g_malloc(sizeof(*ram));
226
    memory_region_init_ram(ram, NULL, "lx60.dram", machine->ram_size,
227
                           &error_fatal);
228
    vmstate_register_ram_global(ram);
229 230 231
    memory_region_add_subregion(system_memory, 0, ram);

    system_io = g_malloc(sizeof(*system_io));
232 233
    memory_region_init_io(system_io, NULL, &lx60_io_ops, NULL, "lx60.io",
                          224 * 1024 * 1024);
234 235
    memory_region_add_subregion(system_memory, 0xf0000000, system_io);
    lx60_fpga_init(system_io, 0x0d020000);
S
Stefan Hajnoczi 已提交
236
    if (nd_table[0].used) {
237 238 239 240 241 242 243 244 245 246 247
        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 已提交
248 249
    dinfo = drive_get(IF_PFLASH, 0, 0);
    if (dinfo) {
250
        flash = pflash_cfi01_register(board->flash_base,
M
Max Filippov 已提交
251
                NULL, "lx60.io.flash", board->flash_size,
252
                blk_by_legacy_dinfo(dinfo),
253
                board->flash_sector_size,
M
Max Filippov 已提交
254 255 256
                board->flash_size / board->flash_sector_size,
                4, 0x0000, 0x0000, 0x0000, 0x0000, be);
        if (flash == NULL) {
257
            error_report("unable to mount pflash");
258
            exit(EXIT_FAILURE);
M
Max Filippov 已提交
259 260 261 262
        }
    }

    /* Use presence of kernel file name as 'boot from SRAM' switch. */
263
    if (kernel_filename) {
264
        uint32_t entry_point = env->pc;
265
        size_t bp_size = 3 * get_tag_size(0); /* first/last and memory tags */
266 267
        uint32_t tagptr = 0xfe000000 + board->sram_size;
        uint32_t cur_tagptr;
268 269 270 271 272
        BpMemInfo memory_location = {
            .type = tswap32(MEMORY_TYPE_CONVENTIONAL),
            .start = tswap32(0),
            .end = tswap32(machine->ram_size),
        };
273 274 275
        uint32_t lowmem_end = machine->ram_size < 0x08000000 ?
            machine->ram_size : 0x08000000;
        uint32_t cur_lowmem = QEMU_ALIGN_UP(lowmem_end / 2, 4096);
276

277
        rom = g_malloc(sizeof(*rom));
278
        memory_region_init_ram(rom, NULL, "lx60.sram", board->sram_size,
279
                               &error_fatal);
280
        vmstate_register_ram_global(rom);
281 282
        memory_region_add_subregion(system_memory, 0xfe000000, rom);

283 284 285
        if (kernel_cmdline) {
            bp_size += get_tag_size(strlen(kernel_cmdline) + 1);
        }
286 287 288
        if (dtb_filename) {
            bp_size += get_tag_size(sizeof(uint32_t));
        }
289 290 291
        if (initrd_filename) {
            bp_size += get_tag_size(sizeof(BpMemInfo));
        }
292

293
        /* Put kernel bootparameters to the end of that SRAM */
294 295
        tagptr = (tagptr - bp_size) & ~0xff;
        cur_tagptr = put_tag(tagptr, BP_TAG_FIRST, 0, NULL);
296 297
        cur_tagptr = put_tag(cur_tagptr, BP_TAG_MEMORY,
                             sizeof(memory_location), &memory_location);
298

299
        if (kernel_cmdline) {
300 301
            cur_tagptr = put_tag(cur_tagptr, BP_TAG_COMMAND_LINE,
                                 strlen(kernel_cmdline) + 1, kernel_cmdline);
302
        }
303 304 305 306 307 308
        if (dtb_filename) {
            int fdt_size;
            void *fdt = load_device_tree(dtb_filename, &fdt_size);
            uint32_t dtb_addr = tswap32(cur_lowmem);

            if (!fdt) {
309
                error_report("could not load DTB '%s'", dtb_filename);
310 311 312 313 314 315 316 317
                exit(EXIT_FAILURE);
            }

            cpu_physical_memory_write(cur_lowmem, fdt, fdt_size);
            cur_tagptr = put_tag(cur_tagptr, BP_TAG_FDT,
                                 sizeof(dtb_addr), &dtb_addr);
            cur_lowmem = QEMU_ALIGN_UP(cur_lowmem + fdt_size, 4096);
        }
318 319 320 321 322 323 324 325 326 327 328
        if (initrd_filename) {
            BpMemInfo initrd_location = { 0 };
            int initrd_size = load_ramdisk(initrd_filename, cur_lowmem,
                                           lowmem_end - cur_lowmem);

            if (initrd_size < 0) {
                initrd_size = load_image_targphys(initrd_filename,
                                                  cur_lowmem,
                                                  lowmem_end - cur_lowmem);
            }
            if (initrd_size < 0) {
329
                error_report("could not load initrd '%s'", initrd_filename);
330 331 332 333 334 335 336 337
                exit(EXIT_FAILURE);
            }
            initrd_location.start = tswap32(cur_lowmem);
            initrd_location.end = tswap32(cur_lowmem + initrd_size);
            cur_tagptr = put_tag(cur_tagptr, BP_TAG_INITRD,
                                 sizeof(initrd_location), &initrd_location);
            cur_lowmem = QEMU_ALIGN_UP(cur_lowmem + initrd_size, 4096);
        }
338 339 340
        cur_tagptr = put_tag(cur_tagptr, BP_TAG_LAST, 0, NULL);
        env->regs[2] = tagptr;

341 342
        uint64_t elf_entry;
        uint64_t elf_lowaddr;
343
        int success = load_elf(kernel_filename, translate_phys_addr, cpu,
344 345
                &elf_entry, &elf_lowaddr, NULL, be, ELF_MACHINE, 0);
        if (success > 0) {
346 347 348 349
            entry_point = elf_entry;
        } else {
            hwaddr ep;
            int is_linux;
350
            success = load_uimage(kernel_filename, &ep, NULL, &is_linux,
351
                                  translate_phys_addr, cpu);
352 353 354
            if (success > 0 && is_linux) {
                entry_point = ep;
            } else {
355
                error_report("could not load kernel '%s'",
356 357 358 359 360 361 362 363 364 365 366 367 368 369
                             kernel_filename);
                exit(EXIT_FAILURE);
            }
        }
        if (entry_point != env->pc) {
            static const uint8_t jx_a0[] = {
#ifdef TARGET_WORDS_BIGENDIAN
                0x0a, 0, 0,
#else
                0xa0, 0, 0,
#endif
            };
            env->regs[0] = entry_point;
            cpu_physical_memory_write(env->pc, jx_a0, sizeof(jx_a0));
370
        }
M
Max Filippov 已提交
371 372 373 374 375
    } else {
        if (flash) {
            MemoryRegion *flash_mr = pflash_cfi01_get_memory(flash);
            MemoryRegion *flash_io = g_malloc(sizeof(*flash_io));

376
            memory_region_init_alias(flash_io, NULL, "lx60.flash",
377 378 379
                    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 已提交
380 381 382
            memory_region_add_subregion(system_memory, 0xfe000000,
                    flash_io);
        }
383 384 385
    }
}

386
static void xtensa_lx60_init(MachineState *machine)
387
{
M
Max Filippov 已提交
388
    static const LxBoardDesc lx60_board = {
389 390
        .flash_base = 0xf8000000,
        .flash_size = 0x00400000,
M
Max Filippov 已提交
391 392 393
        .flash_sector_size = 0x10000,
        .sram_size = 0x20000,
    };
394
    lx_init(&lx60_board, machine);
M
Max Filippov 已提交
395 396
}

397
static void xtensa_lx200_init(MachineState *machine)
M
Max Filippov 已提交
398 399
{
    static const LxBoardDesc lx200_board = {
400 401
        .flash_base = 0xf8000000,
        .flash_size = 0x01000000,
M
Max Filippov 已提交
402 403 404
        .flash_sector_size = 0x20000,
        .sram_size = 0x2000000,
    };
405
    lx_init(&lx200_board, machine);
406 407
}

408
static void xtensa_ml605_init(MachineState *machine)
409 410 411
{
    static const LxBoardDesc ml605_board = {
        .flash_base = 0xf8000000,
412
        .flash_size = 0x01000000,
413 414 415
        .flash_sector_size = 0x20000,
        .sram_size = 0x2000000,
    };
416
    lx_init(&ml605_board, machine);
417 418
}

419
static void xtensa_kc705_init(MachineState *machine)
420 421 422 423
{
    static const LxBoardDesc kc705_board = {
        .flash_base = 0xf0000000,
        .flash_size = 0x08000000,
424
        .flash_boot_base = 0x06000000,
425 426 427
        .flash_sector_size = 0x20000,
        .sram_size = 0x2000000,
    };
428
    lx_init(&kc705_board, machine);
429 430
}

431
static void xtensa_lx60_class_init(ObjectClass *oc, void *data)
432
{
433 434
    MachineClass *mc = MACHINE_CLASS(oc);

435 436 437 438
    mc->desc = "lx60 EVB (" XTENSA_DEFAULT_CPU_MODEL ")";
    mc->init = xtensa_lx60_init;
    mc->max_cpus = 4;
}
439

440 441 442 443 444
static const TypeInfo xtensa_lx60_type = {
    .name = MACHINE_TYPE_NAME("lx60"),
    .parent = TYPE_MACHINE,
    .class_init = xtensa_lx60_class_init,
};
M
Max Filippov 已提交
445

446
static void xtensa_lx200_class_init(ObjectClass *oc, void *data)
447
{
448 449
    MachineClass *mc = MACHINE_CLASS(oc);

450 451 452 453
    mc->desc = "lx200 EVB (" XTENSA_DEFAULT_CPU_MODEL ")";
    mc->init = xtensa_lx200_init;
    mc->max_cpus = 4;
}
454

455 456 457 458 459
static const TypeInfo xtensa_lx200_type = {
    .name = MACHINE_TYPE_NAME("lx200"),
    .parent = TYPE_MACHINE,
    .class_init = xtensa_lx200_class_init,
};
460

461
static void xtensa_ml605_class_init(ObjectClass *oc, void *data)
462
{
463 464
    MachineClass *mc = MACHINE_CLASS(oc);

465 466 467 468 469
    mc->desc = "ml605 EVB (" XTENSA_DEFAULT_CPU_MODEL ")";
    mc->init = xtensa_ml605_init;
    mc->max_cpus = 4;
}

470 471 472 473 474
static const TypeInfo xtensa_ml605_type = {
    .name = MACHINE_TYPE_NAME("ml605"),
    .parent = TYPE_MACHINE,
    .class_init = xtensa_ml605_class_init,
};
475

476
static void xtensa_kc705_class_init(ObjectClass *oc, void *data)
477
{
478 479
    MachineClass *mc = MACHINE_CLASS(oc);

480 481 482
    mc->desc = "kc705 EVB (" XTENSA_DEFAULT_CPU_MODEL ")";
    mc->init = xtensa_kc705_init;
    mc->max_cpus = 4;
483 484
}

485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
static const TypeInfo xtensa_kc705_type = {
    .name = MACHINE_TYPE_NAME("kc705"),
    .parent = TYPE_MACHINE,
    .class_init = xtensa_kc705_class_init,
};

static void xtensa_lx_machines_init(void)
{
    type_register_static(&xtensa_lx60_type);
    type_register_static(&xtensa_lx200_type);
    type_register_static(&xtensa_ml605_type);
    type_register_static(&xtensa_kc705_type);
}

machine_init(xtensa_lx_machines_init)