milkymist.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 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 "sysbus.h"
  20. #include "hw.h"
  21. #include "flash.h"
  22. #include "sysemu/sysemu.h"
  23. #include "devices.h"
  24. #include "boards.h"
  25. #include "loader.h"
  26. #include "elf.h"
  27. #include "sysemu/blockdev.h"
  28. #include "milkymist-hw.h"
  29. #include "lm32.h"
  30. #include "exec/address-spaces.h"
  31. #define BIOS_FILENAME "mmone-bios.bin"
  32. #define BIOS_OFFSET 0x00860000
  33. #define BIOS_SIZE (512*1024)
  34. #define KERNEL_LOAD_ADDR 0x40000000
  35. typedef struct {
  36. LM32CPU *cpu;
  37. hwaddr bootstrap_pc;
  38. hwaddr flash_base;
  39. hwaddr initrd_base;
  40. size_t initrd_size;
  41. hwaddr cmdline_base;
  42. } ResetInfo;
  43. static void cpu_irq_handler(void *opaque, int irq, int level)
  44. {
  45. CPULM32State *env = opaque;
  46. if (level) {
  47. cpu_interrupt(env, CPU_INTERRUPT_HARD);
  48. } else {
  49. cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
  50. }
  51. }
  52. static void main_cpu_reset(void *opaque)
  53. {
  54. ResetInfo *reset_info = opaque;
  55. CPULM32State *env = &reset_info->cpu->env;
  56. cpu_reset(CPU(reset_info->cpu));
  57. /* init defaults */
  58. env->pc = reset_info->bootstrap_pc;
  59. env->regs[R_R1] = reset_info->cmdline_base;
  60. env->regs[R_R2] = reset_info->initrd_base;
  61. env->regs[R_R3] = reset_info->initrd_base + reset_info->initrd_size;
  62. env->eba = reset_info->flash_base;
  63. env->deba = reset_info->flash_base;
  64. }
  65. static void
  66. milkymist_init(QEMUMachineInitArgs *args)
  67. {
  68. const char *cpu_model = args->cpu_model;
  69. const char *kernel_filename = args->kernel_filename;
  70. const char *kernel_cmdline = args->kernel_cmdline;
  71. const char *initrd_filename = args->initrd_filename;
  72. LM32CPU *cpu;
  73. CPULM32State *env;
  74. int kernel_size;
  75. DriveInfo *dinfo;
  76. MemoryRegion *address_space_mem = get_system_memory();
  77. MemoryRegion *phys_sdram = g_new(MemoryRegion, 1);
  78. qemu_irq irq[32], *cpu_irq;
  79. int i;
  80. char *bios_filename;
  81. ResetInfo *reset_info;
  82. /* memory map */
  83. hwaddr flash_base = 0x00000000;
  84. size_t flash_sector_size = 128 * 1024;
  85. size_t flash_size = 32 * 1024 * 1024;
  86. hwaddr sdram_base = 0x40000000;
  87. size_t sdram_size = 128 * 1024 * 1024;
  88. hwaddr initrd_base = sdram_base + 0x1002000;
  89. hwaddr cmdline_base = sdram_base + 0x1000000;
  90. size_t initrd_max = sdram_size - 0x1002000;
  91. reset_info = g_malloc0(sizeof(ResetInfo));
  92. if (cpu_model == NULL) {
  93. cpu_model = "lm32-full";
  94. }
  95. cpu = cpu_lm32_init(cpu_model);
  96. env = &cpu->env;
  97. reset_info->cpu = cpu;
  98. cpu_lm32_set_phys_msb_ignore(env, 1);
  99. memory_region_init_ram(phys_sdram, "milkymist.sdram", sdram_size);
  100. vmstate_register_ram_global(phys_sdram);
  101. memory_region_add_subregion(address_space_mem, sdram_base, phys_sdram);
  102. dinfo = drive_get(IF_PFLASH, 0, 0);
  103. /* Numonyx JS28F256J3F105 */
  104. pflash_cfi01_register(flash_base, NULL, "milkymist.flash", flash_size,
  105. dinfo ? dinfo->bdrv : NULL, flash_sector_size,
  106. flash_size / flash_sector_size, 2,
  107. 0x00, 0x89, 0x00, 0x1d, 1);
  108. /* create irq lines */
  109. cpu_irq = qemu_allocate_irqs(cpu_irq_handler, env, 1);
  110. env->pic_state = lm32_pic_init(*cpu_irq);
  111. for (i = 0; i < 32; i++) {
  112. irq[i] = qdev_get_gpio_in(env->pic_state, i);
  113. }
  114. /* load bios rom */
  115. if (bios_name == NULL) {
  116. bios_name = BIOS_FILENAME;
  117. }
  118. bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  119. if (bios_filename) {
  120. load_image_targphys(bios_filename, BIOS_OFFSET, BIOS_SIZE);
  121. }
  122. reset_info->bootstrap_pc = BIOS_OFFSET;
  123. /* if no kernel is given no valid bios rom is a fatal error */
  124. if (!kernel_filename && !dinfo && !bios_filename) {
  125. fprintf(stderr, "qemu: could not load Milkymist One bios '%s'\n",
  126. bios_name);
  127. exit(1);
  128. }
  129. milkymist_uart_create(0x60000000, irq[0]);
  130. milkymist_sysctl_create(0x60001000, irq[1], irq[2], irq[3],
  131. 80000000, 0x10014d31, 0x0000041f, 0x00000001);
  132. milkymist_hpdmc_create(0x60002000);
  133. milkymist_vgafb_create(0x60003000, 0x40000000, 0x0fffffff);
  134. milkymist_memcard_create(0x60004000);
  135. milkymist_ac97_create(0x60005000, irq[4], irq[5], irq[6], irq[7]);
  136. milkymist_pfpu_create(0x60006000, irq[8]);
  137. milkymist_tmu2_create(0x60007000, irq[9]);
  138. milkymist_minimac2_create(0x60008000, 0x30000000, irq[10], irq[11]);
  139. milkymist_softusb_create(0x6000f000, irq[15],
  140. 0x20000000, 0x1000, 0x20020000, 0x2000);
  141. /* make sure juart isn't the first chardev */
  142. env->juart_state = lm32_juart_init();
  143. if (kernel_filename) {
  144. uint64_t entry;
  145. /* Boots a kernel elf binary. */
  146. kernel_size = load_elf(kernel_filename, NULL, NULL, &entry, NULL, NULL,
  147. 1, ELF_MACHINE, 0);
  148. reset_info->bootstrap_pc = entry;
  149. if (kernel_size < 0) {
  150. kernel_size = load_image_targphys(kernel_filename, sdram_base,
  151. sdram_size);
  152. reset_info->bootstrap_pc = sdram_base;
  153. }
  154. if (kernel_size < 0) {
  155. fprintf(stderr, "qemu: could not load kernel '%s'\n",
  156. kernel_filename);
  157. exit(1);
  158. }
  159. }
  160. if (kernel_cmdline && strlen(kernel_cmdline)) {
  161. pstrcpy_targphys("cmdline", cmdline_base, TARGET_PAGE_SIZE,
  162. kernel_cmdline);
  163. reset_info->cmdline_base = (uint32_t)cmdline_base;
  164. }
  165. if (initrd_filename) {
  166. size_t initrd_size;
  167. initrd_size = load_image_targphys(initrd_filename, initrd_base,
  168. initrd_max);
  169. reset_info->initrd_base = (uint32_t)initrd_base;
  170. reset_info->initrd_size = (uint32_t)initrd_size;
  171. }
  172. qemu_register_reset(main_cpu_reset, reset_info);
  173. }
  174. static QEMUMachine milkymist_machine = {
  175. .name = "milkymist",
  176. .desc = "Milkymist One",
  177. .init = milkymist_init,
  178. .is_default = 0,
  179. DEFAULT_MACHINE_OPTIONS,
  180. };
  181. static void milkymist_machine_init(void)
  182. {
  183. qemu_register_machine(&milkymist_machine);
  184. }
  185. machine_init(milkymist_machine_init);