blob: 3da3e70723c60391a3a8a6a55cb2549da6c76be2 [file] [log] [blame]
thse16fe402006-12-06 21:38:37 +00001/*
2 * QEMU/MIPS pseudo-board
3 *
4 * emulates a simple machine with ISA-like bus.
5 * ISA IO space mapped to the 0x14000000 (PHYS) and
6 * ISA memory at the 0x10000000 (PHYS, 16Mb in size).
7 * All peripherial devices are attached to this "bus" with
8 * the standard PC ISA addresses.
9*/
pbrook87ecb682007-11-17 17:14:51 +000010#include "hw.h"
11#include "mips.h"
12#include "pc.h"
13#include "isa.h"
14#include "net.h"
15#include "sysemu.h"
16#include "boards.h"
thsb305b5b2008-04-20 06:28:28 +000017#include "flash.h"
blueswir13b3fb322008-10-04 07:20:07 +000018#include "qemu-log.h"
bellard6af0bf92005-07-02 14:58:51 +000019
ths2909b292007-01-06 02:24:15 +000020#ifdef TARGET_WORDS_BIGENDIAN
bellard6af0bf92005-07-02 14:58:51 +000021#define BIOS_FILENAME "mips_bios.bin"
thsf7bcd4e2007-01-06 01:37:51 +000022#else
23#define BIOS_FILENAME "mipsel_bios.bin"
24#endif
ths44cbbf12007-01-24 22:00:13 +000025
pbrookc6ee6072007-11-11 12:02:33 +000026#define PHYS_TO_VIRT(x) ((x) | ~(target_ulong)0x7fffffff)
bellard6af0bf92005-07-02 14:58:51 +000027
ths5dc4b742006-12-21 13:48:28 +000028#define VIRT_TO_PHYS_ADDEND (-((int64_t)(int32_t)0x80000000))
bellard66a93e02006-04-26 22:06:55 +000029
thse4bcb142007-12-02 04:51:10 +000030#define MAX_IDE_BUS 2
31
pbrook58126402006-10-29 15:38:28 +000032static const int ide_iobase[2] = { 0x1f0, 0x170 };
33static const int ide_iobase2[2] = { 0x3f6, 0x376 };
34static const int ide_irq[2] = { 14, 15 };
35
thseddbd282006-12-23 00:23:19 +000036static int serial_io[MAX_SERIAL_PORTS] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
37static int serial_irq[MAX_SERIAL_PORTS] = { 4, 3, 4, 3 };
38
thse16fe402006-12-06 21:38:37 +000039static PITState *pit; /* PIT i8254 */
bellard697584a2005-08-21 09:41:56 +000040
ths1b660742007-12-07 01:13:37 +000041/* i8254 PIT is attached to the IRQ0 at PIC i8259 */
bellard6af0bf92005-07-02 14:58:51 +000042
ths7df526e2007-11-09 17:52:11 +000043static struct _loaderparams {
44 int ram_size;
45 const char *kernel_filename;
46 const char *kernel_cmdline;
47 const char *initrd_filename;
48} loaderparams;
49
ths6ae81772006-12-06 17:48:52 +000050static void mips_qemu_writel (void *opaque, target_phys_addr_t addr,
51 uint32_t val)
52{
53 if ((addr & 0xffff) == 0 && val == 42)
54 qemu_system_reset_request ();
55 else if ((addr & 0xffff) == 4 && val == 42)
56 qemu_system_shutdown_request ();
57}
58
59static uint32_t mips_qemu_readl (void *opaque, target_phys_addr_t addr)
60{
61 return 0;
62}
63
64static CPUWriteMemoryFunc *mips_qemu_write[] = {
65 &mips_qemu_writel,
66 &mips_qemu_writel,
67 &mips_qemu_writel,
68};
69
70static CPUReadMemoryFunc *mips_qemu_read[] = {
71 &mips_qemu_readl,
72 &mips_qemu_readl,
73 &mips_qemu_readl,
74};
75
76static int mips_qemu_iomemtype = 0;
77
ths7df526e2007-11-09 17:52:11 +000078static void load_kernel (CPUState *env)
ths6ae81772006-12-06 17:48:52 +000079{
ths74287112007-04-01 17:56:37 +000080 int64_t entry, kernel_low, kernel_high;
ths6ae81772006-12-06 17:48:52 +000081 long kernel_size, initrd_size;
ths74287112007-04-01 17:56:37 +000082 ram_addr_t initrd_offset;
ths6ae81772006-12-06 17:48:52 +000083
ths7df526e2007-11-09 17:52:11 +000084 kernel_size = load_elf(loaderparams.kernel_filename, VIRT_TO_PHYS_ADDEND,
blueswir1b55266b2008-09-20 08:07:15 +000085 (uint64_t *)&entry, (uint64_t *)&kernel_low,
86 (uint64_t *)&kernel_high);
thsc570fd12006-12-21 01:19:56 +000087 if (kernel_size >= 0) {
88 if ((entry & ~0x7fffffffULL) == 0x80000000)
ths5dc4b742006-12-21 13:48:28 +000089 entry = (int32_t)entry;
thsb5dc7732008-06-27 10:02:35 +000090 env->active_tc.PC = entry;
thsc570fd12006-12-21 01:19:56 +000091 } else {
ths9042c0e2006-12-23 14:18:40 +000092 fprintf(stderr, "qemu: could not load kernel '%s'\n",
ths7df526e2007-11-09 17:52:11 +000093 loaderparams.kernel_filename);
ths9042c0e2006-12-23 14:18:40 +000094 exit(1);
ths6ae81772006-12-06 17:48:52 +000095 }
96
97 /* load initrd */
98 initrd_size = 0;
ths74287112007-04-01 17:56:37 +000099 initrd_offset = 0;
ths7df526e2007-11-09 17:52:11 +0000100 if (loaderparams.initrd_filename) {
101 initrd_size = get_image_size (loaderparams.initrd_filename);
ths74287112007-04-01 17:56:37 +0000102 if (initrd_size > 0) {
103 initrd_offset = (kernel_high + ~TARGET_PAGE_MASK) & TARGET_PAGE_MASK;
104 if (initrd_offset + initrd_size > ram_size) {
105 fprintf(stderr,
106 "qemu: memory too small for initial ram disk '%s'\n",
ths7df526e2007-11-09 17:52:11 +0000107 loaderparams.initrd_filename);
ths74287112007-04-01 17:56:37 +0000108 exit(1);
109 }
pbrookdcac9672009-04-09 20:05:49 +0000110 initrd_size = load_image_targphys(loaderparams.initrd_filename,
111 initrd_offset,
112 ram_size - initrd_offset);
ths74287112007-04-01 17:56:37 +0000113 }
ths6ae81772006-12-06 17:48:52 +0000114 if (initrd_size == (target_ulong) -1) {
115 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
ths7df526e2007-11-09 17:52:11 +0000116 loaderparams.initrd_filename);
ths6ae81772006-12-06 17:48:52 +0000117 exit(1);
118 }
119 }
120
121 /* Store command line. */
122 if (initrd_size > 0) {
123 int ret;
blueswir1b55266b2008-09-20 08:07:15 +0000124 ret = sprintf((char *)(phys_ram_base + (16 << 20) - 256),
ths3594c772007-02-20 23:37:21 +0000125 "rd_start=0x" TARGET_FMT_lx " rd_size=%li ",
ths74287112007-04-01 17:56:37 +0000126 PHYS_TO_VIRT((uint32_t)initrd_offset),
ths6ae81772006-12-06 17:48:52 +0000127 initrd_size);
blueswir1b55266b2008-09-20 08:07:15 +0000128 strcpy ((char *)(phys_ram_base + (16 << 20) - 256 + ret),
ths7df526e2007-11-09 17:52:11 +0000129 loaderparams.kernel_cmdline);
ths6ae81772006-12-06 17:48:52 +0000130 }
131 else {
blueswir1b55266b2008-09-20 08:07:15 +0000132 strcpy ((char *)(phys_ram_base + (16 << 20) - 256),
ths7df526e2007-11-09 17:52:11 +0000133 loaderparams.kernel_cmdline);
ths6ae81772006-12-06 17:48:52 +0000134 }
135
ths44cbbf12007-01-24 22:00:13 +0000136 *(int32_t *)(phys_ram_base + (16 << 20) - 260) = tswap32 (0x12345678);
137 *(int32_t *)(phys_ram_base + (16 << 20) - 264) = tswap32 (ram_size);
ths6ae81772006-12-06 17:48:52 +0000138}
139
140static void main_cpu_reset(void *opaque)
141{
142 CPUState *env = opaque;
143 cpu_reset(env);
144
ths7df526e2007-11-09 17:52:11 +0000145 if (loaderparams.kernel_filename)
146 load_kernel (env);
ths6ae81772006-12-06 17:48:52 +0000147}
bellard66a93e02006-04-26 22:06:55 +0000148
thsb305b5b2008-04-20 06:28:28 +0000149static const int sector_len = 32 * 1024;
ths70705262007-02-18 00:10:59 +0000150static
aurel3200f82b82008-04-27 21:12:55 +0000151void mips_r4k_init (ram_addr_t ram_size, int vga_ram_size,
aliguori3023f332009-01-16 19:04:14 +0000152 const char *boot_device,
bellard6af0bf92005-07-02 14:58:51 +0000153 const char *kernel_filename, const char *kernel_cmdline,
j_mayer94fc95c2007-03-05 19:44:02 +0000154 const char *initrd_filename, const char *cpu_model)
bellard6af0bf92005-07-02 14:58:51 +0000155{
156 char buf[1024];
pbrookdcac9672009-04-09 20:05:49 +0000157 ram_addr_t ram_offset;
158 ram_addr_t vga_ram_offset;
159 ram_addr_t bios_offset;
thsf7bcd4e2007-01-06 01:37:51 +0000160 int bios_size;
bellardc68ea702005-11-21 23:33:12 +0000161 CPUState *env;
ths153a08d2007-03-17 15:21:30 +0000162 RTCState *rtc_state;
pbrook58126402006-10-29 15:38:28 +0000163 int i;
pbrookd537cf62007-04-07 18:14:41 +0000164 qemu_irq *i8259;
thse4bcb142007-12-02 04:51:10 +0000165 int index;
166 BlockDriverState *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
bellardc68ea702005-11-21 23:33:12 +0000167
ths33d68b52007-03-18 00:30:29 +0000168 /* init CPUs */
169 if (cpu_model == NULL) {
ths60aa19a2007-04-01 12:36:18 +0000170#ifdef TARGET_MIPS64
ths33d68b52007-03-18 00:30:29 +0000171 cpu_model = "R4000";
172#else
ths1c32f432007-04-28 21:07:41 +0000173 cpu_model = "24Kf";
ths33d68b52007-03-18 00:30:29 +0000174#endif
175 }
bellardaaed9092007-11-10 15:15:54 +0000176 env = cpu_init(cpu_model);
177 if (!env) {
178 fprintf(stderr, "Unable to find CPU definition\n");
179 exit(1);
180 }
ths6ae81772006-12-06 17:48:52 +0000181 qemu_register_reset(main_cpu_reset, env);
bellardc68ea702005-11-21 23:33:12 +0000182
bellard6af0bf92005-07-02 14:58:51 +0000183 /* allocate RAM */
aurel320ccff152009-01-24 15:07:25 +0000184 if (ram_size > (256 << 20)) {
185 fprintf(stderr,
186 "qemu: Too much memory for this machine: %d MB, maximum 256 MB\n",
187 ((unsigned int)ram_size / (1 << 20)));
188 exit(1);
189 }
pbrookdcac9672009-04-09 20:05:49 +0000190 ram_offset = qemu_ram_alloc(ram_size);
191 vga_ram_offset = qemu_ram_alloc(vga_ram_size);
192
193 cpu_register_physical_memory(0, ram_size, ram_offset | IO_MEM_RAM);
bellard66a93e02006-04-26 22:06:55 +0000194
ths6ae81772006-12-06 17:48:52 +0000195 if (!mips_qemu_iomemtype) {
196 mips_qemu_iomemtype = cpu_register_io_memory(0, mips_qemu_read,
ths33d68b52007-03-18 00:30:29 +0000197 mips_qemu_write, NULL);
ths6ae81772006-12-06 17:48:52 +0000198 }
199 cpu_register_physical_memory(0x1fbf0000, 0x10000, mips_qemu_iomemtype);
200
bellard66a93e02006-04-26 22:06:55 +0000201 /* Try to load a BIOS image. If this fails, we continue regardless,
202 but initialize the hardware ourselves. When a kernel gets
203 preloaded we also initialize the hardware, since the BIOS wasn't
204 run. */
j_mayer1192dad2007-10-05 13:08:35 +0000205 if (bios_name == NULL)
206 bios_name = BIOS_FILENAME;
207 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
pbrookdcac9672009-04-09 20:05:49 +0000208 bios_size = get_image_size(buf);
ths2909b292007-01-06 02:24:15 +0000209 if ((bios_size > 0) && (bios_size <= BIOS_SIZE)) {
pbrookdcac9672009-04-09 20:05:49 +0000210 bios_offset = qemu_ram_alloc(BIOS_SIZE);
211 cpu_register_physical_memory(0x1fc00000, BIOS_SIZE,
212 bios_offset | IO_MEM_ROM);
213
214 load_image_targphys(buf, 0x1fc00000, BIOS_SIZE);
thsb305b5b2008-04-20 06:28:28 +0000215 } else if ((index = drive_get_index(IF_PFLASH, 0, 0)) > -1) {
216 uint32_t mips_rom = 0x00400000;
pbrookdcac9672009-04-09 20:05:49 +0000217 bios_offset = qemu_ram_alloc(mips_rom);
218 if (!pflash_cfi01_register(0x1fc00000, bios_offset,
thsb305b5b2008-04-20 06:28:28 +0000219 drives_table[index].bdrv, sector_len, mips_rom / sector_len,
220 4, 0, 0, 0, 0)) {
221 fprintf(stderr, "qemu: Error registering flash memory.\n");
222 }
223 }
224 else {
bellard66a93e02006-04-26 22:06:55 +0000225 /* not fatal */
226 fprintf(stderr, "qemu: Warning, could not load MIPS bios '%s'\n",
227 buf);
bellard6af0bf92005-07-02 14:58:51 +0000228 }
bellard66a93e02006-04-26 22:06:55 +0000229
bellard66a93e02006-04-26 22:06:55 +0000230 if (kernel_filename) {
ths7df526e2007-11-09 17:52:11 +0000231 loaderparams.ram_size = ram_size;
232 loaderparams.kernel_filename = kernel_filename;
233 loaderparams.kernel_cmdline = kernel_cmdline;
234 loaderparams.initrd_filename = initrd_filename;
235 load_kernel (env);
bellard6af0bf92005-07-02 14:58:51 +0000236 }
bellard6af0bf92005-07-02 14:58:51 +0000237
thse16fe402006-12-06 21:38:37 +0000238 /* Init CPU internal devices */
pbrookd537cf62007-04-07 18:14:41 +0000239 cpu_mips_irq_init_cpu(env);
bellardc68ea702005-11-21 23:33:12 +0000240 cpu_mips_clock_init(env);
bellard6af0bf92005-07-02 14:58:51 +0000241
pbrookd537cf62007-04-07 18:14:41 +0000242 /* The PIC is attached to the MIPS CPU INT0 pin */
243 i8259 = i8259_init(env->irq[2]);
244
aurel3242fc73a2009-01-24 18:06:21 +0000245 rtc_state = rtc_init(0x70, i8259[8], 2000);
thsafdfa782006-12-07 18:15:35 +0000246
bellard0699b542005-07-02 15:20:29 +0000247 /* Register 64 KB of ISA IO space at 0x14000000 */
pbrookaef445b2006-09-18 01:15:29 +0000248 isa_mmio_init(0x14000000, 0x00010000);
bellard0699b542005-07-02 15:20:29 +0000249 isa_mem_base = 0x10000000;
250
pbrookd537cf62007-04-07 18:14:41 +0000251 pit = pit_init(0x40, i8259[0]);
thsafdfa782006-12-07 18:15:35 +0000252
thseddbd282006-12-23 00:23:19 +0000253 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
254 if (serial_hds[i]) {
aurel32b6cd0ea2008-05-04 21:42:11 +0000255 serial_init(serial_io[i], i8259[serial_irq[i]], 115200,
256 serial_hds[i]);
thseddbd282006-12-23 00:23:19 +0000257 }
258 }
259
pbrookdcac9672009-04-09 20:05:49 +0000260 isa_vga_init(phys_ram_base + vga_ram_offset, ram_size,
bellard89b6b502006-08-17 10:45:20 +0000261 vga_ram_size);
bellard9827e952005-07-02 15:26:04 +0000262
aliguori0ae18ce2009-01-13 19:39:36 +0000263 if (nd_table[0].vlan)
264 isa_ne2000_init(0x300, i8259[9], &nd_table[0]);
pbrook58126402006-10-29 15:38:28 +0000265
thse4bcb142007-12-02 04:51:10 +0000266 if (drive_get_max_bus(IF_IDE) >= MAX_IDE_BUS) {
267 fprintf(stderr, "qemu: too many IDE bus\n");
268 exit(1);
269 }
270
271 for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
272 index = drive_get_index(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
273 if (index != -1)
274 hd[i] = drives_table[index].bdrv;
275 else
276 hd[i] = NULL;
277 }
278
279 for(i = 0; i < MAX_IDE_BUS; i++)
pbrookd537cf62007-04-07 18:14:41 +0000280 isa_ide_init(ide_iobase[i], ide_iobase2[i], i8259[ide_irq[i]],
thse4bcb142007-12-02 04:51:10 +0000281 hd[MAX_IDE_DEVS * i],
282 hd[MAX_IDE_DEVS * i + 1]);
ths70705262007-02-18 00:10:59 +0000283
pbrookd537cf62007-04-07 18:14:41 +0000284 i8042_init(i8259[1], i8259[12], 0x60);
bellard6af0bf92005-07-02 14:58:51 +0000285}
286
287QEMUMachine mips_machine = {
thseec27432008-08-13 13:01:28 +0000288 .name = "mips",
289 .desc = "mips r4k platform",
290 .init = mips_r4k_init,
291 .ram_require = VGA_RAM_SIZE + BIOS_SIZE,
bellard6af0bf92005-07-02 14:58:51 +0000292};