blob: 689e633199a03bfd518466a8d507f0d523808f7e [file] [log] [blame]
Michael Walle5052d222011-03-07 23:32:44 +01001/*
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
Peter Maydellea99dde2016-01-26 18:16:57 +000020#include "qemu/osdep.h"
Philippe Mathieu-Daudéfc0187c2018-06-25 09:42:19 -030021#include "qemu/units.h"
Alistair Francis0a094a52018-02-03 09:43:05 +010022#include "qemu/error-report.h"
Paolo Bonzini4771d752016-01-19 21:51:44 +010023#include "qemu-common.h"
24#include "cpu.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010025#include "hw/sysbus.h"
26#include "hw/hw.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010027#include "hw/block/flash.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010028#include "sysemu/sysemu.h"
Andreas Färberc00eb5c2013-08-04 16:49:28 +020029#include "sysemu/qtest.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010030#include "hw/boards.h"
31#include "hw/loader.h"
Michael Walle5052d222011-03-07 23:32:44 +010032#include "elf.h"
Paolo Bonzini47b43a12013-03-18 17:36:02 +010033#include "milkymist-hw.h"
Philippe Mathieu-Daudé70cc0c12019-01-30 13:00:05 +010034#include "hw/display/milkymist_tmu2.h"
Paolo Bonzini47b43a12013-03-18 17:36:02 +010035#include "lm32.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010036#include "exec/address-spaces.h"
Michael Walle5052d222011-03-07 23:32:44 +010037
38#define BIOS_FILENAME "mmone-bios.bin"
39#define BIOS_OFFSET 0x00860000
Philippe Mathieu-Daudéfc0187c2018-06-25 09:42:19 -030040#define BIOS_SIZE (512 * KiB)
Michael Walle5052d222011-03-07 23:32:44 +010041#define KERNEL_LOAD_ADDR 0x40000000
42
43typedef struct {
Andreas Färberf6932a82012-05-04 19:05:02 +020044 LM32CPU *cpu;
Avi Kivitya8170e52012-10-23 12:30:10 +020045 hwaddr bootstrap_pc;
46 hwaddr flash_base;
47 hwaddr initrd_base;
Michael Walle5052d222011-03-07 23:32:44 +010048 size_t initrd_size;
Avi Kivitya8170e52012-10-23 12:30:10 +020049 hwaddr cmdline_base;
Michael Walle5052d222011-03-07 23:32:44 +010050} ResetInfo;
51
52static void cpu_irq_handler(void *opaque, int irq, int level)
53{
Andreas Färberd8ed8872013-01-17 22:30:20 +010054 LM32CPU *cpu = opaque;
Andreas Färberd8ed8872013-01-17 22:30:20 +010055 CPUState *cs = CPU(cpu);
Michael Walle5052d222011-03-07 23:32:44 +010056
57 if (level) {
Andreas Färberc3affe52013-01-18 15:03:43 +010058 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
Michael Walle5052d222011-03-07 23:32:44 +010059 } else {
Andreas Färberd8ed8872013-01-17 22:30:20 +010060 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
Michael Walle5052d222011-03-07 23:32:44 +010061 }
62}
63
64static void main_cpu_reset(void *opaque)
65{
66 ResetInfo *reset_info = opaque;
Andreas Färberf6932a82012-05-04 19:05:02 +020067 CPULM32State *env = &reset_info->cpu->env;
Michael Walle5052d222011-03-07 23:32:44 +010068
Andreas Färberf6932a82012-05-04 19:05:02 +020069 cpu_reset(CPU(reset_info->cpu));
Michael Walle5052d222011-03-07 23:32:44 +010070
71 /* init defaults */
72 env->pc = reset_info->bootstrap_pc;
73 env->regs[R_R1] = reset_info->cmdline_base;
74 env->regs[R_R2] = reset_info->initrd_base;
75 env->regs[R_R3] = reset_info->initrd_base + reset_info->initrd_size;
76 env->eba = reset_info->flash_base;
77 env->deba = reset_info->flash_base;
78}
79
80static void
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +030081milkymist_init(MachineState *machine)
Michael Walle5052d222011-03-07 23:32:44 +010082{
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +030083 const char *kernel_filename = machine->kernel_filename;
84 const char *kernel_cmdline = machine->kernel_cmdline;
85 const char *initrd_filename = machine->initrd_filename;
Andreas Färber1328cc02012-05-04 19:03:24 +020086 LM32CPU *cpu;
Andreas Färber93a67402012-03-14 01:38:23 +010087 CPULM32State *env;
Michael Walle5052d222011-03-07 23:32:44 +010088 int kernel_size;
89 DriveInfo *dinfo;
Avi Kivityc50a6de2011-08-08 21:42:57 +030090 MemoryRegion *address_space_mem = get_system_memory();
91 MemoryRegion *phys_sdram = g_new(MemoryRegion, 1);
Shannon Zhaoa9c8a0d2015-05-29 13:27:07 +080092 qemu_irq irq[32];
Michael Walle5052d222011-03-07 23:32:44 +010093 int i;
94 char *bios_filename;
95 ResetInfo *reset_info;
96
97 /* memory map */
Avi Kivitya8170e52012-10-23 12:30:10 +020098 hwaddr flash_base = 0x00000000;
Philippe Mathieu-Daudéfc0187c2018-06-25 09:42:19 -030099 size_t flash_sector_size = 128 * KiB;
100 size_t flash_size = 32 * MiB;
Avi Kivitya8170e52012-10-23 12:30:10 +0200101 hwaddr sdram_base = 0x40000000;
Philippe Mathieu-Daudéfc0187c2018-06-25 09:42:19 -0300102 size_t sdram_size = 128 * MiB;
Michael Walle5052d222011-03-07 23:32:44 +0100103
Avi Kivitya8170e52012-10-23 12:30:10 +0200104 hwaddr initrd_base = sdram_base + 0x1002000;
105 hwaddr cmdline_base = sdram_base + 0x1000000;
Michael Walle5052d222011-03-07 23:32:44 +0100106 size_t initrd_max = sdram_size - 0x1002000;
107
Anthony Liguori7267c092011-08-20 22:09:37 -0500108 reset_info = g_malloc0(sizeof(ResetInfo));
Michael Walle5052d222011-03-07 23:32:44 +0100109
Igor Mammedov201c1602017-10-05 15:50:43 +0200110 cpu = LM32_CPU(cpu_create(machine->cpu_type));
Michael Wallef41152b2013-11-28 19:09:33 +0100111
Andreas Färber1328cc02012-05-04 19:03:24 +0200112 env = &cpu->env;
Andreas Färberf6932a82012-05-04 19:05:02 +0200113 reset_info->cpu = cpu;
Michael Walle5052d222011-03-07 23:32:44 +0100114
115 cpu_lm32_set_phys_msb_ignore(env, 1);
116
Dirk Müllerb7ccb832015-04-04 14:16:18 +0200117 memory_region_allocate_system_memory(phys_sdram, NULL, "milkymist.sdram",
118 sdram_size);
Avi Kivityc50a6de2011-08-08 21:42:57 +0300119 memory_region_add_subregion(address_space_mem, sdram_base, phys_sdram);
Michael Walle5052d222011-03-07 23:32:44 +0100120
Michael Walle5052d222011-03-07 23:32:44 +0100121 dinfo = drive_get(IF_PFLASH, 0, 0);
122 /* Numonyx JS28F256J3F105 */
Markus Armbruster940d5b12019-03-08 10:46:09 +0100123 pflash_cfi01_register(flash_base, "milkymist.flash", flash_size,
Markus Armbruster4be74632014-10-07 13:59:18 +0200124 dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
Markus Armbrusterce147102019-03-08 10:46:10 +0100125 flash_sector_size, 2, 0x00, 0x89, 0x00, 0x1d, 1);
Michael Walle5052d222011-03-07 23:32:44 +0100126
127 /* create irq lines */
Shannon Zhaoa9c8a0d2015-05-29 13:27:07 +0800128 env->pic_state = lm32_pic_init(qemu_allocate_irq(cpu_irq_handler, cpu, 0));
Michael Walle5052d222011-03-07 23:32:44 +0100129 for (i = 0; i < 32; i++) {
130 irq[i] = qdev_get_gpio_in(env->pic_state, i);
131 }
132
133 /* load bios rom */
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) {
Peter Maydell40af11e2018-11-06 11:32:14 +0000140 if (load_image_targphys(bios_filename, BIOS_OFFSET, BIOS_SIZE) < 0) {
141 error_report("could not load bios '%s'", bios_filename);
142 exit(1);
143 }
Michael Walle5052d222011-03-07 23:32:44 +0100144 }
145
146 reset_info->bootstrap_pc = BIOS_OFFSET;
147
148 /* if no kernel is given no valid bios rom is a fatal error */
Andreas Färberc00eb5c2013-08-04 16:49:28 +0200149 if (!kernel_filename && !dinfo && !bios_filename && !qtest_enabled()) {
Alistair Francis0a094a52018-02-03 09:43:05 +0100150 error_report("could not load Milkymist One bios '%s'", bios_name);
Michael Walle5052d222011-03-07 23:32:44 +0100151 exit(1);
152 }
Gongleic2c17a22015-02-27 15:50:17 +0800153 g_free(bios_filename);
Michael Walle5052d222011-03-07 23:32:44 +0100154
Peter Maydell9bca0ed2018-04-20 15:52:43 +0100155 milkymist_uart_create(0x60000000, irq[0], serial_hd(0));
Michael Walle779277c2011-09-16 00:48:34 +0200156 milkymist_sysctl_create(0x60001000, irq[1], irq[2], irq[3],
Michael Walle5052d222011-03-07 23:32:44 +0100157 80000000, 0x10014d31, 0x0000041f, 0x00000001);
158 milkymist_hpdmc_create(0x60002000);
159 milkymist_vgafb_create(0x60003000, 0x40000000, 0x0fffffff);
160 milkymist_memcard_create(0x60004000);
Michael Walle779277c2011-09-16 00:48:34 +0200161 milkymist_ac97_create(0x60005000, irq[4], irq[5], irq[6], irq[7]);
162 milkymist_pfpu_create(0x60006000, irq[8]);
Eduardo Habkostcfc58cf2016-04-19 16:55:25 -0300163 if (machine->enable_graphics) {
Eduardo Habkostcf3dc712016-04-19 16:55:24 -0300164 milkymist_tmu2_create(0x60007000, irq[9]);
165 }
Michael Walle779277c2011-09-16 00:48:34 +0200166 milkymist_minimac2_create(0x60008000, 0x30000000, irq[10], irq[11]);
167 milkymist_softusb_create(0x6000f000, irq[15],
Michael Walle5052d222011-03-07 23:32:44 +0100168 0x20000000, 0x1000, 0x20020000, 0x2000);
169
170 /* make sure juart isn't the first chardev */
Peter Maydell9bca0ed2018-04-20 15:52:43 +0100171 env->juart_state = lm32_juart_init(serial_hd(1));
Michael Walle5052d222011-03-07 23:32:44 +0100172
173 if (kernel_filename) {
174 uint64_t entry;
175
176 /* Boots a kernel elf binary. */
Liam Merwick4366e1d2019-01-15 12:18:03 +0000177 kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
178 &entry, NULL, NULL,
Peter Crosthwaite7ef295e2016-03-04 11:30:21 +0000179 1, EM_LATTICEMICO32, 0, 0);
Michael Walle5052d222011-03-07 23:32:44 +0100180 reset_info->bootstrap_pc = entry;
181
182 if (kernel_size < 0) {
183 kernel_size = load_image_targphys(kernel_filename, sdram_base,
184 sdram_size);
185 reset_info->bootstrap_pc = sdram_base;
186 }
187
188 if (kernel_size < 0) {
Alistair Francis0a094a52018-02-03 09:43:05 +0100189 error_report("could not load kernel '%s'", kernel_filename);
Michael Walle5052d222011-03-07 23:32:44 +0100190 exit(1);
191 }
192 }
193
194 if (kernel_cmdline && strlen(kernel_cmdline)) {
195 pstrcpy_targphys("cmdline", cmdline_base, TARGET_PAGE_SIZE,
196 kernel_cmdline);
197 reset_info->cmdline_base = (uint32_t)cmdline_base;
198 }
199
200 if (initrd_filename) {
201 size_t initrd_size;
202 initrd_size = load_image_targphys(initrd_filename, initrd_base,
203 initrd_max);
204 reset_info->initrd_base = (uint32_t)initrd_base;
205 reset_info->initrd_size = (uint32_t)initrd_size;
206 }
207
208 qemu_register_reset(main_cpu_reset, reset_info);
209}
210
Eduardo Habkoste264d292015-09-04 15:37:08 -0300211static void milkymist_machine_init(MachineClass *mc)
Michael Walle5052d222011-03-07 23:32:44 +0100212{
Eduardo Habkoste264d292015-09-04 15:37:08 -0300213 mc->desc = "Milkymist One";
214 mc->init = milkymist_init;
215 mc->is_default = 0;
Igor Mammedov201c1602017-10-05 15:50:43 +0200216 mc->default_cpu_type = LM32_CPU_TYPE_NAME("lm32-full");
Michael Walle5052d222011-03-07 23:32:44 +0100217}
218
Eduardo Habkoste264d292015-09-04 15:37:08 -0300219DEFINE_MACHINE("milkymist", milkymist_machine_init)