mips_malta.c 31 KB

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