mips_malta.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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. #include "hw.h"
  25. #include "pc.h"
  26. #include "fdc.h"
  27. #include "net.h"
  28. #include "boards.h"
  29. #include "smbus.h"
  30. #include "block.h"
  31. #include "flash.h"
  32. #include "mips.h"
  33. #include "mips_cpudevs.h"
  34. #include "pci.h"
  35. #include "usb-uhci.h"
  36. #include "vmware_vga.h"
  37. #include "qemu-char.h"
  38. #include "sysemu.h"
  39. #include "arch_init.h"
  40. #include "boards.h"
  41. #include "qemu-log.h"
  42. #include "mips-bios.h"
  43. #include "ide.h"
  44. #include "loader.h"
  45. #include "elf.h"
  46. #include "mc146818rtc.h"
  47. #include "blockdev.h"
  48. #include "exec-memory.h"
  49. #include "sysbus.h" /* SysBusDevice */
  50. //#define DEBUG_BOARD_INIT
  51. #define ENVP_ADDR 0x80002000l
  52. #define ENVP_NB_ENTRIES 16
  53. #define ENVP_ENTRY_SIZE 256
  54. #define MAX_IDE_BUS 2
  55. typedef struct {
  56. MemoryRegion iomem;
  57. MemoryRegion iomem_lo; /* 0 - 0x900 */
  58. MemoryRegion iomem_hi; /* 0xa00 - 0x100000 */
  59. uint32_t leds;
  60. uint32_t brk;
  61. uint32_t gpout;
  62. uint32_t i2cin;
  63. uint32_t i2coe;
  64. uint32_t i2cout;
  65. uint32_t i2csel;
  66. CharDriverState *display;
  67. char display_text[9];
  68. SerialState *uart;
  69. } MaltaFPGAState;
  70. typedef struct {
  71. SysBusDevice busdev;
  72. qemu_irq *i8259;
  73. } MaltaState;
  74. static ISADevice *pit;
  75. static struct _loaderparams {
  76. int ram_size;
  77. const char *kernel_filename;
  78. const char *kernel_cmdline;
  79. const char *initrd_filename;
  80. } loaderparams;
  81. /* Malta FPGA */
  82. static void malta_fpga_update_display(void *opaque)
  83. {
  84. char leds_text[9];
  85. int i;
  86. MaltaFPGAState *s = opaque;
  87. for (i = 7 ; i >= 0 ; i--) {
  88. if (s->leds & (1 << i))
  89. leds_text[i] = '#';
  90. else
  91. leds_text[i] = ' ';
  92. }
  93. leds_text[8] = '\0';
  94. qemu_chr_fe_printf(s->display, "\e[H\n\n|\e[32m%-8.8s\e[00m|\r\n", leds_text);
  95. qemu_chr_fe_printf(s->display, "\n\n\n\n|\e[31m%-8.8s\e[00m|", s->display_text);
  96. }
  97. /*
  98. * EEPROM 24C01 / 24C02 emulation.
  99. *
  100. * Emulation for serial EEPROMs:
  101. * 24C01 - 1024 bit (128 x 8)
  102. * 24C02 - 2048 bit (256 x 8)
  103. *
  104. * Typical device names include Microchip 24C02SC or SGS Thomson ST24C02.
  105. */
  106. //~ #define DEBUG
  107. #if defined(DEBUG)
  108. # define logout(fmt, ...) fprintf(stderr, "MALTA\t%-24s" fmt, __func__, ## __VA_ARGS__)
  109. #else
  110. # define logout(fmt, ...) ((void)0)
  111. #endif
  112. struct _eeprom24c0x_t {
  113. uint8_t tick;
  114. uint8_t address;
  115. uint8_t command;
  116. uint8_t ack;
  117. uint8_t scl;
  118. uint8_t sda;
  119. uint8_t data;
  120. //~ uint16_t size;
  121. uint8_t contents[256];
  122. };
  123. typedef struct _eeprom24c0x_t eeprom24c0x_t;
  124. static eeprom24c0x_t eeprom = {
  125. .contents = {
  126. /* 00000000: */ 0x80,0x08,0x04,0x0D,0x0A,0x01,0x40,0x00,
  127. /* 00000008: */ 0x01,0x75,0x54,0x00,0x82,0x08,0x00,0x01,
  128. /* 00000010: */ 0x8F,0x04,0x02,0x01,0x01,0x00,0x0E,0x00,
  129. /* 00000018: */ 0x00,0x00,0x00,0x14,0x0F,0x14,0x2D,0x40,
  130. /* 00000020: */ 0x15,0x08,0x15,0x08,0x00,0x00,0x00,0x00,
  131. /* 00000028: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  132. /* 00000030: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  133. /* 00000038: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x12,0xD0,
  134. /* 00000040: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  135. /* 00000048: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  136. /* 00000050: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  137. /* 00000058: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  138. /* 00000060: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  139. /* 00000068: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  140. /* 00000070: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  141. /* 00000078: */ 0x00,0x00,0x00,0x00,0x00,0x00,0x64,0xF4,
  142. },
  143. };
  144. static uint8_t eeprom24c0x_read(void)
  145. {
  146. logout("%u: scl = %u, sda = %u, data = 0x%02x\n",
  147. eeprom.tick, eeprom.scl, eeprom.sda, eeprom.data);
  148. return eeprom.sda;
  149. }
  150. static void eeprom24c0x_write(int scl, int sda)
  151. {
  152. if (eeprom.scl && scl && (eeprom.sda != sda)) {
  153. logout("%u: scl = %u->%u, sda = %u->%u i2c %s\n",
  154. eeprom.tick, eeprom.scl, scl, eeprom.sda, sda, sda ? "stop" : "start");
  155. if (!sda) {
  156. eeprom.tick = 1;
  157. eeprom.command = 0;
  158. }
  159. } else if (eeprom.tick == 0 && !eeprom.ack) {
  160. /* Waiting for start. */
  161. logout("%u: scl = %u->%u, sda = %u->%u wait for i2c start\n",
  162. eeprom.tick, eeprom.scl, scl, eeprom.sda, sda);
  163. } else if (!eeprom.scl && scl) {
  164. logout("%u: scl = %u->%u, sda = %u->%u trigger bit\n",
  165. eeprom.tick, eeprom.scl, scl, eeprom.sda, sda);
  166. if (eeprom.ack) {
  167. logout("\ti2c ack bit = 0\n");
  168. sda = 0;
  169. eeprom.ack = 0;
  170. } else if (eeprom.sda == sda) {
  171. uint8_t bit = (sda != 0);
  172. logout("\ti2c bit = %d\n", bit);
  173. if (eeprom.tick < 9) {
  174. eeprom.command <<= 1;
  175. eeprom.command += bit;
  176. eeprom.tick++;
  177. if (eeprom.tick == 9) {
  178. logout("\tcommand 0x%04x, %s\n", eeprom.command, bit ? "read" : "write");
  179. eeprom.ack = 1;
  180. }
  181. } else if (eeprom.tick < 17) {
  182. if (eeprom.command & 1) {
  183. sda = ((eeprom.data & 0x80) != 0);
  184. }
  185. eeprom.address <<= 1;
  186. eeprom.address += bit;
  187. eeprom.tick++;
  188. eeprom.data <<= 1;
  189. if (eeprom.tick == 17) {
  190. eeprom.data = eeprom.contents[eeprom.address];
  191. logout("\taddress 0x%04x, data 0x%02x\n", eeprom.address, eeprom.data);
  192. eeprom.ack = 1;
  193. eeprom.tick = 0;
  194. }
  195. } else if (eeprom.tick >= 17) {
  196. sda = 0;
  197. }
  198. } else {
  199. logout("\tsda changed with raising scl\n");
  200. }
  201. } else {
  202. logout("%u: scl = %u->%u, sda = %u->%u\n", eeprom.tick, eeprom.scl, scl, eeprom.sda, sda);
  203. }
  204. eeprom.scl = scl;
  205. eeprom.sda = sda;
  206. }
  207. static uint64_t malta_fpga_read(void *opaque, target_phys_addr_t addr,
  208. unsigned size)
  209. {
  210. MaltaFPGAState *s = opaque;
  211. uint32_t val = 0;
  212. uint32_t saddr;
  213. saddr = (addr & 0xfffff);
  214. switch (saddr) {
  215. /* SWITCH Register */
  216. case 0x00200:
  217. val = 0x00000000; /* All switches closed */
  218. break;
  219. /* STATUS Register */
  220. case 0x00208:
  221. #ifdef TARGET_WORDS_BIGENDIAN
  222. val = 0x00000012;
  223. #else
  224. val = 0x00000010;
  225. #endif
  226. break;
  227. /* JMPRS Register */
  228. case 0x00210:
  229. val = 0x00;
  230. break;
  231. /* LEDBAR Register */
  232. case 0x00408:
  233. val = s->leds;
  234. break;
  235. /* BRKRES Register */
  236. case 0x00508:
  237. val = s->brk;
  238. break;
  239. /* UART Registers are handled directly by the serial device */
  240. /* GPOUT Register */
  241. case 0x00a00:
  242. val = s->gpout;
  243. break;
  244. /* XXX: implement a real I2C controller */
  245. /* GPINP Register */
  246. case 0x00a08:
  247. /* IN = OUT until a real I2C control is implemented */
  248. if (s->i2csel)
  249. val = s->i2cout;
  250. else
  251. val = 0x00;
  252. break;
  253. /* I2CINP Register */
  254. case 0x00b00:
  255. val = ((s->i2cin & ~1) | eeprom24c0x_read());
  256. break;
  257. /* I2COE Register */
  258. case 0x00b08:
  259. val = s->i2coe;
  260. break;
  261. /* I2COUT Register */
  262. case 0x00b10:
  263. val = s->i2cout;
  264. break;
  265. /* I2CSEL Register */
  266. case 0x00b18:
  267. val = s->i2csel;
  268. break;
  269. default:
  270. #if 0
  271. printf ("malta_fpga_read: Bad register offset 0x" TARGET_FMT_lx "\n",
  272. addr);
  273. #endif
  274. break;
  275. }
  276. return val;
  277. }
  278. static void malta_fpga_write(void *opaque, target_phys_addr_t addr,
  279. uint64_t val, unsigned size)
  280. {
  281. MaltaFPGAState *s = opaque;
  282. uint32_t saddr;
  283. saddr = (addr & 0xfffff);
  284. switch (saddr) {
  285. /* SWITCH Register */
  286. case 0x00200:
  287. break;
  288. /* JMPRS Register */
  289. case 0x00210:
  290. break;
  291. /* LEDBAR Register */
  292. /* XXX: implement a 8-LED array */
  293. case 0x00408:
  294. s->leds = val & 0xff;
  295. break;
  296. /* ASCIIWORD Register */
  297. case 0x00410:
  298. snprintf(s->display_text, 9, "%08X", (uint32_t)val);
  299. malta_fpga_update_display(s);
  300. break;
  301. /* ASCIIPOS0 to ASCIIPOS7 Registers */
  302. case 0x00418:
  303. case 0x00420:
  304. case 0x00428:
  305. case 0x00430:
  306. case 0x00438:
  307. case 0x00440:
  308. case 0x00448:
  309. case 0x00450:
  310. s->display_text[(saddr - 0x00418) >> 3] = (char) val;
  311. malta_fpga_update_display(s);
  312. break;
  313. /* SOFTRES Register */
  314. case 0x00500:
  315. if (val == 0x42)
  316. qemu_system_reset_request ();
  317. break;
  318. /* BRKRES Register */
  319. case 0x00508:
  320. s->brk = val & 0xff;
  321. break;
  322. /* UART Registers are handled directly by the serial device */
  323. /* GPOUT Register */
  324. case 0x00a00:
  325. s->gpout = val & 0xff;
  326. break;
  327. /* I2COE Register */
  328. case 0x00b08:
  329. s->i2coe = val & 0x03;
  330. break;
  331. /* I2COUT Register */
  332. case 0x00b10:
  333. eeprom24c0x_write(val & 0x02, val & 0x01);
  334. s->i2cout = val;
  335. break;
  336. /* I2CSEL Register */
  337. case 0x00b18:
  338. s->i2csel = val & 0x01;
  339. break;
  340. default:
  341. #if 0
  342. printf ("malta_fpga_write: Bad register offset 0x" TARGET_FMT_lx "\n",
  343. addr);
  344. #endif
  345. break;
  346. }
  347. }
  348. static const MemoryRegionOps malta_fpga_ops = {
  349. .read = malta_fpga_read,
  350. .write = malta_fpga_write,
  351. .endianness = DEVICE_NATIVE_ENDIAN,
  352. };
  353. static void malta_fpga_reset(void *opaque)
  354. {
  355. MaltaFPGAState *s = opaque;
  356. s->leds = 0x00;
  357. s->brk = 0x0a;
  358. s->gpout = 0x00;
  359. s->i2cin = 0x3;
  360. s->i2coe = 0x0;
  361. s->i2cout = 0x3;
  362. s->i2csel = 0x1;
  363. s->display_text[8] = '\0';
  364. snprintf(s->display_text, 9, " ");
  365. }
  366. static void malta_fpga_led_init(CharDriverState *chr)
  367. {
  368. qemu_chr_fe_printf(chr, "\e[HMalta LEDBAR\r\n");
  369. qemu_chr_fe_printf(chr, "+--------+\r\n");
  370. qemu_chr_fe_printf(chr, "+ +\r\n");
  371. qemu_chr_fe_printf(chr, "+--------+\r\n");
  372. qemu_chr_fe_printf(chr, "\n");
  373. qemu_chr_fe_printf(chr, "Malta ASCII\r\n");
  374. qemu_chr_fe_printf(chr, "+--------+\r\n");
  375. qemu_chr_fe_printf(chr, "+ +\r\n");
  376. qemu_chr_fe_printf(chr, "+--------+\r\n");
  377. }
  378. static MaltaFPGAState *malta_fpga_init(MemoryRegion *address_space,
  379. target_phys_addr_t base, qemu_irq uart_irq, CharDriverState *uart_chr)
  380. {
  381. MaltaFPGAState *s;
  382. s = (MaltaFPGAState *)g_malloc0(sizeof(MaltaFPGAState));
  383. memory_region_init_io(&s->iomem, &malta_fpga_ops, s,
  384. "malta-fpga", 0x100000);
  385. memory_region_init_alias(&s->iomem_lo, "malta-fpga",
  386. &s->iomem, 0, 0x900);
  387. memory_region_init_alias(&s->iomem_hi, "malta-fpga",
  388. &s->iomem, 0xa00, 0x10000-0xa00);
  389. memory_region_add_subregion(address_space, base, &s->iomem_lo);
  390. memory_region_add_subregion(address_space, base + 0xa00, &s->iomem_hi);
  391. s->display = qemu_chr_new("fpga", "vc:320x200", malta_fpga_led_init);
  392. s->uart = serial_mm_init(address_space, base + 0x900, 3, uart_irq,
  393. 230400, uart_chr, DEVICE_NATIVE_ENDIAN);
  394. malta_fpga_reset(s);
  395. qemu_register_reset(malta_fpga_reset, s);
  396. return s;
  397. }
  398. /* Network support */
  399. static void network_init(void)
  400. {
  401. int i;
  402. for(i = 0; i < nb_nics; i++) {
  403. NICInfo *nd = &nd_table[i];
  404. const char *default_devaddr = NULL;
  405. if (i == 0 && (!nd->model || strcmp(nd->model, "pcnet") == 0))
  406. /* The malta board has a PCNet card using PCI SLOT 11 */
  407. default_devaddr = "0b";
  408. pci_nic_init_nofail(nd, "pcnet", default_devaddr);
  409. }
  410. }
  411. /* ROM and pseudo bootloader
  412. The following code implements a very very simple bootloader. It first
  413. loads the registers a0 to a3 to the values expected by the OS, and
  414. then jump at the kernel address.
  415. The bootloader should pass the locations of the kernel arguments and
  416. environment variables tables. Those tables contain the 32-bit address
  417. of NULL terminated strings. The environment variables table should be
  418. terminated by a NULL address.
  419. For a simpler implementation, the number of kernel arguments is fixed
  420. to two (the name of the kernel and the command line), and the two
  421. tables are actually the same one.
  422. The registers a0 to a3 should contain the following values:
  423. a0 - number of kernel arguments
  424. a1 - 32-bit address of the kernel arguments table
  425. a2 - 32-bit address of the environment variables table
  426. a3 - RAM size in bytes
  427. */
  428. static void write_bootloader (CPUState *env, uint8_t *base,
  429. int64_t kernel_entry)
  430. {
  431. uint32_t *p;
  432. /* Small bootloader */
  433. p = (uint32_t *)base;
  434. stl_raw(p++, 0x0bf00160); /* j 0x1fc00580 */
  435. stl_raw(p++, 0x00000000); /* nop */
  436. /* YAMON service vector */
  437. stl_raw(base + 0x500, 0xbfc00580); /* start: */
  438. stl_raw(base + 0x504, 0xbfc0083c); /* print_count: */
  439. stl_raw(base + 0x520, 0xbfc00580); /* start: */
  440. stl_raw(base + 0x52c, 0xbfc00800); /* flush_cache: */
  441. stl_raw(base + 0x534, 0xbfc00808); /* print: */
  442. stl_raw(base + 0x538, 0xbfc00800); /* reg_cpu_isr: */
  443. stl_raw(base + 0x53c, 0xbfc00800); /* unred_cpu_isr: */
  444. stl_raw(base + 0x540, 0xbfc00800); /* reg_ic_isr: */
  445. stl_raw(base + 0x544, 0xbfc00800); /* unred_ic_isr: */
  446. stl_raw(base + 0x548, 0xbfc00800); /* reg_esr: */
  447. stl_raw(base + 0x54c, 0xbfc00800); /* unreg_esr: */
  448. stl_raw(base + 0x550, 0xbfc00800); /* getchar: */
  449. stl_raw(base + 0x554, 0xbfc00800); /* syscon_read: */
  450. /* Second part of the bootloader */
  451. p = (uint32_t *) (base + 0x580);
  452. stl_raw(p++, 0x24040002); /* addiu a0, zero, 2 */
  453. stl_raw(p++, 0x3c1d0000 | (((ENVP_ADDR - 64) >> 16) & 0xffff)); /* lui sp, high(ENVP_ADDR) */
  454. stl_raw(p++, 0x37bd0000 | ((ENVP_ADDR - 64) & 0xffff)); /* ori sp, sp, low(ENVP_ADDR) */
  455. stl_raw(p++, 0x3c050000 | ((ENVP_ADDR >> 16) & 0xffff)); /* lui a1, high(ENVP_ADDR) */
  456. stl_raw(p++, 0x34a50000 | (ENVP_ADDR & 0xffff)); /* ori a1, a1, low(ENVP_ADDR) */
  457. stl_raw(p++, 0x3c060000 | (((ENVP_ADDR + 8) >> 16) & 0xffff)); /* lui a2, high(ENVP_ADDR + 8) */
  458. stl_raw(p++, 0x34c60000 | ((ENVP_ADDR + 8) & 0xffff)); /* ori a2, a2, low(ENVP_ADDR + 8) */
  459. stl_raw(p++, 0x3c070000 | (loaderparams.ram_size >> 16)); /* lui a3, high(ram_size) */
  460. stl_raw(p++, 0x34e70000 | (loaderparams.ram_size & 0xffff)); /* ori a3, a3, low(ram_size) */
  461. /* Load BAR registers as done by YAMON */
  462. stl_raw(p++, 0x3c09b400); /* lui t1, 0xb400 */
  463. #ifdef TARGET_WORDS_BIGENDIAN
  464. stl_raw(p++, 0x3c08df00); /* lui t0, 0xdf00 */
  465. #else
  466. stl_raw(p++, 0x340800df); /* ori t0, r0, 0x00df */
  467. #endif
  468. stl_raw(p++, 0xad280068); /* sw t0, 0x0068(t1) */
  469. stl_raw(p++, 0x3c09bbe0); /* lui t1, 0xbbe0 */
  470. #ifdef TARGET_WORDS_BIGENDIAN
  471. stl_raw(p++, 0x3c08c000); /* lui t0, 0xc000 */
  472. #else
  473. stl_raw(p++, 0x340800c0); /* ori t0, r0, 0x00c0 */
  474. #endif
  475. stl_raw(p++, 0xad280048); /* sw t0, 0x0048(t1) */
  476. #ifdef TARGET_WORDS_BIGENDIAN
  477. stl_raw(p++, 0x3c084000); /* lui t0, 0x4000 */
  478. #else
  479. stl_raw(p++, 0x34080040); /* ori t0, r0, 0x0040 */
  480. #endif
  481. stl_raw(p++, 0xad280050); /* sw t0, 0x0050(t1) */
  482. #ifdef TARGET_WORDS_BIGENDIAN
  483. stl_raw(p++, 0x3c088000); /* lui t0, 0x8000 */
  484. #else
  485. stl_raw(p++, 0x34080080); /* ori t0, r0, 0x0080 */
  486. #endif
  487. stl_raw(p++, 0xad280058); /* sw t0, 0x0058(t1) */
  488. #ifdef TARGET_WORDS_BIGENDIAN
  489. stl_raw(p++, 0x3c083f00); /* lui t0, 0x3f00 */
  490. #else
  491. stl_raw(p++, 0x3408003f); /* ori t0, r0, 0x003f */
  492. #endif
  493. stl_raw(p++, 0xad280060); /* sw t0, 0x0060(t1) */
  494. #ifdef TARGET_WORDS_BIGENDIAN
  495. stl_raw(p++, 0x3c08c100); /* lui t0, 0xc100 */
  496. #else
  497. stl_raw(p++, 0x340800c1); /* ori t0, r0, 0x00c1 */
  498. #endif
  499. stl_raw(p++, 0xad280080); /* sw t0, 0x0080(t1) */
  500. #ifdef TARGET_WORDS_BIGENDIAN
  501. stl_raw(p++, 0x3c085e00); /* lui t0, 0x5e00 */
  502. #else
  503. stl_raw(p++, 0x3408005e); /* ori t0, r0, 0x005e */
  504. #endif
  505. stl_raw(p++, 0xad280088); /* sw t0, 0x0088(t1) */
  506. /* Jump to kernel code */
  507. stl_raw(p++, 0x3c1f0000 | ((kernel_entry >> 16) & 0xffff)); /* lui ra, high(kernel_entry) */
  508. stl_raw(p++, 0x37ff0000 | (kernel_entry & 0xffff)); /* ori ra, ra, low(kernel_entry) */
  509. stl_raw(p++, 0x03e00008); /* jr ra */
  510. stl_raw(p++, 0x00000000); /* nop */
  511. /* YAMON subroutines */
  512. p = (uint32_t *) (base + 0x800);
  513. stl_raw(p++, 0x03e00008); /* jr ra */
  514. stl_raw(p++, 0x24020000); /* li v0,0 */
  515. /* 808 YAMON print */
  516. stl_raw(p++, 0x03e06821); /* move t5,ra */
  517. stl_raw(p++, 0x00805821); /* move t3,a0 */
  518. stl_raw(p++, 0x00a05021); /* move t2,a1 */
  519. stl_raw(p++, 0x91440000); /* lbu a0,0(t2) */
  520. stl_raw(p++, 0x254a0001); /* addiu t2,t2,1 */
  521. stl_raw(p++, 0x10800005); /* beqz a0,834 */
  522. stl_raw(p++, 0x00000000); /* nop */
  523. stl_raw(p++, 0x0ff0021c); /* jal 870 */
  524. stl_raw(p++, 0x00000000); /* nop */
  525. stl_raw(p++, 0x08000205); /* j 814 */
  526. stl_raw(p++, 0x00000000); /* nop */
  527. stl_raw(p++, 0x01a00008); /* jr t5 */
  528. stl_raw(p++, 0x01602021); /* move a0,t3 */
  529. /* 0x83c YAMON print_count */
  530. stl_raw(p++, 0x03e06821); /* move t5,ra */
  531. stl_raw(p++, 0x00805821); /* move t3,a0 */
  532. stl_raw(p++, 0x00a05021); /* move t2,a1 */
  533. stl_raw(p++, 0x00c06021); /* move t4,a2 */
  534. stl_raw(p++, 0x91440000); /* lbu a0,0(t2) */
  535. stl_raw(p++, 0x0ff0021c); /* jal 870 */
  536. stl_raw(p++, 0x00000000); /* nop */
  537. stl_raw(p++, 0x254a0001); /* addiu t2,t2,1 */
  538. stl_raw(p++, 0x258cffff); /* addiu t4,t4,-1 */
  539. stl_raw(p++, 0x1580fffa); /* bnez t4,84c */
  540. stl_raw(p++, 0x00000000); /* nop */
  541. stl_raw(p++, 0x01a00008); /* jr t5 */
  542. stl_raw(p++, 0x01602021); /* move a0,t3 */
  543. /* 0x870 */
  544. stl_raw(p++, 0x3c08b800); /* lui t0,0xb400 */
  545. stl_raw(p++, 0x350803f8); /* ori t0,t0,0x3f8 */
  546. stl_raw(p++, 0x91090005); /* lbu t1,5(t0) */
  547. stl_raw(p++, 0x00000000); /* nop */
  548. stl_raw(p++, 0x31290040); /* andi t1,t1,0x40 */
  549. stl_raw(p++, 0x1120fffc); /* beqz t1,878 <outch+0x8> */
  550. stl_raw(p++, 0x00000000); /* nop */
  551. stl_raw(p++, 0x03e00008); /* jr ra */
  552. stl_raw(p++, 0xa1040000); /* sb a0,0(t0) */
  553. }
  554. static void GCC_FMT_ATTR(3, 4) prom_set(uint32_t* prom_buf, int index,
  555. const char *string, ...)
  556. {
  557. va_list ap;
  558. int32_t table_addr;
  559. if (index >= ENVP_NB_ENTRIES)
  560. return;
  561. if (string == NULL) {
  562. prom_buf[index] = 0;
  563. return;
  564. }
  565. table_addr = sizeof(int32_t) * ENVP_NB_ENTRIES + index * ENVP_ENTRY_SIZE;
  566. prom_buf[index] = tswap32(ENVP_ADDR + table_addr);
  567. va_start(ap, string);
  568. vsnprintf((char *)prom_buf + table_addr, ENVP_ENTRY_SIZE, string, ap);
  569. va_end(ap);
  570. }
  571. /* Kernel */
  572. static int64_t load_kernel (void)
  573. {
  574. int64_t kernel_entry, kernel_high;
  575. long initrd_size;
  576. ram_addr_t initrd_offset;
  577. int big_endian;
  578. uint32_t *prom_buf;
  579. long prom_size;
  580. int prom_index = 0;
  581. #ifdef TARGET_WORDS_BIGENDIAN
  582. big_endian = 1;
  583. #else
  584. big_endian = 0;
  585. #endif
  586. if (load_elf(loaderparams.kernel_filename, cpu_mips_kseg0_to_phys, NULL,
  587. (uint64_t *)&kernel_entry, NULL, (uint64_t *)&kernel_high,
  588. big_endian, ELF_MACHINE, 1) < 0) {
  589. fprintf(stderr, "qemu: could not load kernel '%s'\n",
  590. loaderparams.kernel_filename);
  591. exit(1);
  592. }
  593. /* load initrd */
  594. initrd_size = 0;
  595. initrd_offset = 0;
  596. if (loaderparams.initrd_filename) {
  597. initrd_size = get_image_size (loaderparams.initrd_filename);
  598. if (initrd_size > 0) {
  599. initrd_offset = (kernel_high + ~TARGET_PAGE_MASK) & TARGET_PAGE_MASK;
  600. if (initrd_offset + initrd_size > ram_size) {
  601. fprintf(stderr,
  602. "qemu: memory too small for initial ram disk '%s'\n",
  603. loaderparams.initrd_filename);
  604. exit(1);
  605. }
  606. initrd_size = load_image_targphys(loaderparams.initrd_filename,
  607. initrd_offset,
  608. ram_size - initrd_offset);
  609. }
  610. if (initrd_size == (target_ulong) -1) {
  611. fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
  612. loaderparams.initrd_filename);
  613. exit(1);
  614. }
  615. }
  616. /* Setup prom parameters. */
  617. prom_size = ENVP_NB_ENTRIES * (sizeof(int32_t) + ENVP_ENTRY_SIZE);
  618. prom_buf = g_malloc(prom_size);
  619. prom_set(prom_buf, prom_index++, "%s", loaderparams.kernel_filename);
  620. if (initrd_size > 0) {
  621. prom_set(prom_buf, prom_index++, "rd_start=0x%" PRIx64 " rd_size=%li %s",
  622. cpu_mips_phys_to_kseg0(NULL, initrd_offset), initrd_size,
  623. loaderparams.kernel_cmdline);
  624. } else {
  625. prom_set(prom_buf, prom_index++, "%s", loaderparams.kernel_cmdline);
  626. }
  627. prom_set(prom_buf, prom_index++, "memsize");
  628. prom_set(prom_buf, prom_index++, "%i", loaderparams.ram_size);
  629. prom_set(prom_buf, prom_index++, "modetty0");
  630. prom_set(prom_buf, prom_index++, "38400n8r");
  631. prom_set(prom_buf, prom_index++, NULL);
  632. rom_add_blob_fixed("prom", prom_buf, prom_size,
  633. cpu_mips_kseg0_to_phys(NULL, ENVP_ADDR));
  634. return kernel_entry;
  635. }
  636. static void malta_mips_config(CPUState *env)
  637. {
  638. env->mvp->CP0_MVPConf0 |= ((smp_cpus - 1) << CP0MVPC0_PVPE) |
  639. ((smp_cpus * env->nr_threads - 1) << CP0MVPC0_PTC);
  640. }
  641. static void main_cpu_reset(void *opaque)
  642. {
  643. CPUState *env = opaque;
  644. cpu_reset(env);
  645. /* The bootloader does not need to be rewritten as it is located in a
  646. read only location. The kernel location and the arguments table
  647. location does not change. */
  648. if (loaderparams.kernel_filename) {
  649. env->CP0_Status &= ~((1 << CP0St_BEV) | (1 << CP0St_ERL));
  650. }
  651. malta_mips_config(env);
  652. }
  653. static void cpu_request_exit(void *opaque, int irq, int level)
  654. {
  655. CPUState *env = cpu_single_env;
  656. if (env && level) {
  657. cpu_exit(env);
  658. }
  659. }
  660. static
  661. void mips_malta_init (ram_addr_t ram_size,
  662. const char *boot_device,
  663. const char *kernel_filename, const char *kernel_cmdline,
  664. const char *initrd_filename, const char *cpu_model)
  665. {
  666. char *filename;
  667. pflash_t *fl;
  668. MemoryRegion *system_memory = get_system_memory();
  669. MemoryRegion *ram = g_new(MemoryRegion, 1);
  670. MemoryRegion *bios, *bios_alias = g_new(MemoryRegion, 1);
  671. target_long bios_size;
  672. int64_t kernel_entry;
  673. PCIBus *pci_bus;
  674. CPUState *env;
  675. qemu_irq *isa_irq;
  676. qemu_irq *cpu_exit_irq;
  677. int piix4_devfn;
  678. i2c_bus *smbus;
  679. int i;
  680. DriveInfo *dinfo;
  681. DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
  682. DriveInfo *fd[MAX_FD];
  683. int fl_idx = 0;
  684. int fl_sectors = 0;
  685. int be;
  686. DeviceState *dev = qdev_create(NULL, "mips-malta");
  687. MaltaState *s = DO_UPCAST(MaltaState, busdev.qdev, dev);
  688. qdev_init_nofail(dev);
  689. /* Make sure the first 3 serial ports are associated with a device. */
  690. for(i = 0; i < 3; i++) {
  691. if (!serial_hds[i]) {
  692. char label[32];
  693. snprintf(label, sizeof(label), "serial%d", i);
  694. serial_hds[i] = qemu_chr_new(label, "null", NULL);
  695. }
  696. }
  697. /* init CPUs */
  698. if (cpu_model == NULL) {
  699. #ifdef TARGET_MIPS64
  700. cpu_model = "20Kc";
  701. #else
  702. cpu_model = "24Kf";
  703. #endif
  704. }
  705. for (i = 0; i < smp_cpus; i++) {
  706. env = cpu_init(cpu_model);
  707. if (!env) {
  708. fprintf(stderr, "Unable to find CPU definition\n");
  709. exit(1);
  710. }
  711. /* Init internal devices */
  712. cpu_mips_irq_init_cpu(env);
  713. cpu_mips_clock_init(env);
  714. qemu_register_reset(main_cpu_reset, env);
  715. }
  716. env = first_cpu;
  717. /* allocate RAM */
  718. if (ram_size > (256 << 20)) {
  719. fprintf(stderr,
  720. "qemu: Too much memory for this machine: %d MB, maximum 256 MB\n",
  721. ((unsigned int)ram_size / (1 << 20)));
  722. exit(1);
  723. }
  724. memory_region_init_ram(ram, NULL, "mips_malta.ram", ram_size);
  725. memory_region_add_subregion(system_memory, 0, ram);
  726. #ifdef TARGET_WORDS_BIGENDIAN
  727. be = 1;
  728. #else
  729. be = 0;
  730. #endif
  731. /* FPGA */
  732. malta_fpga_init(system_memory, 0x1f000000LL, env->irq[2], serial_hds[2]);
  733. /* Load firmware in flash / BIOS unless we boot directly into a kernel. */
  734. if (kernel_filename) {
  735. /* Write a small bootloader to the flash location. */
  736. bios = g_new(MemoryRegion, 1);
  737. memory_region_init_ram(bios, NULL, "mips_malta.bios", BIOS_SIZE);
  738. memory_region_set_readonly(bios, true);
  739. memory_region_init_alias(bios_alias, "bios.1fc", bios, 0, BIOS_SIZE);
  740. /* Map the bios at two physical locations, as on the real board. */
  741. memory_region_add_subregion(system_memory, 0x1e000000LL, bios);
  742. memory_region_add_subregion(system_memory, 0x1fc00000LL, bios_alias);
  743. loaderparams.ram_size = ram_size;
  744. loaderparams.kernel_filename = kernel_filename;
  745. loaderparams.kernel_cmdline = kernel_cmdline;
  746. loaderparams.initrd_filename = initrd_filename;
  747. kernel_entry = load_kernel();
  748. write_bootloader(env, memory_region_get_ram_ptr(bios), kernel_entry);
  749. } else {
  750. dinfo = drive_get(IF_PFLASH, 0, fl_idx);
  751. if (dinfo) {
  752. /* Load firmware from flash. */
  753. bios_size = 0x400000;
  754. fl_sectors = bios_size >> 16;
  755. #ifdef DEBUG_BOARD_INIT
  756. printf("Register parallel flash %d size " TARGET_FMT_lx " at "
  757. "addr %08llx '%s' %x\n",
  758. fl_idx, bios_size, 0x1e000000LL,
  759. bdrv_get_device_name(dinfo->bdrv), fl_sectors);
  760. #endif
  761. fl = pflash_cfi01_register(0x1e000000LL,
  762. NULL, "mips_malta.bios", BIOS_SIZE,
  763. dinfo->bdrv, 65536, fl_sectors,
  764. 4, 0x0000, 0x0000, 0x0000, 0x0000, be);
  765. bios = pflash_cfi01_get_memory(fl);
  766. /* Map the bios at two physical locations, as on the real board. */
  767. memory_region_init_alias(bios_alias, "bios.1fc",
  768. bios, 0, BIOS_SIZE);
  769. memory_region_add_subregion(system_memory, 0x1fc00000LL,
  770. bios_alias);
  771. fl_idx++;
  772. } else {
  773. bios = g_new(MemoryRegion, 1);
  774. memory_region_init_ram(bios, NULL, "mips_malta.bios", BIOS_SIZE);
  775. memory_region_set_readonly(bios, true);
  776. memory_region_init_alias(bios_alias, "bios.1fc",
  777. bios, 0, BIOS_SIZE);
  778. /* Map the bios at two physical locations, as on the real board. */
  779. memory_region_add_subregion(system_memory, 0x1e000000LL, bios);
  780. memory_region_add_subregion(system_memory, 0x1fc00000LL,
  781. bios_alias);
  782. /* Load a BIOS image. */
  783. if (bios_name == NULL)
  784. bios_name = BIOS_FILENAME;
  785. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  786. if (filename) {
  787. bios_size = load_image_targphys(filename, 0x1fc00000LL,
  788. BIOS_SIZE);
  789. g_free(filename);
  790. } else {
  791. bios_size = -1;
  792. }
  793. if ((bios_size < 0 || bios_size > BIOS_SIZE) && !kernel_filename) {
  794. fprintf(stderr,
  795. "qemu: Could not load MIPS bios '%s', and no -kernel argument was specified\n",
  796. bios_name);
  797. exit(1);
  798. }
  799. }
  800. /* In little endian mode the 32bit words in the bios are swapped,
  801. a neat trick which allows bi-endian firmware. */
  802. #ifndef TARGET_WORDS_BIGENDIAN
  803. {
  804. uint32_t *addr = memory_region_get_ram_ptr(bios);
  805. uint32_t *end = addr + bios_size;
  806. while (addr < end) {
  807. bswap32s(addr);
  808. }
  809. }
  810. #endif
  811. }
  812. /* Board ID = 0x420 (Malta Board with CoreLV)
  813. XXX: theoretically 0x1e000010 should map to flash and 0x1fc00010 should
  814. map to the board ID. */
  815. stl_p(memory_region_get_ram_ptr(bios) + 0x10, 0x00000420);
  816. /* Init internal devices */
  817. cpu_mips_irq_init_cpu(env);
  818. cpu_mips_clock_init(env);
  819. /*
  820. * We have a circular dependency problem: pci_bus depends on isa_irq,
  821. * isa_irq is provided by i8259, i8259 depends on ISA, ISA depends
  822. * on piix4, and piix4 depends on pci_bus. To stop the cycle we have
  823. * qemu_irq_proxy() adds an extra bit of indirection, allowing us
  824. * to resolve the isa_irq -> i8259 dependency after i8259 is initialized.
  825. */
  826. isa_irq = qemu_irq_proxy(&s->i8259, 16);
  827. /* Northbridge */
  828. pci_bus = gt64120_register(isa_irq);
  829. /* Southbridge */
  830. ide_drive_get(hd, MAX_IDE_BUS);
  831. piix4_devfn = piix4_init(pci_bus, 80);
  832. /* Interrupt controller */
  833. /* The 8259 is attached to the MIPS CPU INT0 pin, ie interrupt 2 */
  834. s->i8259 = i8259_init(env->irq[2]);
  835. isa_bus_irqs(s->i8259);
  836. pci_piix4_ide_init(pci_bus, hd, piix4_devfn + 1);
  837. usb_uhci_piix4_init(pci_bus, piix4_devfn + 2);
  838. smbus = piix4_pm_init(pci_bus, piix4_devfn + 3, 0x1100, isa_get_irq(9),
  839. NULL, NULL, 0);
  840. /* TODO: Populate SPD eeprom data. */
  841. smbus_eeprom_init(smbus, 8, NULL, 0);
  842. pit = pit_init(0x40, 0);
  843. cpu_exit_irq = qemu_allocate_irqs(cpu_request_exit, NULL, 1);
  844. DMA_init(0, cpu_exit_irq);
  845. /* Super I/O */
  846. isa_create_simple("i8042");
  847. rtc_init(2000, NULL);
  848. serial_isa_init(0, serial_hds[0]);
  849. serial_isa_init(1, serial_hds[1]);
  850. if (parallel_hds[0])
  851. parallel_init(0, parallel_hds[0]);
  852. for(i = 0; i < MAX_FD; i++) {
  853. fd[i] = drive_get(IF_FLOPPY, 0, i);
  854. }
  855. fdctrl_init_isa(fd);
  856. /* Sound card */
  857. audio_init(NULL, pci_bus);
  858. /* Network card */
  859. network_init();
  860. /* Optional PCI video card */
  861. if (cirrus_vga_enabled) {
  862. pci_cirrus_vga_init(pci_bus);
  863. } else if (vmsvga_enabled) {
  864. if (!pci_vmsvga_init(pci_bus)) {
  865. fprintf(stderr, "Warning: vmware_vga not available,"
  866. " using standard VGA instead\n");
  867. pci_vga_init(pci_bus);
  868. }
  869. } else if (std_vga_enabled) {
  870. pci_vga_init(pci_bus);
  871. }
  872. }
  873. static int mips_malta_sysbus_device_init(SysBusDevice *sysbusdev)
  874. {
  875. return 0;
  876. }
  877. static SysBusDeviceInfo mips_malta_device = {
  878. .init = mips_malta_sysbus_device_init,
  879. .qdev.name = "mips-malta",
  880. .qdev.size = sizeof(MaltaState),
  881. .qdev.props = (Property[]) {
  882. DEFINE_PROP_END_OF_LIST(),
  883. }
  884. };
  885. static QEMUMachine mips_malta_machine = {
  886. .name = "malta",
  887. .desc = "MIPS Malta Core LV",
  888. .init = mips_malta_init,
  889. .max_cpus = 16,
  890. .is_default = 1,
  891. };
  892. static void mips_malta_device_init(void)
  893. {
  894. sysbus_register_withprop(&mips_malta_device);
  895. }
  896. static void mips_malta_machine_init(void)
  897. {
  898. qemu_register_machine(&mips_malta_machine);
  899. }
  900. device_init(mips_malta_device_init);
  901. machine_init(mips_malta_machine_init);