fuloong2e.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * QEMU fuloong 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. * Fuloong 2e mini pc is based on ICT/ST Loongson 2e CPU (MIPS III like, 800MHz)
  14. * https://www.linux-mips.org/wiki/Fuloong_2E
  15. *
  16. * Loongson 2e manuals:
  17. * https://github.com/loongson-community/docs/tree/master/2E
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu/datadir.h"
  21. #include "qemu/units.h"
  22. #include "qapi/error.h"
  23. #include "cpu.h"
  24. #include "hw/clock.h"
  25. #include "net/net.h"
  26. #include "hw/boards.h"
  27. #include "hw/i2c/smbus_eeprom.h"
  28. #include "hw/block/flash.h"
  29. #include "hw/mips/mips.h"
  30. #include "hw/mips/bootloader.h"
  31. #include "hw/pci/pci.h"
  32. #include "hw/loader.h"
  33. #include "hw/ide/pci.h"
  34. #include "hw/qdev-properties.h"
  35. #include "elf.h"
  36. #include "hw/isa/vt82c686.h"
  37. #include "system/qtest.h"
  38. #include "system/reset.h"
  39. #include "system/system.h"
  40. #include "qemu/error-report.h"
  41. #include "exec/tswap.h"
  42. #define ENVP_PADDR 0x2000
  43. #define ENVP_VADDR cpu_mips_phys_to_kseg0(NULL, ENVP_PADDR)
  44. #define ENVP_NB_ENTRIES 16
  45. #define ENVP_ENTRY_SIZE 256
  46. /* Fuloong 2e has a 512k flash: Winbond W39L040AP70Z */
  47. #define BIOS_SIZE (512 * KiB)
  48. /*
  49. * PMON is not part of qemu and released with BSD license, anyone
  50. * who want to build a pmon binary please first git-clone the source
  51. * from the git repository at:
  52. * https://github.com/loongson-community/pmon
  53. */
  54. #define FULOONG_BIOSNAME "pmon_2e.bin"
  55. /* PCI SLOT in Fuloong 2e */
  56. #define FULOONG2E_VIA_SLOT 5
  57. #define FULOONG2E_ATI_SLOT 6
  58. #define FULOONG2E_RTL8139_SLOT 7
  59. static struct _loaderparams {
  60. int ram_size;
  61. const char *kernel_filename;
  62. const char *kernel_cmdline;
  63. const char *initrd_filename;
  64. } loaderparams;
  65. static void G_GNUC_PRINTF(3, 4) prom_set(uint32_t *prom_buf, int index,
  66. const char *string, ...)
  67. {
  68. va_list ap;
  69. int32_t table_addr;
  70. if (index >= ENVP_NB_ENTRIES) {
  71. return;
  72. }
  73. if (string == NULL) {
  74. prom_buf[index] = 0;
  75. return;
  76. }
  77. table_addr = sizeof(int32_t) * ENVP_NB_ENTRIES + index * ENVP_ENTRY_SIZE;
  78. prom_buf[index] = tswap32(ENVP_VADDR + table_addr);
  79. va_start(ap, string);
  80. vsnprintf((char *)prom_buf + table_addr, ENVP_ENTRY_SIZE, string, ap);
  81. va_end(ap);
  82. }
  83. static uint64_t load_kernel(MIPSCPU *cpu)
  84. {
  85. uint64_t kernel_entry, kernel_high, initrd_size;
  86. int index = 0;
  87. long kernel_size;
  88. ram_addr_t initrd_offset;
  89. uint32_t *prom_buf;
  90. long prom_size;
  91. kernel_size = load_elf(loaderparams.kernel_filename, NULL,
  92. cpu_mips_kseg0_to_phys, NULL,
  93. &kernel_entry, NULL,
  94. &kernel_high, NULL,
  95. ELFDATA2LSB, EM_MIPS, 1, 0);
  96. if (kernel_size < 0) {
  97. error_report("could not load kernel '%s': %s",
  98. loaderparams.kernel_filename,
  99. load_elf_strerror(kernel_size));
  100. exit(1);
  101. }
  102. /* load initrd */
  103. initrd_size = 0;
  104. initrd_offset = 0;
  105. if (loaderparams.initrd_filename) {
  106. initrd_size = get_image_size(loaderparams.initrd_filename);
  107. if (initrd_size > 0) {
  108. initrd_offset = ROUND_UP(kernel_high, INITRD_PAGE_SIZE);
  109. if (initrd_offset + initrd_size > loaderparams.ram_size) {
  110. error_report("memory too small for initial ram disk '%s'",
  111. loaderparams.initrd_filename);
  112. exit(1);
  113. }
  114. initrd_size = load_image_targphys(loaderparams.initrd_filename,
  115. initrd_offset,
  116. loaderparams.ram_size - initrd_offset);
  117. }
  118. if (initrd_size == (target_ulong) -1) {
  119. error_report("could not load initial ram disk '%s'",
  120. loaderparams.initrd_filename);
  121. exit(1);
  122. }
  123. }
  124. /* Setup prom parameters. */
  125. prom_size = ENVP_NB_ENTRIES * (sizeof(int32_t) + ENVP_ENTRY_SIZE);
  126. prom_buf = g_malloc(prom_size);
  127. prom_set(prom_buf, index++, "%s", loaderparams.kernel_filename);
  128. if (initrd_size > 0) {
  129. prom_set(prom_buf, index++,
  130. "rd_start=0x%" PRIx64 " rd_size=%" PRId64 " %s",
  131. cpu_mips_phys_to_kseg0(NULL, initrd_offset),
  132. initrd_size, loaderparams.kernel_cmdline);
  133. } else {
  134. prom_set(prom_buf, index++, "%s", loaderparams.kernel_cmdline);
  135. }
  136. /* Setup minimum environment variables */
  137. prom_set(prom_buf, index++, "busclock=33000000");
  138. prom_set(prom_buf, index++, "cpuclock=%u", clock_get_hz(cpu->clock));
  139. prom_set(prom_buf, index++, "memsize=%"PRIi64, loaderparams.ram_size / MiB);
  140. prom_set(prom_buf, index++, NULL);
  141. rom_add_blob_fixed("prom", prom_buf, prom_size, ENVP_PADDR);
  142. g_free(prom_buf);
  143. return kernel_entry;
  144. }
  145. static void write_bootloader(CPUMIPSState *env, uint8_t *base,
  146. uint64_t kernel_addr)
  147. {
  148. uint32_t *p;
  149. /* Small bootloader */
  150. p = (uint32_t *)base;
  151. /* j 0x1fc00040 */
  152. stl_p(p++, 0x0bf00010);
  153. /* nop */
  154. stl_p(p++, 0x00000000);
  155. /* Second part of the bootloader */
  156. p = (uint32_t *)(base + 0x040);
  157. bl_gen_jump_kernel((void **)&p,
  158. true, ENVP_VADDR - 64,
  159. true, 2, true, ENVP_VADDR,
  160. true, ENVP_VADDR + 8,
  161. true, loaderparams.ram_size,
  162. kernel_addr);
  163. }
  164. static void main_cpu_reset(void *opaque)
  165. {
  166. MIPSCPU *cpu = opaque;
  167. CPUMIPSState *env = &cpu->env;
  168. cpu_reset(CPU(cpu));
  169. /* TODO: 2E reset stuff */
  170. if (loaderparams.kernel_filename) {
  171. env->CP0_Status &= ~((1 << CP0St_BEV) | (1 << CP0St_ERL));
  172. }
  173. }
  174. /* Network support */
  175. static void network_init(PCIBus *pci_bus)
  176. {
  177. /* The Fuloong board has a RTL8139 card using PCI SLOT 7 */
  178. pci_init_nic_in_slot(pci_bus, "rtl8139", NULL, "07");
  179. pci_init_nic_devices(pci_bus, "rtl8139");
  180. }
  181. static void mips_fuloong2e_init(MachineState *machine)
  182. {
  183. const char *kernel_filename = machine->kernel_filename;
  184. const char *kernel_cmdline = machine->kernel_cmdline;
  185. const char *initrd_filename = machine->initrd_filename;
  186. char *filename;
  187. MemoryRegion *address_space_mem = get_system_memory();
  188. MemoryRegion *bios = g_new(MemoryRegion, 1);
  189. long bios_size;
  190. uint8_t *spd_data;
  191. uint64_t kernel_entry;
  192. PCIDevice *pci_dev;
  193. PCIBus *pci_bus;
  194. I2CBus *smbus;
  195. Clock *cpuclk;
  196. MIPSCPU *cpu;
  197. CPUMIPSState *env;
  198. DeviceState *dev;
  199. cpuclk = clock_new(OBJECT(machine), "cpu-refclk");
  200. clock_set_hz(cpuclk, 533080000); /* ~533 MHz */
  201. /* init CPUs */
  202. cpu = mips_cpu_create_with_clock(machine->cpu_type, cpuclk, false);
  203. env = &cpu->env;
  204. qemu_register_reset(main_cpu_reset, cpu);
  205. /* TODO: support more than 256M RAM as highmem */
  206. if (machine->ram_size != 256 * MiB) {
  207. error_report("Invalid RAM size, should be 256MB");
  208. exit(EXIT_FAILURE);
  209. }
  210. memory_region_add_subregion(address_space_mem, 0, machine->ram);
  211. /* Boot ROM */
  212. memory_region_init_rom(bios, NULL, "fuloong2e.bios", BIOS_SIZE,
  213. &error_fatal);
  214. memory_region_add_subregion(address_space_mem, 0x1fc00000LL, bios);
  215. /*
  216. * We do not support flash operation, just loading pmon.bin as raw BIOS.
  217. * Please use -L to set the BIOS path and -bios to set bios name.
  218. */
  219. if (kernel_filename) {
  220. loaderparams.ram_size = machine->ram_size;
  221. loaderparams.kernel_filename = kernel_filename;
  222. loaderparams.kernel_cmdline = kernel_cmdline;
  223. loaderparams.initrd_filename = initrd_filename;
  224. kernel_entry = load_kernel(cpu);
  225. write_bootloader(env, memory_region_get_ram_ptr(bios), kernel_entry);
  226. } else {
  227. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS,
  228. machine->firmware ?: FULOONG_BIOSNAME);
  229. if (filename) {
  230. bios_size = load_image_targphys(filename, 0x1fc00000LL,
  231. BIOS_SIZE);
  232. g_free(filename);
  233. } else {
  234. bios_size = -1;
  235. }
  236. if ((bios_size < 0 || bios_size > BIOS_SIZE) &&
  237. machine->firmware && !qtest_enabled()) {
  238. error_report("Could not load MIPS bios '%s'", machine->firmware);
  239. exit(1);
  240. }
  241. }
  242. /* Init internal devices */
  243. cpu_mips_irq_init_cpu(cpu);
  244. cpu_mips_clock_init(cpu);
  245. /* North bridge, Bonito --> IP2 */
  246. pci_bus = bonito_init((qemu_irq *)&(env->irq[2]));
  247. /* South bridge -> IP5 */
  248. pci_dev = pci_new_multifunction(PCI_DEVFN(FULOONG2E_VIA_SLOT, 0),
  249. TYPE_VT82C686B_ISA);
  250. /* Set properties on individual devices before realizing the south bridge */
  251. if (machine->audiodev) {
  252. dev = DEVICE(object_resolve_path_component(OBJECT(pci_dev), "ac97"));
  253. qdev_prop_set_string(dev, "audiodev", machine->audiodev);
  254. }
  255. pci_realize_and_unref(pci_dev, pci_bus, &error_abort);
  256. object_property_add_alias(OBJECT(machine), "rtc-time",
  257. object_resolve_path_component(OBJECT(pci_dev),
  258. "rtc"),
  259. "date");
  260. qdev_connect_gpio_out_named(DEVICE(pci_dev), "intr", 0, env->irq[5]);
  261. dev = DEVICE(object_resolve_path_component(OBJECT(pci_dev), "ide"));
  262. pci_ide_create_devs(PCI_DEVICE(dev));
  263. dev = DEVICE(object_resolve_path_component(OBJECT(pci_dev), "pm"));
  264. smbus = I2C_BUS(qdev_get_child_bus(dev, "i2c"));
  265. /* GPU */
  266. if (vga_interface_type != VGA_NONE) {
  267. vga_interface_created = true;
  268. pci_dev = pci_new(-1, "ati-vga");
  269. dev = DEVICE(pci_dev);
  270. qdev_prop_set_uint32(dev, "vgamem_mb", 16);
  271. qdev_prop_set_uint16(dev, "x-device-id", 0x5159);
  272. pci_realize_and_unref(pci_dev, pci_bus, &error_fatal);
  273. }
  274. /* Populate SPD eeprom data */
  275. spd_data = spd_data_generate(DDR, machine->ram_size);
  276. smbus_eeprom_init_one(smbus, 0x50, spd_data);
  277. /* Network card: RTL8139D */
  278. network_init(pci_bus);
  279. }
  280. static void mips_fuloong2e_machine_init(MachineClass *mc)
  281. {
  282. mc->desc = "Fuloong 2e mini pc";
  283. mc->init = mips_fuloong2e_init;
  284. mc->block_default_type = IF_IDE;
  285. mc->default_cpu_type = MIPS_CPU_TYPE_NAME("Loongson-2E");
  286. mc->default_ram_size = 256 * MiB;
  287. mc->default_ram_id = "fuloong2e.ram";
  288. machine_add_audiodev_property(mc);
  289. }
  290. DEFINE_MACHINE("fuloong2e", mips_fuloong2e_machine_init)