r2d.c 10.2 KB
Newer Older
T
ths 已提交
1 2 3 4
/*
 * Renesas SH7751R R2D-PLUS emulation
 *
 * Copyright (c) 2007 Magnus Damm
A
aurel32 已提交
5
 * Copyright (c) 2008 Paul Mundt
T
ths 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

26 27
#include "hw/sysbus.h"
#include "hw/hw.h"
P
Paolo Bonzini 已提交
28
#include "hw/sh4/sh.h"
29
#include "hw/devices.h"
30
#include "sysemu/sysemu.h"
31 32
#include "hw/boards.h"
#include "hw/pci/pci.h"
P
Paolo Bonzini 已提交
33
#include "net/net.h"
34
#include "sh7750_regs.h"
35 36 37
#include "hw/ide.h"
#include "hw/loader.h"
#include "hw/usb.h"
P
Paolo Bonzini 已提交
38
#include "hw/block/flash.h"
39
#include "sysemu/blockdev.h"
40
#include "exec/address-spaces.h"
A
Aurelien Jarno 已提交
41 42 43

#define FLASH_BASE 0x00000000
#define FLASH_SIZE 0x02000000
T
ths 已提交
44 45 46 47

#define SDRAM_BASE 0x0c000000 /* Physical location of SDRAM: Area 3 */
#define SDRAM_SIZE 0x04000000

B
blueswir1 已提交
48 49
#define SM501_VRAM_SIZE 0x800000

A
Aurelien Jarno 已提交
50
#define BOOT_PARAMS_OFFSET 0x0010000
51
/* CONFIG_BOOT_LINK_OFFSET of Linux kernel */
A
Aurelien Jarno 已提交
52 53
#define LINUX_LOAD_OFFSET  0x0800000
#define INITRD_LOAD_OFFSET 0x1800000
54

55
#define PA_IRLMSK	0x00
A
aurel32 已提交
56 57 58 59 60 61
#define PA_POWOFF	0x30
#define PA_VERREG	0x32
#define PA_OUTPORT	0x36

typedef struct {
    uint16_t bcr;
62
    uint16_t irlmsk;
A
aurel32 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    uint16_t irlmon;
    uint16_t cfctl;
    uint16_t cfpow;
    uint16_t dispctl;
    uint16_t sdmpow;
    uint16_t rtcce;
    uint16_t pcicd;
    uint16_t voyagerrts;
    uint16_t cfrst;
    uint16_t admrts;
    uint16_t extrst;
    uint16_t cfcdintclr;
    uint16_t keyctlclr;
    uint16_t pad0;
    uint16_t pad1;
    uint16_t verreg;
    uint16_t inport;
    uint16_t outport;
    uint16_t bverreg;
82 83 84

/* output pin */
    qemu_irq irl;
A
Avi Kivity 已提交
85
    MemoryRegion iomem;
A
Anthony Liguori 已提交
86
} r2d_fpga_t;
A
aurel32 已提交
87

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
enum r2d_fpga_irq {
    PCI_INTD, CF_IDE, CF_CD, PCI_INTC, SM501, KEY, RTC_A, RTC_T,
    SDCARD, PCI_INTA, PCI_INTB, EXT, TP,
    NR_IRQS
};

static const struct { short irl; uint16_t msk; } irqtab[NR_IRQS] = {
    [CF_IDE]	= {  1, 1<<9 },
    [CF_CD]	= {  2, 1<<8 },
    [PCI_INTA]	= {  9, 1<<14 },
    [PCI_INTB]	= { 10, 1<<13 },
    [PCI_INTC]	= {  3, 1<<12 },
    [PCI_INTD]	= {  0, 1<<11 },
    [SM501]	= {  4, 1<<10 },
    [KEY]	= {  5, 1<<6 },
    [RTC_A]	= {  6, 1<<5 },
    [RTC_T]	= {  7, 1<<4 },
    [SDCARD]	= {  8, 1<<7 },
    [EXT]	= { 11, 1<<0 },
    [TP]	= { 12, 1<<15 },
};

