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