blob: c4f616f4fba80eec4adcb9a499ab3b4d001ed496 [file] [log] [blame]
Max Filippov0200db62011-10-16 02:56:46 +04001/*
2 * Copyright (c) 2011, Max Filippov, Open Source and Linux Lab.
3 * All rights reserved.
4 *
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.
15 *
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.
26 */
27
28#include "sysemu.h"
29#include "boards.h"
30#include "loader.h"
31#include "elf.h"
32#include "memory.h"
33#include "exec-memory.h"
34#include "pc.h"
35#include "sysbus.h"
Max Filippov82b25dc2011-10-30 21:21:15 +040036#include "flash.h"
Max Filippov8aab0312012-06-08 13:50:34 +040037#include "blockdev.h"
Max Filippov292627b2011-10-30 21:24:26 +040038#include "xtensa_bootparam.h"
Max Filippov82b25dc2011-10-30 21:21:15 +040039
40typedef struct LxBoardDesc {
41 size_t flash_size;
42 size_t flash_sector_size;
43 size_t sram_size;
44} LxBoardDesc;
Max Filippov0200db62011-10-16 02:56:46 +040045
46typedef struct Lx60FpgaState {
47 MemoryRegion iomem;
48 uint32_t leds;
49 uint32_t switches;
50} Lx60FpgaState;
51
52static void lx60_fpga_reset(void *opaque)
53{
54 Lx60FpgaState *s = opaque;
55
56 s->leds = 0;
57 s->switches = 0;
58}
59
60static uint64_t lx60_fpga_read(void *opaque, target_phys_addr_t addr,
61 unsigned size)
62{
63 Lx60FpgaState *s = opaque;
64
65 switch (addr) {
66 case 0x0: /*build date code*/
Max Filippov556ba662011-10-30 21:18:27 +040067 return 0x09272011;
Max Filippov0200db62011-10-16 02:56:46 +040068
69 case 0x4: /*processor clock frequency, Hz*/
70 return 10000000;
71
72 case 0x8: /*LEDs (off = 0, on = 1)*/
73 return s->leds;
74
75 case 0xc: /*DIP switches (off = 0, on = 1)*/
76 return s->switches;
77 }
78 return 0;
79}
80
81static void lx60_fpga_write(void *opaque, target_phys_addr_t addr,
82 uint64_t val, unsigned size)
83{
84 Lx60FpgaState *s = opaque;
85
86 switch (addr) {
87 case 0x8: /*LEDs (off = 0, on = 1)*/
88 s->leds = val;
89 break;
90
91 case 0x10: /*board reset*/
92 if (val == 0xdead) {
93 qemu_system_reset_request();
94 }
95 break;
96 }
97}
98
99static const MemoryRegionOps lx60_fpga_ops = {
100 .read = lx60_fpga_read,
101 .write = lx60_fpga_write,
102 .endianness = DEVICE_NATIVE_ENDIAN,
103};
104
105static Lx60FpgaState *lx60_fpga_init(MemoryRegion *address_space,
106 target_phys_addr_t base)
107{
108 Lx60FpgaState *s = g_malloc(sizeof(Lx60FpgaState));
109
110 memory_region_init_io(&s->iomem, &lx60_fpga_ops, s,
Max Filippov556ba662011-10-30 21:18:27 +0400111 "lx60.fpga", 0x10000);
Max Filippov0200db62011-10-16 02:56:46 +0400112 memory_region_add_subregion(address_space, base, &s->iomem);
113 lx60_fpga_reset(s);
114 qemu_register_reset(lx60_fpga_reset, s);
115 return s;
116}
117
118static void lx60_net_init(MemoryRegion *address_space,
119 target_phys_addr_t base,
120 target_phys_addr_t descriptors,
121 target_phys_addr_t buffers,
122 qemu_irq irq, NICInfo *nd)
123{
124 DeviceState *dev;
125 SysBusDevice *s;
126 MemoryRegion *ram;
127
128 dev = qdev_create(NULL, "open_eth");
129 qdev_set_nic_properties(dev, nd);
130 qdev_init_nofail(dev);
131
132 s = sysbus_from_qdev(dev);
133 sysbus_connect_irq(s, 0, irq);
134 memory_region_add_subregion(address_space, base,
135 sysbus_mmio_get_region(s, 0));
136 memory_region_add_subregion(address_space, descriptors,
137 sysbus_mmio_get_region(s, 1));
138
139 ram = g_malloc(sizeof(*ram));
Avi Kivityc5705a72011-12-20 15:59:12 +0200140 memory_region_init_ram(ram, "open_eth.ram", 16384);
141 vmstate_register_ram_global(ram);
Max Filippov0200db62011-10-16 02:56:46 +0400142 memory_region_add_subregion(address_space, buffers, ram);
143}
144
145static uint64_t translate_phys_addr(void *env, uint64_t addr)
146{
147 return cpu_get_phys_page_debug(env, addr);
148}
149
Andreas Färber1bba0dc2012-02-08 03:03:33 +0100150static void lx60_reset(void *opaque)
Max Filippov0200db62011-10-16 02:56:46 +0400151{
Andreas Färbereded1262012-05-04 19:33:05 +0200152 XtensaCPU *cpu = opaque;
Andreas Färber1bba0dc2012-02-08 03:03:33 +0100153
Andreas Färbereded1262012-05-04 19:33:05 +0200154 cpu_reset(CPU(cpu));
Max Filippov0200db62011-10-16 02:56:46 +0400155}
156
Max Filippov82b25dc2011-10-30 21:21:15 +0400157static void lx_init(const LxBoardDesc *board,
158 ram_addr_t ram_size, const char *boot_device,
Max Filippov0200db62011-10-16 02:56:46 +0400159 const char *kernel_filename, const char *kernel_cmdline,
160 const char *initrd_filename, const char *cpu_model)
161{
162#ifdef TARGET_WORDS_BIGENDIAN
163 int be = 1;
164#else
165 int be = 0;
166#endif
167 MemoryRegion *system_memory = get_system_memory();
Andreas Färberadbb0f72012-05-04 19:31:25 +0200168 XtensaCPU *cpu = NULL;
Andreas Färber5bfcb362012-03-14 01:38:24 +0100169 CPUXtensaState *env = NULL;
Max Filippov0200db62011-10-16 02:56:46 +0400170 MemoryRegion *ram, *rom, *system_io;
Max Filippov82b25dc2011-10-30 21:21:15 +0400171 DriveInfo *dinfo;
172 pflash_t *flash = NULL;
Max Filippov0200db62011-10-16 02:56:46 +0400173 int n;
174
Max Filippov82b25dc2011-10-30 21:21:15 +0400175 if (!cpu_model) {
176 cpu_model = "dc232b";
177 }
178
Max Filippov0200db62011-10-16 02:56:46 +0400179 for (n = 0; n < smp_cpus; n++) {
Andreas Färberadbb0f72012-05-04 19:31:25 +0200180 cpu = cpu_xtensa_init(cpu_model);
181 if (cpu == NULL) {
Max Filippov0200db62011-10-16 02:56:46 +0400182 fprintf(stderr, "Unable to find CPU definition\n");
183 exit(1);
184 }
Andreas Färberadbb0f72012-05-04 19:31:25 +0200185 env = &cpu->env;
186
Max Filippov0200db62011-10-16 02:56:46 +0400187 env->sregs[PRID] = n;
Andreas Färbereded1262012-05-04 19:33:05 +0200188 qemu_register_reset(lx60_reset, cpu);
Max Filippov0200db62011-10-16 02:56:46 +0400189 /* Need MMU initialized prior to ELF loading,
190 * so that ELF gets loaded into virtual addresses
191 */
Andreas Färberadbb0f72012-05-04 19:31:25 +0200192 cpu_reset(CPU(cpu));
Max Filippov0200db62011-10-16 02:56:46 +0400193 }
194
195 ram = g_malloc(sizeof(*ram));
Avi Kivityc5705a72011-12-20 15:59:12 +0200196 memory_region_init_ram(ram, "lx60.dram", ram_size);
197 vmstate_register_ram_global(ram);
Max Filippov0200db62011-10-16 02:56:46 +0400198 memory_region_add_subregion(system_memory, 0, ram);
199
Max Filippov0200db62011-10-16 02:56:46 +0400200 system_io = g_malloc(sizeof(*system_io));
Max Filippov556ba662011-10-30 21:18:27 +0400201 memory_region_init(system_io, "lx60.io", 224 * 1024 * 1024);
Max Filippov0200db62011-10-16 02:56:46 +0400202 memory_region_add_subregion(system_memory, 0xf0000000, system_io);
203 lx60_fpga_init(system_io, 0x0d020000);
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100204 if (nd_table[0].used) {
Max Filippov0200db62011-10-16 02:56:46 +0400205 lx60_net_init(system_io, 0x0d030000, 0x0d030400, 0x0d800000,
206 xtensa_get_extint(env, 1), nd_table);
207 }
208
209 if (!serial_hds[0]) {
210 serial_hds[0] = qemu_chr_new("serial0", "null", NULL);
211 }
212
213 serial_mm_init(system_io, 0x0d050020, 2, xtensa_get_extint(env, 0),
214 115200, serial_hds[0], DEVICE_NATIVE_ENDIAN);
215
Max Filippov82b25dc2011-10-30 21:21:15 +0400216 dinfo = drive_get(IF_PFLASH, 0, 0);
217 if (dinfo) {
218 flash = pflash_cfi01_register(0xf8000000,
219 NULL, "lx60.io.flash", board->flash_size,
220 dinfo->bdrv, board->flash_sector_size,
221 board->flash_size / board->flash_sector_size,
222 4, 0x0000, 0x0000, 0x0000, 0x0000, be);
223 if (flash == NULL) {
224 fprintf(stderr, "Unable to mount pflash\n");
225 exit(1);
226 }
227 }
228
229 /* Use presence of kernel file name as 'boot from SRAM' switch. */
Max Filippov0200db62011-10-16 02:56:46 +0400230 if (kernel_filename) {
Max Filippov292627b2011-10-30 21:24:26 +0400231 rom = g_malloc(sizeof(*rom));
Avi Kivityc5705a72011-12-20 15:59:12 +0200232 memory_region_init_ram(rom, "lx60.sram", board->sram_size);
233 vmstate_register_ram_global(rom);
Max Filippov292627b2011-10-30 21:24:26 +0400234 memory_region_add_subregion(system_memory, 0xfe000000, rom);
235
236 /* Put kernel bootparameters to the end of that SRAM */
237 if (kernel_cmdline) {
238 size_t cmdline_size = strlen(kernel_cmdline) + 1;
239 size_t bp_size = sizeof(BpTag[4]) + cmdline_size;
240 uint32_t tagptr = (0xfe000000 + board->sram_size - bp_size) & ~0xff;
241
242 env->regs[2] = tagptr;
243
244 tagptr = put_tag(tagptr, 0x7b0b, 0, NULL);
245 if (cmdline_size > 1) {
246 tagptr = put_tag(tagptr, 0x1001,
247 cmdline_size, kernel_cmdline);
248 }
249 tagptr = put_tag(tagptr, 0x7e0b, 0, NULL);
250 }
Max Filippov0200db62011-10-16 02:56:46 +0400251 uint64_t elf_entry;
252 uint64_t elf_lowaddr;
253 int success = load_elf(kernel_filename, translate_phys_addr, env,
254 &elf_entry, &elf_lowaddr, NULL, be, ELF_MACHINE, 0);
255 if (success > 0) {
256 env->pc = elf_entry;
257 }
Max Filippov82b25dc2011-10-30 21:21:15 +0400258 } else {
259 if (flash) {
260 MemoryRegion *flash_mr = pflash_cfi01_get_memory(flash);
261 MemoryRegion *flash_io = g_malloc(sizeof(*flash_io));
262
263 memory_region_init_alias(flash_io, "lx60.flash",
264 flash_mr, 0, board->flash_size);
265 memory_region_add_subregion(system_memory, 0xfe000000,
266 flash_io);
267 }
Max Filippov0200db62011-10-16 02:56:46 +0400268 }
269}
270
271static void xtensa_lx60_init(ram_addr_t ram_size,
272 const char *boot_device,
273 const char *kernel_filename, const char *kernel_cmdline,
274 const char *initrd_filename, const char *cpu_model)
275{
Max Filippov82b25dc2011-10-30 21:21:15 +0400276 static const LxBoardDesc lx60_board = {
277 .flash_size = 0x400000,
278 .flash_sector_size = 0x10000,
279 .sram_size = 0x20000,
280 };
281 lx_init(&lx60_board, ram_size, boot_device,
282 kernel_filename, kernel_cmdline,
283 initrd_filename, cpu_model);
284}
285
286static void xtensa_lx200_init(ram_addr_t ram_size,
287 const char *boot_device,
288 const char *kernel_filename, const char *kernel_cmdline,
289 const char *initrd_filename, const char *cpu_model)
290{
291 static const LxBoardDesc lx200_board = {
292 .flash_size = 0x1000000,
293 .flash_sector_size = 0x20000,
294 .sram_size = 0x2000000,
295 };
296 lx_init(&lx200_board, ram_size, boot_device,
297 kernel_filename, kernel_cmdline,
Max Filippov0200db62011-10-16 02:56:46 +0400298 initrd_filename, cpu_model);
299}
300
301static QEMUMachine xtensa_lx60_machine = {
302 .name = "lx60",
303 .desc = "lx60 EVB (dc232b)",
304 .init = xtensa_lx60_init,
305 .max_cpus = 4,
306};
307
Max Filippov82b25dc2011-10-30 21:21:15 +0400308static QEMUMachine xtensa_lx200_machine = {
309 .name = "lx200",
310 .desc = "lx200 EVB (dc232b)",
311 .init = xtensa_lx200_init,
312 .max_cpus = 4,
313};
314
315static void xtensa_lx_machines_init(void)
Max Filippov0200db62011-10-16 02:56:46 +0400316{
317 qemu_register_machine(&xtensa_lx60_machine);
Max Filippov82b25dc2011-10-30 21:21:15 +0400318 qemu_register_machine(&xtensa_lx200_machine);
Max Filippov0200db62011-10-16 02:56:46 +0400319}
320
Max Filippov82b25dc2011-10-30 21:21:15 +0400321machine_init(xtensa_lx_machines_init);