2007-09-30 03:24:41 +08:00
|
|
|
/*
|
|
|
|
* Renesas SH7751R R2D-PLUS emulation
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007 Magnus Damm
|
2008-09-03 00:18:38 +08:00
|
|
|
* Copyright (c) 2008 Paul Mundt
|
2007-09-30 03:24:41 +08:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2011-01-20 01:23:59 +08:00
|
|
|
#include "sysbus.h"
|
2007-11-18 01:14:51 +08:00
|
|
|
#include "hw.h"
|
|
|
|
#include "sh.h"
|
2008-11-06 04:24:35 +08:00
|
|
|
#include "devices.h"
|
2007-11-18 01:14:51 +08:00
|
|
|
#include "sysemu.h"
|
|
|
|
#include "boards.h"
|
2008-12-08 03:20:43 +08:00
|
|
|
#include "pci.h"
|
|
|
|
#include "net.h"
|
|
|
|
#include "sh7750_regs.h"
|
2009-08-20 21:22:22 +08:00
|
|
|
#include "ide.h"
|
2009-09-20 22:58:02 +08:00
|
|
|
#include "loader.h"
|
2010-04-02 18:16:04 +08:00
|
|
|
#include "usb.h"
|
2010-04-11 09:58:19 +08:00
|
|
|
#include "flash.h"
|
2010-08-24 23:22:24 +08:00
|
|
|
#include "blockdev.h"
|
2010-04-11 09:58:19 +08:00
|
|
|
|
|
|
|
#define FLASH_BASE 0x00000000
|
|
|
|
#define FLASH_SIZE 0x02000000
|
2007-09-30 03:24:41 +08:00
|
|
|
|
|
|
|
#define SDRAM_BASE 0x0c000000 /* Physical location of SDRAM: Area 3 */
|
|
|
|
#define SDRAM_SIZE 0x04000000
|
|
|
|
|
2008-11-06 04:24:35 +08:00
|
|
|
#define SM501_VRAM_SIZE 0x800000
|
|
|
|
|
2010-04-12 01:20:32 +08:00
|
|
|
#define BOOT_PARAMS_OFFSET 0x0010000
|
2009-03-29 07:14:32 +08:00
|
|
|
/* CONFIG_BOOT_LINK_OFFSET of Linux kernel */
|
2010-04-12 01:20:32 +08:00
|
|
|
#define LINUX_LOAD_OFFSET 0x0800000
|
|
|
|
#define INITRD_LOAD_OFFSET 0x1800000
|
2009-03-29 07:14:32 +08:00
|
|
|
|
2008-12-08 02:59:57 +08:00
|
|
|
#define PA_IRLMSK 0x00
|
2008-09-03 00:18:38 +08:00
|
|
|
#define PA_POWOFF 0x30
|
|
|
|
#define PA_VERREG 0x32
|
|
|
|
#define PA_OUTPORT 0x36
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint16_t bcr;
|
2008-12-08 02:59:57 +08:00
|
|
|
uint16_t irlmsk;
|
2008-09-03 00:18:38 +08:00
|
|
|
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;
|
2008-12-08 02:59:57 +08:00
|
|
|
|
|
|
|
/* output pin */
|
|
|
|
qemu_irq irl;
|
2009-10-02 05:12:16 +08:00
|
|
|
} r2d_fpga_t;
|
2008-09-03 00:18:38 +08:00
|
|
|
|
2008-12-08 02:59:57 +08:00
|
|
|
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 },
|
|
|
|
};
|
|
|
|
|
2009-10-02 05:12:16 +08:00
|
|
|
static void update_irl(r2d_fpga_t *fpga)
|
2008-12-08 02:59:57 +08:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2009-10-02 05:12:16 +08:00
|
|
|
r2d_fpga_t *fpga = opaque;
|
2008-12-08 02:59:57 +08:00
|
|
|
if (level)
|
|
|
|
fpga->irlmon |= irqtab[n].msk;
|
|
|
|
else
|
|
|
|
fpga->irlmon &= ~irqtab[n].msk;
|
|
|
|
update_irl(fpga);
|
|
|
|
}
|
|
|
|
|
2009-10-02 05:12:16 +08:00
|
|
|
static uint32_t r2d_fpga_read(void *opaque, target_phys_addr_t addr)
|
2008-09-03 00:18:38 +08:00
|
|
|
{
|
2009-10-02 05:12:16 +08:00
|
|
|
r2d_fpga_t *s = opaque;
|
2008-09-03 00:18:38 +08:00
|
|
|
|
|
|
|
switch (addr) {
|
2008-12-08 02:59:57 +08:00
|
|
|
case PA_IRLMSK:
|
|
|
|
return s->irlmsk;
|
2008-09-03 00:18:38 +08:00
|
|
|
case PA_OUTPORT:
|
|
|
|
return s->outport;
|
|
|
|
case PA_POWOFF:
|
2010-01-31 03:41:33 +08:00
|
|
|
return 0x00;
|
2008-09-03 00:18:38 +08:00
|
|
|
case PA_VERREG:
|
|
|
|
return 0x10;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-10-02 05:12:16 +08:00
|
|
|
r2d_fpga_write(void *opaque, target_phys_addr_t addr, uint32_t value)
|
2008-09-03 00:18:38 +08:00
|
|
|
{
|
2009-10-02 05:12:16 +08:00
|
|
|
r2d_fpga_t *s = opaque;
|
2008-09-03 00:18:38 +08:00
|
|
|
|
|
|
|
switch (addr) {
|
2008-12-08 02:59:57 +08:00
|
|
|
case PA_IRLMSK:
|
|
|
|
s->irlmsk = value;
|
|
|
|
update_irl(s);
|
|
|
|
break;
|
2008-09-03 00:18:38 +08:00
|
|
|
case PA_OUTPORT:
|
|
|
|
s->outport = value;
|
|
|
|
break;
|
|
|
|
case PA_POWOFF:
|
2010-01-31 03:41:33 +08:00
|
|
|
if (value & 1) {
|
|
|
|
qemu_system_shutdown_request();
|
|
|
|
}
|
|
|
|
break;
|
2008-09-03 00:18:38 +08:00
|
|
|
case PA_VERREG:
|
|
|
|
/* Discard writes */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-26 02:29:31 +08:00
|
|
|
static CPUReadMemoryFunc * const r2d_fpga_readfn[] = {
|
2008-09-03 00:18:38 +08:00
|
|
|
r2d_fpga_read,
|
|
|
|
r2d_fpga_read,
|
2008-09-03 07:26:23 +08:00
|
|
|
NULL,
|
2008-09-03 00:18:38 +08:00
|
|
|
};
|
|
|
|
|
2009-08-26 02:29:31 +08:00
|
|
|
static CPUWriteMemoryFunc * const r2d_fpga_writefn[] = {
|
2008-09-03 00:18:38 +08:00
|
|
|
r2d_fpga_write,
|
|
|
|
r2d_fpga_write,
|
2008-09-03 07:26:23 +08:00
|
|
|
NULL,
|
2008-09-03 00:18:38 +08:00
|
|
|
};
|
|
|
|
|
2009-10-02 05:12:16 +08:00
|
|
|
static qemu_irq *r2d_fpga_init(target_phys_addr_t base, qemu_irq irl)
|
2008-09-03 00:18:38 +08:00
|
|
|
{
|
|
|
|
int iomemtype;
|
2009-10-02 05:12:16 +08:00
|
|
|
r2d_fpga_t *s;
|
2008-09-03 00:18:38 +08:00
|
|
|
|
2011-08-21 11:09:37 +08:00
|
|
|
s = g_malloc0(sizeof(r2d_fpga_t));
|
2008-12-08 02:59:57 +08:00
|
|
|
|
|
|
|
s->irl = irl;
|
2008-09-03 00:18:38 +08:00
|
|
|
|
2009-06-14 16:38:51 +08:00
|
|
|
iomemtype = cpu_register_io_memory(r2d_fpga_readfn,
|
2010-12-08 19:05:37 +08:00
|
|
|
r2d_fpga_writefn, s,
|
|
|
|
DEVICE_NATIVE_ENDIAN);
|
2008-09-03 00:18:38 +08:00
|
|
|
cpu_register_physical_memory(base, 0x40, iomemtype);
|
2008-12-08 02:59:57 +08:00
|
|
|
return qemu_allocate_irqs(r2d_fpga_irq_set, s, NR_IRQS);
|
2008-09-03 00:18:38 +08:00
|
|
|
}
|
|
|
|
|
2011-01-15 03:39:18 +08:00
|
|
|
typedef struct ResetData {
|
|
|
|
CPUState *env;
|
|
|
|
uint32_t vector;
|
|
|
|
} ResetData;
|
|
|
|
|
|
|
|
static void main_cpu_reset(void *opaque)
|
|
|
|
{
|
|
|
|
ResetData *s = (ResetData *)opaque;
|
|
|
|
CPUState *env = s->env;
|
|
|
|
|
|
|
|
cpu_reset(env);
|
|
|
|
env->pc = s->vector;
|
|
|
|
}
|
|
|
|
|
2010-04-12 01:20:32 +08:00
|
|
|
static struct __attribute__((__packed__))
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2009-10-02 05:12:16 +08:00
|
|
|
static void r2d_init(ram_addr_t ram_size,
|
2009-01-17 03:04:14 +08:00
|
|
|
const char *boot_device,
|
2007-09-30 03:24:41 +08:00
|
|
|
const char *kernel_filename, const char *kernel_cmdline,
|
|
|
|
const char *initrd_filename, const char *cpu_model)
|
|
|
|
{
|
|
|
|
CPUState *env;
|
2011-01-15 03:39:18 +08:00
|
|
|
ResetData *reset_info;
|
2007-09-30 03:24:41 +08:00
|
|
|
struct SH7750State *s;
|
2009-10-02 05:12:16 +08:00
|
|
|
ram_addr_t sdram_addr;
|
2008-12-08 02:59:57 +08:00
|
|
|
qemu_irq *irq;
|
2009-07-22 22:42:57 +08:00
|
|
|
DriveInfo *dinfo;
|
2008-12-08 03:20:43 +08:00
|
|
|
int i;
|
2007-09-30 03:24:41 +08:00
|
|
|
|
2007-11-10 23:15:54 +08:00
|
|
|
if (!cpu_model)
|
2008-09-03 00:18:28 +08:00
|
|
|
cpu_model = "SH7751R";
|
2007-11-10 23:15:54 +08:00
|
|
|
|
|
|
|
env = cpu_init(cpu_model);
|
|
|
|
if (!env) {
|
|
|
|
fprintf(stderr, "Unable to find CPU definition\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-08-21 11:09:37 +08:00
|
|
|
reset_info = g_malloc0(sizeof(ResetData));
|
2011-01-15 03:39:18 +08:00
|
|
|
reset_info->env = env;
|
|
|
|
reset_info->vector = env->pc;
|
|
|
|
qemu_register_reset(main_cpu_reset, reset_info);
|
2007-09-30 03:24:41 +08:00
|
|
|
|
|
|
|
/* Allocate memory space */
|
2010-06-26 01:09:35 +08:00
|
|
|
sdram_addr = qemu_ram_alloc(NULL, "r2d.sdram", SDRAM_SIZE);
|
2008-11-06 04:24:35 +08:00
|
|
|
cpu_register_physical_memory(SDRAM_BASE, SDRAM_SIZE, sdram_addr);
|
2007-09-30 03:24:41 +08:00
|
|
|
/* Register peripherals */
|
|
|
|
s = sh7750_init(env);
|
2008-12-08 02:59:57 +08:00
|
|
|
irq = r2d_fpga_init(0x04000000, sh7750_irl(s));
|
2011-01-20 01:23:59 +08:00
|
|
|
sysbus_create_varargs("sh_pci", 0x1e200000, irq[PCI_INTA], irq[PCI_INTB],
|
|
|
|
irq[PCI_INTC], irq[PCI_INTD], NULL);
|
2008-12-08 02:59:57 +08:00
|
|
|
|
2009-04-19 17:15:50 +08:00
|
|
|
sm501_init(0x10000000, SM501_VRAM_SIZE, irq[SM501], serial_hds[2]);
|
2008-12-08 02:41:42 +08:00
|
|
|
|
|
|
|
/* onboard CF (True IDE mode, Master only). */
|
2010-04-12 04:27:23 +08:00
|
|
|
dinfo = drive_get(IF_IDE, 0, 0);
|
|
|
|
mmio_ide_init(0x14001000, 0x1400080c, irq[CF_IDE], 1,
|
|
|
|
dinfo, NULL);
|
2008-12-08 02:41:42 +08:00
|
|
|
|
2010-04-11 09:58:19 +08:00
|
|
|
/* onboard flash memory */
|
2010-06-18 04:19:53 +08:00
|
|
|
dinfo = drive_get(IF_PFLASH, 0, 0);
|
2010-06-26 01:09:35 +08:00
|
|
|
pflash_cfi02_register(0x0, qemu_ram_alloc(NULL, "r2d.flash", FLASH_SIZE),
|
2010-04-12 04:27:23 +08:00
|
|
|
dinfo ? dinfo->bdrv : NULL, (16 * 1024),
|
|
|
|
FLASH_SIZE >> 16,
|
|
|
|
1, 4, 0x0000, 0x0000, 0x0000, 0x0000,
|
|
|
|
0x555, 0x2aa, 0);
|
2010-04-11 09:58:19 +08:00
|
|
|
|
2008-12-08 03:20:43 +08:00
|
|
|
/* NIC: rtl8139 on-board, and 2 slots. */
|
2009-03-03 14:23:17 +08:00
|
|
|
for (i = 0; i < nb_nics; i++)
|
2009-09-25 09:53:51 +08:00
|
|
|
pci_nic_init_nofail(&nd_table[i], "rtl8139", i==0 ? "2" : NULL);
|
2008-12-08 03:20:43 +08:00
|
|
|
|
2010-04-02 18:16:04 +08:00
|
|
|
/* USB keyboard */
|
|
|
|
usbdevice_create("keyboard");
|
|
|
|
|
2007-09-30 03:24:41 +08:00
|
|
|
/* Todo: register on board registers */
|
2010-04-12 01:20:32 +08:00
|
|
|
memset(&boot_params, 0, sizeof(boot_params));
|
|
|
|
|
2009-03-29 07:14:32 +08:00
|
|
|
if (kernel_filename) {
|
2010-04-12 01:20:32 +08:00
|
|
|
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 */
|
|
|
|
stl_phys(SH7750_BCR1, 1<<3); /* cs3 SDRAM */
|
|
|
|
stw_phys(SH7750_BCR2, 3<<(3*2)); /* cs3 32bit */
|
2011-01-15 03:39:18 +08:00
|
|
|
reset_info->vector = (SDRAM_BASE + LINUX_LOAD_OFFSET) | 0xa0000000; /* Start from P2 area */
|
2007-09-30 03:24:41 +08:00
|
|
|
}
|
2010-04-12 01:20:32 +08:00
|
|
|
|
|
|
|
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) {
|
|
|
|
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);
|
2007-09-30 03:24:41 +08:00
|
|
|
}
|
|
|
|
|
2009-05-21 07:38:09 +08:00
|
|
|
static QEMUMachine r2d_machine = {
|
2008-10-08 04:34:35 +08:00
|
|
|
.name = "r2d",
|
|
|
|
.desc = "r2d-plus board",
|
|
|
|
.init = r2d_init,
|
2007-09-30 03:24:41 +08:00
|
|
|
};
|
2009-05-21 07:38:09 +08:00
|
|
|
|
|
|
|
static void r2d_machine_init(void)
|
|
|
|
{
|
|
|
|
qemu_register_machine(&r2d_machine);
|
|
|
|
}
|
|
|
|
|
|
|
|
machine_init(r2d_machine_init);
|