A
Anthony Liguori 已提交
110
static void update_irl(r2d_fpga_t *fpga)
111 112 113 114 115 116 117 118 119 120 121
{
    int i, irl = 15;
    for (i = 0; i < NR_IRQS; i++)
        if (fpga->irlmon & fpga->irlmsk & irqtab[i].msk)
            if (irqtab[i].irl < irl)
                irl = irqtab[i].irl;
    qemu_set_irq(fpga->irl, irl ^ 15);
}

static void r2d_fpga_irq_set(void *opaque, int n, int level)
{
A
Anthony Liguori 已提交
122
    r2d_fpga_t *fpga = opaque;
123 124 125 126 127 128 129
    if (level)
        fpga->irlmon |= irqtab[n].msk;
    else
        fpga->irlmon &= ~irqtab[n].msk;
    update_irl(fpga);
}

A
Avi Kivity 已提交
130
static uint32_t r2d_fpga_read(void *opaque, hwaddr addr)
A
aurel32 已提交
131
{
A
Anthony Liguori 已提交
132
    r2d_fpga_t *s = opaque;
A
aurel32 已提交
133 134

    switch (addr) {
135 136
    case PA_IRLMSK:
        return s->irlmsk;
A
aurel32 已提交
137 138 139
    case PA_OUTPORT:
	return s->outport;
    case PA_POWOFF:
A
Aurelien Jarno 已提交
140
	return 0x00;
A
aurel32 已提交
141 142 143 144 145 146 147 148
    case PA_VERREG:
	return 0x10;
    }

    return 0;
}

static void
A
Avi Kivity 已提交
149
r2d_fpga_write(void *opaque, hwaddr addr, uint32_t value)
A
aurel32 已提交
150
{
A
Anthony Liguori 已提交
151
    r2d_fpga_t *s = opaque;
A
aurel32 已提交
152 153

    switch (addr) {
154 155 156 157
    case PA_IRLMSK:
        s->irlmsk = value;
        update_irl(s);
	break;
A
aurel32 已提交
158 159 160 161
    case PA_OUTPORT:
	s->outport = value;
	break;
    case PA_POWOFF:
A
Aurelien Jarno 已提交
162 163 164 165
        if (value & 1) {
            qemu_system_shutdown_request();
        }
        break;
A
aurel32 已提交
166 167 168 169 170 171
    case PA_VERREG:
	/* Discard writes */
	break;
    }
}

A
Avi Kivity 已提交
172 173 174 175 176 177
static const MemoryRegionOps r2d_fpga_ops = {
    .old_mmio = {
        .read = { r2d_fpga_read, r2d_fpga_read, NULL, },
        .write = { r2d_fpga_write, r2d_fpga_write, NULL, },
    },
    .endianness = DEVICE_NATIVE_ENDIAN,
A
aurel32 已提交
178 179
};

A
Avi Kivity 已提交
180
static qemu_irq *r2d_fpga_init(MemoryRegion *sysmem,
A
Avi Kivity 已提交
181
                               hwaddr base, qemu_irq irl)
A
aurel32 已提交
182
{
A
Anthony Liguori 已提交
183
    r2d_fpga_t *s;
A
aurel32 已提交
184

185
    s = g_malloc0(sizeof(r2d_fpga_t));
186 187

    s->irl = irl;
A
aurel32 已提交
188

189
    memory_region_init_io(&s->iomem, NULL, &r2d_fpga_ops, s, "r2d-fpga", 0x40);
A
Avi Kivity 已提交
190
    memory_region_add_subregion(sysmem, base, &s->iomem);
191
    return qemu_allocate_irqs(r2d_fpga_irq_set, s, NR_IRQS);
A
aurel32 已提交
192 193
}

A
Aurelien Jarno 已提交
194
typedef struct ResetData {
195
    SuperHCPU *cpu;
A
Aurelien Jarno 已提交
196 197 198 199 200 201
    uint32_t vector;
} ResetData;

static void main_cpu_reset(void *opaque)
{
    ResetData *s = (ResetData *)opaque;
202
    CPUSH4State *env = &s->cpu->env;
A
Aurelien Jarno 已提交
203

204
    cpu_reset(CPU(s->cpu));
A
Aurelien Jarno 已提交
205 206 207
    env->pc = s->vector;
}

