mips_fulong2e.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * QEMU fulong 2e mini pc support
  3. *
  4. * Copyright (c) 2008 yajin (yajin@vm-kernel.org)
  5. * Copyright (c) 2009 chenming (chenming@rdc.faw.com.cn)
  6. * Copyright (c) 2010 Huacai Chen (zltjiangshi@gmail.com)
  7. * This code is licensed under the GNU GPL v2.
  8. *
  9. * Contributions after 2012-01-13 are licensed under the terms of the
  10. * GNU GPL, version 2 or (at your option) any later version.
  11. */
  12. /*
  13. * Fulong 2e mini pc is based on ICT/ST Loongson 2e CPU (MIPS III like, 800MHz)
  14. * http://www.linux-mips.org/wiki/Fulong
  15. *
  16. * Loongson 2e user manual:
  17. * http://www.loongsondeveloper.com/doc/Loongson2EUserGuide.pdf
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu-common.h"
  21. #include "qemu/units.h"
  22. #include "qapi/error.h"
  23. #include "cpu.h"
  24. #include "hw/i386/pc.h"
  25. #include "hw/dma/i8257.h"
  26. #include "hw/isa/superio.h"
  27. #include "net/net.h"
  28. #include "hw/boards.h"
  29. #include "hw/i2c/smbus_eeprom.h"
  30. #include "hw/block/flash.h"
  31. #include "hw/mips/mips.h"
  32. #include "hw/mips/cpudevs.h"
  33. #include "hw/pci/pci.h"
  34. #include "audio/audio.h"
  35. #include "qemu/log.h"
  36. #include "hw/loader.h"
  37. #include "hw/ide.h"
  38. #include "elf.h"
  39. #include "hw/isa/vt82c686.h"
  40. #include "hw/rtc/mc146818rtc.h"
  41. #include "hw/timer/i8254.h"
  42. #include "exec/address-spaces.h"
  43. #include "sysemu/qtest.h"
  44. #include "sysemu/reset.h"
  45. #include "qemu/error-report.h"
  46. #define DEBUG_FULONG2E_INIT
  47. #define ENVP_ADDR 0x80002000l
  48. #define ENVP_NB_ENTRIES 16
  49. #define ENVP_ENTRY_SIZE 256
  50. /* fulong 2e has a 512k flash: Winbond W39L040AP70Z */
  51. #define BIOS_SIZE (512 * KiB)
  52. #define MAX_IDE_BUS 2
  53. /*
  54. * PMON is not part of qemu and released with BSD license, anyone
  55. * who want to build a pmon binary please first git-clone the source
  56. * from the git repository at:
  57. * http://www.loongson.cn/support/git/pmon
  58. * Then follow the "Compile Guide" available at:
  59. * http://dev.lemote.com/code/pmon
  60. *
  61. * Notes:
  62. * 1, don't use the source at http://dev.lemote.com/http_git/pmon.git
  63. * 2, use "Bonito2edev" to replace "dir_corresponding_to_your_target_hardware"
  64. * in the "Compile Guide".
  65. */
  66. #define FULONG_BIOSNAME "pmon_fulong2e.bin"
  67. /* PCI SLOT in fulong 2e */
  68. #define FULONG2E_VIA_SLOT 5
  69. #define FULONG2E_ATI_SLOT 6
  70. #define FULONG2E_RTL8139_SLOT 7
  71. static struct _loaderparams {
  72. int ram_size;
  73. const char *kernel_filename;
  74. const char *kernel_cmdline;
  75. const char *initrd_filename;
  76. } loaderparams;
  77. static void GCC_FMT_ATTR(3, 4) prom_set(uint32_t *prom_buf, int index,
  78. const char *string, ...)
  79. {
  80. va_list ap;
  81. int32_t table_addr;
  82. if (index >= ENVP_NB_ENTRIES) {
  83. return;
  84. }
  85. if (string == NULL) {
  86. prom_buf[index] = 0;
  87. return;
  88. }
  89. table_addr = sizeof(int32_t) * ENVP_NB_ENTRIES + index * ENVP_ENTRY_SIZE;
  90. prom_buf[index] = tswap32(ENVP_ADDR + table_addr);
  91. va_start(ap, string);
  92. vsnprintf((char *)prom_buf + table_addr, ENVP_ENTRY_SIZE, string, ap);
  93. va_end(ap);
  94. }
  95. static int64_t load_kernel(CPUMIPSState *env)
  96. {
  97. int64_t kernel_entry, kernel_low, kernel_high, initrd_size;
  98. int index = 0;
  99. long kernel_size;
  100. ram_addr_t initrd_offset;
  101. uint32_t *prom_buf;
  102. long prom_size;
  103. kernel_size = load_elf(loaderparams.kernel_filename, NULL,
  104. cpu_mips_kseg0_to_phys, NULL,
  105. (uint64_t *)&kernel_entry,
  106. (uint64_t *)&kernel_low, (uint64_t *)&kernel_high,
  107. 0, EM_MIPS, 1, 0);
  108. if (kernel_size < 0) {
  109. error_report("could not load kernel '%s': %s",
  110. loaderparams.kernel_filename,
  111. load_elf_strerror(kernel_size));
  112. exit(1);
  113. }
  114. /* load initrd */
  115. initrd_size = 0;
  116. initrd_offset = 0;
  117. if (loaderparams.initrd_filename) {
  118. initrd_size = get_image_size(loaderparams.initrd_filename);
  119. if (initrd_size > 0) {
  120. initrd_offset = (kernel_high + ~INITRD_PAGE_MASK) &
  121. INITRD_PAGE_MASK;
  122. if (initrd_offset + initrd_size > ram_size) {
  123. error_report("memory too small for initial ram disk '%s'",
  124. loaderparams.initrd_filename);
  125. exit(1);
  126. }
  127. initrd_size = load_image_targphys(loaderparams.initrd_filename,
  128. initrd_offset,
  129. ram_size - initrd_offset);
  130. }
  131. if (initrd_size == (target_ulong) -1) {
  132. error_report("could not load initial ram disk '%s'",
  133. loaderparams.initrd_filename);
  134. exit(1);
  135. }
  136. }
  137. /* Setup prom parameters. */
  138. prom_size = ENVP_NB_ENTRIES * (sizeof(int32_t) + ENVP_ENTRY_SIZE);
  139. prom_buf = g_malloc(prom_size);
  140. prom_set(prom_buf, index++, "%s", loaderparams.kernel_filename);
  141. if (initrd_size > 0) {
  142. prom_set(prom_buf, index++,
  143. "rd_start=0x%" PRIx64 " rd_size=%" PRId64 " %s",
  144. cpu_mips_phys_to_kseg0(NULL, initrd_offset),
  145. initrd_size, loaderparams.kernel_cmdline);
  146. } else {
  147. prom_set(prom_buf, index++, "%s", loaderparams.kernel_cmdline);
  148. }
  149. /* Setup minimum environment variables */
  150. prom_set(prom_buf, index++, "busclock=33000000");
  151. prom_set(prom_buf, index++, "cpuclock=100000000");
  152. prom_set(prom_buf, index++, "memsize=%"PRIi64, loaderparams.ram_size / MiB);
  153. prom_set(prom_buf, index++, "modetty0=38400n8r");
  154. prom_set(prom_buf, index++, NULL);
  155. rom_add_blob_fixed("prom", prom_buf, prom_size,
  156. cpu_mips_kseg0_to_phys(NULL, ENVP_ADDR));
  157. g_free(prom_buf);
  158. return kernel_entry;
  159. }
  160. static void write_bootloader(CPUMIPSState *env, uint8_t *base,
  161. int64_t kernel_addr)
  162. {
  163. uint32_t *p;
  164. /* Small bootloader */
  165. p = (uint32_t *)base;
  166. /* j 0x1fc00040 */
  167. stl_p(p++, 0x0bf00010);
  168. /* nop */
  169. stl_p(p++, 0x00000000);
  170. /* Second part of the bootloader */
  171. p = (uint32_t *)(base + 0x040);
  172. /* lui a0, 0 */
  173. stl_p(p++, 0x3c040000);
  174. /* ori a0, a0, 2 */
  175. stl_p(p++, 0x34840002);
  176. /* lui a1, high(ENVP_ADDR) */
  177. stl_p(p++, 0x3c050000 | ((ENVP_ADDR >> 16) & 0xffff));
  178. /* ori a1, a0, low(ENVP_ADDR) */
  179. stl_p(p++, 0x34a50000 | (ENVP_ADDR & 0xffff));
  180. /* lui a2, high(ENVP_ADDR + 8) */
  181. stl_p(p++, 0x3c060000 | (((ENVP_ADDR + 8) >> 16) & 0xffff));
  182. /* ori a2, a2, low(ENVP_ADDR + 8) */
  183. stl_p(p++, 0x34c60000 | ((ENVP_ADDR + 8) & 0xffff));
  184. /* lui a3, high(env->ram_size) */
  185. stl_p(p++, 0x3c070000 | (loaderparams.ram_size >> 16));
  186. /* ori a3, a3, low(env->ram_size) */
  187. stl_p(p++, 0x34e70000 | (loaderparams.ram_size & 0xffff));
  188. /* lui ra, high(kernel_addr) */
  189. stl_p(p++, 0x3c1f0000 | ((kernel_addr >> 16) & 0xffff));
  190. /* ori ra, ra, low(kernel_addr) */
  191. stl_p(p++, 0x37ff0000 | (kernel_addr & 0xffff));
  192. /* jr ra */
  193. stl_p(p++, 0x03e00008);
  194. /* nop */
  195. stl_p(p++, 0x00000000);
  196. }
  197. static void main_cpu_reset(void *opaque)
  198. {
  199. MIPSCPU *cpu = opaque;
  200. CPUMIPSState *env = &cpu->env;
  201. cpu_reset(CPU(cpu));
  202. /* TODO: 2E reset stuff */
  203. if (loaderparams.kernel_filename) {
  204. env->CP0_Status &= ~((1 << CP0St_BEV) | (1 << CP0St_ERL));
  205. }
  206. }
  207. static void vt82c686b_southbridge_init(PCIBus *pci_bus, int slot, qemu_irq intc,
  208. I2CBus **i2c_bus, ISABus **p_isa_bus)
  209. {
  210. qemu_irq *i8259;
  211. ISABus *isa_bus;
  212. DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
  213. isa_bus = vt82c686b_isa_init(pci_bus, PCI_DEVFN(slot, 0));
  214. if (!isa_bus) {
  215. fprintf(stderr, "vt82c686b_init error\n");
  216. exit(1);
  217. }
  218. *p_isa_bus = isa_bus;
  219. /* Interrupt controller */
  220. /* The 8259 -> IP5 */
  221. i8259 = i8259_init(isa_bus, intc);
  222. isa_bus_irqs(isa_bus, i8259);
  223. /* init other devices */
  224. i8254_pit_init(isa_bus, 0x40, 0, NULL);
  225. i8257_dma_init(isa_bus, 0);
  226. /* Super I/O */
  227. isa_create_simple(isa_bus, TYPE_VT82C686B_SUPERIO);
  228. ide_drive_get(hd, ARRAY_SIZE(hd));
  229. via_ide_init(pci_bus, hd, PCI_DEVFN(slot, 1));
  230. pci_create_simple(pci_bus, PCI_DEVFN(slot, 2), "vt82c686b-usb-uhci");
  231. pci_create_simple(pci_bus, PCI_DEVFN(slot, 3), "vt82c686b-usb-uhci");
  232. *i2c_bus = vt82c686b_pm_init(pci_bus, PCI_DEVFN(slot, 4), 0xeee1, NULL);
  233. /* Audio support */
  234. vt82c686b_ac97_init(pci_bus, PCI_DEVFN(slot, 5));
  235. vt82c686b_mc97_init(pci_bus, PCI_DEVFN(slot, 6));
  236. }
  237. /* Network support */
  238. static void network_init(PCIBus *pci_bus)
  239. {
  240. int i;
  241. for (i = 0; i < nb_nics; i++) {
  242. NICInfo *nd = &nd_table[i];
  243. const char *default_devaddr = NULL;
  244. if (i == 0 && (!nd->model || strcmp(nd->model, "rtl8139") == 0)) {
  245. /* The fulong board has a RTL8139 card using PCI SLOT 7 */
  246. default_devaddr = "07";
  247. }
  248. pci_nic_init_nofail(nd, pci_bus, "rtl8139", default_devaddr);
  249. }
  250. }
  251. static void mips_fulong2e_init(MachineState *machine)
  252. {
  253. const char *kernel_filename = machine->kernel_filename;
  254. const char *kernel_cmdline = machine->kernel_cmdline;
  255. const char *initrd_filename = machine->initrd_filename;
  256. char *filename;
  257. MemoryRegion *address_space_mem = get_system_memory();
  258. MemoryRegion *ram = g_new(MemoryRegion, 1);
  259. MemoryRegion *bios = g_new(MemoryRegion, 1);
  260. ram_addr_t ram_size = machine->ram_size;
  261. long bios_size;
  262. uint8_t *spd_data;
  263. Error *err = NULL;
  264. int64_t kernel_entry;
  265. PCIBus *pci_bus;
  266. ISABus *isa_bus;
  267. I2CBus *smbus;
  268. MIPSCPU *cpu;
  269. CPUMIPSState *env;
  270. DeviceState *dev;
  271. /* init CPUs */
  272. cpu = MIPS_CPU(cpu_create(machine->cpu_type));
  273. env = &cpu->env;
  274. qemu_register_reset(main_cpu_reset, cpu);
  275. /* TODO: support more than 256M RAM as highmem */
  276. ram_size = 256 * MiB;
  277. /* allocate RAM */
  278. memory_region_allocate_system_memory(ram, NULL, "fulong2e.ram", ram_size);
  279. memory_region_init_ram(bios, NULL, "fulong2e.bios", BIOS_SIZE,
  280. &error_fatal);
  281. memory_region_set_readonly(bios, true);
  282. memory_region_add_subregion(address_space_mem, 0, ram);
  283. memory_region_add_subregion(address_space_mem, 0x1fc00000LL, bios);
  284. /*
  285. * We do not support flash operation, just loading pmon.bin as raw BIOS.
  286. * Please use -L to set the BIOS path and -bios to set bios name.
  287. */
  288. if (kernel_filename) {
  289. loaderparams.ram_size = ram_size;
  290. loaderparams.kernel_filename = kernel_filename;
  291. loaderparams.kernel_cmdline = kernel_cmdline;
  292. loaderparams.initrd_filename = initrd_filename;
  293. kernel_entry = load_kernel(env);
  294. write_bootloader(env, memory_region_get_ram_ptr(bios), kernel_entry);
  295. } else {
  296. if (bios_name == NULL) {
  297. bios_name = FULONG_BIOSNAME;
  298. }
  299. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  300. if (filename) {
  301. bios_size = load_image_targphys(filename, 0x1fc00000LL,
  302. BIOS_SIZE);
  303. g_free(filename);
  304. } else {
  305. bios_size = -1;
  306. }
  307. if ((bios_size < 0 || bios_size > BIOS_SIZE) &&
  308. !kernel_filename && !qtest_enabled()) {
  309. error_report("Could not load MIPS bios '%s'", bios_name);
  310. exit(1);
  311. }
  312. }
  313. /* Init internal devices */
  314. cpu_mips_irq_init_cpu(cpu);
  315. cpu_mips_clock_init(cpu);
  316. /* North bridge, Bonito --> IP2 */
  317. pci_bus = bonito_init((qemu_irq *)&(env->irq[2]));
  318. /* South bridge -> IP5 */
  319. vt82c686b_southbridge_init(pci_bus, FULONG2E_VIA_SLOT, env->irq[5],
  320. &smbus, &isa_bus);
  321. /* GPU */
  322. if (vga_interface_type != VGA_NONE) {
  323. dev = DEVICE(pci_create(pci_bus, -1, "ati-vga"));
  324. qdev_prop_set_uint32(dev, "vgamem_mb", 16);
  325. qdev_prop_set_uint16(dev, "x-device-id", 0x5159);
  326. qdev_init_nofail(dev);
  327. }
  328. /* Populate SPD eeprom data */
  329. spd_data = spd_data_generate(DDR, ram_size, &err);
  330. if (err) {
  331. warn_report_err(err);
  332. }
  333. if (spd_data) {
  334. smbus_eeprom_init_one(smbus, 0x50, spd_data);
  335. }
  336. mc146818_rtc_init(isa_bus, 2000, NULL);
  337. /* Network card: RTL8139D */
  338. network_init(pci_bus);
  339. }
  340. static void mips_fulong2e_machine_init(MachineClass *mc)
  341. {
  342. mc->desc = "Fulong 2e mini pc";
  343. mc->init = mips_fulong2e_init;
  344. mc->block_default_type = IF_IDE;
  345. mc->default_cpu_type = MIPS_CPU_TYPE_NAME("Loongson-2E");
  346. mc->default_ram_size = 256 * MiB;
  347. }
  348. DEFINE_MACHINE("fulong2e", mips_fulong2e_machine_init)