]>
Commit | Line | Data |
---|---|---|
5856de80 TS |
1 | /* |
2 | * QEMU Malta board support | |
3 | * | |
4 | * Copyright (c) 2006 Aurelien Jarno | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
c684822a | 25 | #include "qemu/osdep.h" |
be01029e | 26 | #include "qemu/units.h" |
4771d756 PB |
27 | #include "qemu-common.h" |
28 | #include "cpu.h" | |
83c9f4ca | 29 | #include "hw/hw.h" |
0d09e41a | 30 | #include "hw/i386/pc.h" |
7313b1f2 | 31 | #include "hw/isa/superio.h" |
55f613ac | 32 | #include "hw/dma/i8257.h" |
0d09e41a | 33 | #include "hw/char/serial.h" |
1422e32d | 34 | #include "net/net.h" |
83c9f4ca | 35 | #include "hw/boards.h" |
93198b6c | 36 | #include "hw/i2c/smbus_eeprom.h" |
0d09e41a PB |
37 | #include "hw/block/flash.h" |
38 | #include "hw/mips/mips.h" | |
39 | #include "hw/mips/cpudevs.h" | |
83c9f4ca | 40 | #include "hw/pci/pci.h" |
9c17d615 PB |
41 | #include "sysemu/sysemu.h" |
42 | #include "sysemu/arch_init.h" | |
1de7afc9 | 43 | #include "qemu/log.h" |
0d09e41a | 44 | #include "hw/mips/bios.h" |
83c9f4ca PB |
45 | #include "hw/ide.h" |
46 | #include "hw/loader.h" | |
ca20cf32 | 47 | #include "elf.h" |
0d09e41a PB |
48 | #include "hw/timer/mc146818rtc.h" |
49 | #include "hw/timer/i8254.h" | |
022c62cb | 50 | #include "exec/address-spaces.h" |
83c9f4ca | 51 | #include "hw/sysbus.h" /* SysBusDevice */ |
02bccc77 | 52 | #include "qemu/host-utils.h" |
2c57bd9b | 53 | #include "sysemu/qtest.h" |
e688df6b | 54 | #include "qapi/error.h" |
2e985fe0 | 55 | #include "qemu/error-report.h" |
cc413a39 | 56 | #include "hw/empty_slot.h" |
b0311811 | 57 | #include "sysemu/kvm.h" |
3b3c1694 | 58 | #include "exec/semihost.h" |
bff384a4 | 59 | #include "hw/mips/cps.h" |
5856de80 | 60 | |
409dbce5 | 61 | #define ENVP_ADDR 0x80002000l |
5856de80 TS |
62 | #define ENVP_NB_ENTRIES 16 |
63 | #define ENVP_ENTRY_SIZE 256 | |
64 | ||
03a1a8e1 SW |
65 | /* Hardware addresses */ |
66 | #define FLASH_ADDRESS 0x1e000000ULL | |
67 | #define FPGA_ADDRESS 0x1f000000ULL | |
68 | #define RESET_ADDRESS 0x1fc00000ULL | |
69 | ||
70 | #define FLASH_SIZE 0x400000 | |
71 | ||
e4bcb14c TS |
72 | #define MAX_IDE_BUS 2 |
73 | ||
5856de80 | 74 | typedef struct { |
ea85df72 AK |
75 | MemoryRegion iomem; |
76 | MemoryRegion iomem_lo; /* 0 - 0x900 */ | |
77 | MemoryRegion iomem_hi; /* 0xa00 - 0x100000 */ | |
5856de80 TS |
78 | uint32_t leds; |
79 | uint32_t brk; | |
80 | uint32_t gpout; | |
130751ee | 81 | uint32_t i2cin; |
5856de80 TS |
82 | uint32_t i2coe; |
83 | uint32_t i2cout; | |
84 | uint32_t i2csel; | |
32a6ebec | 85 | CharBackend display; |
5856de80 | 86 | char display_text[9]; |
a4bc3afc | 87 | SerialState *uart; |
9850b05d | 88 | bool display_inited; |
5856de80 TS |
89 | } MaltaFPGAState; |
90 | ||
cba5cb67 AF |
91 | #define TYPE_MIPS_MALTA "mips-malta" |
92 | #define MIPS_MALTA(obj) OBJECT_CHECK(MaltaState, (obj), TYPE_MIPS_MALTA) | |
93 | ||
e9b40fd3 | 94 | typedef struct { |
cba5cb67 AF |
95 | SysBusDevice parent_obj; |
96 | ||
bff384a4 | 97 | MIPSCPSState *cps; |
e9b40fd3 SW |
98 | qemu_irq *i8259; |
99 | } MaltaState; | |
100 | ||
64d7e9a4 | 101 | static ISADevice *pit; |
5856de80 | 102 | |
7df526e3 | 103 | static struct _loaderparams { |
71c199c8 | 104 | int ram_size, ram_low_size; |
7df526e3 TS |
105 | const char *kernel_filename; |
106 | const char *kernel_cmdline; | |
107 | const char *initrd_filename; | |
108 | } loaderparams; | |
109 | ||
5856de80 TS |
110 | /* Malta FPGA */ |
111 | static void malta_fpga_update_display(void *opaque) | |
112 | { | |
113 | char leds_text[9]; | |
114 | int i; | |
115 | MaltaFPGAState *s = opaque; | |
116 | ||
07cf0ba0 TS |
117 | for (i = 7 ; i >= 0 ; i--) { |
118 | if (s->leds & (1 << i)) | |
119 | leds_text[i] = '#'; | |
120 | else | |
121 | leds_text[i] = ' '; | |
87ee1669 | 122 | } |
07cf0ba0 TS |
123 | leds_text[8] = '\0'; |
124 | ||
5345fdb4 | 125 | qemu_chr_fe_printf(&s->display, "\e[H\n\n|\e[32m%-8.8s\e[00m|\r\n", |
32a6ebec | 126 | leds_text); |
5345fdb4 | 127 | qemu_chr_fe_printf(&s->display, "\n\n\n\n|\e[31m%-8.8s\e[00m|", |
32a6ebec | 128 | s->display_text); |
5856de80 TS |
129 | } |
130 | ||
130751ee TS |
131 | /* |
132 | * EEPROM 24C01 / 24C02 emulation. | |
133 | * | |
134 | * Emulation for serial EEPROMs: | |
135 | * 24C01 - 1024 bit (128 x 8) | |
136 | * 24C02 - 2048 bit (256 x 8) | |
137 | * | |
138 | * Typical device names include Microchip 24C02SC or SGS Thomson ST24C02. | |
139 | */ | |
140 | ||
141 | //~ #define DEBUG | |
142 | ||
143 | #if defined(DEBUG) | |
001faf32 | 144 | # define logout(fmt, ...) fprintf(stderr, "MALTA\t%-24s" fmt, __func__, ## __VA_ARGS__) |
130751ee | 145 | #else |
001faf32 | 146 | # define logout(fmt, ...) ((void)0) |
130751ee TS |
147 | #endif |
148 | ||
c227f099 | 149 | struct _eeprom24c0x_t { |
130751ee TS |
150 | uint8_t tick; |
151 | uint8_t address; | |
152 | uint8_t command; | |
153 | uint8_t ack; | |
154 | uint8_t scl; | |
155 | uint8_t sda; | |
156 | uint8_t data; | |
157 | //~ uint16_t size; | |
158 | uint8_t contents[256]; | |
159 | }; | |
160 | ||
c227f099 | 161 | typedef struct _eeprom24c0x_t eeprom24c0x_t; |
130751ee | 162 | |
35c64807 | 163 | static eeprom24c0x_t spd_eeprom = { |
284b08f1 | 164 | .contents = { |
02bccc77 | 165 | /* 00000000: */ 0x80,0x08,0xFF,0x0D,0x0A,0xFF,0x40,0x00, |
130751ee | 166 | /* 00000008: */ 0x01,0x75,0x54,0x00,0x82,0x08,0x00,0x01, |
02bccc77 PB |
167 | /* 00000010: */ 0x8F,0x04,0x02,0x01,0x01,0x00,0x00,0x00, |
168 | /* 00000018: */ 0x00,0x00,0x00,0x14,0x0F,0x14,0x2D,0xFF, | |
130751ee TS |
169 | /* 00000020: */ 0x15,0x08,0x15,0x08,0x00,0x00,0x00,0x00, |
170 | /* 00000028: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
171 | /* 00000030: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
172 | /* 00000038: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x12,0xD0, | |
173 | /* 00000040: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
174 | /* 00000048: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
175 | /* 00000050: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
176 | /* 00000058: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
177 | /* 00000060: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
178 | /* 00000068: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
179 | /* 00000070: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
180 | /* 00000078: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x64,0xF4, | |
181 | }, | |
182 | }; | |
183 | ||
35c64807 | 184 | static void generate_eeprom_spd(uint8_t *eeprom, ram_addr_t ram_size) |
02bccc77 PB |
185 | { |
186 | enum { SDR = 0x4, DDR2 = 0x8 } type; | |
35c64807 | 187 | uint8_t *spd = spd_eeprom.contents; |
02bccc77 PB |
188 | uint8_t nbanks = 0; |
189 | uint16_t density = 0; | |
190 | int i; | |
191 | ||
192 | /* work in terms of MB */ | |
be01029e | 193 | ram_size /= MiB; |
02bccc77 PB |
194 | |
195 | while ((ram_size >= 4) && (nbanks <= 2)) { | |
196 | int sz_log2 = MIN(31 - clz32(ram_size), 14); | |
197 | nbanks++; | |
198 | density |= 1 << (sz_log2 - 2); | |
199 | ram_size -= 1 << sz_log2; | |
200 | } | |
201 | ||
202 | /* split to 2 banks if possible */ | |
203 | if ((nbanks == 1) && (density > 1)) { | |
204 | nbanks++; | |
205 | density >>= 1; | |
206 | } | |
207 | ||
208 | if (density & 0xff00) { | |
209 | density = (density & 0xe0) | ((density >> 8) & 0x1f); | |
210 | type = DDR2; | |
211 | } else if (!(density & 0x1f)) { | |
212 | type = DDR2; | |
213 | } else { | |
214 | type = SDR; | |
215 | } | |
216 | ||
217 | if (ram_size) { | |
b62e39b4 AF |
218 | warn_report("SPD cannot represent final " RAM_ADDR_FMT "MB" |
219 | " of SDRAM", ram_size); | |
02bccc77 PB |
220 | } |
221 | ||
222 | /* fill in SPD memory information */ | |
223 | spd[2] = type; | |
224 | spd[5] = nbanks; | |
225 | spd[31] = density; | |
226 | ||
227 | /* checksum */ | |
228 | spd[63] = 0; | |
229 | for (i = 0; i < 63; i++) { | |
230 | spd[63] += spd[i]; | |
231 | } | |
35c64807 PB |
232 | |
233 | /* copy for SMBUS */ | |
234 | memcpy(eeprom, spd, sizeof(spd_eeprom.contents)); | |
235 | } | |
236 | ||
237 | static void generate_eeprom_serial(uint8_t *eeprom) | |
238 | { | |
239 | int i, pos = 0; | |
240 | uint8_t mac[6] = { 0x00 }; | |
241 | uint8_t sn[5] = { 0x01, 0x23, 0x45, 0x67, 0x89 }; | |
242 | ||
243 | /* version */ | |
244 | eeprom[pos++] = 0x01; | |
245 | ||
246 | /* count */ | |
247 | eeprom[pos++] = 0x02; | |
248 | ||
249 | /* MAC address */ | |
250 | eeprom[pos++] = 0x01; /* MAC */ | |
251 | eeprom[pos++] = 0x06; /* length */ | |
252 | memcpy(&eeprom[pos], mac, sizeof(mac)); | |
253 | pos += sizeof(mac); | |
254 | ||
255 | /* serial number */ | |
256 | eeprom[pos++] = 0x02; /* serial */ | |
257 | eeprom[pos++] = 0x05; /* length */ | |
258 | memcpy(&eeprom[pos], sn, sizeof(sn)); | |
259 | pos += sizeof(sn); | |
260 | ||
261 | /* checksum */ | |
262 | eeprom[pos] = 0; | |
263 | for (i = 0; i < pos; i++) { | |
264 | eeprom[pos] += eeprom[i]; | |
265 | } | |
02bccc77 PB |
266 | } |
267 | ||
35c64807 | 268 | static uint8_t eeprom24c0x_read(eeprom24c0x_t *eeprom) |
130751ee TS |
269 | { |
270 | logout("%u: scl = %u, sda = %u, data = 0x%02x\n", | |
35c64807 PB |
271 | eeprom->tick, eeprom->scl, eeprom->sda, eeprom->data); |
272 | return eeprom->sda; | |
130751ee TS |
273 | } |
274 | ||
35c64807 | 275 | static void eeprom24c0x_write(eeprom24c0x_t *eeprom, int scl, int sda) |
130751ee | 276 | { |
35c64807 | 277 | if (eeprom->scl && scl && (eeprom->sda != sda)) { |
130751ee | 278 | logout("%u: scl = %u->%u, sda = %u->%u i2c %s\n", |
35c64807 PB |
279 | eeprom->tick, eeprom->scl, scl, eeprom->sda, sda, |
280 | sda ? "stop" : "start"); | |
130751ee | 281 | if (!sda) { |
35c64807 PB |
282 | eeprom->tick = 1; |
283 | eeprom->command = 0; | |
130751ee | 284 | } |
35c64807 | 285 | } else if (eeprom->tick == 0 && !eeprom->ack) { |
130751ee TS |
286 | /* Waiting for start. */ |
287 | logout("%u: scl = %u->%u, sda = %u->%u wait for i2c start\n", | |
35c64807 PB |
288 | eeprom->tick, eeprom->scl, scl, eeprom->sda, sda); |
289 | } else if (!eeprom->scl && scl) { | |
130751ee | 290 | logout("%u: scl = %u->%u, sda = %u->%u trigger bit\n", |
35c64807 PB |
291 | eeprom->tick, eeprom->scl, scl, eeprom->sda, sda); |
292 | if (eeprom->ack) { | |
130751ee TS |
293 | logout("\ti2c ack bit = 0\n"); |
294 | sda = 0; | |
35c64807 PB |
295 | eeprom->ack = 0; |
296 | } else if (eeprom->sda == sda) { | |
130751ee TS |
297 | uint8_t bit = (sda != 0); |
298 | logout("\ti2c bit = %d\n", bit); | |
35c64807 PB |
299 | if (eeprom->tick < 9) { |
300 | eeprom->command <<= 1; | |
301 | eeprom->command += bit; | |
302 | eeprom->tick++; | |
303 | if (eeprom->tick == 9) { | |
304 | logout("\tcommand 0x%04x, %s\n", eeprom->command, | |
305 | bit ? "read" : "write"); | |
306 | eeprom->ack = 1; | |
130751ee | 307 | } |
35c64807 PB |
308 | } else if (eeprom->tick < 17) { |
309 | if (eeprom->command & 1) { | |
310 | sda = ((eeprom->data & 0x80) != 0); | |
130751ee | 311 | } |
35c64807 PB |
312 | eeprom->address <<= 1; |
313 | eeprom->address += bit; | |
314 | eeprom->tick++; | |
315 | eeprom->data <<= 1; | |
316 | if (eeprom->tick == 17) { | |
317 | eeprom->data = eeprom->contents[eeprom->address]; | |
318 | logout("\taddress 0x%04x, data 0x%02x\n", | |
319 | eeprom->address, eeprom->data); | |
320 | eeprom->ack = 1; | |
321 | eeprom->tick = 0; | |
130751ee | 322 | } |
35c64807 | 323 | } else if (eeprom->tick >= 17) { |
130751ee TS |
324 | sda = 0; |
325 | } | |
326 | } else { | |
327 | logout("\tsda changed with raising scl\n"); | |
328 | } | |
329 | } else { | |
35c64807 PB |
330 | logout("%u: scl = %u->%u, sda = %u->%u\n", eeprom->tick, eeprom->scl, |
331 | scl, eeprom->sda, sda); | |
130751ee | 332 | } |
35c64807 PB |
333 | eeprom->scl = scl; |
334 | eeprom->sda = sda; | |
130751ee TS |
335 | } |
336 | ||
a8170e5e | 337 | static uint64_t malta_fpga_read(void *opaque, hwaddr addr, |
ea85df72 | 338 | unsigned size) |
5856de80 TS |
339 | { |
340 | MaltaFPGAState *s = opaque; | |
341 | uint32_t val = 0; | |
342 | uint32_t saddr; | |
343 | ||
344 | saddr = (addr & 0xfffff); | |
345 | ||
346 | switch (saddr) { | |
347 | ||
348 | /* SWITCH Register */ | |
349 | case 0x00200: | |
350 | val = 0x00000000; /* All switches closed */ | |
593c0d10 | 351 | break; |
5856de80 TS |
352 | |
353 | /* STATUS Register */ | |
354 | case 0x00208: | |
355 | #ifdef TARGET_WORDS_BIGENDIAN | |
356 | val = 0x00000012; | |
357 | #else | |
358 | val = 0x00000010; | |
359 | #endif | |
360 | break; | |
361 | ||
362 | /* JMPRS Register */ | |
363 | case 0x00210: | |
364 | val = 0x00; | |
365 | break; | |
366 | ||
367 | /* LEDBAR Register */ | |
368 | case 0x00408: | |
369 | val = s->leds; | |
370 | break; | |
371 | ||
372 | /* BRKRES Register */ | |
373 | case 0x00508: | |
374 | val = s->brk; | |
375 | break; | |
376 | ||
b6dc7ebb | 377 | /* UART Registers are handled directly by the serial device */ |
a4bc3afc | 378 | |
5856de80 TS |
379 | /* GPOUT Register */ |
380 | case 0x00a00: | |
381 | val = s->gpout; | |
382 | break; | |
383 | ||
384 | /* XXX: implement a real I2C controller */ | |
385 | ||
386 | /* GPINP Register */ | |
387 | case 0x00a08: | |
388 | /* IN = OUT until a real I2C control is implemented */ | |
389 | if (s->i2csel) | |
390 | val = s->i2cout; | |
391 | else | |
392 | val = 0x00; | |
393 | break; | |
394 | ||
395 | /* I2CINP Register */ | |
396 | case 0x00b00: | |
35c64807 | 397 | val = ((s->i2cin & ~1) | eeprom24c0x_read(&spd_eeprom)); |
5856de80 TS |
398 | break; |
399 | ||
400 | /* I2COE Register */ | |
401 | case 0x00b08: | |
402 | val = s->i2coe; | |
403 | break; | |
404 | ||
405 | /* I2COUT Register */ | |
406 | case 0x00b10: | |
407 | val = s->i2cout; | |
408 | break; | |
409 | ||
410 | /* I2CSEL Register */ | |
411 | case 0x00b18: | |
130751ee | 412 | val = s->i2csel; |
5856de80 TS |
413 | break; |
414 | ||
415 | default: | |
416 | #if 0 | |
3594c774 | 417 | printf ("malta_fpga_read: Bad register offset 0x" TARGET_FMT_lx "\n", |
593c0d10 | 418 | addr); |
5856de80 TS |
419 | #endif |
420 | break; | |
421 | } | |
422 | return val; | |
423 | } | |
424 | ||
a8170e5e | 425 | static void malta_fpga_write(void *opaque, hwaddr addr, |
ea85df72 | 426 | uint64_t val, unsigned size) |
5856de80 TS |
427 | { |
428 | MaltaFPGAState *s = opaque; | |
429 | uint32_t saddr; | |
430 | ||
431 | saddr = (addr & 0xfffff); | |
432 | ||
433 | switch (saddr) { | |
434 | ||
435 | /* SWITCH Register */ | |
436 | case 0x00200: | |
437 | break; | |
438 | ||
439 | /* JMPRS Register */ | |
440 | case 0x00210: | |
441 | break; | |
442 | ||
443 | /* LEDBAR Register */ | |
5856de80 TS |
444 | case 0x00408: |
445 | s->leds = val & 0xff; | |
1d7a1197 | 446 | malta_fpga_update_display(s); |
5856de80 TS |
447 | break; |
448 | ||
449 | /* ASCIIWORD Register */ | |
450 | case 0x00410: | |
ea85df72 | 451 | snprintf(s->display_text, 9, "%08X", (uint32_t)val); |
5856de80 TS |
452 | malta_fpga_update_display(s); |
453 | break; | |
454 | ||
455 | /* ASCIIPOS0 to ASCIIPOS7 Registers */ | |
456 | case 0x00418: | |
457 | case 0x00420: | |
458 | case 0x00428: | |
459 | case 0x00430: | |
460 | case 0x00438: | |
461 | case 0x00440: | |
462 | case 0x00448: | |
463 | case 0x00450: | |
464 | s->display_text[(saddr - 0x00418) >> 3] = (char) val; | |
465 | malta_fpga_update_display(s); | |
466 | break; | |
467 | ||
468 | /* SOFTRES Register */ | |
469 | case 0x00500: | |
470 | if (val == 0x42) | |
cf83f140 | 471 | qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); |
5856de80 TS |
472 | break; |
473 | ||
474 | /* BRKRES Register */ | |
475 | case 0x00508: | |
476 | s->brk = val & 0xff; | |
477 | break; | |
478 | ||
b6dc7ebb | 479 | /* UART Registers are handled directly by the serial device */ |
a4bc3afc | 480 | |
5856de80 TS |
481 | /* GPOUT Register */ |
482 | case 0x00a00: | |
483 | s->gpout = val & 0xff; | |
484 | break; | |
485 | ||
486 | /* I2COE Register */ | |
487 | case 0x00b08: | |
488 | s->i2coe = val & 0x03; | |
489 | break; | |
490 | ||
491 | /* I2COUT Register */ | |
492 | case 0x00b10: | |
35c64807 | 493 | eeprom24c0x_write(&spd_eeprom, val & 0x02, val & 0x01); |
130751ee | 494 | s->i2cout = val; |
5856de80 TS |
495 | break; |
496 | ||
497 | /* I2CSEL Register */ | |
498 | case 0x00b18: | |
130751ee | 499 | s->i2csel = val & 0x01; |
5856de80 TS |
500 | break; |
501 | ||
502 | default: | |
503 | #if 0 | |
3594c774 | 504 | printf ("malta_fpga_write: Bad register offset 0x" TARGET_FMT_lx "\n", |
593c0d10 | 505 | addr); |
5856de80 TS |
506 | #endif |
507 | break; | |
508 | } | |
509 | } | |
510 | ||
ea85df72 AK |
511 | static const MemoryRegionOps malta_fpga_ops = { |
512 | .read = malta_fpga_read, | |
513 | .write = malta_fpga_write, | |
514 | .endianness = DEVICE_NATIVE_ENDIAN, | |
5856de80 TS |
515 | }; |
516 | ||
9596ebb7 | 517 | static void malta_fpga_reset(void *opaque) |
5856de80 TS |
518 | { |
519 | MaltaFPGAState *s = opaque; | |
520 | ||
521 | s->leds = 0x00; | |
522 | s->brk = 0x0a; | |
523 | s->gpout = 0x00; | |
130751ee | 524 | s->i2cin = 0x3; |
5856de80 TS |
525 | s->i2coe = 0x0; |
526 | s->i2cout = 0x3; | |
527 | s->i2csel = 0x1; | |
528 | ||
529 | s->display_text[8] = '\0'; | |
530 | snprintf(s->display_text, 9, " "); | |
ceecf1d1 AJ |
531 | } |
532 | ||
9850b05d | 533 | static void malta_fgpa_display_event(void *opaque, int event) |
ceecf1d1 | 534 | { |
9850b05d MAL |
535 | MaltaFPGAState *s = opaque; |
536 | ||
537 | if (event == CHR_EVENT_OPENED && !s->display_inited) { | |
5345fdb4 MAL |
538 | qemu_chr_fe_printf(&s->display, "\e[HMalta LEDBAR\r\n"); |
539 | qemu_chr_fe_printf(&s->display, "+--------+\r\n"); | |
540 | qemu_chr_fe_printf(&s->display, "+ +\r\n"); | |
541 | qemu_chr_fe_printf(&s->display, "+--------+\r\n"); | |
542 | qemu_chr_fe_printf(&s->display, "\n"); | |
543 | qemu_chr_fe_printf(&s->display, "Malta ASCII\r\n"); | |
544 | qemu_chr_fe_printf(&s->display, "+--------+\r\n"); | |
545 | qemu_chr_fe_printf(&s->display, "+ +\r\n"); | |
546 | qemu_chr_fe_printf(&s->display, "+--------+\r\n"); | |
9850b05d MAL |
547 | s->display_inited = true; |
548 | } | |
5856de80 TS |
549 | } |
550 | ||
ea85df72 | 551 | static MaltaFPGAState *malta_fpga_init(MemoryRegion *address_space, |
0ec7b3e7 | 552 | hwaddr base, qemu_irq uart_irq, Chardev *uart_chr) |
5856de80 TS |
553 | { |
554 | MaltaFPGAState *s; | |
0ec7b3e7 | 555 | Chardev *chr; |
5856de80 | 556 | |
7267c094 | 557 | s = (MaltaFPGAState *)g_malloc0(sizeof(MaltaFPGAState)); |
5856de80 | 558 | |
2c9b15ca | 559 | memory_region_init_io(&s->iomem, NULL, &malta_fpga_ops, s, |
ea85df72 | 560 | "malta-fpga", 0x100000); |
2c9b15ca | 561 | memory_region_init_alias(&s->iomem_lo, NULL, "malta-fpga", |
ea85df72 | 562 | &s->iomem, 0, 0x900); |
2c9b15ca | 563 | memory_region_init_alias(&s->iomem_hi, NULL, "malta-fpga", |
ea85df72 | 564 | &s->iomem, 0xa00, 0x10000-0xa00); |
a4bc3afc | 565 | |
ea85df72 AK |
566 | memory_region_add_subregion(address_space, base, &s->iomem_lo); |
567 | memory_region_add_subregion(address_space, base + 0xa00, &s->iomem_hi); | |
5856de80 | 568 | |
4ad6f6cb | 569 | chr = qemu_chr_new("fpga", "vc:320x200", NULL); |
5345fdb4 MAL |
570 | qemu_chr_fe_init(&s->display, chr, NULL); |
571 | qemu_chr_fe_set_handlers(&s->display, NULL, NULL, | |
81517ba3 | 572 | malta_fgpa_display_event, NULL, s, NULL, true); |
ceecf1d1 | 573 | |
39186d8a RH |
574 | s->uart = serial_mm_init(address_space, base + 0x900, 3, uart_irq, |
575 | 230400, uart_chr, DEVICE_NATIVE_ENDIAN); | |
a4bc3afc | 576 | |
5856de80 | 577 | malta_fpga_reset(s); |
a08d4367 | 578 | qemu_register_reset(malta_fpga_reset, s); |
5856de80 TS |
579 | |
580 | return s; | |
581 | } | |
582 | ||
5856de80 | 583 | /* Network support */ |
29b358f9 | 584 | static void network_init(PCIBus *pci_bus) |
5856de80 TS |
585 | { |
586 | int i; | |
5856de80 TS |
587 | |
588 | for(i = 0; i < nb_nics; i++) { | |
cb457d76 | 589 | NICInfo *nd = &nd_table[i]; |
5607c388 | 590 | const char *default_devaddr = NULL; |
cb457d76 AL |
591 | |
592 | if (i == 0 && (!nd->model || strcmp(nd->model, "pcnet") == 0)) | |
5856de80 | 593 | /* The malta board has a PCNet card using PCI SLOT 11 */ |
5607c388 | 594 | default_devaddr = "0b"; |
cb457d76 | 595 | |
29b358f9 | 596 | pci_nic_init_nofail(nd, pci_bus, "pcnet", default_devaddr); |
5856de80 TS |
597 | } |
598 | } | |
599 | ||
ce3940cc MF |
600 | static void write_bootloader_nanomips(uint8_t *base, int64_t run_addr, |
601 | int64_t kernel_entry) | |
602 | { | |
603 | uint16_t *p; | |
604 | ||
605 | /* Small bootloader */ | |
606 | p = (uint16_t *)base; | |
607 | ||
608 | #define NM_HI1(VAL) (((VAL) >> 16) & 0x1f) | |
609 | #define NM_HI2(VAL) \ | |
28861af8 | 610 | (((VAL) & 0xf000) | (((VAL) >> 19) & 0xffc) | (((VAL) >> 31) & 0x1)) |
ce3940cc MF |
611 | #define NM_LO(VAL) ((VAL) & 0xfff) |
612 | ||
28861af8 PB |
613 | stw_p(p++, 0x2800); stw_p(p++, 0x001c); |
614 | /* bc to_here */ | |
615 | stw_p(p++, 0x8000); stw_p(p++, 0xc000); | |
616 | /* nop */ | |
617 | stw_p(p++, 0x8000); stw_p(p++, 0xc000); | |
618 | /* nop */ | |
619 | stw_p(p++, 0x8000); stw_p(p++, 0xc000); | |
620 | /* nop */ | |
621 | stw_p(p++, 0x8000); stw_p(p++, 0xc000); | |
622 | /* nop */ | |
623 | stw_p(p++, 0x8000); stw_p(p++, 0xc000); | |
624 | /* nop */ | |
625 | stw_p(p++, 0x8000); stw_p(p++, 0xc000); | |
626 | /* nop */ | |
627 | stw_p(p++, 0x8000); stw_p(p++, 0xc000); | |
628 | /* nop */ | |
ce3940cc MF |
629 | |
630 | /* to_here: */ | |
bf4667d0 SM |
631 | if (semihosting_get_argc()) { |
632 | /* Preserve a0 content as arguments have been passed */ | |
633 | stw_p(p++, 0x8000); stw_p(p++, 0xc000); | |
634 | /* nop */ | |
635 | } else { | |
636 | stw_p(p++, 0x0080); stw_p(p++, 0x0002); | |
28861af8 | 637 | /* li a0,2 */ |
bf4667d0 | 638 | } |
28861af8 | 639 | |
ce3940cc | 640 | stw_p(p++, 0xe3a0 | NM_HI1(ENVP_ADDR - 64)); |
28861af8 | 641 | |
ce3940cc | 642 | stw_p(p++, NM_HI2(ENVP_ADDR - 64)); |
28861af8 PB |
643 | /* lui sp,%hi(ENVP_ADDR - 64) */ |
644 | ||
ce3940cc MF |
645 | stw_p(p++, 0x83bd); stw_p(p++, NM_LO(ENVP_ADDR - 64)); |
646 | /* ori sp,sp,%lo(ENVP_ADDR - 64) */ | |
28861af8 | 647 | |
ce3940cc | 648 | stw_p(p++, 0xe0a0 | NM_HI1(ENVP_ADDR)); |
28861af8 | 649 | |
ce3940cc | 650 | stw_p(p++, NM_HI2(ENVP_ADDR)); |
28861af8 PB |
651 | /* lui a1,%hi(ENVP_ADDR) */ |
652 | ||
ce3940cc | 653 | stw_p(p++, 0x80a5); stw_p(p++, NM_LO(ENVP_ADDR)); |
28861af8 PB |
654 | /* ori a1,a1,%lo(ENVP_ADDR) */ |
655 | ||
ce3940cc | 656 | stw_p(p++, 0xe0c0 | NM_HI1(ENVP_ADDR + 8)); |
28861af8 | 657 | |
ce3940cc | 658 | stw_p(p++, NM_HI2(ENVP_ADDR + 8)); |
28861af8 PB |
659 | /* lui a2,%hi(ENVP_ADDR + 8) */ |
660 | ||
ce3940cc MF |
661 | stw_p(p++, 0x80c6); stw_p(p++, NM_LO(ENVP_ADDR + 8)); |
662 | /* ori a2,a2,%lo(ENVP_ADDR + 8) */ | |
28861af8 | 663 | |
ce3940cc | 664 | stw_p(p++, 0xe0e0 | NM_HI1(loaderparams.ram_low_size)); |
28861af8 | 665 | |
ce3940cc MF |
666 | stw_p(p++, NM_HI2(loaderparams.ram_low_size)); |
667 | /* lui a3,%hi(loaderparams.ram_low_size) */ | |
28861af8 | 668 | |
ce3940cc MF |
669 | stw_p(p++, 0x80e7); stw_p(p++, NM_LO(loaderparams.ram_low_size)); |
670 | /* ori a3,a3,%lo(loaderparams.ram_low_size) */ | |
28861af8 PB |
671 | |
672 | /* | |
673 | * Load BAR registers as done by YAMON: | |
674 | * | |
675 | * - set up PCI0 I/O BARs from 0x18000000 to 0x181fffff | |
676 | * - set up PCI0 MEM0 at 0x10000000, size 0x8000000 | |
677 | * - set up PCI0 MEM1 at 0x18200000, size 0xbe00000 | |
678 | * | |
679 | */ | |
680 | stw_p(p++, 0xe040); stw_p(p++, 0x0681); | |
681 | /* lui t1, %hi(0xb4000000) */ | |
682 | ||
683 | #ifdef TARGET_WORDS_BIGENDIAN | |
684 | ||
685 | stw_p(p++, 0xe020); stw_p(p++, 0x0be1); | |
686 | /* lui t0, %hi(0xdf000000) */ | |
687 | ||
688 | /* 0x68 corresponds to GT_ISD (from hw/mips/gt64xxx_pci.c) */ | |
689 | stw_p(p++, 0x8422); stw_p(p++, 0x9068); | |
690 | /* sw t0, 0x68(t1) */ | |
691 | ||
692 | stw_p(p++, 0xe040); stw_p(p++, 0x077d); | |
693 | /* lui t1, %hi(0xbbe00000) */ | |
694 | ||
695 | stw_p(p++, 0xe020); stw_p(p++, 0x0801); | |
696 | /* lui t0, %hi(0xc0000000) */ | |
697 | ||
698 | /* 0x48 corresponds to GT_PCI0IOLD */ | |
699 | stw_p(p++, 0x8422); stw_p(p++, 0x9048); | |
700 | /* sw t0, 0x48(t1) */ | |
701 | ||
702 | stw_p(p++, 0xe020); stw_p(p++, 0x0800); | |
703 | /* lui t0, %hi(0x40000000) */ | |
704 | ||
705 | /* 0x50 corresponds to GT_PCI0IOHD */ | |
706 | stw_p(p++, 0x8422); stw_p(p++, 0x9050); | |
707 | /* sw t0, 0x50(t1) */ | |
708 | ||
709 | stw_p(p++, 0xe020); stw_p(p++, 0x0001); | |
710 | /* lui t0, %hi(0x80000000) */ | |
711 | ||
712 | /* 0x58 corresponds to GT_PCI0M0LD */ | |
713 | stw_p(p++, 0x8422); stw_p(p++, 0x9058); | |
714 | /* sw t0, 0x58(t1) */ | |
715 | ||
716 | stw_p(p++, 0xe020); stw_p(p++, 0x07e0); | |
717 | /* lui t0, %hi(0x3f000000) */ | |
718 | ||
719 | /* 0x60 corresponds to GT_PCI0M0HD */ | |
720 | stw_p(p++, 0x8422); stw_p(p++, 0x9060); | |
721 | /* sw t0, 0x60(t1) */ | |
722 | ||
723 | stw_p(p++, 0xe020); stw_p(p++, 0x0821); | |
724 | /* lui t0, %hi(0xc1000000) */ | |
725 | ||
726 | /* 0x80 corresponds to GT_PCI0M1LD */ | |
727 | stw_p(p++, 0x8422); stw_p(p++, 0x9080); | |
728 | /* sw t0, 0x80(t1) */ | |
729 | ||
730 | stw_p(p++, 0xe020); stw_p(p++, 0x0bc0); | |
731 | /* lui t0, %hi(0x5e000000) */ | |
732 | ||
733 | #else | |
734 | ||
735 | stw_p(p++, 0x0020); stw_p(p++, 0x00df); | |
736 | /* addiu[32] t0, $0, 0xdf */ | |
737 | ||
738 | /* 0x68 corresponds to GT_ISD */ | |
739 | stw_p(p++, 0x8422); stw_p(p++, 0x9068); | |
740 | /* sw t0, 0x68(t1) */ | |
741 | ||
742 | /* Use kseg2 remapped address 0x1be00000 */ | |
743 | stw_p(p++, 0xe040); stw_p(p++, 0x077d); | |
744 | /* lui t1, %hi(0xbbe00000) */ | |
745 | ||
746 | stw_p(p++, 0x0020); stw_p(p++, 0x00c0); | |
747 | /* addiu[32] t0, $0, 0xc0 */ | |
748 | ||
749 | /* 0x48 corresponds to GT_PCI0IOLD */ | |
750 | stw_p(p++, 0x8422); stw_p(p++, 0x9048); | |
751 | /* sw t0, 0x48(t1) */ | |
752 | ||
753 | stw_p(p++, 0x0020); stw_p(p++, 0x0040); | |
754 | /* addiu[32] t0, $0, 0x40 */ | |
755 | ||
756 | /* 0x50 corresponds to GT_PCI0IOHD */ | |
757 | stw_p(p++, 0x8422); stw_p(p++, 0x9050); | |
758 | /* sw t0, 0x50(t1) */ | |
759 | ||
760 | stw_p(p++, 0x0020); stw_p(p++, 0x0080); | |
761 | /* addiu[32] t0, $0, 0x80 */ | |
762 | ||
763 | /* 0x58 corresponds to GT_PCI0M0LD */ | |
764 | stw_p(p++, 0x8422); stw_p(p++, 0x9058); | |
765 | /* sw t0, 0x58(t1) */ | |
766 | ||
767 | stw_p(p++, 0x0020); stw_p(p++, 0x003f); | |
768 | /* addiu[32] t0, $0, 0x3f */ | |
769 | ||
770 | /* 0x60 corresponds to GT_PCI0M0HD */ | |
771 | stw_p(p++, 0x8422); stw_p(p++, 0x9060); | |
772 | /* sw t0, 0x60(t1) */ | |
773 | ||
774 | stw_p(p++, 0x0020); stw_p(p++, 0x00c1); | |
775 | /* addiu[32] t0, $0, 0xc1 */ | |
776 | ||
777 | /* 0x80 corresponds to GT_PCI0M1LD */ | |
778 | stw_p(p++, 0x8422); stw_p(p++, 0x9080); | |
779 | /* sw t0, 0x80(t1) */ | |
780 | ||
781 | stw_p(p++, 0x0020); stw_p(p++, 0x005e); | |
782 | /* addiu[32] t0, $0, 0x5e */ | |
783 | ||
784 | #endif | |
785 | ||
786 | /* 0x88 corresponds to GT_PCI0M1HD */ | |
787 | stw_p(p++, 0x8422); stw_p(p++, 0x9088); | |
788 | /* sw t0, 0x88(t1) */ | |
789 | ||
ce3940cc | 790 | stw_p(p++, 0xe320 | NM_HI1(kernel_entry)); |
28861af8 | 791 | |
ce3940cc | 792 | stw_p(p++, NM_HI2(kernel_entry)); |
28861af8 PB |
793 | /* lui t9,%hi(kernel_entry) */ |
794 | ||
ce3940cc | 795 | stw_p(p++, 0x8339); stw_p(p++, NM_LO(kernel_entry)); |
28861af8 PB |
796 | /* ori t9,t9,%lo(kernel_entry) */ |
797 | ||
ce3940cc | 798 | stw_p(p++, 0x4bf9); stw_p(p++, 0x0000); |
28861af8 | 799 | /* jalrc t8 */ |
ce3940cc MF |
800 | } |
801 | ||
5856de80 TS |
802 | /* ROM and pseudo bootloader |
803 | ||
804 | The following code implements a very very simple bootloader. It first | |
805 | loads the registers a0 to a3 to the values expected by the OS, and | |
806 | then jump at the kernel address. | |
807 | ||
808 | The bootloader should pass the locations of the kernel arguments and | |
809 | environment variables tables. Those tables contain the 32-bit address | |
810 | of NULL terminated strings. The environment variables table should be | |
811 | terminated by a NULL address. | |
812 | ||
813 | For a simpler implementation, the number of kernel arguments is fixed | |
814 | to two (the name of the kernel and the command line), and the two | |
815 | tables are actually the same one. | |
816 | ||
817 | The registers a0 to a3 should contain the following values: | |
818 | a0 - number of kernel arguments | |
819 | a1 - 32-bit address of the kernel arguments table | |
820 | a2 - 32-bit address of the environment variables table | |
821 | a3 - RAM size in bytes | |
822 | */ | |
cc518af0 LA |
823 | static void write_bootloader(uint8_t *base, int64_t run_addr, |
824 | int64_t kernel_entry) | |
5856de80 TS |
825 | { |
826 | uint32_t *p; | |
827 | ||
828 | /* Small bootloader */ | |
d7585251 | 829 | p = (uint32_t *)base; |
b0311811 JH |
830 | |
831 | stl_p(p++, 0x08000000 | /* j 0x1fc00580 */ | |
832 | ((run_addr + 0x580) & 0x0fffffff) >> 2); | |
0983979b | 833 | stl_p(p++, 0x00000000); /* nop */ |
5856de80 | 834 | |
26ea0918 | 835 | /* YAMON service vector */ |
b0311811 JH |
836 | stl_p(base + 0x500, run_addr + 0x0580); /* start: */ |
837 | stl_p(base + 0x504, run_addr + 0x083c); /* print_count: */ | |
838 | stl_p(base + 0x520, run_addr + 0x0580); /* start: */ | |
839 | stl_p(base + 0x52c, run_addr + 0x0800); /* flush_cache: */ | |
840 | stl_p(base + 0x534, run_addr + 0x0808); /* print: */ | |
841 | stl_p(base + 0x538, run_addr + 0x0800); /* reg_cpu_isr: */ | |
842 | stl_p(base + 0x53c, run_addr + 0x0800); /* unred_cpu_isr: */ | |
843 | stl_p(base + 0x540, run_addr + 0x0800); /* reg_ic_isr: */ | |
844 | stl_p(base + 0x544, run_addr + 0x0800); /* unred_ic_isr: */ | |
845 | stl_p(base + 0x548, run_addr + 0x0800); /* reg_esr: */ | |
846 | stl_p(base + 0x54c, run_addr + 0x0800); /* unreg_esr: */ | |
847 | stl_p(base + 0x550, run_addr + 0x0800); /* getchar: */ | |
848 | stl_p(base + 0x554, run_addr + 0x0800); /* syscon_read: */ | |
26ea0918 TS |
849 | |
850 | ||
5856de80 | 851 | /* Second part of the bootloader */ |
d7585251 | 852 | p = (uint32_t *) (base + 0x580); |
3b3c1694 LA |
853 | |
854 | if (semihosting_get_argc()) { | |
855 | /* Preserve a0 content as arguments have been passed */ | |
856 | stl_p(p++, 0x00000000); /* nop */ | |
857 | } else { | |
858 | stl_p(p++, 0x24040002); /* addiu a0, zero, 2 */ | |
859 | } | |
0983979b PB |
860 | stl_p(p++, 0x3c1d0000 | (((ENVP_ADDR - 64) >> 16) & 0xffff)); /* lui sp, high(ENVP_ADDR) */ |
861 | stl_p(p++, 0x37bd0000 | ((ENVP_ADDR - 64) & 0xffff)); /* ori sp, sp, low(ENVP_ADDR) */ | |
862 | stl_p(p++, 0x3c050000 | ((ENVP_ADDR >> 16) & 0xffff)); /* lui a1, high(ENVP_ADDR) */ | |
863 | stl_p(p++, 0x34a50000 | (ENVP_ADDR & 0xffff)); /* ori a1, a1, low(ENVP_ADDR) */ | |
864 | stl_p(p++, 0x3c060000 | (((ENVP_ADDR + 8) >> 16) & 0xffff)); /* lui a2, high(ENVP_ADDR + 8) */ | |
865 | stl_p(p++, 0x34c60000 | ((ENVP_ADDR + 8) & 0xffff)); /* ori a2, a2, low(ENVP_ADDR + 8) */ | |
71c199c8 PB |
866 | stl_p(p++, 0x3c070000 | (loaderparams.ram_low_size >> 16)); /* lui a3, high(ram_low_size) */ |
867 | stl_p(p++, 0x34e70000 | (loaderparams.ram_low_size & 0xffff)); /* ori a3, a3, low(ram_low_size) */ | |
2802bfe3 TS |
868 | |
869 | /* Load BAR registers as done by YAMON */ | |
0983979b | 870 | stl_p(p++, 0x3c09b400); /* lui t1, 0xb400 */ |
a0a8793e TS |
871 | |
872 | #ifdef TARGET_WORDS_BIGENDIAN | |
0983979b | 873 | stl_p(p++, 0x3c08df00); /* lui t0, 0xdf00 */ |
a0a8793e | 874 | #else |
0983979b | 875 | stl_p(p++, 0x340800df); /* ori t0, r0, 0x00df */ |
a0a8793e | 876 | #endif |
0983979b | 877 | stl_p(p++, 0xad280068); /* sw t0, 0x0068(t1) */ |
a0a8793e | 878 | |
0983979b | 879 | stl_p(p++, 0x3c09bbe0); /* lui t1, 0xbbe0 */ |
2802bfe3 TS |
880 | |
881 | #ifdef TARGET_WORDS_BIGENDIAN | |
0983979b | 882 | stl_p(p++, 0x3c08c000); /* lui t0, 0xc000 */ |
2802bfe3 | 883 | #else |
0983979b | 884 | stl_p(p++, 0x340800c0); /* ori t0, r0, 0x00c0 */ |
2802bfe3 | 885 | #endif |
0983979b | 886 | stl_p(p++, 0xad280048); /* sw t0, 0x0048(t1) */ |
2802bfe3 | 887 | #ifdef TARGET_WORDS_BIGENDIAN |
0983979b | 888 | stl_p(p++, 0x3c084000); /* lui t0, 0x4000 */ |
2802bfe3 | 889 | #else |
0983979b | 890 | stl_p(p++, 0x34080040); /* ori t0, r0, 0x0040 */ |
2802bfe3 | 891 | #endif |
0983979b | 892 | stl_p(p++, 0xad280050); /* sw t0, 0x0050(t1) */ |
2802bfe3 TS |
893 | |
894 | #ifdef TARGET_WORDS_BIGENDIAN | |
0983979b | 895 | stl_p(p++, 0x3c088000); /* lui t0, 0x8000 */ |
2802bfe3 | 896 | #else |
0983979b | 897 | stl_p(p++, 0x34080080); /* ori t0, r0, 0x0080 */ |
2802bfe3 | 898 | #endif |
0983979b | 899 | stl_p(p++, 0xad280058); /* sw t0, 0x0058(t1) */ |
2802bfe3 | 900 | #ifdef TARGET_WORDS_BIGENDIAN |
0983979b | 901 | stl_p(p++, 0x3c083f00); /* lui t0, 0x3f00 */ |
2802bfe3 | 902 | #else |
0983979b | 903 | stl_p(p++, 0x3408003f); /* ori t0, r0, 0x003f */ |
2802bfe3 | 904 | #endif |
0983979b | 905 | stl_p(p++, 0xad280060); /* sw t0, 0x0060(t1) */ |
2802bfe3 TS |
906 | |
907 | #ifdef TARGET_WORDS_BIGENDIAN | |
0983979b | 908 | stl_p(p++, 0x3c08c100); /* lui t0, 0xc100 */ |
2802bfe3 | 909 | #else |
0983979b | 910 | stl_p(p++, 0x340800c1); /* ori t0, r0, 0x00c1 */ |
2802bfe3 | 911 | #endif |
0983979b | 912 | stl_p(p++, 0xad280080); /* sw t0, 0x0080(t1) */ |
2802bfe3 | 913 | #ifdef TARGET_WORDS_BIGENDIAN |
0983979b | 914 | stl_p(p++, 0x3c085e00); /* lui t0, 0x5e00 */ |
2802bfe3 | 915 | #else |
0983979b | 916 | stl_p(p++, 0x3408005e); /* ori t0, r0, 0x005e */ |
2802bfe3 | 917 | #endif |
0983979b | 918 | stl_p(p++, 0xad280088); /* sw t0, 0x0088(t1) */ |
2802bfe3 TS |
919 | |
920 | /* Jump to kernel code */ | |
0983979b PB |
921 | stl_p(p++, 0x3c1f0000 | ((kernel_entry >> 16) & 0xffff)); /* lui ra, high(kernel_entry) */ |
922 | stl_p(p++, 0x37ff0000 | (kernel_entry & 0xffff)); /* ori ra, ra, low(kernel_entry) */ | |
9fba1500 | 923 | stl_p(p++, 0x03e00009); /* jalr ra */ |
0983979b | 924 | stl_p(p++, 0x00000000); /* nop */ |
26ea0918 TS |
925 | |
926 | /* YAMON subroutines */ | |
d7585251 | 927 | p = (uint32_t *) (base + 0x800); |
9fba1500 | 928 | stl_p(p++, 0x03e00009); /* jalr ra */ |
0983979b | 929 | stl_p(p++, 0x24020000); /* li v0,0 */ |
b0311811 | 930 | /* 808 YAMON print */ |
0983979b PB |
931 | stl_p(p++, 0x03e06821); /* move t5,ra */ |
932 | stl_p(p++, 0x00805821); /* move t3,a0 */ | |
933 | stl_p(p++, 0x00a05021); /* move t2,a1 */ | |
934 | stl_p(p++, 0x91440000); /* lbu a0,0(t2) */ | |
935 | stl_p(p++, 0x254a0001); /* addiu t2,t2,1 */ | |
936 | stl_p(p++, 0x10800005); /* beqz a0,834 */ | |
937 | stl_p(p++, 0x00000000); /* nop */ | |
938 | stl_p(p++, 0x0ff0021c); /* jal 870 */ | |
939 | stl_p(p++, 0x00000000); /* nop */ | |
7f81dbb9 | 940 | stl_p(p++, 0x1000fff9); /* b 814 */ |
0983979b | 941 | stl_p(p++, 0x00000000); /* nop */ |
9fba1500 | 942 | stl_p(p++, 0x01a00009); /* jalr t5 */ |
0983979b | 943 | stl_p(p++, 0x01602021); /* move a0,t3 */ |
26ea0918 | 944 | /* 0x83c YAMON print_count */ |
0983979b PB |
945 | stl_p(p++, 0x03e06821); /* move t5,ra */ |
946 | stl_p(p++, 0x00805821); /* move t3,a0 */ | |
947 | stl_p(p++, 0x00a05021); /* move t2,a1 */ | |
948 | stl_p(p++, 0x00c06021); /* move t4,a2 */ | |
949 | stl_p(p++, 0x91440000); /* lbu a0,0(t2) */ | |
950 | stl_p(p++, 0x0ff0021c); /* jal 870 */ | |
951 | stl_p(p++, 0x00000000); /* nop */ | |
952 | stl_p(p++, 0x254a0001); /* addiu t2,t2,1 */ | |
953 | stl_p(p++, 0x258cffff); /* addiu t4,t4,-1 */ | |
954 | stl_p(p++, 0x1580fffa); /* bnez t4,84c */ | |
955 | stl_p(p++, 0x00000000); /* nop */ | |
9fba1500 | 956 | stl_p(p++, 0x01a00009); /* jalr t5 */ |
0983979b | 957 | stl_p(p++, 0x01602021); /* move a0,t3 */ |
26ea0918 | 958 | /* 0x870 */ |
0983979b PB |
959 | stl_p(p++, 0x3c08b800); /* lui t0,0xb400 */ |
960 | stl_p(p++, 0x350803f8); /* ori t0,t0,0x3f8 */ | |
961 | stl_p(p++, 0x91090005); /* lbu t1,5(t0) */ | |
962 | stl_p(p++, 0x00000000); /* nop */ | |
963 | stl_p(p++, 0x31290040); /* andi t1,t1,0x40 */ | |
964 | stl_p(p++, 0x1120fffc); /* beqz t1,878 <outch+0x8> */ | |
965 | stl_p(p++, 0x00000000); /* nop */ | |
9fba1500 | 966 | stl_p(p++, 0x03e00009); /* jalr ra */ |
0983979b | 967 | stl_p(p++, 0xa1040000); /* sb a0,0(t0) */ |
26ea0918 | 968 | |
5856de80 TS |
969 | } |
970 | ||
8b7968f7 SW |
971 | static void GCC_FMT_ATTR(3, 4) prom_set(uint32_t* prom_buf, int index, |
972 | const char *string, ...) | |
5856de80 TS |
973 | { |
974 | va_list ap; | |
3ddd0065 | 975 | int32_t table_addr; |
5856de80 TS |
976 | |
977 | if (index >= ENVP_NB_ENTRIES) | |
978 | return; | |
979 | ||
5856de80 | 980 | if (string == NULL) { |
c938ada2 | 981 | prom_buf[index] = 0; |
5856de80 TS |
982 | return; |
983 | } | |
984 | ||
c938ada2 AJ |
985 | table_addr = sizeof(int32_t) * ENVP_NB_ENTRIES + index * ENVP_ENTRY_SIZE; |
986 | prom_buf[index] = tswap32(ENVP_ADDR + table_addr); | |
5856de80 TS |
987 | |
988 | va_start(ap, string); | |
c938ada2 | 989 | vsnprintf((char *)prom_buf + table_addr, ENVP_ENTRY_SIZE, string, ap); |
5856de80 TS |
990 | va_end(ap); |
991 | } | |
992 | ||
993 | /* Kernel */ | |
e16ad5b0 | 994 | static int64_t load_kernel (void) |
5856de80 | 995 | { |
f3839fda LZ |
996 | int64_t kernel_entry, kernel_high, initrd_size; |
997 | long kernel_size; | |
c227f099 | 998 | ram_addr_t initrd_offset; |
ca20cf32 | 999 | int big_endian; |
c938ada2 AJ |
1000 | uint32_t *prom_buf; |
1001 | long prom_size; | |
1002 | int prom_index = 0; | |
b0311811 | 1003 | uint64_t (*xlate_to_kseg0) (void *opaque, uint64_t addr); |
ca20cf32 BS |
1004 | |
1005 | #ifdef TARGET_WORDS_BIGENDIAN | |
1006 | big_endian = 1; | |
1007 | #else | |
1008 | big_endian = 0; | |
1009 | #endif | |
5856de80 | 1010 | |
4366e1db LM |
1011 | kernel_size = load_elf(loaderparams.kernel_filename, NULL, |
1012 | cpu_mips_kseg0_to_phys, NULL, | |
1013 | (uint64_t *)&kernel_entry, NULL, | |
3ee3122c AJ |
1014 | (uint64_t *)&kernel_high, big_endian, EM_MIPS, 1, 0); |
1015 | if (kernel_size < 0) { | |
bd6e1d81 | 1016 | error_report("could not load kernel '%s': %s", |
3ee3122c AJ |
1017 | loaderparams.kernel_filename, |
1018 | load_elf_strerror(kernel_size)); | |
acdf72bb | 1019 | exit(1); |
5856de80 | 1020 | } |
f7f15245 | 1021 | |
d3d93c6c JH |
1022 | /* Check where the kernel has been linked */ |
1023 | if (kernel_entry & 0x80000000ll) { | |
1024 | if (kvm_enabled()) { | |
f7f15245 JH |
1025 | error_report("KVM guest kernels must be linked in useg. " |
1026 | "Did you forget to enable CONFIG_KVM_GUEST?"); | |
1027 | exit(1); | |
1028 | } | |
1029 | ||
d3d93c6c | 1030 | xlate_to_kseg0 = cpu_mips_phys_to_kseg0; |
b0311811 | 1031 | } else { |
d3d93c6c JH |
1032 | /* if kernel entry is in useg it is probably a KVM T&E kernel */ |
1033 | mips_um_ksegs_enable(); | |
f7f15245 | 1034 | |
d3d93c6c | 1035 | xlate_to_kseg0 = cpu_mips_kvm_um_phys_to_kseg0; |
b0311811 | 1036 | } |
5856de80 TS |
1037 | |
1038 | /* load initrd */ | |
1039 | initrd_size = 0; | |
74287114 | 1040 | initrd_offset = 0; |
7df526e3 TS |
1041 | if (loaderparams.initrd_filename) { |
1042 | initrd_size = get_image_size (loaderparams.initrd_filename); | |
74287114 | 1043 | if (initrd_size > 0) { |
9652ef24 AJ |
1044 | /* The kernel allocates the bootmap memory in the low memory after |
1045 | the initrd. It takes at most 128kiB for 2GB RAM and 4kiB | |
1046 | pages. */ | |
be01029e PMD |
1047 | initrd_offset = (loaderparams.ram_low_size - initrd_size |
1048 | - (128 * KiB) | |
9768e2ab AJ |
1049 | - ~INITRD_PAGE_MASK) & INITRD_PAGE_MASK; |
1050 | if (kernel_high >= initrd_offset) { | |
bd6e1d81 AF |
1051 | error_report("memory too small for initial ram disk '%s'", |
1052 | loaderparams.initrd_filename); | |
74287114 TS |
1053 | exit(1); |
1054 | } | |
dcac9679 PB |
1055 | initrd_size = load_image_targphys(loaderparams.initrd_filename, |
1056 | initrd_offset, | |
1057 | ram_size - initrd_offset); | |
74287114 | 1058 | } |
5856de80 | 1059 | if (initrd_size == (target_ulong) -1) { |
bd6e1d81 AF |
1060 | error_report("could not load initial ram disk '%s'", |
1061 | loaderparams.initrd_filename); | |
5856de80 TS |
1062 | exit(1); |
1063 | } | |
1064 | } | |
1065 | ||
c938ada2 AJ |
1066 | /* Setup prom parameters. */ |
1067 | prom_size = ENVP_NB_ENTRIES * (sizeof(int32_t) + ENVP_ENTRY_SIZE); | |
7267c094 | 1068 | prom_buf = g_malloc(prom_size); |
c938ada2 | 1069 | |
f36d53ef | 1070 | prom_set(prom_buf, prom_index++, "%s", loaderparams.kernel_filename); |
c938ada2 | 1071 | if (initrd_size > 0) { |
f3839fda | 1072 | prom_set(prom_buf, prom_index++, "rd_start=0x%" PRIx64 " rd_size=%" PRId64 " %s", |
b0311811 | 1073 | xlate_to_kseg0(NULL, initrd_offset), initrd_size, |
7df526e3 | 1074 | loaderparams.kernel_cmdline); |
c938ada2 | 1075 | } else { |
f36d53ef | 1076 | prom_set(prom_buf, prom_index++, "%s", loaderparams.kernel_cmdline); |
c938ada2 AJ |
1077 | } |
1078 | ||
1079 | prom_set(prom_buf, prom_index++, "memsize"); | |
71c199c8 PB |
1080 | prom_set(prom_buf, prom_index++, "%u", loaderparams.ram_low_size); |
1081 | ||
1082 | prom_set(prom_buf, prom_index++, "ememsize"); | |
1083 | prom_set(prom_buf, prom_index++, "%u", loaderparams.ram_size); | |
b0311811 | 1084 | |
c938ada2 AJ |
1085 | prom_set(prom_buf, prom_index++, "modetty0"); |
1086 | prom_set(prom_buf, prom_index++, "38400n8r"); | |
1087 | prom_set(prom_buf, prom_index++, NULL); | |
1088 | ||
1089 | rom_add_blob_fixed("prom", prom_buf, prom_size, | |
409dbce5 | 1090 | cpu_mips_kseg0_to_phys(NULL, ENVP_ADDR)); |
5856de80 | 1091 | |
3ad9fd5a | 1092 | g_free(prom_buf); |
74287114 | 1093 | return kernel_entry; |
5856de80 TS |
1094 | } |
1095 | ||
ce3960eb | 1096 | static void malta_mips_config(MIPSCPU *cpu) |
c4cb2578 | 1097 | { |
ce3960eb AF |
1098 | CPUMIPSState *env = &cpu->env; |
1099 | CPUState *cs = CPU(cpu); | |
1100 | ||
c4cb2578 | 1101 | env->mvp->CP0_MVPConf0 |= ((smp_cpus - 1) << CP0MVPC0_PVPE) | |
ce3960eb | 1102 | ((smp_cpus * cs->nr_threads - 1) << CP0MVPC0_PTC); |
c4cb2578 EI |
1103 | } |
1104 | ||
5856de80 TS |
1105 | static void main_cpu_reset(void *opaque) |
1106 | { | |
1004ee8d AF |
1107 | MIPSCPU *cpu = opaque; |
1108 | CPUMIPSState *env = &cpu->env; | |
1109 | ||
1110 | cpu_reset(CPU(cpu)); | |
5856de80 | 1111 | |
5c43485f | 1112 | /* The bootloader does not need to be rewritten as it is located in a |
5856de80 TS |
1113 | read only location. The kernel location and the arguments table |
1114 | location does not change. */ | |
7df526e3 | 1115 | if (loaderparams.kernel_filename) { |
d6ca4277 | 1116 | env->CP0_Status &= ~(1 << CP0St_ERL); |
fb82fea0 | 1117 | } |
c4cb2578 | 1118 | |
ce3960eb | 1119 | malta_mips_config(cpu); |
b0311811 JH |
1120 | |
1121 | if (kvm_enabled()) { | |
1122 | /* Start running from the bootloader we wrote to end of RAM */ | |
ca2f6bbb | 1123 | env->active_tc.PC = 0x40000000 + loaderparams.ram_low_size; |
b0311811 | 1124 | } |
5856de80 TS |
1125 | } |
1126 | ||
a7519f2b | 1127 | static void create_cpu_without_cps(const char *cpu_type, |
bff384a4 | 1128 | qemu_irq *cbus_irq, qemu_irq *i8259_irq) |
67a54961 LA |
1129 | { |
1130 | CPUMIPSState *env; | |
1131 | MIPSCPU *cpu; | |
1132 | int i; | |
67a54961 LA |
1133 | |
1134 | for (i = 0; i < smp_cpus; i++) { | |
a7519f2b | 1135 | cpu = MIPS_CPU(cpu_create(cpu_type)); |
67a54961 LA |
1136 | |
1137 | /* Init internal devices */ | |
5a975d43 PB |
1138 | cpu_mips_irq_init_cpu(cpu); |
1139 | cpu_mips_clock_init(cpu); | |
67a54961 LA |
1140 | qemu_register_reset(main_cpu_reset, cpu); |
1141 | } | |
1142 | ||
1143 | cpu = MIPS_CPU(first_cpu); | |
1144 | env = &cpu->env; | |
1145 | *i8259_irq = env->irq[2]; | |
1146 | *cbus_irq = env->irq[4]; | |
1147 | } | |
1148 | ||
a7519f2b | 1149 | static void create_cps(MaltaState *s, const char *cpu_type, |
bff384a4 LA |
1150 | qemu_irq *cbus_irq, qemu_irq *i8259_irq) |
1151 | { | |
1152 | Error *err = NULL; | |
bff384a4 | 1153 | |
81491c28 | 1154 | s->cps = MIPS_CPS(object_new(TYPE_MIPS_CPS)); |
bff384a4 LA |
1155 | qdev_set_parent_bus(DEVICE(s->cps), sysbus_get_default()); |
1156 | ||
a7519f2b | 1157 | object_property_set_str(OBJECT(s->cps), cpu_type, "cpu-type", &err); |
bff384a4 LA |
1158 | object_property_set_int(OBJECT(s->cps), smp_cpus, "num-vp", &err); |
1159 | object_property_set_bool(OBJECT(s->cps), true, "realized", &err); | |
1160 | if (err != NULL) { | |
1161 | error_report("%s", error_get_pretty(err)); | |
1162 | exit(1); | |
1163 | } | |
1164 | ||
1165 | sysbus_mmio_map_overlap(SYS_BUS_DEVICE(s->cps), 0, 0, 1); | |
1166 | ||
19494f81 | 1167 | *i8259_irq = get_cps_irq(s->cps, 3); |
bff384a4 LA |
1168 | *cbus_irq = NULL; |
1169 | } | |
1170 | ||
a7519f2b IM |
1171 | static void mips_create_cpu(MaltaState *s, const char *cpu_type, |
1172 | qemu_irq *cbus_irq, qemu_irq *i8259_irq) | |
bff384a4 | 1173 | { |
a7519f2b IM |
1174 | if ((smp_cpus > 1) && cpu_supports_cps_smp(cpu_type)) { |
1175 | create_cps(s, cpu_type, cbus_irq, i8259_irq); | |
bff384a4 | 1176 | } else { |
a7519f2b | 1177 | create_cpu_without_cps(cpu_type, cbus_irq, i8259_irq); |
bff384a4 LA |
1178 | } |
1179 | } | |
1180 | ||
70705261 | 1181 | static |
3ef96221 | 1182 | void mips_malta_init(MachineState *machine) |
5856de80 | 1183 | { |
3ef96221 | 1184 | ram_addr_t ram_size = machine->ram_size; |
b0311811 | 1185 | ram_addr_t ram_low_size; |
3ef96221 MA |
1186 | const char *kernel_filename = machine->kernel_filename; |
1187 | const char *kernel_cmdline = machine->kernel_cmdline; | |
1188 | const char *initrd_filename = machine->initrd_filename; | |
5cea8590 | 1189 | char *filename; |
16434065 | 1190 | PFlashCFI01 *fl; |
cfe5f011 | 1191 | MemoryRegion *system_memory = get_system_memory(); |
94c2b6af PB |
1192 | MemoryRegion *ram_high = g_new(MemoryRegion, 1); |
1193 | MemoryRegion *ram_low_preio = g_new(MemoryRegion, 1); | |
1194 | MemoryRegion *ram_low_postio; | |
a427338b | 1195 | MemoryRegion *bios, *bios_copy = g_new(MemoryRegion, 1); |
35c64807 PB |
1196 | const size_t smbus_eeprom_size = 8 * 256; |
1197 | uint8_t *smbus_eeprom_buf = g_malloc0(smbus_eeprom_size); | |
b0311811 | 1198 | int64_t kernel_entry, bootloader_run_addr; |
5856de80 | 1199 | PCIBus *pci_bus; |
48a18b3c | 1200 | ISABus *isa_bus; |
e9b40fd3 | 1201 | qemu_irq *isa_irq; |
67a54961 | 1202 | qemu_irq cbus_irq, i8259_irq; |
7b717336 | 1203 | int piix4_devfn; |
a5c82852 | 1204 | I2CBus *smbus; |
751c6a17 | 1205 | DriveInfo *dinfo; |
f455e98c | 1206 | DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS]; |
c8b153d7 | 1207 | int fl_idx = 0; |
01e0451a | 1208 | int be; |
5856de80 | 1209 | |
cba5cb67 AF |
1210 | DeviceState *dev = qdev_create(NULL, TYPE_MIPS_MALTA); |
1211 | MaltaState *s = MIPS_MALTA(dev); | |
e9b40fd3 | 1212 | |
cc413a39 AJ |
1213 | /* The whole address space decoded by the GT-64120A doesn't generate |
1214 | exception when accessing invalid memory. Create an empty slot to | |
1215 | emulate this feature. */ | |
1216 | empty_slot_init(0, 0x20000000); | |
1217 | ||
e9b40fd3 SW |
1218 | qdev_init_nofail(dev); |
1219 | ||
bff384a4 | 1220 | /* create CPU */ |
a7519f2b | 1221 | mips_create_cpu(s, machine->cpu_type, &cbus_irq, &i8259_irq); |
5856de80 TS |
1222 | |
1223 | /* allocate RAM */ | |
be01029e PMD |
1224 | if (ram_size > 2 * GiB) { |
1225 | error_report("Too much memory for this machine: %" PRId64 "MB," | |
1226 | " maximum 2048MB", ram_size / MiB); | |
0ccff151 AJ |
1227 | exit(1); |
1228 | } | |
94c2b6af PB |
1229 | |
1230 | /* register RAM at high address where it is undisturbed by IO */ | |
6a926fbc DM |
1231 | memory_region_allocate_system_memory(ram_high, NULL, "mips_malta.ram", |
1232 | ram_size); | |
94c2b6af PB |
1233 | memory_region_add_subregion(system_memory, 0x80000000, ram_high); |
1234 | ||
1235 | /* alias for pre IO hole access */ | |
1236 | memory_region_init_alias(ram_low_preio, NULL, "mips_malta_low_preio.ram", | |
be01029e | 1237 | ram_high, 0, MIN(ram_size, 256 * MiB)); |
94c2b6af PB |
1238 | memory_region_add_subregion(system_memory, 0, ram_low_preio); |
1239 | ||
1240 | /* alias for post IO hole access, if there is enough RAM */ | |
be01029e | 1241 | if (ram_size > 512 * MiB) { |
94c2b6af PB |
1242 | ram_low_postio = g_new(MemoryRegion, 1); |
1243 | memory_region_init_alias(ram_low_postio, NULL, | |
1244 | "mips_malta_low_postio.ram", | |
be01029e PMD |
1245 | ram_high, 512 * MiB, |
1246 | ram_size - 512 * MiB); | |
1247 | memory_region_add_subregion(system_memory, 512 * MiB, | |
1248 | ram_low_postio); | |
94c2b6af | 1249 | } |
5856de80 | 1250 | |
01e0451a AL |
1251 | #ifdef TARGET_WORDS_BIGENDIAN |
1252 | be = 1; | |
1253 | #else | |
1254 | be = 0; | |
1255 | #endif | |
7313b1f2 | 1256 | |
070ce5ed | 1257 | /* FPGA */ |
7313b1f2 | 1258 | |
68d00192 | 1259 | /* The CBUS UART is attached to the MIPS CPU INT2 pin, ie interrupt 4 */ |
9bca0edb | 1260 | malta_fpga_init(system_memory, FPGA_ADDRESS, cbus_irq, serial_hd(2)); |
070ce5ed | 1261 | |
bb4b3358 SW |
1262 | /* Load firmware in flash / BIOS. */ |
1263 | dinfo = drive_get(IF_PFLASH, 0, fl_idx); | |
940d5b13 | 1264 | fl = pflash_cfi01_register(FLASH_ADDRESS, "mips_malta.bios", |
7ebfece5 | 1265 | FLASH_SIZE, |
4be74634 | 1266 | dinfo ? blk_by_legacy_dinfo(dinfo) : NULL, |
ce14710f | 1267 | 65536, |
bb4b3358 SW |
1268 | 4, 0x0000, 0x0000, 0x0000, 0x0000, be); |
1269 | bios = pflash_cfi01_get_memory(fl); | |
1270 | fl_idx++; | |
c8b153d7 | 1271 | if (kernel_filename) { |
be01029e | 1272 | ram_low_size = MIN(ram_size, 256 * MiB); |
fbdb1d95 | 1273 | /* For KVM we reserve 1MB of RAM for running bootloader */ |
b0311811 JH |
1274 | if (kvm_enabled()) { |
1275 | ram_low_size -= 0x100000; | |
1276 | bootloader_run_addr = 0x40000000 + ram_low_size; | |
1277 | } else { | |
1278 | bootloader_run_addr = 0xbfc00000; | |
1279 | } | |
1280 | ||
c8b153d7 | 1281 | /* Write a small bootloader to the flash location. */ |
71c199c8 PB |
1282 | loaderparams.ram_size = ram_size; |
1283 | loaderparams.ram_low_size = ram_low_size; | |
c8b153d7 TS |
1284 | loaderparams.kernel_filename = kernel_filename; |
1285 | loaderparams.kernel_cmdline = kernel_cmdline; | |
1286 | loaderparams.initrd_filename = initrd_filename; | |
e16ad5b0 | 1287 | kernel_entry = load_kernel(); |
b0311811 | 1288 | |
ce3940cc MF |
1289 | if (!cpu_supports_isa(machine->cpu_type, ISA_NANOMIPS32)) { |
1290 | write_bootloader(memory_region_get_ram_ptr(bios), | |
1291 | bootloader_run_addr, kernel_entry); | |
1292 | } else { | |
1293 | write_bootloader_nanomips(memory_region_get_ram_ptr(bios), | |
1294 | bootloader_run_addr, kernel_entry); | |
1295 | } | |
b0311811 JH |
1296 | if (kvm_enabled()) { |
1297 | /* Write the bootloader code @ the end of RAM, 1MB reserved */ | |
cc518af0 | 1298 | write_bootloader(memory_region_get_ram_ptr(ram_low_preio) + |
b0311811 JH |
1299 | ram_low_size, |
1300 | bootloader_run_addr, kernel_entry); | |
1301 | } | |
c8b153d7 | 1302 | } else { |
74c02ebd | 1303 | target_long bios_size = FLASH_SIZE; |
fbdb1d95 | 1304 | /* The flash region isn't executable from a KVM guest */ |
3c5d0be5 JH |
1305 | if (kvm_enabled()) { |
1306 | error_report("KVM enabled but no -kernel argument was specified. " | |
fbdb1d95 | 1307 | "Booting from flash is not supported with KVM."); |
3c5d0be5 JH |
1308 | exit(1); |
1309 | } | |
bb4b3358 SW |
1310 | /* Load firmware from flash. */ |
1311 | if (!dinfo) { | |
c8b153d7 | 1312 | /* Load a BIOS image. */ |
bb4b3358 | 1313 | if (bios_name == NULL) { |
c8b153d7 | 1314 | bios_name = BIOS_FILENAME; |
bb4b3358 | 1315 | } |
5cea8590 PB |
1316 | filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); |
1317 | if (filename) { | |
03a1a8e1 | 1318 | bios_size = load_image_targphys(filename, FLASH_ADDRESS, |
5cea8590 | 1319 | BIOS_SIZE); |
7267c094 | 1320 | g_free(filename); |
5cea8590 PB |
1321 | } else { |
1322 | bios_size = -1; | |
1323 | } | |
2c57bd9b AF |
1324 | if ((bios_size < 0 || bios_size > BIOS_SIZE) && |
1325 | !kernel_filename && !qtest_enabled()) { | |
2e985fe0 AJ |
1326 | error_report("Could not load MIPS bios '%s', and no " |
1327 | "-kernel argument was specified", bios_name); | |
1328 | exit(1); | |
c8b153d7 | 1329 | } |
070ce5ed | 1330 | } |
3187ef03 TS |
1331 | /* In little endian mode the 32bit words in the bios are swapped, |
1332 | a neat trick which allows bi-endian firmware. */ | |
1333 | #ifndef TARGET_WORDS_BIGENDIAN | |
1334 | { | |
0f0f8b61 TH |
1335 | uint32_t *end, *addr; |
1336 | const size_t swapsize = MIN(bios_size, 0x3e0000); | |
1337 | addr = rom_ptr(FLASH_ADDRESS, swapsize); | |
a2b8813d PB |
1338 | if (!addr) { |
1339 | addr = memory_region_get_ram_ptr(bios); | |
1340 | } | |
0f0f8b61 | 1341 | end = (void *)addr + swapsize; |
d7585251 PB |
1342 | while (addr < end) { |
1343 | bswap32s(addr); | |
a30cfee5 | 1344 | addr++; |
3187ef03 TS |
1345 | } |
1346 | } | |
1347 | #endif | |
070ce5ed TS |
1348 | } |
1349 | ||
a427338b PB |
1350 | /* |
1351 | * Map the BIOS at a 2nd physical location, as on the real board. | |
1352 | * Copy it so that we can patch in the MIPS revision, which cannot be | |
1353 | * handled by an overlapping region as the resulting ROM code subpage | |
1354 | * regions are not executable. | |
1355 | */ | |
917b77f5 | 1356 | memory_region_init_ram(bios_copy, NULL, "bios.1fc", BIOS_SIZE, |
f8ed85ac | 1357 | &error_fatal); |
a427338b | 1358 | if (!rom_copy(memory_region_get_ram_ptr(bios_copy), |
f05d4d94 | 1359 | FLASH_ADDRESS, BIOS_SIZE)) { |
a427338b | 1360 | memcpy(memory_region_get_ram_ptr(bios_copy), |
f05d4d94 | 1361 | memory_region_get_ram_ptr(bios), BIOS_SIZE); |
a427338b PB |
1362 | } |
1363 | memory_region_set_readonly(bios_copy, true); | |
1364 | memory_region_add_subregion(system_memory, RESET_ADDRESS, bios_copy); | |
82a9807b | 1365 | |
a427338b PB |
1366 | /* Board ID = 0x420 (Malta Board with CoreLV) */ |
1367 | stl_p(memory_region_get_ram_ptr(bios_copy) + 0x10, 0x00000420); | |
5856de80 | 1368 | |
5632ae46 AK |
1369 | /* |
1370 | * We have a circular dependency problem: pci_bus depends on isa_irq, | |
1371 | * isa_irq is provided by i8259, i8259 depends on ISA, ISA depends | |
1372 | * on piix4, and piix4 depends on pci_bus. To stop the cycle we have | |
1373 | * qemu_irq_proxy() adds an extra bit of indirection, allowing us | |
1374 | * to resolve the isa_irq -> i8259 dependency after i8259 is initialized. | |
1375 | */ | |
e9b40fd3 | 1376 | isa_irq = qemu_irq_proxy(&s->i8259, 16); |
5856de80 TS |
1377 | |
1378 | /* Northbridge */ | |
5632ae46 | 1379 | pci_bus = gt64120_register(isa_irq); |
5856de80 TS |
1380 | |
1381 | /* Southbridge */ | |
d8f94e1b | 1382 | ide_drive_get(hd, ARRAY_SIZE(hd)); |
e4bcb14c | 1383 | |
142e9787 | 1384 | piix4_devfn = piix4_init(pci_bus, &isa_bus, 80); |
5632ae46 AK |
1385 | |
1386 | /* Interrupt controller */ | |
1387 | /* The 8259 is attached to the MIPS CPU INT0 pin, ie interrupt 2 */ | |
67a54961 | 1388 | s->i8259 = i8259_init(isa_bus, i8259_irq); |
5632ae46 | 1389 | |
e9b40fd3 | 1390 | isa_bus_irqs(isa_bus, s->i8259); |
ae027ad3 | 1391 | pci_piix4_ide_init(pci_bus, hd, piix4_devfn + 1); |
afb9a60e | 1392 | pci_create_simple(pci_bus, piix4_devfn + 2, "piix4-usb-uhci"); |
48a18b3c | 1393 | smbus = piix4_pm_init(pci_bus, piix4_devfn + 3, 0x1100, |
6e7d8249 | 1394 | isa_get_irq(NULL, 9), NULL, 0, NULL); |
acf695ec | 1395 | pit = i8254_pit_init(isa_bus, 0x40, 0, NULL); |
55f613ac | 1396 | i8257_dma_init(isa_bus, 0); |
78f16256 PMD |
1397 | mc146818_rtc_init(isa_bus, 2000, NULL); |
1398 | ||
1399 | /* generate SPD EEPROM data */ | |
1400 | generate_eeprom_spd(&smbus_eeprom_buf[0 * 256], ram_size); | |
1401 | generate_eeprom_serial(&smbus_eeprom_buf[6 * 256]); | |
1402 | smbus_eeprom_init(smbus, 8, smbus_eeprom_buf, smbus_eeprom_size); | |
1403 | g_free(smbus_eeprom_buf); | |
5856de80 | 1404 | |
7313b1f2 PMD |
1405 | /* Super I/O: SMS FDC37M817 */ |
1406 | isa_create_simple(isa_bus, TYPE_FDC37M81X_SUPERIO); | |
5856de80 | 1407 | |
5856de80 | 1408 | /* Network card */ |
29b358f9 | 1409 | network_init(pci_bus); |
11f29511 TS |
1410 | |
1411 | /* Optional PCI video card */ | |
9c59864d | 1412 | pci_vga_init(pci_bus); |
5856de80 TS |
1413 | } |
1414 | ||
8c43a6f0 | 1415 | static const TypeInfo mips_malta_device = { |
cba5cb67 | 1416 | .name = TYPE_MIPS_MALTA, |
39bffca2 AL |
1417 | .parent = TYPE_SYS_BUS_DEVICE, |
1418 | .instance_size = sizeof(MaltaState), | |
e9b40fd3 SW |
1419 | }; |
1420 | ||
e264d29d | 1421 | static void mips_malta_machine_init(MachineClass *mc) |
e9b40fd3 | 1422 | { |
e264d29d EH |
1423 | mc->desc = "MIPS Malta Core LV"; |
1424 | mc->init = mips_malta_init; | |
2059839b | 1425 | mc->block_default_type = IF_IDE; |
e264d29d EH |
1426 | mc->max_cpus = 16; |
1427 | mc->is_default = 1; | |
a7519f2b IM |
1428 | #ifdef TARGET_MIPS64 |
1429 | mc->default_cpu_type = MIPS_CPU_TYPE_NAME("20Kc"); | |
1430 | #else | |
1431 | mc->default_cpu_type = MIPS_CPU_TYPE_NAME("24Kf"); | |
1432 | #endif | |
e9b40fd3 SW |
1433 | } |
1434 | ||
e264d29d EH |
1435 | DEFINE_MACHINE("malta", mips_malta_machine_init) |
1436 | ||
1437 | static void mips_malta_register_types(void) | |
f80f9ec9 | 1438 | { |
e264d29d | 1439 | type_register_static(&mips_malta_device); |
f80f9ec9 AL |
1440 | } |
1441 | ||
83f7d43a | 1442 | type_init(mips_malta_register_types) |