machine: Conversion of QEMUMachineInitArgs to MachineState
[qemu/ar7.git] / hw / xtensa / xtensa_lx60.c
blob507dd88452973b64a7a254fc8b56e0985a328704
1 /*
2 * Copyright (c) 2011, Max Filippov, Open Source and Linux Lab.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the Open Source and Linux Lab nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "sysemu/sysemu.h"
29 #include "hw/boards.h"
30 #include "hw/loader.h"
31 #include "elf.h"
32 #include "exec/memory.h"
33 #include "exec/address-spaces.h"
34 #include "hw/char/serial.h"
35 #include "net/net.h"
36 #include "hw/sysbus.h"
37 #include "hw/block/flash.h"
38 #include "sysemu/blockdev.h"
39 #include "sysemu/char.h"
40 #include "xtensa_bootparam.h"
42 typedef struct LxBoardDesc {
43 hwaddr flash_base;
44 size_t flash_size;
45 size_t flash_sector_size;
46 size_t sram_size;
47 } LxBoardDesc;
49 typedef struct Lx60FpgaState {
50 MemoryRegion iomem;
51 uint32_t leds;
52 uint32_t switches;
53 } Lx60FpgaState;
55 static void lx60_fpga_reset(void *opaque)
57 Lx60FpgaState *s = opaque;
59 s->leds = 0;
60 s->switches = 0;
63 static uint64_t lx60_fpga_read(void *opaque, hwaddr addr,
64 unsigned size)
66 Lx60FpgaState *s = opaque;
68 switch (addr) {
69 case 0x0: /*build date code*/
70 return 0x09272011;
72 case 0x4: /*processor clock frequency, Hz*/
73 return 10000000;
75 case 0x8: /*LEDs (off = 0, on = 1)*/
76 return s->leds;
78 case 0xc: /*DIP switches (off = 0, on = 1)*/
79 return s->switches;
81 return 0;
84 static void lx60_fpga_write(void *opaque, hwaddr addr,
85 uint64_t val, unsigned size)
87 Lx60FpgaState *s = opaque;
89 switch (addr) {
90 case 0x8: /*LEDs (off = 0, on = 1)*/
91 s->leds = val;
92 break;
94 case 0x10: /*board reset*/
95 if (val == 0xdead) {
96 qemu_system_reset_request();
98 break;
102 static const MemoryRegionOps lx60_fpga_ops = {
103 .read = lx60_fpga_read,
104 .write = lx60_fpga_write,
105 .endianness = DEVICE_NATIVE_ENDIAN,
108 static Lx60FpgaState *lx60_fpga_init(MemoryRegion *address_space,
109 hwaddr base)
111 Lx60FpgaState *s = g_malloc(sizeof(Lx60FpgaState));
113 memory_region_init_io(&s->iomem, NULL, &lx60_fpga_ops, s,
114 "lx60.fpga", 0x10000);
115 memory_region_add_subregion(address_space, base, &s->iomem);
116 lx60_fpga_reset(s);
117 qemu_register_reset(lx60_fpga_reset, s);
118 return s;
121 static void lx60_net_init(MemoryRegion *address_space,
122 hwaddr base,
123 hwaddr descriptors,
124 hwaddr buffers,
125 qemu_irq irq, NICInfo *nd)
127 DeviceState *dev;
128 SysBusDevice *s;
129 MemoryRegion *ram;
131 dev = qdev_create(NULL, "open_eth");
132 qdev_set_nic_properties(dev, nd);
133 qdev_init_nofail(dev);
135 s = SYS_BUS_DEVICE(dev);
136 sysbus_connect_irq(s, 0, irq);
137 memory_region_add_subregion(address_space, base,
138 sysbus_mmio_get_region(s, 0));
139 memory_region_add_subregion(address_space, descriptors,
140 sysbus_mmio_get_region(s, 1));
142 ram = g_malloc(sizeof(*ram));
143 memory_region_init_ram(ram, OBJECT(s), "open_eth.ram", 16384);
144 vmstate_register_ram_global(ram);
145 memory_region_add_subregion(address_space, buffers, ram);
148 static uint64_t translate_phys_addr(void *opaque, uint64_t addr)
150 XtensaCPU *cpu = opaque;
152 return cpu_get_phys_page_debug(CPU(cpu), addr);
155 static void lx60_reset(void *opaque)
157 XtensaCPU *cpu = opaque;
159 cpu_reset(CPU(cpu));
162 static void lx_init(const LxBoardDesc *board, MachineState *machine)
164 #ifdef TARGET_WORDS_BIGENDIAN
165 int be = 1;
166 #else
167 int be = 0;
168 #endif
169 MemoryRegion *system_memory = get_system_memory();
170 XtensaCPU *cpu = NULL;
171 CPUXtensaState *env = NULL;
172 MemoryRegion *ram, *rom, *system_io;
173 DriveInfo *dinfo;
174 pflash_t *flash = NULL;
175 const char *cpu_model = machine->cpu_model;
176 const char *kernel_filename = machine->kernel_filename;
177 const char *kernel_cmdline = machine->kernel_cmdline;
178 int n;
180 if (!cpu_model) {
181 cpu_model = XTENSA_DEFAULT_CPU_MODEL;
184 for (n = 0; n < smp_cpus; n++) {
185 cpu = cpu_xtensa_init(cpu_model);
186 if (cpu == NULL) {
187 fprintf(stderr, "Unable to find CPU definition\n");
188 exit(1);
190 env = &cpu->env;
192 env->sregs[PRID] = n;
193 qemu_register_reset(lx60_reset, cpu);
194 /* Need MMU initialized prior to ELF loading,
195 * so that ELF gets loaded into virtual addresses
197 cpu_reset(CPU(cpu));
200 ram = g_malloc(sizeof(*ram));
201 memory_region_init_ram(ram, NULL, "lx60.dram", machine->ram_size);
202 vmstate_register_ram_global(ram);
203 memory_region_add_subregion(system_memory, 0, ram);
205 system_io = g_malloc(sizeof(*system_io));
206 memory_region_init(system_io, NULL, "lx60.io", 224 * 1024 * 1024);
207 memory_region_add_subregion(system_memory, 0xf0000000, system_io);
208 lx60_fpga_init(system_io, 0x0d020000);
209 if (nd_table[0].used) {
210 lx60_net_init(system_io, 0x0d030000, 0x0d030400, 0x0d800000,
211 xtensa_get_extint(env, 1), nd_table);
214 if (!serial_hds[0]) {
215 serial_hds[0] = qemu_chr_new("serial0", "null", NULL);
218 serial_mm_init(system_io, 0x0d050020, 2, xtensa_get_extint(env, 0),
219 115200, serial_hds[0], DEVICE_NATIVE_ENDIAN);
221 dinfo = drive_get(IF_PFLASH, 0, 0);
222 if (dinfo) {
223 flash = pflash_cfi01_register(board->flash_base,
224 NULL, "lx60.io.flash", board->flash_size,
225 dinfo->bdrv, board->flash_sector_size,
226 board->flash_size / board->flash_sector_size,
227 4, 0x0000, 0x0000, 0x0000, 0x0000, be);
228 if (flash == NULL) {
229 fprintf(stderr, "Unable to mount pflash\n");
230 exit(1);
234 /* Use presence of kernel file name as 'boot from SRAM' switch. */
235 if (kernel_filename) {
236 rom = g_malloc(sizeof(*rom));
237 memory_region_init_ram(rom, NULL, "lx60.sram", board->sram_size);
238 vmstate_register_ram_global(rom);
239 memory_region_add_subregion(system_memory, 0xfe000000, rom);
241 /* Put kernel bootparameters to the end of that SRAM */
242 if (kernel_cmdline) {
243 size_t cmdline_size = strlen(kernel_cmdline) + 1;
244 size_t bp_size = sizeof(BpTag[4]) + cmdline_size;
245 uint32_t tagptr = (0xfe000000 + board->sram_size - bp_size) & ~0xff;
247 env->regs[2] = tagptr;
249 tagptr = put_tag(tagptr, 0x7b0b, 0, NULL);
250 if (cmdline_size > 1) {
251 tagptr = put_tag(tagptr, 0x1001,
252 cmdline_size, kernel_cmdline);
254 tagptr = put_tag(tagptr, 0x7e0b, 0, NULL);
256 uint64_t elf_entry;
257 uint64_t elf_lowaddr;
258 int success = load_elf(kernel_filename, translate_phys_addr, cpu,
259 &elf_entry, &elf_lowaddr, NULL, be, ELF_MACHINE, 0);
260 if (success > 0) {
261 env->pc = elf_entry;
263 } else {
264 if (flash) {
265 MemoryRegion *flash_mr = pflash_cfi01_get_memory(flash);
266 MemoryRegion *flash_io = g_malloc(sizeof(*flash_io));
268 memory_region_init_alias(flash_io, NULL, "lx60.flash",
269 flash_mr, 0,
270 board->flash_size < 0x02000000 ?
271 board->flash_size : 0x02000000);
272 memory_region_add_subregion(system_memory, 0xfe000000,
273 flash_io);
278 static void xtensa_lx60_init(MachineState *machine)
280 static const LxBoardDesc lx60_board = {
281 .flash_base = 0xf8000000,
282 .flash_size = 0x00400000,
283 .flash_sector_size = 0x10000,
284 .sram_size = 0x20000,
286 lx_init(&lx60_board, machine);
289 static void xtensa_lx200_init(MachineState *machine)
291 static const LxBoardDesc lx200_board = {
292 .flash_base = 0xf8000000,
293 .flash_size = 0x01000000,
294 .flash_sector_size = 0x20000,
295 .sram_size = 0x2000000,
297 lx_init(&lx200_board, machine);
300 static void xtensa_ml605_init(MachineState *machine)
302 static const LxBoardDesc ml605_board = {
303 .flash_base = 0xf8000000,
304 .flash_size = 0x02000000,
305 .flash_sector_size = 0x20000,
306 .sram_size = 0x2000000,
308 lx_init(&ml605_board, machine);
311 static void xtensa_kc705_init(MachineState *machine)
313 static const LxBoardDesc kc705_board = {
314 .flash_base = 0xf0000000,
315 .flash_size = 0x08000000,
316 .flash_sector_size = 0x20000,
317 .sram_size = 0x2000000,
319 lx_init(&kc705_board, machine);
322 static QEMUMachine xtensa_lx60_machine = {
323 .name = "lx60",
324 .desc = "lx60 EVB (" XTENSA_DEFAULT_CPU_MODEL ")",
325 .init = xtensa_lx60_init,
326 .max_cpus = 4,
329 static QEMUMachine xtensa_lx200_machine = {
330 .name = "lx200",
331 .desc = "lx200 EVB (" XTENSA_DEFAULT_CPU_MODEL ")",
332 .init = xtensa_lx200_init,
333 .max_cpus = 4,
336 static QEMUMachine xtensa_ml605_machine = {
337 .name = "ml605",
338 .desc = "ml605 EVB (" XTENSA_DEFAULT_CPU_MODEL ")",
339 .init = xtensa_ml605_init,
340 .max_cpus = 4,
343 static QEMUMachine xtensa_kc705_machine = {
344 .name = "kc705",
345 .desc = "kc705 EVB (" XTENSA_DEFAULT_CPU_MODEL ")",
346 .init = xtensa_kc705_init,
347 .max_cpus = 4,
350 static void xtensa_lx_machines_init(void)
352 qemu_register_machine(&xtensa_lx60_machine);
353 qemu_register_machine(&xtensa_lx200_machine);
354 qemu_register_machine(&xtensa_ml605_machine);
355 qemu_register_machine(&xtensa_kc705_machine);
358 machine_init(xtensa_lx_machines_init);