qemu/hw/lm32/milkymist.c
<<
>>
Prefs
   1/*
   2 *  QEMU model for the Milkymist board.
   3 *
   4 *  Copyright (c) 2010 Michael Walle <michael@walle.cc>
   5 *
   6 * This library is free software; you can redistribute it and/or
   7 * modify it under the terms of the GNU Lesser General Public
   8 * License as published by the Free Software Foundation; either
   9 * version 2.1 of the License, or (at your option) any later version.
  10 *
  11 * This library is distributed in the hope that it will be useful,
  12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14 * Lesser General Public License for more details.
  15 *
  16 * You should have received a copy of the GNU Lesser General Public
  17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18 */
  19
  20#include "qemu/osdep.h"
  21#include "qemu/units.h"
  22#include "qemu/error-report.h"
  23#include "qemu-common.h"
  24#include "qemu/datadir.h"
  25#include "cpu.h"
  26#include "hw/sysbus.h"
  27#include "hw/irq.h"
  28#include "hw/block/flash.h"
  29#include "sysemu/sysemu.h"
  30#include "sysemu/qtest.h"
  31#include "sysemu/reset.h"
  32#include "hw/boards.h"
  33#include "hw/loader.h"
  34#include "hw/qdev-properties.h"
  35#include "elf.h"
  36#include "milkymist-hw.h"
  37#include "hw/display/milkymist_tmu2.h"
  38#include "hw/sd/sd.h"
  39#include "lm32.h"
  40#include "exec/address-spaces.h"
  41#include "qemu/cutils.h"
  42
  43#define BIOS_FILENAME    "mmone-bios.bin"
  44#define BIOS_OFFSET      0x00860000
  45#define BIOS_SIZE        (512 * KiB)
  46#define KERNEL_LOAD_ADDR 0x40000000
  47
  48typedef struct {
  49    LM32CPU *cpu;
  50    hwaddr bootstrap_pc;
  51    hwaddr flash_base;
  52    hwaddr initrd_base;
  53    size_t initrd_size;
  54    hwaddr cmdline_base;
  55} ResetInfo;
  56
  57static void cpu_irq_handler(void *opaque, int irq, int level)
  58{
  59    LM32CPU *cpu = opaque;
  60    CPUState *cs = CPU(cpu);
  61
  62    if (level) {
  63        cpu_interrupt(cs, CPU_INTERRUPT_HARD);
  64    } else {
  65        cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
  66    }
  67}
  68
  69static void main_cpu_reset(void *opaque)
  70{
  71    ResetInfo *reset_info = opaque;
  72    CPULM32State *env = &reset_info->cpu->env;
  73
  74    cpu_reset(CPU(reset_info->cpu));
  75
  76    /* init defaults */
  77    env->pc = reset_info->bootstrap_pc;
  78    env->regs[R_R1] = reset_info->cmdline_base;
  79    env->regs[R_R2] = reset_info->initrd_base;
  80    env->regs[R_R3] = reset_info->initrd_base + reset_info->initrd_size;
  81    env->eba = reset_info->flash_base;
  82    env->deba = reset_info->flash_base;
  83}
  84
  85static DeviceState *milkymist_memcard_create(hwaddr base)
  86{
  87    DeviceState *dev;
  88    DriveInfo *dinfo;
  89
  90    dev = qdev_new("milkymist-memcard");
  91    sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  92    sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
  93
  94    dinfo = drive_get_next(IF_SD);
  95    if (dinfo) {
  96        DeviceState *card;
  97
  98        card = qdev_new(TYPE_SD_CARD);
  99        qdev_prop_set_drive_err(card, "drive", blk_by_legacy_dinfo(dinfo),
 100                                &error_fatal);
 101        qdev_realize_and_unref(card, qdev_get_child_bus(dev, "sd-bus"),
 102                               &error_fatal);
 103    }
 104
 105    return dev;
 106}
 107
 108static void
 109milkymist_init(MachineState *machine)
 110{
 111    MachineClass *mc = MACHINE_GET_CLASS(machine);
 112    const char *bios_name = machine->firmware ?: BIOS_FILENAME;
 113    const char *kernel_filename = machine->kernel_filename;
 114    const char *kernel_cmdline = machine->kernel_cmdline;
 115    const char *initrd_filename = machine->initrd_filename;
 116    LM32CPU *cpu;
 117    CPULM32State *env;
 118    int kernel_size;
 119    DriveInfo *dinfo;
 120    MemoryRegion *address_space_mem = get_system_memory();
 121    qemu_irq irq[32];
 122    int i;
 123    char *bios_filename;
 124    ResetInfo *reset_info;
 125
 126    if (machine->ram_size != mc->default_ram_size) {
 127        char *sz = size_to_str(mc->default_ram_size);
 128        error_report("Invalid RAM size, should be %s", sz);
 129        g_free(sz);
 130        exit(EXIT_FAILURE);
 131    }
 132
 133    /* memory map */
 134    hwaddr flash_base   = 0x00000000;
 135    size_t flash_sector_size        = 128 * KiB;
 136    size_t flash_size               = 32 * MiB;
 137    hwaddr sdram_base   = 0x40000000;
 138
 139    hwaddr initrd_base  = sdram_base + 0x1002000;
 140    hwaddr cmdline_base = sdram_base + 0x1000000;
 141    size_t initrd_max = machine->ram_size - 0x1002000;
 142
 143    reset_info = g_malloc0(sizeof(ResetInfo));
 144
 145    cpu = LM32_CPU(cpu_create(machine->cpu_type));
 146
 147    env = &cpu->env;
 148    reset_info->cpu = cpu;
 149
 150    cpu_lm32_set_phys_msb_ignore(env, 1);
 151
 152    memory_region_add_subregion(address_space_mem, sdram_base, machine->ram);
 153
 154    dinfo = drive_get(IF_PFLASH, 0, 0);
 155    /* Numonyx JS28F256J3F105 */
 156    pflash_cfi01_register(flash_base, "milkymist.flash", flash_size,
 157                          dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
 158                          flash_sector_size, 2, 0x00, 0x89, 0x00, 0x1d, 1);
 159
 160    /* create irq lines */
 161    env->pic_state = lm32_pic_init(qemu_allocate_irq(cpu_irq_handler, cpu, 0));
 162    for (i = 0; i < 32; i++) {
 163        irq[i] = qdev_get_gpio_in(env->pic_state, i);
 164    }
 165
 166    /* load bios rom */
 167    bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
 168
 169    if (bios_filename) {
 170        if (load_image_targphys(bios_filename, BIOS_OFFSET, BIOS_SIZE) < 0) {
 171            error_report("could not load bios '%s'", bios_filename);
 172            exit(1);
 173        }
 174    }
 175
 176    reset_info->bootstrap_pc = BIOS_OFFSET;
 177
 178    /* if no kernel is given no valid bios rom is a fatal error */
 179    if (!kernel_filename && !dinfo && !bios_filename && !qtest_enabled()) {
 180        error_report("could not load Milkymist One bios '%s'", bios_name);
 181        exit(1);
 182    }
 183    g_free(bios_filename);
 184
 185    milkymist_uart_create(0x60000000, irq[0], serial_hd(0));
 186    milkymist_sysctl_create(0x60001000, irq[1], irq[2], irq[3],
 187            80000000, 0x10014d31, 0x0000041f, 0x00000001);
 188    milkymist_hpdmc_create(0x60002000);
 189    milkymist_vgafb_create(0x60003000, 0x40000000, 0x0fffffff);
 190    milkymist_memcard_create(0x60004000);
 191    milkymist_ac97_create(0x60005000, irq[4], irq[5], irq[6], irq[7]);
 192    milkymist_pfpu_create(0x60006000, irq[8]);
 193    if (machine->enable_graphics) {
 194        milkymist_tmu2_create(0x60007000, irq[9]);
 195    }
 196    milkymist_minimac2_create(0x60008000, 0x30000000, irq[10], irq[11]);
 197    milkymist_softusb_create(0x6000f000, irq[15],
 198            0x20000000, 0x1000, 0x20020000, 0x2000);
 199
 200    /* make sure juart isn't the first chardev */
 201    env->juart_state = lm32_juart_init(serial_hd(1));
 202
 203    if (kernel_filename) {
 204        uint64_t entry;
 205
 206        /* Boots a kernel elf binary.  */
 207        kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
 208                               &entry, NULL, NULL, NULL,
 209                               1, EM_LATTICEMICO32, 0, 0);
 210        reset_info->bootstrap_pc = entry;
 211
 212        if (kernel_size < 0) {
 213            kernel_size = load_image_targphys(kernel_filename, sdram_base,
 214                                              machine->ram_size);
 215            reset_info->bootstrap_pc = sdram_base;
 216        }
 217
 218        if (kernel_size < 0) {
 219            error_report("could not load kernel '%s'", kernel_filename);
 220            exit(1);
 221        }
 222    }
 223
 224    if (kernel_cmdline && strlen(kernel_cmdline)) {
 225        pstrcpy_targphys("cmdline", cmdline_base, TARGET_PAGE_SIZE,
 226                kernel_cmdline);
 227        reset_info->cmdline_base = (uint32_t)cmdline_base;
 228    }
 229
 230    if (initrd_filename) {
 231        size_t initrd_size;
 232        initrd_size = load_image_targphys(initrd_filename, initrd_base,
 233                initrd_max);
 234        reset_info->initrd_base = (uint32_t)initrd_base;
 235        reset_info->initrd_size = (uint32_t)initrd_size;
 236    }
 237
 238    qemu_register_reset(main_cpu_reset, reset_info);
 239}
 240
 241static void milkymist_machine_init(MachineClass *mc)
 242{
 243    mc->desc = "Milkymist One";
 244    mc->init = milkymist_init;
 245    mc->default_cpu_type = LM32_CPU_TYPE_NAME("lm32-full");
 246    mc->default_ram_size = 128 * MiB;
 247    mc->default_ram_id = "milkymist.sdram";
 248}
 249
 250DEFINE_MACHINE("milkymist", milkymist_machine_init)
 251