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 "xtensa_bootparam.h"
42 typedef struct LxBoardDesc
{
45 size_t flash_sector_size
;
49 typedef struct Lx60FpgaState
{
55 static void lx60_fpga_reset(void *opaque
)
57 Lx60FpgaState
*s
= opaque
;
63 static uint64_t lx60_fpga_read(void *opaque
, hwaddr addr
,
66 Lx60FpgaState
*s
= opaque
;
69 case 0x0: /*build date code*/
72 case 0x4: /*processor clock frequency, Hz*/
75 case 0x8: /*LEDs (off = 0, on = 1)*/
78 case 0xc: /*DIP switches (off = 0, on = 1)*/
84 static void lx60_fpga_write(void *opaque
, hwaddr addr
,
85 uint64_t val
, unsigned size
)
87 Lx60FpgaState
*s
= opaque
;
90 case 0x8: /*LEDs (off = 0, on = 1)*/
94 case 0x10: /*board reset*/
96 qemu_system_reset_request();
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
,
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
);
117 qemu_register_reset(lx60_fpga_reset
, s
);
121 static void lx60_net_init(MemoryRegion
*address_space
,
125 qemu_irq irq
, NICInfo
*nd
)
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
;
162 static void lx_init(const LxBoardDesc
*board
, QEMUMachineInitArgs
*args
)
164 #ifdef TARGET_WORDS_BIGENDIAN
169 MemoryRegion
*system_memory
= get_system_memory();
170 XtensaCPU
*cpu
= NULL
;
171 CPUXtensaState
*env
= NULL
;
172 MemoryRegion
*ram
, *rom
, *system_io
;
174 pflash_t
*flash
= NULL
;
175 const char *cpu_model
= args
->cpu_model
;
176 const char *kernel_filename
= args
->kernel_filename
;
177 const char *kernel_cmdline
= args
->kernel_cmdline
;
181 cpu_model
= XTENSA_DEFAULT_CPU_MODEL
;
184 for (n
= 0; n
< smp_cpus
; n
++) {
185 cpu
= cpu_xtensa_init(cpu_model
);
187 fprintf(stderr
, "Unable to find CPU definition\n");
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
200 ram
= g_malloc(sizeof(*ram
));
201 memory_region_init_ram(ram
, NULL
, "lx60.dram", args
->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);
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
);
229 fprintf(stderr
, "Unable to mount pflash\n");
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
);
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);
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",
270 board
->flash_size
< 0x02000000 ?
271 board
->flash_size
: 0x02000000);
272 memory_region_add_subregion(system_memory
, 0xfe000000,
278 static void xtensa_lx60_init(QEMUMachineInitArgs
*args
)
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
, args
);
289 static void xtensa_lx200_init(QEMUMachineInitArgs
*args
)
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
, args
);
300 static void xtensa_ml605_init(QEMUMachineInitArgs
*args
)
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
, args
);
311 static void xtensa_kc705_init(QEMUMachineInitArgs
*args
)
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
, args
);
322 static QEMUMachine xtensa_lx60_machine
= {
324 .desc
= "lx60 EVB (" XTENSA_DEFAULT_CPU_MODEL
")",
325 .init
= xtensa_lx60_init
,
329 static QEMUMachine xtensa_lx200_machine
= {
331 .desc
= "lx200 EVB (" XTENSA_DEFAULT_CPU_MODEL
")",
332 .init
= xtensa_lx200_init
,
336 static QEMUMachine xtensa_ml605_machine
= {
338 .desc
= "ml605 EVB (" XTENSA_DEFAULT_CPU_MODEL
")",
339 .init
= xtensa_ml605_init
,
343 static QEMUMachine xtensa_kc705_machine
= {
345 .desc
= "kc705 EVB (" XTENSA_DEFAULT_CPU_MODEL
")",
346 .init
= xtensa_kc705_init
,
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
);