]> git.proxmox.com Git - mirror_qemu.git/blame - hw/xtensa/xtfpga.c
Merge remote-tracking branch 'remotes/rth/tags/pull-rth-20160712' into staging
[mirror_qemu.git] / hw / xtensa / xtfpga.c
CommitLineData
0200db65
MF
1/*
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
09aae23d 28#include "qemu/osdep.h"
da34e65c 29#include "qapi/error.h"
4771d756
PB
30#include "qemu-common.h"
31#include "cpu.h"
9c17d615 32#include "sysemu/sysemu.h"
83c9f4ca
PB
33#include "hw/boards.h"
34#include "hw/loader.h"
0200db65 35#include "elf.h"
022c62cb
PB
36#include "exec/memory.h"
37#include "exec/address-spaces.h"
0d09e41a 38#include "hw/char/serial.h"
1422e32d 39#include "net/net.h"
83c9f4ca 40#include "hw/sysbus.h"
0d09e41a 41#include "hw/block/flash.h"
fa1d36df 42#include "sysemu/block-backend.h"
dccfcd0e 43#include "sysemu/char.h"
996dfe98 44#include "sysemu/device_tree.h"
8488ab02 45#include "qemu/error-report.h"
b707ab75 46#include "bootparam.h"
82b25dc8
MF
47
48typedef struct LxBoardDesc {
e0db904d 49 hwaddr flash_base;
82b25dc8 50 size_t flash_size;
37ed7c4b 51 size_t flash_boot_base;
82b25dc8
MF
52 size_t flash_sector_size;
53 size_t sram_size;
54} LxBoardDesc;
0200db65
MF
55
56typedef struct Lx60FpgaState {
57 MemoryRegion iomem;
58 uint32_t leds;
59 uint32_t switches;
60} Lx60FpgaState;
61
62static void lx60_fpga_reset(void *opaque)
63{
64 Lx60FpgaState *s = opaque;
65
66 s->leds = 0;
67 s->switches = 0;
68}
69
a8170e5e 70static uint64_t lx60_fpga_read(void *opaque, hwaddr addr,
0200db65
MF
71 unsigned size)
72{
73 Lx60FpgaState *s = opaque;
74
75 switch (addr) {
76 case 0x0: /*build date code*/
556ba668 77 return 0x09272011;
0200db65
MF
78
79 case 0x4: /*processor clock frequency, Hz*/
80 return 10000000;
81
82 case 0x8: /*LEDs (off = 0, on = 1)*/
83 return s->leds;
84
85 case 0xc: /*DIP switches (off = 0, on = 1)*/
86 return s->switches;
87 }
88 return 0;
89}
90
a8170e5e 91static void lx60_fpga_write(void *opaque, hwaddr addr,
0200db65
MF
92 uint64_t val, unsigned size)
93{
94 Lx60FpgaState *s = opaque;
95
96 switch (addr) {
97 case 0x8: /*LEDs (off = 0, on = 1)*/
98 s->leds = val;
99 break;
100
101 case 0x10: /*board reset*/
102 if (val == 0xdead) {
103 qemu_system_reset_request();
104 }
105 break;
106 }
107}
108
109static const MemoryRegionOps lx60_fpga_ops = {
110 .read = lx60_fpga_read,
111 .write = lx60_fpga_write,
112 .endianness = DEVICE_NATIVE_ENDIAN,
113};
114
115static Lx60FpgaState *lx60_fpga_init(MemoryRegion *address_space,
a8170e5e 116 hwaddr base)
0200db65
MF
117{
118 Lx60FpgaState *s = g_malloc(sizeof(Lx60FpgaState));
119
2c9b15ca 120 memory_region_init_io(&s->iomem, NULL, &lx60_fpga_ops, s,
556ba668 121 "lx60.fpga", 0x10000);
0200db65
MF
122 memory_region_add_subregion(address_space, base, &s->iomem);
123 lx60_fpga_reset(s);
124 qemu_register_reset(lx60_fpga_reset, s);
125 return s;
126}
127
128static void lx60_net_init(MemoryRegion *address_space,
a8170e5e
AK
129 hwaddr base,
130 hwaddr descriptors,
131 hwaddr buffers,
0200db65
MF
132 qemu_irq irq, NICInfo *nd)
133{
134 DeviceState *dev;
135 SysBusDevice *s;
136 MemoryRegion *ram;
137
138 dev = qdev_create(NULL, "open_eth");
139 qdev_set_nic_properties(dev, nd);
140 qdev_init_nofail(dev);
141
1356b98d 142 s = SYS_BUS_DEVICE(dev);
0200db65
MF
143 sysbus_connect_irq(s, 0, irq);
144 memory_region_add_subregion(address_space, base,
145 sysbus_mmio_get_region(s, 0));
146 memory_region_add_subregion(address_space, descriptors,
147 sysbus_mmio_get_region(s, 1));
148
149 ram = g_malloc(sizeof(*ram));
f8ed85ac
MA
150 memory_region_init_ram(ram, OBJECT(s), "open_eth.ram", 16384,
151 &error_fatal);
c5705a77 152 vmstate_register_ram_global(ram);
0200db65
MF
153 memory_region_add_subregion(address_space, buffers, ram);
154}
155
68931a40
MF
156static pflash_t *xtfpga_flash_init(MemoryRegion *address_space,
157 const LxBoardDesc *board,
158 DriveInfo *dinfo, int be)
159{
160 SysBusDevice *s;
161 DeviceState *dev = qdev_create(NULL, "cfi.pflash01");
162
163 qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(dinfo),
164 &error_abort);
165 qdev_prop_set_uint32(dev, "num-blocks",
166 board->flash_size / board->flash_sector_size);
167 qdev_prop_set_uint64(dev, "sector-length", board->flash_sector_size);
168 qdev_prop_set_uint8(dev, "width", 4);
169 qdev_prop_set_bit(dev, "big-endian", be);
170 qdev_prop_set_string(dev, "name", "lx60.io.flash");
171 qdev_init_nofail(dev);
172 s = SYS_BUS_DEVICE(dev);
173 memory_region_add_subregion(address_space, board->flash_base,
174 sysbus_mmio_get_region(s, 0));
175 return OBJECT_CHECK(pflash_t, (dev), "cfi.pflash01");
176}
177
00b941e5 178static uint64_t translate_phys_addr(void *opaque, uint64_t addr)
0200db65 179{
00b941e5
AF
180 XtensaCPU *cpu = opaque;
181
182 return cpu_get_phys_page_debug(CPU(cpu), addr);
0200db65
MF
183}
184
1bba0dc9 185static void lx60_reset(void *opaque)
0200db65 186{
eded1267 187 XtensaCPU *cpu = opaque;
1bba0dc9 188
eded1267 189 cpu_reset(CPU(cpu));
0200db65
MF
190}
191
8bb3b575
MF
192static uint64_t lx60_io_read(void *opaque, hwaddr addr,
193 unsigned size)
194{
195 return 0;
196}
197
198static void lx60_io_write(void *opaque, hwaddr addr,
199 uint64_t val, unsigned size)
200{
201}
202
203static const MemoryRegionOps lx60_io_ops = {
204 .read = lx60_io_read,
205 .write = lx60_io_write,
206 .endianness = DEVICE_NATIVE_ENDIAN,
207};
208
3ef96221 209static void lx_init(const LxBoardDesc *board, MachineState *machine)
0200db65
MF
210{
211#ifdef TARGET_WORDS_BIGENDIAN
212 int be = 1;
213#else
214 int be = 0;
215#endif
216 MemoryRegion *system_memory = get_system_memory();
adbb0f75 217 XtensaCPU *cpu = NULL;
5bfcb36e 218 CPUXtensaState *env = NULL;
0200db65 219 MemoryRegion *ram, *rom, *system_io;
82b25dc8
MF
220 DriveInfo *dinfo;
221 pflash_t *flash = NULL;
37b259d0 222 QemuOpts *machine_opts = qemu_get_machine_opts();
3ef96221 223 const char *cpu_model = machine->cpu_model;
37b259d0
MF
224 const char *kernel_filename = qemu_opt_get(machine_opts, "kernel");
225 const char *kernel_cmdline = qemu_opt_get(machine_opts, "append");
996dfe98 226 const char *dtb_filename = qemu_opt_get(machine_opts, "dtb");
f55b32e7 227 const char *initrd_filename = qemu_opt_get(machine_opts, "initrd");
0200db65
MF
228 int n;
229
82b25dc8 230 if (!cpu_model) {
e38077ff 231 cpu_model = XTENSA_DEFAULT_CPU_MODEL;
82b25dc8
MF
232 }
233
0200db65 234 for (n = 0; n < smp_cpus; n++) {
adbb0f75
AF
235 cpu = cpu_xtensa_init(cpu_model);
236 if (cpu == NULL) {
ebbb419a 237 error_report("unable to find CPU definition '%s'",
8488ab02
MF
238 cpu_model);
239 exit(EXIT_FAILURE);
0200db65 240 }
adbb0f75
AF
241 env = &cpu->env;
242
0200db65 243 env->sregs[PRID] = n;
eded1267 244 qemu_register_reset(lx60_reset, cpu);
0200db65
MF
245 /* Need MMU initialized prior to ELF loading,
246 * so that ELF gets loaded into virtual addresses
247 */
adbb0f75 248 cpu_reset(CPU(cpu));
0200db65
MF
249 }
250
251 ram = g_malloc(sizeof(*ram));
49946538 252 memory_region_init_ram(ram, NULL, "lx60.dram", machine->ram_size,
f8ed85ac 253 &error_fatal);
c5705a77 254 vmstate_register_ram_global(ram);
0200db65
MF
255 memory_region_add_subregion(system_memory, 0, ram);
256
0200db65 257 system_io = g_malloc(sizeof(*system_io));
8bb3b575
MF
258 memory_region_init_io(system_io, NULL, &lx60_io_ops, NULL, "lx60.io",
259 224 * 1024 * 1024);
0200db65
MF
260 memory_region_add_subregion(system_memory, 0xf0000000, system_io);
261 lx60_fpga_init(system_io, 0x0d020000);
a005d073 262 if (nd_table[0].used) {
0200db65
MF
263 lx60_net_init(system_io, 0x0d030000, 0x0d030400, 0x0d800000,
264 xtensa_get_extint(env, 1), nd_table);
265 }
266
267 if (!serial_hds[0]) {
268 serial_hds[0] = qemu_chr_new("serial0", "null", NULL);
269 }
270
271 serial_mm_init(system_io, 0x0d050020, 2, xtensa_get_extint(env, 0),
272 115200, serial_hds[0], DEVICE_NATIVE_ENDIAN);
273
82b25dc8
MF
274 dinfo = drive_get(IF_PFLASH, 0, 0);
275 if (dinfo) {
68931a40 276 flash = xtfpga_flash_init(system_io, board, dinfo, be);
82b25dc8
MF
277 }
278
279 /* Use presence of kernel file name as 'boot from SRAM' switch. */
0200db65 280 if (kernel_filename) {
364d4802 281 uint32_t entry_point = env->pc;
b6edea8b 282 size_t bp_size = 3 * get_tag_size(0); /* first/last and memory tags */
a9a28591
MF
283 uint32_t tagptr = 0xfe000000 + board->sram_size;
284 uint32_t cur_tagptr;
b6edea8b
MF
285 BpMemInfo memory_location = {
286 .type = tswap32(MEMORY_TYPE_CONVENTIONAL),
287 .start = tswap32(0),
288 .end = tswap32(machine->ram_size),
289 };
996dfe98
MF
290 uint32_t lowmem_end = machine->ram_size < 0x08000000 ?
291 machine->ram_size : 0x08000000;
292 uint32_t cur_lowmem = QEMU_ALIGN_UP(lowmem_end / 2, 4096);
a9a28591 293
292627bb 294 rom = g_malloc(sizeof(*rom));
49946538 295 memory_region_init_ram(rom, NULL, "lx60.sram", board->sram_size,
f8ed85ac 296 &error_fatal);
c5705a77 297 vmstate_register_ram_global(rom);
292627bb
MF
298 memory_region_add_subregion(system_memory, 0xfe000000, rom);
299
a9a28591
MF
300 if (kernel_cmdline) {
301 bp_size += get_tag_size(strlen(kernel_cmdline) + 1);
302 }
996dfe98
MF
303 if (dtb_filename) {
304 bp_size += get_tag_size(sizeof(uint32_t));
305 }
f55b32e7
MF
306 if (initrd_filename) {
307 bp_size += get_tag_size(sizeof(BpMemInfo));
308 }
a9a28591 309
292627bb 310 /* Put kernel bootparameters to the end of that SRAM */
a9a28591
MF
311 tagptr = (tagptr - bp_size) & ~0xff;
312 cur_tagptr = put_tag(tagptr, BP_TAG_FIRST, 0, NULL);
b6edea8b
MF
313 cur_tagptr = put_tag(cur_tagptr, BP_TAG_MEMORY,
314 sizeof(memory_location), &memory_location);
a9a28591 315
292627bb 316 if (kernel_cmdline) {
a9a28591
MF
317 cur_tagptr = put_tag(cur_tagptr, BP_TAG_COMMAND_LINE,
318 strlen(kernel_cmdline) + 1, kernel_cmdline);
292627bb 319 }
996dfe98
MF
320 if (dtb_filename) {
321 int fdt_size;
322 void *fdt = load_device_tree(dtb_filename, &fdt_size);
323 uint32_t dtb_addr = tswap32(cur_lowmem);
324
325 if (!fdt) {
ebbb419a 326 error_report("could not load DTB '%s'", dtb_filename);
996dfe98
MF
327 exit(EXIT_FAILURE);
328 }
329
330 cpu_physical_memory_write(cur_lowmem, fdt, fdt_size);
331 cur_tagptr = put_tag(cur_tagptr, BP_TAG_FDT,
332 sizeof(dtb_addr), &dtb_addr);
333 cur_lowmem = QEMU_ALIGN_UP(cur_lowmem + fdt_size, 4096);
334 }
f55b32e7
MF
335 if (initrd_filename) {
336 BpMemInfo initrd_location = { 0 };
337 int initrd_size = load_ramdisk(initrd_filename, cur_lowmem,
338 lowmem_end - cur_lowmem);
339
340 if (initrd_size < 0) {
341 initrd_size = load_image_targphys(initrd_filename,
342 cur_lowmem,
343 lowmem_end - cur_lowmem);
344 }
345 if (initrd_size < 0) {
ebbb419a 346 error_report("could not load initrd '%s'", initrd_filename);
f55b32e7
MF
347 exit(EXIT_FAILURE);
348 }
349 initrd_location.start = tswap32(cur_lowmem);
350 initrd_location.end = tswap32(cur_lowmem + initrd_size);
351 cur_tagptr = put_tag(cur_tagptr, BP_TAG_INITRD,
352 sizeof(initrd_location), &initrd_location);
353 cur_lowmem = QEMU_ALIGN_UP(cur_lowmem + initrd_size, 4096);
354 }
a9a28591
MF
355 cur_tagptr = put_tag(cur_tagptr, BP_TAG_LAST, 0, NULL);
356 env->regs[2] = tagptr;
357
0200db65
MF
358 uint64_t elf_entry;
359 uint64_t elf_lowaddr;
00b941e5 360 int success = load_elf(kernel_filename, translate_phys_addr, cpu,
7ef295ea 361 &elf_entry, &elf_lowaddr, NULL, be, EM_XTENSA, 0, 0);
0200db65 362 if (success > 0) {
364d4802
MF
363 entry_point = elf_entry;
364 } else {
365 hwaddr ep;
366 int is_linux;
25bda50a 367 success = load_uimage(kernel_filename, &ep, NULL, &is_linux,
6d2e4530 368 translate_phys_addr, cpu);
364d4802
MF
369 if (success > 0 && is_linux) {
370 entry_point = ep;
371 } else {
ebbb419a 372 error_report("could not load kernel '%s'",
364d4802
MF
373 kernel_filename);
374 exit(EXIT_FAILURE);
375 }
376 }
377 if (entry_point != env->pc) {
378 static const uint8_t jx_a0[] = {
379#ifdef TARGET_WORDS_BIGENDIAN
380 0x0a, 0, 0,
381#else
382 0xa0, 0, 0,
383#endif
384 };
385 env->regs[0] = entry_point;
386 cpu_physical_memory_write(env->pc, jx_a0, sizeof(jx_a0));
0200db65 387 }
82b25dc8
MF
388 } else {
389 if (flash) {
390 MemoryRegion *flash_mr = pflash_cfi01_get_memory(flash);
391 MemoryRegion *flash_io = g_malloc(sizeof(*flash_io));
392
2c9b15ca 393 memory_region_init_alias(flash_io, NULL, "lx60.flash",
37ed7c4b
MF
394 flash_mr, board->flash_boot_base,
395 board->flash_size - board->flash_boot_base < 0x02000000 ?
396 board->flash_size - board->flash_boot_base : 0x02000000);
82b25dc8
MF
397 memory_region_add_subregion(system_memory, 0xfe000000,
398 flash_io);
399 }
0200db65
MF
400 }
401}
402
3ef96221 403static void xtensa_lx60_init(MachineState *machine)
0200db65 404{
82b25dc8 405 static const LxBoardDesc lx60_board = {
68931a40 406 .flash_base = 0x08000000,
e0db904d 407 .flash_size = 0x00400000,
82b25dc8
MF
408 .flash_sector_size = 0x10000,
409 .sram_size = 0x20000,
410 };
3ef96221 411 lx_init(&lx60_board, machine);
82b25dc8
MF
412}
413
3ef96221 414static void xtensa_lx200_init(MachineState *machine)
82b25dc8
MF
415{
416 static const LxBoardDesc lx200_board = {
68931a40 417 .flash_base = 0x08000000,
e0db904d 418 .flash_size = 0x01000000,
82b25dc8
MF
419 .flash_sector_size = 0x20000,
420 .sram_size = 0x2000000,
421 };
3ef96221 422 lx_init(&lx200_board, machine);
0200db65
MF
423}
424
3ef96221 425static void xtensa_ml605_init(MachineState *machine)
e0db904d
MF
426{
427 static const LxBoardDesc ml605_board = {
68931a40 428 .flash_base = 0x08000000,
12004c9e 429 .flash_size = 0x01000000,
e0db904d
MF
430 .flash_sector_size = 0x20000,
431 .sram_size = 0x2000000,
432 };
3ef96221 433 lx_init(&ml605_board, machine);
e0db904d
MF
434}
435
3ef96221 436static void xtensa_kc705_init(MachineState *machine)
e0db904d
MF
437{
438 static const LxBoardDesc kc705_board = {
68931a40 439 .flash_base = 0x00000000,
e0db904d 440 .flash_size = 0x08000000,
37ed7c4b 441 .flash_boot_base = 0x06000000,
e0db904d
MF
442 .flash_sector_size = 0x20000,
443 .sram_size = 0x2000000,
444 };
3ef96221 445 lx_init(&kc705_board, machine);
e0db904d
MF
446}
447
8a661aea 448static void xtensa_lx60_class_init(ObjectClass *oc, void *data)
e264d29d 449{
8a661aea
AF
450 MachineClass *mc = MACHINE_CLASS(oc);
451
e264d29d
EH
452 mc->desc = "lx60 EVB (" XTENSA_DEFAULT_CPU_MODEL ")";
453 mc->init = xtensa_lx60_init;
454 mc->max_cpus = 4;
455}
0200db65 456
8a661aea
AF
457static const TypeInfo xtensa_lx60_type = {
458 .name = MACHINE_TYPE_NAME("lx60"),
459 .parent = TYPE_MACHINE,
460 .class_init = xtensa_lx60_class_init,
461};
82b25dc8 462
8a661aea 463static void xtensa_lx200_class_init(ObjectClass *oc, void *data)
e264d29d 464{
8a661aea
AF
465 MachineClass *mc = MACHINE_CLASS(oc);
466
e264d29d
EH
467 mc->desc = "lx200 EVB (" XTENSA_DEFAULT_CPU_MODEL ")";
468 mc->init = xtensa_lx200_init;
469 mc->max_cpus = 4;
470}
e0db904d 471
8a661aea
AF
472static const TypeInfo xtensa_lx200_type = {
473 .name = MACHINE_TYPE_NAME("lx200"),
474 .parent = TYPE_MACHINE,
475 .class_init = xtensa_lx200_class_init,
476};
e264d29d 477
8a661aea 478static void xtensa_ml605_class_init(ObjectClass *oc, void *data)
e264d29d 479{
8a661aea
AF
480 MachineClass *mc = MACHINE_CLASS(oc);
481
e264d29d
EH
482 mc->desc = "ml605 EVB (" XTENSA_DEFAULT_CPU_MODEL ")";
483 mc->init = xtensa_ml605_init;
484 mc->max_cpus = 4;
485}
486
8a661aea
AF
487static const TypeInfo xtensa_ml605_type = {
488 .name = MACHINE_TYPE_NAME("ml605"),
489 .parent = TYPE_MACHINE,
490 .class_init = xtensa_ml605_class_init,
491};
e0db904d 492
8a661aea 493static void xtensa_kc705_class_init(ObjectClass *oc, void *data)
0200db65 494{
8a661aea
AF
495 MachineClass *mc = MACHINE_CLASS(oc);
496
e264d29d
EH
497 mc->desc = "kc705 EVB (" XTENSA_DEFAULT_CPU_MODEL ")";
498 mc->init = xtensa_kc705_init;
499 mc->max_cpus = 4;
0200db65
MF
500}
501
8a661aea
AF
502static const TypeInfo xtensa_kc705_type = {
503 .name = MACHINE_TYPE_NAME("kc705"),
504 .parent = TYPE_MACHINE,
505 .class_init = xtensa_kc705_class_init,
506};
507
508static void xtensa_lx_machines_init(void)
509{
510 type_register_static(&xtensa_lx60_type);
511 type_register_static(&xtensa_lx200_type);
512 type_register_static(&xtensa_ml605_type);
513 type_register_static(&xtensa_kc705_type);
514}
515
0e6aac87 516type_init(xtensa_lx_machines_init)