meson: convert hw/vfio
[qemu.git] / hw / lm32 / milkymist.c
1 /*
2 * QEMU model for the Milkymist board.
3 *
4 * Copyright (c) 2010 Michael Walle <michael@walle.cc>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "qemu/osdep.h"
21 #include "qemu/units.h"
22 #include "qemu/error-report.h"
23 #include "qemu-common.h"
24 #include "cpu.h"
25 #include "hw/sysbus.h"
26 #include "hw/irq.h"
27 #include "hw/block/flash.h"
28 #include "sysemu/sysemu.h"
29 #include "sysemu/qtest.h"
30 #include "sysemu/reset.h"
31 #include "hw/boards.h"
32 #include "hw/loader.h"
33 #include "hw/qdev-properties.h"
34 #include "elf.h"
35 #include "milkymist-hw.h"
36 #include "hw/display/milkymist_tmu2.h"
37 #include "lm32.h"
38 #include "exec/address-spaces.h"
39 #include "qemu/cutils.h"
40
41 #define BIOS_FILENAME "mmone-bios.bin"
42 #define BIOS_OFFSET 0x00860000
43 #define BIOS_SIZE (512 * KiB)
44 #define KERNEL_LOAD_ADDR 0x40000000
45
46 typedef struct {
47 LM32CPU *cpu;
48 hwaddr bootstrap_pc;
49 hwaddr flash_base;
50 hwaddr initrd_base;
51 size_t initrd_size;
52 hwaddr cmdline_base;
53 } ResetInfo;
54
55 static void cpu_irq_handler(void *opaque, int irq, int level)
56 {
57 LM32CPU *cpu = opaque;
58 CPUState *cs = CPU(cpu);
59
60 if (level) {
61 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
62 } else {
63 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
64 }
65 }
66
67 static void main_cpu_reset(void *opaque)
68 {
69 ResetInfo *reset_info = opaque;
70 CPULM32State *env = &reset_info->cpu->env;
71
72 cpu_reset(CPU(reset_info->cpu));
73
74 /* init defaults */
75 env->pc = reset_info->bootstrap_pc;
76 env->regs[R_R1] = reset_info->cmdline_base;
77 env->regs[R_R2] = reset_info->initrd_base;
78 env->regs[R_R3] = reset_info->initrd_base + reset_info->initrd_size;
79 env->eba = reset_info->flash_base;
80 env->deba = reset_info->flash_base;
81 }
82
83 static void
84 milkymist_init(MachineState *machine)
85 {
86 MachineClass *mc = MACHINE_GET_CLASS(machine);
87 const char *kernel_filename = machine->kernel_filename;
88 const char *kernel_cmdline = machine->kernel_cmdline;
89 const char *initrd_filename = machine->initrd_filename;
90 LM32CPU *cpu;
91 CPULM32State *env;
92 int kernel_size;
93 DriveInfo *dinfo;
94 MemoryRegion *address_space_mem = get_system_memory();
95 qemu_irq irq[32];
96 int i;
97 char *bios_filename;
98 ResetInfo *reset_info;
99
100 if (machine->ram_size != mc->default_ram_size) {
101 char *sz = size_to_str(mc->default_ram_size);
102 error_report("Invalid RAM size, should be %s", sz);
103 g_free(sz);
104 exit(EXIT_FAILURE);
105 }
106
107 /* memory map */
108 hwaddr flash_base = 0x00000000;
109 size_t flash_sector_size = 128 * KiB;
110 size_t flash_size = 32 * MiB;
111 hwaddr sdram_base = 0x40000000;
112
113 hwaddr initrd_base = sdram_base + 0x1002000;
114 hwaddr cmdline_base = sdram_base + 0x1000000;
115 size_t initrd_max = machine->ram_size - 0x1002000;
116
117 reset_info = g_malloc0(sizeof(ResetInfo));
118
119 cpu = LM32_CPU(cpu_create(machine->cpu_type));
120
121 env = &cpu->env;
122 reset_info->cpu = cpu;
123
124 cpu_lm32_set_phys_msb_ignore(env, 1);
125
126 memory_region_add_subregion(address_space_mem, sdram_base, machine->ram);
127
128 dinfo = drive_get(IF_PFLASH, 0, 0);
129 /* Numonyx JS28F256J3F105 */
130 pflash_cfi01_register(flash_base, "milkymist.flash", flash_size,
131 dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
132 flash_sector_size, 2, 0x00, 0x89, 0x00, 0x1d, 1);
133
134 /* create irq lines */
135 env->pic_state = lm32_pic_init(qemu_allocate_irq(cpu_irq_handler, cpu, 0));
136 for (i = 0; i < 32; i++) {
137 irq[i] = qdev_get_gpio_in(env->pic_state, i);
138 }
139
140 /* load bios rom */
141 if (bios_name == NULL) {
142 bios_name = BIOS_FILENAME;
143 }
144 bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
145
146 if (bios_filename) {
147 if (load_image_targphys(bios_filename, BIOS_OFFSET, BIOS_SIZE) < 0) {
148 error_report("could not load bios '%s'", bios_filename);
149 exit(1);
150 }
151 }
152
153 reset_info->bootstrap_pc = BIOS_OFFSET;
154
155 /* if no kernel is given no valid bios rom is a fatal error */
156 if (!kernel_filename && !dinfo && !bios_filename && !qtest_enabled()) {
157 error_report("could not load Milkymist One bios '%s'", bios_name);
158 exit(1);
159 }
160 g_free(bios_filename);
161
162 milkymist_uart_create(0x60000000, irq[0], serial_hd(0));
163 milkymist_sysctl_create(0x60001000, irq[1], irq[2], irq[3],
164 80000000, 0x10014d31, 0x0000041f, 0x00000001);
165 milkymist_hpdmc_create(0x60002000);
166 milkymist_vgafb_create(0x60003000, 0x40000000, 0x0fffffff);
167 milkymist_memcard_create(0x60004000);
168 milkymist_ac97_create(0x60005000, irq[4], irq[5], irq[6], irq[7]);
169 milkymist_pfpu_create(0x60006000, irq[8]);
170 if (machine->enable_graphics) {
171 milkymist_tmu2_create(0x60007000, irq[9]);
172 }
173 milkymist_minimac2_create(0x60008000, 0x30000000, irq[10], irq[11]);
174 milkymist_softusb_create(0x6000f000, irq[15],
175 0x20000000, 0x1000, 0x20020000, 0x2000);
176
177 /* make sure juart isn't the first chardev */
178 env->juart_state = lm32_juart_init(serial_hd(1));
179
180 if (kernel_filename) {
181 uint64_t entry;
182
183 /* Boots a kernel elf binary. */
184 kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
185 &entry, NULL, NULL, NULL,
186 1, EM_LATTICEMICO32, 0, 0);
187 reset_info->bootstrap_pc = entry;
188
189 if (kernel_size < 0) {
190 kernel_size = load_image_targphys(kernel_filename, sdram_base,
191 machine->ram_size);
192 reset_info->bootstrap_pc = sdram_base;
193 }
194
195 if (kernel_size < 0) {
196 error_report("could not load kernel '%s'", kernel_filename);
197 exit(1);
198 }
199 }
200
201 if (kernel_cmdline && strlen(kernel_cmdline)) {
202 pstrcpy_targphys("cmdline", cmdline_base, TARGET_PAGE_SIZE,
203 kernel_cmdline);
204 reset_info->cmdline_base = (uint32_t)cmdline_base;
205 }
206
207 if (initrd_filename) {
208 size_t initrd_size;
209 initrd_size = load_image_targphys(initrd_filename, initrd_base,
210 initrd_max);
211 reset_info->initrd_base = (uint32_t)initrd_base;
212 reset_info->initrd_size = (uint32_t)initrd_size;
213 }
214
215 qemu_register_reset(main_cpu_reset, reset_info);
216 }
217
218 static void milkymist_machine_init(MachineClass *mc)
219 {
220 mc->desc = "Milkymist One";
221 mc->init = milkymist_init;
222 mc->default_cpu_type = LM32_CPU_TYPE_NAME("lm32-full");
223 mc->default_ram_size = 128 * MiB;
224 mc->default_ram_id = "milkymist.sdram";
225 }
226
227 DEFINE_MACHINE("milkymist", milkymist_machine_init)