]> Git Repo - qemu.git/blob - hw/xtensa/xtfpga.c
hw/xtensa: remove extraneous xtensa_ prefix from file names
[qemu.git] / hw / xtensa / xtfpga.c
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
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 "bootparam.h"
41
42 typedef struct LxBoardDesc {
43     hwaddr flash_base;
44     size_t flash_size;
45     size_t flash_boot_base;
46     size_t flash_sector_size;
47     size_t sram_size;
48 } LxBoardDesc;
49
50 typedef struct Lx60FpgaState {
51     MemoryRegion iomem;
52     uint32_t leds;
53     uint32_t switches;
54 } Lx60FpgaState;
55
56 static void lx60_fpga_reset(void *opaque)
57 {
58     Lx60FpgaState *s = opaque;
59
60     s->leds = 0;
61     s->switches = 0;
62 }
63
64 static uint64_t lx60_fpga_read(void *opaque, hwaddr addr,
65         unsigned size)
66 {
67     Lx60FpgaState *s = opaque;
68
69     switch (addr) {
70     case 0x0: /*build date code*/
71         return 0x09272011;
72
73     case 0x4: /*processor clock frequency, Hz*/
74         return 10000000;
75
76     case 0x8: /*LEDs (off = 0, on = 1)*/
77         return s->leds;
78
79     case 0xc: /*DIP switches (off = 0, on = 1)*/
80         return s->switches;
81     }
82     return 0;
83 }
84
85 static void lx60_fpga_write(void *opaque, hwaddr addr,
86         uint64_t val, unsigned size)
87 {
88     Lx60FpgaState *s = opaque;
89
90     switch (addr) {
91     case 0x8: /*LEDs (off = 0, on = 1)*/
92         s->leds = val;
93         break;
94
95     case 0x10: /*board reset*/
96         if (val == 0xdead) {
97             qemu_system_reset_request();
98         }
99         break;
100     }
101 }
102
103 static const MemoryRegionOps lx60_fpga_ops = {
104     .read = lx60_fpga_read,
105     .write = lx60_fpga_write,
106     .endianness = DEVICE_NATIVE_ENDIAN,
107 };
108
109 static Lx60FpgaState *lx60_fpga_init(MemoryRegion *address_space,
110         hwaddr base)
111 {
112     Lx60FpgaState *s = g_malloc(sizeof(Lx60FpgaState));
113
114     memory_region_init_io(&s->iomem, NULL, &lx60_fpga_ops, s,
115             "lx60.fpga", 0x10000);
116     memory_region_add_subregion(address_space, base, &s->iomem);
117     lx60_fpga_reset(s);
118     qemu_register_reset(lx60_fpga_reset, s);
119     return s;
120 }
121
122 static void lx60_net_init(MemoryRegion *address_space,
123         hwaddr base,
124         hwaddr descriptors,
125         hwaddr buffers,
126         qemu_irq irq, NICInfo *nd)
127 {
128     DeviceState *dev;
129     SysBusDevice *s;
130     MemoryRegion *ram;
131
132     dev = qdev_create(NULL, "open_eth");
133     qdev_set_nic_properties(dev, nd);
134     qdev_init_nofail(dev);
135
136     s = SYS_BUS_DEVICE(dev);
137     sysbus_connect_irq(s, 0, irq);
138     memory_region_add_subregion(address_space, base,
139             sysbus_mmio_get_region(s, 0));
140     memory_region_add_subregion(address_space, descriptors,
141             sysbus_mmio_get_region(s, 1));
142
143     ram = g_malloc(sizeof(*ram));
144     memory_region_init_ram(ram, OBJECT(s), "open_eth.ram", 16384);
145     vmstate_register_ram_global(ram);
146     memory_region_add_subregion(address_space, buffers, ram);
147 }
148
149 static uint64_t translate_phys_addr(void *opaque, uint64_t addr)
150 {
151     XtensaCPU *cpu = opaque;
152
153     return cpu_get_phys_page_debug(CPU(cpu), addr);
154 }
155
156 static void lx60_reset(void *opaque)
157 {
158     XtensaCPU *cpu = opaque;
159
160     cpu_reset(CPU(cpu));
161 }
162
163 static void lx_init(const LxBoardDesc *board, MachineState *machine)
164 {
165 #ifdef TARGET_WORDS_BIGENDIAN
166     int be = 1;
167 #else
168     int be = 0;
169 #endif
170     MemoryRegion *system_memory = get_system_memory();
171     XtensaCPU *cpu = NULL;
172     CPUXtensaState *env = NULL;
173     MemoryRegion *ram, *rom, *system_io;
174     DriveInfo *dinfo;
175     pflash_t *flash = NULL;
176     const char *cpu_model = machine->cpu_model;
177     const char *kernel_filename = machine->kernel_filename;
178     const char *kernel_cmdline = machine->kernel_cmdline;
179     int n;
180
181     if (!cpu_model) {
182         cpu_model = XTENSA_DEFAULT_CPU_MODEL;
183     }
184
185     for (n = 0; n < smp_cpus; n++) {
186         cpu = cpu_xtensa_init(cpu_model);
187         if (cpu == NULL) {
188             fprintf(stderr, "Unable to find CPU definition\n");
189             exit(1);
190         }
191         env = &cpu->env;
192
193         env->sregs[PRID] = n;
194         qemu_register_reset(lx60_reset, cpu);
195         /* Need MMU initialized prior to ELF loading,
196          * so that ELF gets loaded into virtual addresses
197          */
198         cpu_reset(CPU(cpu));
199     }
200
201     ram = g_malloc(sizeof(*ram));
202     memory_region_init_ram(ram, NULL, "lx60.dram", machine->ram_size);
203     vmstate_register_ram_global(ram);
204     memory_region_add_subregion(system_memory, 0, ram);
205
206     system_io = g_malloc(sizeof(*system_io));
207     memory_region_init(system_io, NULL, "lx60.io", 224 * 1024 * 1024);
208     memory_region_add_subregion(system_memory, 0xf0000000, system_io);
209     lx60_fpga_init(system_io, 0x0d020000);
210     if (nd_table[0].used) {
211         lx60_net_init(system_io, 0x0d030000, 0x0d030400, 0x0d800000,
212                 xtensa_get_extint(env, 1), nd_table);
213     }
214
215     if (!serial_hds[0]) {
216         serial_hds[0] = qemu_chr_new("serial0", "null", NULL);
217     }
218
219     serial_mm_init(system_io, 0x0d050020, 2, xtensa_get_extint(env, 0),
220             115200, serial_hds[0], DEVICE_NATIVE_ENDIAN);
221
222     dinfo = drive_get(IF_PFLASH, 0, 0);
223     if (dinfo) {
224         flash = pflash_cfi01_register(board->flash_base,
225                 NULL, "lx60.io.flash", board->flash_size,
226                 dinfo->bdrv, board->flash_sector_size,
227                 board->flash_size / board->flash_sector_size,
228                 4, 0x0000, 0x0000, 0x0000, 0x0000, be);
229         if (flash == NULL) {
230             fprintf(stderr, "Unable to mount pflash\n");
231             exit(1);
232         }
233     }
234
235     /* Use presence of kernel file name as 'boot from SRAM' switch. */
236     if (kernel_filename) {
237         rom = g_malloc(sizeof(*rom));
238         memory_region_init_ram(rom, NULL, "lx60.sram", board->sram_size);
239         vmstate_register_ram_global(rom);
240         memory_region_add_subregion(system_memory, 0xfe000000, rom);
241
242         /* Put kernel bootparameters to the end of that SRAM */
243         if (kernel_cmdline) {
244             size_t cmdline_size = strlen(kernel_cmdline) + 1;
245             size_t bp_size = sizeof(BpTag[4]) + cmdline_size;
246             uint32_t tagptr = (0xfe000000 + board->sram_size - bp_size) & ~0xff;
247
248             env->regs[2] = tagptr;
249
250             tagptr = put_tag(tagptr, 0x7b0b, 0, NULL);
251             if (cmdline_size > 1) {
252                 tagptr = put_tag(tagptr, 0x1001,
253                         cmdline_size, kernel_cmdline);
254             }
255             tagptr = put_tag(tagptr, 0x7e0b, 0, NULL);
256         }
257         uint64_t elf_entry;
258         uint64_t elf_lowaddr;
259         int success = load_elf(kernel_filename, translate_phys_addr, cpu,
260                 &elf_entry, &elf_lowaddr, NULL, be, ELF_MACHINE, 0);
261         if (success > 0) {
262             env->pc = elf_entry;
263         }
264     } else {
265         if (flash) {
266             MemoryRegion *flash_mr = pflash_cfi01_get_memory(flash);
267             MemoryRegion *flash_io = g_malloc(sizeof(*flash_io));
268
269             memory_region_init_alias(flash_io, NULL, "lx60.flash",
270                     flash_mr, board->flash_boot_base,
271                     board->flash_size - board->flash_boot_base < 0x02000000 ?
272                     board->flash_size - board->flash_boot_base : 0x02000000);
273             memory_region_add_subregion(system_memory, 0xfe000000,
274                     flash_io);
275         }
276     }
277 }
278
279 static void xtensa_lx60_init(MachineState *machine)
280 {
281     static const LxBoardDesc lx60_board = {
282         .flash_base = 0xf8000000,
283         .flash_size = 0x00400000,
284         .flash_sector_size = 0x10000,
285         .sram_size = 0x20000,
286     };
287     lx_init(&lx60_board, machine);
288 }
289
290 static void xtensa_lx200_init(MachineState *machine)
291 {
292     static const LxBoardDesc lx200_board = {
293         .flash_base = 0xf8000000,
294         .flash_size = 0x01000000,
295         .flash_sector_size = 0x20000,
296         .sram_size = 0x2000000,
297     };
298     lx_init(&lx200_board, machine);
299 }
300
301 static void xtensa_ml605_init(MachineState *machine)
302 {
303     static const LxBoardDesc ml605_board = {
304         .flash_base = 0xf8000000,
305         .flash_size = 0x02000000,
306         .flash_sector_size = 0x20000,
307         .sram_size = 0x2000000,
308     };
309     lx_init(&ml605_board, machine);
310 }
311
312 static void xtensa_kc705_init(MachineState *machine)
313 {
314     static const LxBoardDesc kc705_board = {
315         .flash_base = 0xf0000000,
316         .flash_size = 0x08000000,
317         .flash_boot_base = 0x06000000,
318         .flash_sector_size = 0x20000,
319         .sram_size = 0x2000000,
320     };
321     lx_init(&kc705_board, machine);
322 }
323
324 static QEMUMachine xtensa_lx60_machine = {
325     .name = "lx60",
326     .desc = "lx60 EVB (" XTENSA_DEFAULT_CPU_MODEL ")",
327     .init = xtensa_lx60_init,
328     .max_cpus = 4,
329 };
330
331 static QEMUMachine xtensa_lx200_machine = {
332     .name = "lx200",
333     .desc = "lx200 EVB (" XTENSA_DEFAULT_CPU_MODEL ")",
334     .init = xtensa_lx200_init,
335     .max_cpus = 4,
336 };
337
338 static QEMUMachine xtensa_ml605_machine = {
339     .name = "ml605",
340     .desc = "ml605 EVB (" XTENSA_DEFAULT_CPU_MODEL ")",
341     .init = xtensa_ml605_init,
342     .max_cpus = 4,
343 };
344
345 static QEMUMachine xtensa_kc705_machine = {
346     .name = "kc705",
347     .desc = "kc705 EVB (" XTENSA_DEFAULT_CPU_MODEL ")",
348     .init = xtensa_kc705_init,
349     .max_cpus = 4,
350 };
351
352 static void xtensa_lx_machines_init(void)
353 {
354     qemu_register_machine(&xtensa_lx60_machine);
355     qemu_register_machine(&xtensa_lx200_machine);
356     qemu_register_machine(&xtensa_ml605_machine);
357     qemu_register_machine(&xtensa_kc705_machine);
358 }
359
360 machine_init(xtensa_lx_machines_init);
This page took 0.044298 seconds and 4 git commands to generate.