2 * Copyright (c) 2011, Max Filippov, Open Source and Linux Lab.
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"
32 #include "exec/memory.h"
33 #include "exec/address-spaces.h"
34 #include "hw/char/serial.h"
36 #include "hw/sysbus.h"
37 #include "hw/block/flash.h"
38 #include "sysemu/blockdev.h"
39 #include "sysemu/char.h"
40 #include "qemu/error-report.h"
41 #include "bootparam.h"
43 typedef struct LxBoardDesc {
46 size_t flash_boot_base;
47 size_t flash_sector_size;
51 typedef struct Lx60FpgaState {
57 static void lx60_fpga_reset(void *opaque)
59 Lx60FpgaState *s = opaque;
65 static uint64_t lx60_fpga_read(void *opaque, hwaddr addr,
68 Lx60FpgaState *s = opaque;
71 case 0x0: /*build date code*/
74 case 0x4: /*processor clock frequency, Hz*/
77 case 0x8: /*LEDs (off = 0, on = 1)*/
80 case 0xc: /*DIP switches (off = 0, on = 1)*/
86 static void lx60_fpga_write(void *opaque, hwaddr addr,
87 uint64_t val, unsigned size)
89 Lx60FpgaState *s = opaque;
92 case 0x8: /*LEDs (off = 0, on = 1)*/
96 case 0x10: /*board reset*/
98 qemu_system_reset_request();
104 static const MemoryRegionOps lx60_fpga_ops = {
105 .read = lx60_fpga_read,
106 .write = lx60_fpga_write,
107 .endianness = DEVICE_NATIVE_ENDIAN,
110 static Lx60FpgaState *lx60_fpga_init(MemoryRegion *address_space,
113 Lx60FpgaState *s = g_malloc(sizeof(Lx60FpgaState));
115 memory_region_init_io(&s->iomem, NULL, &lx60_fpga_ops, s,
116 "lx60.fpga", 0x10000);
117 memory_region_add_subregion(address_space, base, &s->iomem);
119 qemu_register_reset(lx60_fpga_reset, s);
123 static void lx60_net_init(MemoryRegion *address_space,
127 qemu_irq irq, NICInfo *nd)
133 dev = qdev_create(NULL, "open_eth");
134 qdev_set_nic_properties(dev, nd);
135 qdev_init_nofail(dev);
137 s = SYS_BUS_DEVICE(dev);
138 sysbus_connect_irq(s, 0, irq);
139 memory_region_add_subregion(address_space, base,
140 sysbus_mmio_get_region(s, 0));
141 memory_region_add_subregion(address_space, descriptors,
142 sysbus_mmio_get_region(s, 1));
144 ram = g_malloc(sizeof(*ram));
145 memory_region_init_ram(ram, OBJECT(s), "open_eth.ram", 16384);
146 vmstate_register_ram_global(ram);
147 memory_region_add_subregion(address_space, buffers, ram);
150 static uint64_t translate_phys_addr(void *opaque, uint64_t addr)
152 XtensaCPU *cpu = opaque;
154 return cpu_get_phys_page_debug(CPU(cpu), addr);
157 static void lx60_reset(void *opaque)
159 XtensaCPU *cpu = opaque;
164 static void lx_init(const LxBoardDesc *board, MachineState *machine)
166 #ifdef TARGET_WORDS_BIGENDIAN
171 MemoryRegion *system_memory = get_system_memory();
172 XtensaCPU *cpu = NULL;
173 CPUXtensaState *env = NULL;
174 MemoryRegion *ram, *rom, *system_io;
176 pflash_t *flash = NULL;
177 QemuOpts *machine_opts = qemu_get_machine_opts();
178 const char *cpu_model = machine->cpu_model;
179 const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
180 const char *kernel_cmdline = qemu_opt_get(machine_opts, "append");
184 cpu_model = XTENSA_DEFAULT_CPU_MODEL;
187 for (n = 0; n < smp_cpus; n++) {
188 cpu = cpu_xtensa_init(cpu_model);
190 error_report("unable to find CPU definition '%s'\n",
196 env->sregs[PRID] = n;
197 qemu_register_reset(lx60_reset, cpu);
198 /* Need MMU initialized prior to ELF loading,
199 * so that ELF gets loaded into virtual addresses
204 ram = g_malloc(sizeof(*ram));
205 memory_region_init_ram(ram, NULL, "lx60.dram", machine->ram_size);
206 vmstate_register_ram_global(ram);
207 memory_region_add_subregion(system_memory, 0, ram);
209 system_io = g_malloc(sizeof(*system_io));
210 memory_region_init(system_io, NULL, "lx60.io", 224 * 1024 * 1024);
211 memory_region_add_subregion(system_memory, 0xf0000000, system_io);
212 lx60_fpga_init(system_io, 0x0d020000);
213 if (nd_table[0].used) {
214 lx60_net_init(system_io, 0x0d030000, 0x0d030400, 0x0d800000,
215 xtensa_get_extint(env, 1), nd_table);
218 if (!serial_hds[0]) {
219 serial_hds[0] = qemu_chr_new("serial0", "null", NULL);
222 serial_mm_init(system_io, 0x0d050020, 2, xtensa_get_extint(env, 0),
223 115200, serial_hds[0], DEVICE_NATIVE_ENDIAN);
225 dinfo = drive_get(IF_PFLASH, 0, 0);
227 flash = pflash_cfi01_register(board->flash_base,
228 NULL, "lx60.io.flash", board->flash_size,
229 dinfo->bdrv, board->flash_sector_size,
230 board->flash_size / board->flash_sector_size,
231 4, 0x0000, 0x0000, 0x0000, 0x0000, be);
233 error_report("unable to mount pflash\n");
238 /* Use presence of kernel file name as 'boot from SRAM' switch. */
239 if (kernel_filename) {
240 uint32_t entry_point = env->pc;
241 size_t bp_size = 3 * get_tag_size(0); /* first/last and memory tags */
242 uint32_t tagptr = 0xfe000000 + board->sram_size;
244 BpMemInfo memory_location = {
245 .type = tswap32(MEMORY_TYPE_CONVENTIONAL),
247 .end = tswap32(machine->ram_size),
250 rom = g_malloc(sizeof(*rom));
251 memory_region_init_ram(rom, NULL, "lx60.sram", board->sram_size);
252 vmstate_register_ram_global(rom);
253 memory_region_add_subregion(system_memory, 0xfe000000, rom);
255 if (kernel_cmdline) {
256 bp_size += get_tag_size(strlen(kernel_cmdline) + 1);
259 /* Put kernel bootparameters to the end of that SRAM */
260 tagptr = (tagptr - bp_size) & ~0xff;
261 cur_tagptr = put_tag(tagptr, BP_TAG_FIRST, 0, NULL);
262 cur_tagptr = put_tag(cur_tagptr, BP_TAG_MEMORY,
263 sizeof(memory_location), &memory_location);
265 if (kernel_cmdline) {
266 cur_tagptr = put_tag(cur_tagptr, BP_TAG_COMMAND_LINE,
267 strlen(kernel_cmdline) + 1, kernel_cmdline);
269 cur_tagptr = put_tag(cur_tagptr, BP_TAG_LAST, 0, NULL);
270 env->regs[2] = tagptr;
273 uint64_t elf_lowaddr;
274 int success = load_elf(kernel_filename, translate_phys_addr, cpu,
275 &elf_entry, &elf_lowaddr, NULL, be, ELF_MACHINE, 0);
277 entry_point = elf_entry;
281 success = load_uimage(kernel_filename, &ep, NULL, &is_linux);
282 if (success > 0 && is_linux) {
285 error_report("could not load kernel '%s'\n",
290 if (entry_point != env->pc) {
291 static const uint8_t jx_a0[] = {
292 #ifdef TARGET_WORDS_BIGENDIAN
298 env->regs[0] = entry_point;
299 cpu_physical_memory_write(env->pc, jx_a0, sizeof(jx_a0));
303 MemoryRegion *flash_mr = pflash_cfi01_get_memory(flash);
304 MemoryRegion *flash_io = g_malloc(sizeof(*flash_io));
306 memory_region_init_alias(flash_io, NULL, "lx60.flash",
307 flash_mr, board->flash_boot_base,
308 board->flash_size - board->flash_boot_base < 0x02000000 ?
309 board->flash_size - board->flash_boot_base : 0x02000000);
310 memory_region_add_subregion(system_memory, 0xfe000000,
316 static void xtensa_lx60_init(MachineState *machine)
318 static const LxBoardDesc lx60_board = {
319 .flash_base = 0xf8000000,
320 .flash_size = 0x00400000,
321 .flash_sector_size = 0x10000,
322 .sram_size = 0x20000,
324 lx_init(&lx60_board, machine);
327 static void xtensa_lx200_init(MachineState *machine)
329 static const LxBoardDesc lx200_board = {
330 .flash_base = 0xf8000000,
331 .flash_size = 0x01000000,
332 .flash_sector_size = 0x20000,
333 .sram_size = 0x2000000,
335 lx_init(&lx200_board, machine);
338 static void xtensa_ml605_init(MachineState *machine)
340 static const LxBoardDesc ml605_board = {
341 .flash_base = 0xf8000000,
342 .flash_size = 0x02000000,
343 .flash_sector_size = 0x20000,
344 .sram_size = 0x2000000,
346 lx_init(&ml605_board, machine);
349 static void xtensa_kc705_init(MachineState *machine)
351 static const LxBoardDesc kc705_board = {
352 .flash_base = 0xf0000000,
353 .flash_size = 0x08000000,
354 .flash_boot_base = 0x06000000,
355 .flash_sector_size = 0x20000,
356 .sram_size = 0x2000000,
358 lx_init(&kc705_board, machine);
361 static QEMUMachine xtensa_lx60_machine = {
363 .desc = "lx60 EVB (" XTENSA_DEFAULT_CPU_MODEL ")",
364 .init = xtensa_lx60_init,
368 static QEMUMachine xtensa_lx200_machine = {
370 .desc = "lx200 EVB (" XTENSA_DEFAULT_CPU_MODEL ")",
371 .init = xtensa_lx200_init,
375 static QEMUMachine xtensa_ml605_machine = {
377 .desc = "ml605 EVB (" XTENSA_DEFAULT_CPU_MODEL ")",
378 .init = xtensa_ml605_init,
382 static QEMUMachine xtensa_kc705_machine = {
384 .desc = "kc705 EVB (" XTENSA_DEFAULT_CPU_MODEL ")",
385 .init = xtensa_kc705_init,
389 static void xtensa_lx_machines_init(void)
391 qemu_register_machine(&xtensa_lx60_machine);
392 qemu_register_machine(&xtensa_lx200_machine);
393 qemu_register_machine(&xtensa_ml605_machine);
394 qemu_register_machine(&xtensa_kc705_machine);
397 machine_init(xtensa_lx_machines_init);