1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include "hw/sysbus.h"
21#include "hw/hw.h"
22#include "hw/block/flash.h"
23#include "sysemu/sysemu.h"
24#include "hw/devices.h"
25#include "hw/boards.h"
26#include "hw/loader.h"
27#include "elf.h"
28#include "sysemu/blockdev.h"
29#include "milkymist-hw.h"
30#include "lm32.h"
31#include "exec/address-spaces.h"
32
33#define BIOS_FILENAME "mmone-bios.bin"
34#define BIOS_OFFSET 0x00860000
35#define BIOS_SIZE (512*1024)
36#define KERNEL_LOAD_ADDR 0x40000000
37
38typedef struct {
39 LM32CPU *cpu;
40 hwaddr bootstrap_pc;
41 hwaddr flash_base;
42 hwaddr initrd_base;
43 size_t initrd_size;
44 hwaddr cmdline_base;
45} ResetInfo;
46
47static void cpu_irq_handler(void *opaque, int irq, int level)
48{
49 LM32CPU *cpu = opaque;
50 CPUState *cs = CPU(cpu);
51
52 if (level) {
53 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
54 } else {
55 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
56 }
57}
58
59static void main_cpu_reset(void *opaque)
60{
61 ResetInfo *reset_info = opaque;
62 CPULM32State *env = &reset_info->cpu->env;
63
64 cpu_reset(CPU(reset_info->cpu));
65
66
67 env->pc = reset_info->bootstrap_pc;
68 env->regs[R_R1] = reset_info->cmdline_base;
69 env->regs[R_R2] = reset_info->initrd_base;
70 env->regs[R_R3] = reset_info->initrd_base + reset_info->initrd_size;
71 env->eba = reset_info->flash_base;
72 env->deba = reset_info->flash_base;
73}
74
75static void
76milkymist_init(QEMUMachineInitArgs *args)
77{
78 const char *cpu_model = args->cpu_model;
79 const char *kernel_filename = args->kernel_filename;
80 const char *kernel_cmdline = args->kernel_cmdline;
81 const char *initrd_filename = args->initrd_filename;
82 LM32CPU *cpu;
83 CPULM32State *env;
84 int kernel_size;
85 DriveInfo *dinfo;
86 MemoryRegion *address_space_mem = get_system_memory();
87 MemoryRegion *phys_sdram = g_new(MemoryRegion, 1);
88 qemu_irq irq[32], *cpu_irq;
89 int i;
90 char *bios_filename;
91 ResetInfo *reset_info;
92
93
94 hwaddr flash_base = 0x00000000;
95 size_t flash_sector_size = 128 * 1024;
96 size_t flash_size = 32 * 1024 * 1024;
97 hwaddr sdram_base = 0x40000000;
98 size_t sdram_size = 128 * 1024 * 1024;
99
100 hwaddr initrd_base = sdram_base + 0x1002000;
101 hwaddr cmdline_base = sdram_base + 0x1000000;
102 size_t initrd_max = sdram_size - 0x1002000;
103
104 reset_info = g_malloc0(sizeof(ResetInfo));
105
106 if (cpu_model == NULL) {
107 cpu_model = "lm32-full";
108 }
109 cpu = cpu_lm32_init(cpu_model);
110 env = &cpu->env;
111 reset_info->cpu = cpu;
112
113 cpu_lm32_set_phys_msb_ignore(env, 1);
114
115 memory_region_init_ram(phys_sdram, "milkymist.sdram", sdram_size);
116 vmstate_register_ram_global(phys_sdram);
117 memory_region_add_subregion(address_space_mem, sdram_base, phys_sdram);
118
119 dinfo = drive_get(IF_PFLASH, 0, 0);
120
121 pflash_cfi01_register(flash_base, NULL, "milkymist.flash", flash_size,
122 dinfo ? dinfo->bdrv : NULL, flash_sector_size,
123 flash_size / flash_sector_size, 2,
124 0x00, 0x89, 0x00, 0x1d, 1);
125
126
127 cpu_irq = qemu_allocate_irqs(cpu_irq_handler, cpu, 1);
128 env->pic_state = lm32_pic_init(*cpu_irq);
129 for (i = 0; i < 32; i++) {
130 irq[i] = qdev_get_gpio_in(env->pic_state, i);
131 }
132
133
134 if (bios_name == NULL) {
135 bios_name = BIOS_FILENAME;
136 }
137 bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
138
139 if (bios_filename) {
140 load_image_targphys(bios_filename, BIOS_OFFSET, BIOS_SIZE);
141 }
142
143 reset_info->bootstrap_pc = BIOS_OFFSET;
144
145
146 if (!kernel_filename && !dinfo && !bios_filename) {
147 fprintf(stderr, "qemu: could not load Milkymist One bios '%s'\n",
148 bios_name);
149 exit(1);
150 }
151
152 milkymist_uart_create(0x60000000, irq[0]);
153 milkymist_sysctl_create(0x60001000, irq[1], irq[2], irq[3],
154 80000000, 0x10014d31, 0x0000041f, 0x00000001);
155 milkymist_hpdmc_create(0x60002000);
156 milkymist_vgafb_create(0x60003000, 0x40000000, 0x0fffffff);
157 milkymist_memcard_create(0x60004000);
158 milkymist_ac97_create(0x60005000, irq[4], irq[5], irq[6], irq[7]);
159 milkymist_pfpu_create(0x60006000, irq[8]);
160 milkymist_tmu2_create(0x60007000, irq[9]);
161 milkymist_minimac2_create(0x60008000, 0x30000000, irq[10], irq[11]);
162 milkymist_softusb_create(0x6000f000, irq[15],
163 0x20000000, 0x1000, 0x20020000, 0x2000);
164
165
166 env->juart_state = lm32_juart_init();
167
168 if (kernel_filename) {
169 uint64_t entry;
170
171
172 kernel_size = load_elf(kernel_filename, NULL, NULL, &entry, NULL, NULL,
173 1, ELF_MACHINE, 0);
174 reset_info->bootstrap_pc = entry;
175
176 if (kernel_size < 0) {
177 kernel_size = load_image_targphys(kernel_filename, sdram_base,
178 sdram_size);
179 reset_info->bootstrap_pc = sdram_base;
180 }
181
182 if (kernel_size < 0) {
183 fprintf(stderr, "qemu: could not load kernel '%s'\n",
184 kernel_filename);
185 exit(1);
186 }
187 }
188
189 if (kernel_cmdline && strlen(kernel_cmdline)) {
190 pstrcpy_targphys("cmdline", cmdline_base, TARGET_PAGE_SIZE,
191 kernel_cmdline);
192 reset_info->cmdline_base = (uint32_t)cmdline_base;
193 }
194
195 if (initrd_filename) {
196 size_t initrd_size;
197 initrd_size = load_image_targphys(initrd_filename, initrd_base,
198 initrd_max);
199 reset_info->initrd_base = (uint32_t)initrd_base;
200 reset_info->initrd_size = (uint32_t)initrd_size;
201 }
202
203 qemu_register_reset(main_cpu_reset, reset_info);
204}
205
206static QEMUMachine milkymist_machine = {
207 .name = "milkymist",
208 .desc = "Milkymist One",
209 .init = milkymist_init,
210 .is_default = 0,
211 DEFAULT_MACHINE_OPTIONS,
212};
213
214static void milkymist_machine_init(void)
215{
216 qemu_register_machine(&milkymist_machine);
217}
218
219machine_init(milkymist_machine_init);
220