blob: c55ef767248d4bfea01d69a4fbd953f6bbd6a526 [file] [log] [blame]
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -07001/* Copyright (C) 2007-2008 The Android Open Source Project
2**
3** This software is licensed under the terms of the GNU General Public
4** License version 2, as published by the Free Software Foundation, and
5** may be copied, distributed, and modified under those terms.
6**
7** This program is distributed in the hope that it will be useful,
8** but WITHOUT ANY WARRANTY; without even the implied warranty of
9** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10** GNU General Public License for more details.
11*/
David 'Digit' Turner2ec695a2013-12-17 10:03:39 +010012#include "hw/hw.h"
David 'Digit' Turnerd1298762013-12-17 10:22:24 +010013#include "hw/boards.h"
14#include "hw/devices.h"
David 'Digit' Turnercc330d42013-12-14 23:26:42 +010015#include "net/net.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +010016#include "sysemu/sysemu.h"
David 'Digit' Turnerc7169652013-12-17 11:10:33 +010017#include "hw/mips/mips.h"
David 'Digit' Turnerf5bc01c2013-12-17 10:33:07 +010018#include "hw/android/goldfish/device.h"
David 'Digit' Turnera2c14f92014-02-04 01:02:30 +010019#include "hw/android/goldfish/pipe.h"
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -070020#include "android/globals.h"
21#include "audio/audio.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +010022#include "sysemu/blockdev.h"
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -070023#ifdef CONFIG_MEMCHECK
24#include "memcheck/memcheck_api.h"
25#endif // CONFIG_MEMCHECK
26
27#include "android/utils/debug.h"
28
29#define D(...) VERBOSE_PRINT(init,__VA_ARGS__)
30
31#define MIPS_CPU_SAVE_VERSION 1
32#define GOLDFISH_IO_SPACE 0x1f000000
33#define GOLDFISH_INTERRUPT 0x1f000000
34#define GOLDFISH_DEVICEBUS 0x1f001000
35#define GOLDFISH_TTY 0x1f002000
36#define GOLDFISH_RTC 0x1f003000
37#define GOLDFISH_AUDIO 0x1f004000
38#define GOLDFISH_MMC 0x1f005000
39#define GOLDFISH_MEMLOG 0x1f006000
40#define GOLDFISH_DEVICES 0x1f010000
41
42char* audio_input_source = NULL;
43
44void goldfish_memlog_init(uint32_t base);
45
46static struct goldfish_device event0_device = {
47 .name = "goldfish_events",
48 .id = 0,
49 .size = 0x1000,
50 .irq_count = 1
51};
52
53static struct goldfish_device nand_device = {
54 .name = "goldfish_nand",
55 .id = 0,
56 .size = 0x1000
57};
58
59/* Board init. */
60
61#define TEST_SWITCH 1
62#if TEST_SWITCH
63uint32_t switch_test_write(void *opaque, uint32_t state)
64{
65 goldfish_switch_set_state(opaque, state);
66 return state;
67}
68#endif
69
70#define VIRT_TO_PHYS_ADDEND (-((int64_t)(int32_t)0x80000000))
71
72#define PHYS_TO_VIRT(x) ((x) | ~(target_ulong)0x7fffffff)
73
74static void android_load_kernel(CPUState *env, int ram_size, const char *kernel_filename,
75 const char *kernel_cmdline, const char *initrd_filename)
76{
77 int initrd_size;
78 ram_addr_t initrd_offset;
79 uint64_t kernel_entry, kernel_low, kernel_high;
80 unsigned int cmdline;
81
82 /* Load the kernel. */
83 if (!kernel_filename) {
84 fprintf(stderr, "Kernel image must be specified\n");
85 exit(1);
86 }
87 if (load_elf(kernel_filename, VIRT_TO_PHYS_ADDEND,
88 (uint64_t *)&kernel_entry, (uint64_t *)&kernel_low,
89 (uint64_t *)&kernel_high) < 0) {
90 fprintf(stderr, "qemu: could not load kernel '%s'\n", kernel_filename);
91 exit(1);
92 }
93 env->active_tc.PC = (int32_t)kernel_entry;
94
95 /* load initrd */
96 initrd_size = 0;
97 initrd_offset = 0;
98 if (initrd_filename) {
99 initrd_size = get_image_size (initrd_filename);
100 if (initrd_size > 0) {
101 initrd_offset = (kernel_high + ~TARGET_PAGE_MASK) & TARGET_PAGE_MASK;
102 if (initrd_offset + initrd_size > ram_size) {
103 fprintf(stderr,
104 "qemu: memory too small for initial ram disk '%s'\n",
105 initrd_filename);
106 exit(1);
107 }
108 initrd_size = load_image_targphys(initrd_filename,
109 initrd_offset,
110 ram_size - initrd_offset);
111
112 }
113 if (initrd_size == (target_ulong) -1) {
114 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
115 initrd_filename);
116 exit(1);
117 }
118 }
119
120 /* Store command line in top page of memory
121 * kernel will copy the command line to a loca buffer
122 */
123 cmdline = ram_size - TARGET_PAGE_SIZE;
124 char kernel_cmd[1024];
125 if (initrd_size > 0)
126 sprintf (kernel_cmd, "%s rd_start=0x" TARGET_FMT_lx " rd_size=%li",
127 kernel_cmdline,
David 'Digit' Turnera2c14f92014-02-04 01:02:30 +0100128 (hwaddr)PHYS_TO_VIRT(initrd_offset),
129 (long int)initrd_size);
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -0700130 else
131 strcpy (kernel_cmd, kernel_cmdline);
132
133 cpu_physical_memory_write(ram_size - TARGET_PAGE_SIZE, (void *)kernel_cmd, strlen(kernel_cmd) + 1);
134
135#if 0
136 if (initrd_size > 0)
137 sprintf (phys_ram_base+cmdline, "%s rd_start=0x" TARGET_FMT_lx " rd_size=%li",
138 kernel_cmdline,
139 PHYS_TO_VIRT(initrd_offset), initrd_size);
140 else
141 strcpy (phys_ram_base+cmdline, kernel_cmdline);
142#endif
143
144 env->active_tc.gpr[4] = PHYS_TO_VIRT(cmdline);/* a0 */
145 env->active_tc.gpr[5] = ram_size; /* a1 */
146 env->active_tc.gpr[6] = 0; /* a2 */
147 env->active_tc.gpr[7] = 0; /* a3 */
148
149}
150
151
152static void android_mips_init_(ram_addr_t ram_size,
153 const char *boot_device,
154 const char *kernel_filename,
155 const char *kernel_cmdline,
156 const char *initrd_filename,
157 const char *cpu_model)
158{
159 CPUState *env;
160 qemu_irq *goldfish_pic;
161 int i;
162 ram_addr_t ram_offset;
163
164 if (!cpu_model)
165 cpu_model = "24Kf";
166
167 env = cpu_init(cpu_model);
168
169 register_savevm( "cpu", 0, MIPS_CPU_SAVE_VERSION, cpu_save, cpu_load, env );
170
171 if (ram_size > GOLDFISH_IO_SPACE)
172 ram_size = GOLDFISH_IO_SPACE; /* avoid overlap of ram and IO regs */
173 ram_offset = qemu_ram_alloc(NULL, "android_mips", ram_size);
174 cpu_register_physical_memory(0, ram_size, ram_offset | IO_MEM_RAM);
175
176 /* Init internal devices */
177 cpu_mips_irq_init_cpu(env);
178 cpu_mips_clock_init(env);
179
180 goldfish_pic = goldfish_interrupt_init(GOLDFISH_INTERRUPT,
181 env->irq[2], env->irq[3]);
182 goldfish_device_init(goldfish_pic, GOLDFISH_DEVICES, 0x7f0000, 10, 22);
183
184 goldfish_device_bus_init(GOLDFISH_DEVICEBUS, 1);
185
186 goldfish_timer_and_rtc_init(GOLDFISH_RTC, 3);
187
188 goldfish_tty_add(serial_hds[0], 0, GOLDFISH_TTY, 4);
189 for(i = 1; i < MAX_SERIAL_PORTS; i++) {
190 if(serial_hds[i]) {
191 goldfish_tty_add(serial_hds[i], i, 0, 0);
192 }
193 }
194
195 for(i = 0; i < MAX_NICS; i++) {
196 if (nd_table[i].vlan) {
197 if (nd_table[i].model == NULL
198 || strcmp(nd_table[i].model, "smc91c111") == 0) {
199 struct goldfish_device *smc_device;
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +0100200 smc_device = g_malloc0(sizeof(*smc_device));
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -0700201 smc_device->name = "smc91x";
202 smc_device->id = i;
203 smc_device->size = 0x1000;
204 smc_device->irq_count = 1;
205 goldfish_add_device_no_io(smc_device);
206 smc91c111_init(&nd_table[i], smc_device->base, goldfish_pic[smc_device->irq]);
207 } else {
208 fprintf(stderr, "qemu: Unsupported NIC: %s\n", nd_table[0].model);
209 exit (1);
210 }
211 }
212 }
213
214 goldfish_fb_init(0);
215#ifdef HAS_AUDIO
216 goldfish_audio_init(GOLDFISH_AUDIO, 0, audio_input_source);
217#endif
218 {
219 DriveInfo* info = drive_get( IF_IDE, 0, 0 );
220 if (info != NULL) {
221 goldfish_mmc_init(GOLDFISH_MMC, 0, info->bdrv);
222 }
223 }
224 goldfish_memlog_init(GOLDFISH_MEMLOG);
225
David 'Digit' Turner685b2c22014-01-28 03:42:03 +0100226 goldfish_battery_init(android_hw->hw_battery);
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -0700227
228 goldfish_add_device_no_io(&event0_device);
229 events_dev_init(event0_device.base, goldfish_pic[event0_device.irq]);
230
231#ifdef CONFIG_NAND
232 goldfish_add_device_no_io(&nand_device);
233 nand_dev_init(nand_device.base);
234#endif
235#ifdef CONFIG_TRACE
236 extern const char *trace_filename;
237 /* Init trace device if either tracing, or memory checking is enabled. */
238 if (trace_filename != NULL
239#ifdef CONFIG_MEMCHECK
240 || memcheck_enabled
241#endif // CONFIG_MEMCHECK
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -0700242 ) {
243 trace_dev_init();
244 }
245 if (trace_filename != NULL) {
246 D( "Trace file name is set to %s\n", trace_filename );
247 } else {
248 D("Trace file name is not set\n");
249 }
250#endif
251
252 pipe_dev_init();
253
254#if TEST_SWITCH
255 {
256 void *sw;
257 sw = goldfish_switch_add("test", NULL, NULL, 0);
258 goldfish_switch_set_state(sw, 1);
259 goldfish_switch_add("test2", switch_test_write, sw, 1);
260 }
261#endif
262
263 android_load_kernel(env, ram_size, kernel_filename, kernel_cmdline, initrd_filename);
264}
265
266
267QEMUMachine android_mips_machine = {
268 "android_mips",
269 "MIPS Android Emulator",
270 android_mips_init_,
271 0,
272 0,
273 1,
274 NULL
275};
276
277static void android_mips_init(void)
278{
279 qemu_register_machine(&android_mips_machine);
280}
281
282machine_init(android_mips_init);