208
static struct QEMU_PACKED
A
Aurelien Jarno 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221
{
    int mount_root_rdonly;
    int ramdisk_flags;
    int orig_root_dev;
    int loader_type;
    int initrd_start;
    int initrd_size;

    char pad[232];

    char kernel_cmdline[256];
} boot_params;

222
static void r2d_init(MachineState *machine)
T
ths 已提交
223
{
224 225 226 227
    const char *cpu_model = machine->cpu_model;
    const char *kernel_filename = machine->kernel_filename;
    const char *kernel_cmdline = machine->kernel_cmdline;
    const char *initrd_filename = machine->initrd_filename;
228
    SuperHCPU *cpu;
A
Andreas Färber 已提交
229
    CPUSH4State *env;
A
Aurelien Jarno 已提交
230
    ResetData *reset_info;
T
ths 已提交
231
    struct SH7750State *s;
A
Avi Kivity 已提交
232
    MemoryRegion *sdram = g_new(MemoryRegion, 1);
233
    qemu_irq *irq;
G
Gerd Hoffmann 已提交
234
    DriveInfo *dinfo;
B
balrog 已提交
235
    int i;
236 237
    DeviceState *dev;
    SysBusDevice *busdev;
238
    MemoryRegion *address_space_mem = get_system_memory();
239
    PCIBus *pci_bus;
T
ths 已提交
240

241
    if (cpu_model == NULL) {
A
aurel32 已提交
242
        cpu_model = "SH7751R";
243
    }
B
bellard 已提交
244

245 246
    cpu = cpu_sh4_init(cpu_model);
    if (cpu == NULL) {
B
bellard 已提交
247 248 249
        fprintf(stderr, "Unable to find CPU definition\n");
        exit(1);
    }
250 251
    env = &cpu->env;

252
    reset_info = g_malloc0(sizeof(ResetData));
253
    reset_info->cpu = cpu;
A
Aurelien Jarno 已提交
254 255
    reset_info->vector = env->pc;
    qemu_register_reset(main_cpu_reset, reset_info);
T
ths 已提交
256 257

    /* Allocate memory space */
258
    memory_region_init_ram(sdram, NULL, "r2d.sdram", SDRAM_SIZE, &error_abort);
259
    vmstate_register_ram_global(sdram);
A
Avi Kivity 已提交
260
    memory_region_add_subregion(address_space_mem, SDRAM_BASE, sdram);
T
ths 已提交
261
    /* Register peripherals */
262
    s = sh7750_init(cpu, address_space_mem);
A
Avi Kivity 已提交
263
    irq = r2d_fpga_init(address_space_mem, 0x04000000, sh7750_irl(s));
264 265

    dev = qdev_create(NULL, "sh_pci");
266
    busdev = SYS_BUS_DEVICE(dev);
267
    qdev_init_nofail(dev);
268
    pci_bus = PCI_BUS(qdev_get_child_bus(dev, "pci"));
269 270 271 272 273 274
    sysbus_mmio_map(busdev, 0, P4ADDR(0x1e200000));
    sysbus_mmio_map(busdev, 1, A7ADDR(0x1e200000));
    sysbus_connect_irq(busdev, 0, irq[PCI_INTA]);
    sysbus_connect_irq(busdev, 1, irq[PCI_INTB]);
    sysbus_connect_irq(busdev, 2, irq[PCI_INTC]);
    sysbus_connect_irq(busdev, 3, irq[PCI_INTD]);
275

276 277
    sm501_init(address_space_mem, 0x10000000, SM501_VRAM_SIZE,
               irq[SM501], serial_hds[2]);
278 279

    /* onboard CF (True IDE mode, Master only). */
280
    dinfo = drive_get(IF_IDE, 0, 0);
281 282 283 284 285 286 287 288
    dev = qdev_create(NULL, "mmio-ide");
    busdev = SYS_BUS_DEVICE(dev);
    sysbus_connect_irq(busdev, 0, irq[CF_IDE]);
    qdev_prop_set_uint32(dev, "shift", 1);
    qdev_init_nofail(dev);
    sysbus_mmio_map(busdev, 0, 0x14001000);
    sysbus_mmio_map(busdev, 1, 0x1400080c);
    mmio_ide_init_drives(dev, dinfo, NULL);
289

A
Aurelien Jarno 已提交
290
    /* onboard flash memory */
A
Aurelien Jarno 已提交
291
    dinfo = drive_get(IF_PFLASH, 0, 0);
292
    pflash_cfi02_register(0x0, NULL, "r2d.flash", FLASH_SIZE,
293 294 295
                          dinfo ? dinfo->bdrv : NULL, (16 * 1024),
                          FLASH_SIZE >> 16,
                          1, 4, 0x0000, 0x0000, 0x0000, 0x0000,
296
                          0x555, 0x2aa, 0);
A
Aurelien Jarno 已提交
297

B
balrog 已提交
298
    /* NIC: rtl8139 on-board, and 2 slots. */
A
aurel32 已提交
299
    for (i = 0; i < nb_nics; i++)
300 301
        pci_nic_init_nofail(&nd_table[i], pci_bus,
                            "rtl8139", i==0 ? "2" : NULL);
B
balrog 已提交
302

A
Aurelien Jarno 已提交
303 304 305
    /* USB keyboard */
    usbdevice_create("keyboard");

T
ths 已提交
306
    /* Todo: register on board registers */
A
Aurelien Jarno 已提交
307 308
    memset(&boot_params, 0, sizeof(boot_params));

309
    if (kernel_filename) {
A
Aurelien Jarno 已提交
310 311 312 313 314 315 316 317 318 319 320
        int kernel_size;

        kernel_size = load_image_targphys(kernel_filename,
                                          SDRAM_BASE + LINUX_LOAD_OFFSET,
                                          INITRD_LOAD_OFFSET - LINUX_LOAD_OFFSET);
        if (kernel_size < 0) {
          fprintf(stderr, "qemu: could not load kernel '%s'\n", kernel_filename);
          exit(1);
        }

        /* initialization which should be done by firmware */
321
        stl_phys(&address_space_memory, SH7750_BCR1, 1<<3); /* cs3 SDRAM */
322
        stw_phys(&address_space_memory, SH7750_BCR2, 3<<(3*2)); /* cs3 32bit */
A
Aurelien Jarno 已提交
323
        reset_info->vector = (SDRAM_BASE + LINUX_LOAD_OFFSET) | 0xa0000000; /* Start from P2 area */
T
ths 已提交
324
    }
A
Aurelien Jarno 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344

    if (initrd_filename) {
        int initrd_size;

        initrd_size = load_image_targphys(initrd_filename,
                                          SDRAM_BASE + INITRD_LOAD_OFFSET,
                                          SDRAM_SIZE - INITRD_LOAD_OFFSET);

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

        /* initialization which should be done by firmware */
        boot_params.loader_type = 1;
        boot_params.initrd_start = INITRD_LOAD_OFFSET;
        boot_params.initrd_size = initrd_size;
    }

    if (kernel_cmdline) {
345 346
        /* I see no evidence that this .kernel_cmdline buffer requires
           NUL-termination, so using strncpy should be ok. */
A
Aurelien Jarno 已提交
347 348 349 350 351 352
        strncpy(boot_params.kernel_cmdline, kernel_cmdline,
                sizeof(boot_params.kernel_cmdline));
    }

    rom_add_blob_fixed("boot_params", &boot_params, sizeof(boot_params),
                       SDRAM_BASE + BOOT_PARAMS_OFFSET);
T
ths 已提交
353 354
}

355
static QEMUMachine r2d_machine = {
356 357 358
    .name = "r2d",
    .desc = "r2d-plus board",
    .init = r2d_init,
T
ths 已提交
359
};
360 361 362 363 364 365 366

static void r2d_machine_init(void)
{
    qemu_register_machine(&r2d_machine);
}

machine_init(r2d_machine_init);