milkymist.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * QEMU model for the Milkymist board.
  3. *
  4. * Copyright (c) 2010 Michael Walle <michael@walle.cc>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu/units.h"
  21. #include "qemu/error-report.h"
  22. #include "qemu-common.h"
  23. #include "qemu/datadir.h"
  24. #include "cpu.h"
  25. #include "hw/sysbus.h"
  26. #include "hw/irq.h"
  27. #include "hw/block/flash.h"
  28. #include "sysemu/sysemu.h"
  29. #include "sysemu/qtest.h"
  30. #include "sysemu/reset.h"
  31. #include "hw/boards.h"
  32. #include "hw/loader.h"
  33. #include "hw/qdev-properties.h"
  34. #include "elf.h"
  35. #include "milkymist-hw.h"
  36. #include "hw/display/milkymist_tmu2.h"
  37. #include "hw/sd/sd.h"
  38. #include "lm32.h"
  39. #include "exec/address-spaces.h"
  40. #include "qemu/cutils.h"
  41. #define BIOS_FILENAME "mmone-bios.bin"
  42. #define BIOS_OFFSET 0x00860000
  43. #define BIOS_SIZE (512 * KiB)
  44. #define KERNEL_LOAD_ADDR 0x40000000
  45. typedef struct {
  46. LM32CPU *cpu;
  47. hwaddr bootstrap_pc;
  48. hwaddr flash_base;
  49. hwaddr initrd_base;
  50. size_t initrd_size;
  51. hwaddr cmdline_base;
  52. } ResetInfo;
  53. static void cpu_irq_handler(void *opaque, int irq, int level)
  54. {
  55. LM32CPU *cpu = opaque;
  56. CPUState *cs = CPU(cpu);
  57. if (level) {
  58. cpu_interrupt(cs, CPU_INTERRUPT_HARD);
  59. } else {
  60. cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
  61. }
  62. }
  63. static void main_cpu_reset(void *opaque)
  64. {
  65. ResetInfo *reset_info = opaque;
  66. CPULM32State *env = &reset_info->cpu->env;
  67. cpu_reset(CPU(reset_info->cpu));
  68. /* init defaults */
  69. env->pc = reset_info->bootstrap_pc;
  70. env->regs[R_R1] = reset_info->cmdline_base;
  71. env->regs[R_R2] = reset_info->initrd_base;
  72. env->regs[R_R3] = reset_info->initrd_base + reset_info->initrd_size;
  73. env->eba = reset_info->flash_base;
  74. env->deba = reset_info->flash_base;
  75. }
  76. static DeviceState *milkymist_memcard_create(hwaddr base)
  77. {
  78. DeviceState *dev;
  79. DriveInfo *dinfo;
  80. dev = qdev_new("milkymist-memcard");
  81. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  82. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
  83. dinfo = drive_get_next(IF_SD);
  84. if (dinfo) {
  85. DeviceState *card;
  86. card = qdev_new(TYPE_SD_CARD);
  87. qdev_prop_set_drive_err(card, "drive", blk_by_legacy_dinfo(dinfo),
  88. &error_fatal);
  89. qdev_realize_and_unref(card, qdev_get_child_bus(dev, "sd-bus"),
  90. &error_fatal);
  91. }
  92. return dev;
  93. }
  94. static void
  95. milkymist_init(MachineState *machine)
  96. {
  97. MachineClass *mc = MACHINE_GET_CLASS(machine);
  98. const char *bios_name = machine->firmware ?: BIOS_FILENAME;
  99. const char *kernel_filename = machine->kernel_filename;
  100. const char *kernel_cmdline = machine->kernel_cmdline;
  101. const char *initrd_filename = machine->initrd_filename;
  102. LM32CPU *cpu;
  103. CPULM32State *env;
  104. int kernel_size;
  105. DriveInfo *dinfo;
  106. MemoryRegion *address_space_mem = get_system_memory();
  107. qemu_irq irq[32];
  108. int i;
  109. char *bios_filename;
  110. ResetInfo *reset_info;
  111. if (machine->ram_size != mc->default_ram_size) {
  112. char *sz = size_to_str(mc->default_ram_size);
  113. error_report("Invalid RAM size, should be %s", sz);
  114. g_free(sz);
  115. exit(EXIT_FAILURE);
  116. }
  117. /* memory map */
  118. hwaddr flash_base = 0x00000000;
  119. size_t flash_sector_size = 128 * KiB;
  120. size_t flash_size = 32 * MiB;
  121. hwaddr sdram_base = 0x40000000;
  122. hwaddr initrd_base = sdram_base + 0x1002000;
  123. hwaddr cmdline_base = sdram_base + 0x1000000;
  124. size_t initrd_max = machine->ram_size - 0x1002000;
  125. reset_info = g_malloc0(sizeof(ResetInfo));
  126. cpu = LM32_CPU(cpu_create(machine->cpu_type));
  127. env = &cpu->env;
  128. reset_info->cpu = cpu;
  129. cpu_lm32_set_phys_msb_ignore(env, 1);
  130. memory_region_add_subregion(address_space_mem, sdram_base, machine->ram);
  131. dinfo = drive_get(IF_PFLASH, 0, 0);
  132. /* Numonyx JS28F256J3F105 */
  133. pflash_cfi01_register(flash_base, "milkymist.flash", flash_size,
  134. dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
  135. flash_sector_size, 2, 0x00, 0x89, 0x00, 0x1d, 1);
  136. /* create irq lines */
  137. env->pic_state = lm32_pic_init(qemu_allocate_irq(cpu_irq_handler, cpu, 0));
  138. for (i = 0; i < 32; i++) {
  139. irq[i] = qdev_get_gpio_in(env->pic_state, i);
  140. }
  141. /* load bios rom */
  142. bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  143. if (bios_filename) {
  144. if (load_image_targphys(bios_filename, BIOS_OFFSET, BIOS_SIZE) < 0) {
  145. error_report("could not load bios '%s'", bios_filename);
  146. exit(1);
  147. }
  148. }
  149. reset_info->bootstrap_pc = BIOS_OFFSET;
  150. /* if no kernel is given no valid bios rom is a fatal error */
  151. if (!kernel_filename && !dinfo && !bios_filename && !qtest_enabled()) {
  152. error_report("could not load Milkymist One bios '%s'", bios_name);
  153. exit(1);
  154. }
  155. g_free(bios_filename);
  156. milkymist_uart_create(0x60000000, irq[0], serial_hd(0));
  157. milkymist_sysctl_create(0x60001000, irq[1], irq[2], irq[3],
  158. 80000000, 0x10014d31, 0x0000041f, 0x00000001);
  159. milkymist_hpdmc_create(0x60002000);
  160. milkymist_vgafb_create(0x60003000, 0x40000000, 0x0fffffff);
  161. milkymist_memcard_create(0x60004000);
  162. milkymist_ac97_create(0x60005000, irq[4], irq[5], irq[6], irq[7]);
  163. milkymist_pfpu_create(0x60006000, irq[8]);
  164. if (machine->enable_graphics) {
  165. milkymist_tmu2_create(0x60007000, irq[9]);
  166. }
  167. milkymist_minimac2_create(0x60008000, 0x30000000, irq[10], irq[11]);
  168. milkymist_softusb_create(0x6000f000, irq[15],
  169. 0x20000000, 0x1000, 0x20020000, 0x2000);
  170. /* make sure juart isn't the first chardev */
  171. env->juart_state = lm32_juart_init(serial_hd(1));
  172. if (kernel_filename) {
  173. uint64_t entry;
  174. /* Boots a kernel elf binary. */
  175. kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
  176. &entry, NULL, NULL, NULL,
  177. 1, EM_LATTICEMICO32, 0, 0);
  178. reset_info->bootstrap_pc = entry;
  179. if (kernel_size < 0) {
  180. kernel_size = load_image_targphys(kernel_filename, sdram_base,
  181. machine->ram_size);
  182. reset_info->bootstrap_pc = sdram_base;
  183. }
  184. if (kernel_size < 0) {
  185. error_report("could not load kernel '%s'", kernel_filename);
  186. exit(1);
  187. }
  188. }
  189. if (kernel_cmdline && strlen(kernel_cmdline)) {
  190. pstrcpy_targphys("cmdline", cmdline_base, TARGET_PAGE_SIZE,
  191. kernel_cmdline);
  192. reset_info->cmdline_base = (uint32_t)cmdline_base;
  193. }
  194. if (initrd_filename) {
  195. size_t initrd_size;
  196. initrd_size = load_image_targphys(initrd_filename, initrd_base,
  197. initrd_max);
  198. reset_info->initrd_base = (uint32_t)initrd_base;
  199. reset_info->initrd_size = (uint32_t)initrd_size;
  200. }
  201. qemu_register_reset(main_cpu_reset, reset_info);
  202. }
  203. static void milkymist_machine_init(MachineClass *mc)
  204. {
  205. mc->desc = "Milkymist One";
  206. mc->init = milkymist_init;
  207. mc->default_cpu_type = LM32_CPU_TYPE_NAME("lm32-full");
  208. mc->default_ram_size = 128 * MiB;
  209. mc->default_ram_id = "milkymist.sdram";
  210. }
  211. DEFINE_MACHINE("milkymist", milkymist_machine_init)