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