lm32_boards.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * QEMU models for LatticeMico32 uclinux and evr32 boards.
  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 "qemu/osdep.h"
  20. #include "qemu/units.h"
  21. #include "qemu/error-report.h"
  22. #include "cpu.h"
  23. #include "hw/sysbus.h"
  24. #include "hw/irq.h"
  25. #include "hw/block/flash.h"
  26. #include "hw/boards.h"
  27. #include "hw/loader.h"
  28. #include "elf.h"
  29. #include "lm32_hwsetup.h"
  30. #include "lm32.h"
  31. #include "exec/address-spaces.h"
  32. #include "sysemu/reset.h"
  33. #include "sysemu/sysemu.h"
  34. typedef struct {
  35. LM32CPU *cpu;
  36. hwaddr bootstrap_pc;
  37. hwaddr flash_base;
  38. hwaddr hwsetup_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. LM32CPU *cpu = opaque;
  46. CPUState *cs = CPU(cpu);
  47. if (level) {
  48. cpu_interrupt(cs, CPU_INTERRUPT_HARD);
  49. } else {
  50. cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
  51. }
  52. }
  53. static void main_cpu_reset(void *opaque)
  54. {
  55. ResetInfo *reset_info = opaque;
  56. CPULM32State *env = &reset_info->cpu->env;
  57. cpu_reset(CPU(reset_info->cpu));
  58. /* init defaults */
  59. env->pc = (uint32_t)reset_info->bootstrap_pc;
  60. env->regs[R_R1] = (uint32_t)reset_info->hwsetup_base;
  61. env->regs[R_R2] = (uint32_t)reset_info->cmdline_base;
  62. env->regs[R_R3] = (uint32_t)reset_info->initrd_base;
  63. env->regs[R_R4] = (uint32_t)(reset_info->initrd_base +
  64. reset_info->initrd_size);
  65. env->eba = reset_info->flash_base;
  66. env->deba = reset_info->flash_base;
  67. }
  68. static void lm32_evr_init(MachineState *machine)
  69. {
  70. const char *kernel_filename = machine->kernel_filename;
  71. LM32CPU *cpu;
  72. CPULM32State *env;
  73. DriveInfo *dinfo;
  74. MemoryRegion *address_space_mem = get_system_memory();
  75. MemoryRegion *phys_ram = g_new(MemoryRegion, 1);
  76. qemu_irq irq[32];
  77. ResetInfo *reset_info;
  78. int i;
  79. /* memory map */
  80. hwaddr flash_base = 0x04000000;
  81. size_t flash_sector_size = 256 * KiB;
  82. size_t flash_size = 32 * MiB;
  83. hwaddr ram_base = 0x08000000;
  84. size_t ram_size = 64 * MiB;
  85. hwaddr timer0_base = 0x80002000;
  86. hwaddr uart0_base = 0x80006000;
  87. hwaddr timer1_base = 0x8000a000;
  88. int uart0_irq = 0;
  89. int timer0_irq = 1;
  90. int timer1_irq = 3;
  91. reset_info = g_malloc0(sizeof(ResetInfo));
  92. cpu = LM32_CPU(cpu_create(machine->cpu_type));
  93. env = &cpu->env;
  94. reset_info->cpu = cpu;
  95. reset_info->flash_base = flash_base;
  96. memory_region_allocate_system_memory(phys_ram, NULL, "lm32_evr.sdram",
  97. ram_size);
  98. memory_region_add_subregion(address_space_mem, ram_base, phys_ram);
  99. dinfo = drive_get(IF_PFLASH, 0, 0);
  100. /* Spansion S29NS128P */
  101. pflash_cfi02_register(flash_base, "lm32_evr.flash", flash_size,
  102. dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
  103. flash_sector_size,
  104. 1, 2, 0x01, 0x7e, 0x43, 0x00, 0x555, 0x2aa, 1);
  105. /* create irq lines */
  106. env->pic_state = lm32_pic_init(qemu_allocate_irq(cpu_irq_handler, cpu, 0));
  107. for (i = 0; i < 32; i++) {
  108. irq[i] = qdev_get_gpio_in(env->pic_state, i);
  109. }
  110. lm32_uart_create(uart0_base, irq[uart0_irq], serial_hd(0));
  111. sysbus_create_simple("lm32-timer", timer0_base, irq[timer0_irq]);
  112. sysbus_create_simple("lm32-timer", timer1_base, irq[timer1_irq]);
  113. /* make sure juart isn't the first chardev */
  114. env->juart_state = lm32_juart_init(serial_hd(1));
  115. reset_info->bootstrap_pc = flash_base;
  116. if (kernel_filename) {
  117. uint64_t entry;
  118. int kernel_size;
  119. kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
  120. &entry, NULL, NULL,
  121. 1, EM_LATTICEMICO32, 0, 0);
  122. reset_info->bootstrap_pc = entry;
  123. if (kernel_size < 0) {
  124. kernel_size = load_image_targphys(kernel_filename, ram_base,
  125. ram_size);
  126. reset_info->bootstrap_pc = ram_base;
  127. }
  128. if (kernel_size < 0) {
  129. error_report("could not load kernel '%s'", kernel_filename);
  130. exit(1);
  131. }
  132. }
  133. qemu_register_reset(main_cpu_reset, reset_info);
  134. }
  135. static void lm32_uclinux_init(MachineState *machine)
  136. {
  137. const char *kernel_filename = machine->kernel_filename;
  138. const char *kernel_cmdline = machine->kernel_cmdline;
  139. const char *initrd_filename = machine->initrd_filename;
  140. LM32CPU *cpu;
  141. CPULM32State *env;
  142. DriveInfo *dinfo;
  143. MemoryRegion *address_space_mem = get_system_memory();
  144. MemoryRegion *phys_ram = g_new(MemoryRegion, 1);
  145. qemu_irq irq[32];
  146. HWSetup *hw;
  147. ResetInfo *reset_info;
  148. int i;
  149. /* memory map */
  150. hwaddr flash_base = 0x04000000;
  151. size_t flash_sector_size = 256 * KiB;
  152. size_t flash_size = 32 * MiB;
  153. hwaddr ram_base = 0x08000000;
  154. size_t ram_size = 64 * MiB;
  155. hwaddr uart0_base = 0x80000000;
  156. hwaddr timer0_base = 0x80002000;
  157. hwaddr timer1_base = 0x80010000;
  158. hwaddr timer2_base = 0x80012000;
  159. int uart0_irq = 0;
  160. int timer0_irq = 1;
  161. int timer1_irq = 20;
  162. int timer2_irq = 21;
  163. hwaddr hwsetup_base = 0x0bffe000;
  164. hwaddr cmdline_base = 0x0bfff000;
  165. hwaddr initrd_base = 0x08400000;
  166. size_t initrd_max = 0x01000000;
  167. reset_info = g_malloc0(sizeof(ResetInfo));
  168. cpu = LM32_CPU(cpu_create(machine->cpu_type));
  169. env = &cpu->env;
  170. reset_info->cpu = cpu;
  171. reset_info->flash_base = flash_base;
  172. memory_region_allocate_system_memory(phys_ram, NULL,
  173. "lm32_uclinux.sdram", ram_size);
  174. memory_region_add_subregion(address_space_mem, ram_base, phys_ram);
  175. dinfo = drive_get(IF_PFLASH, 0, 0);
  176. /* Spansion S29NS128P */
  177. pflash_cfi02_register(flash_base, "lm32_uclinux.flash", flash_size,
  178. dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
  179. flash_sector_size,
  180. 1, 2, 0x01, 0x7e, 0x43, 0x00, 0x555, 0x2aa, 1);
  181. /* create irq lines */
  182. env->pic_state = lm32_pic_init(qemu_allocate_irq(cpu_irq_handler, env, 0));
  183. for (i = 0; i < 32; i++) {
  184. irq[i] = qdev_get_gpio_in(env->pic_state, i);
  185. }
  186. lm32_uart_create(uart0_base, irq[uart0_irq], serial_hd(0));
  187. sysbus_create_simple("lm32-timer", timer0_base, irq[timer0_irq]);
  188. sysbus_create_simple("lm32-timer", timer1_base, irq[timer1_irq]);
  189. sysbus_create_simple("lm32-timer", timer2_base, irq[timer2_irq]);
  190. /* make sure juart isn't the first chardev */
  191. env->juart_state = lm32_juart_init(serial_hd(1));
  192. reset_info->bootstrap_pc = flash_base;
  193. if (kernel_filename) {
  194. uint64_t entry;
  195. int kernel_size;
  196. kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
  197. &entry, NULL, NULL,
  198. 1, EM_LATTICEMICO32, 0, 0);
  199. reset_info->bootstrap_pc = entry;
  200. if (kernel_size < 0) {
  201. kernel_size = load_image_targphys(kernel_filename, ram_base,
  202. ram_size);
  203. reset_info->bootstrap_pc = ram_base;
  204. }
  205. if (kernel_size < 0) {
  206. error_report("could not load kernel '%s'", kernel_filename);
  207. exit(1);
  208. }
  209. }
  210. /* generate a rom with the hardware description */
  211. hw = hwsetup_init();
  212. hwsetup_add_cpu(hw, "LM32", 75000000);
  213. hwsetup_add_flash(hw, "flash", flash_base, flash_size);
  214. hwsetup_add_ddr_sdram(hw, "ddr_sdram", ram_base, ram_size);
  215. hwsetup_add_timer(hw, "timer0", timer0_base, timer0_irq);
  216. hwsetup_add_timer(hw, "timer1_dev_only", timer1_base, timer1_irq);
  217. hwsetup_add_timer(hw, "timer2_dev_only", timer2_base, timer2_irq);
  218. hwsetup_add_uart(hw, "uart", uart0_base, uart0_irq);
  219. hwsetup_add_trailer(hw);
  220. hwsetup_create_rom(hw, hwsetup_base);
  221. hwsetup_free(hw);
  222. reset_info->hwsetup_base = hwsetup_base;
  223. if (kernel_cmdline && strlen(kernel_cmdline)) {
  224. pstrcpy_targphys("cmdline", cmdline_base, TARGET_PAGE_SIZE,
  225. kernel_cmdline);
  226. reset_info->cmdline_base = cmdline_base;
  227. }
  228. if (initrd_filename) {
  229. size_t initrd_size;
  230. initrd_size = load_image_targphys(initrd_filename, initrd_base,
  231. initrd_max);
  232. reset_info->initrd_base = initrd_base;
  233. reset_info->initrd_size = initrd_size;
  234. }
  235. qemu_register_reset(main_cpu_reset, reset_info);
  236. }
  237. static void lm32_evr_class_init(ObjectClass *oc, void *data)
  238. {
  239. MachineClass *mc = MACHINE_CLASS(oc);
  240. mc->desc = "LatticeMico32 EVR32 eval system";
  241. mc->init = lm32_evr_init;
  242. mc->is_default = 1;
  243. mc->default_cpu_type = LM32_CPU_TYPE_NAME("lm32-full");
  244. }
  245. static const TypeInfo lm32_evr_type = {
  246. .name = MACHINE_TYPE_NAME("lm32-evr"),
  247. .parent = TYPE_MACHINE,
  248. .class_init = lm32_evr_class_init,
  249. };
  250. static void lm32_uclinux_class_init(ObjectClass *oc, void *data)
  251. {
  252. MachineClass *mc = MACHINE_CLASS(oc);
  253. mc->desc = "lm32 platform for uClinux and u-boot by Theobroma Systems";
  254. mc->init = lm32_uclinux_init;
  255. mc->is_default = 0;
  256. mc->default_cpu_type = LM32_CPU_TYPE_NAME("lm32-full");
  257. }
  258. static const TypeInfo lm32_uclinux_type = {
  259. .name = MACHINE_TYPE_NAME("lm32-uclinux"),
  260. .parent = TYPE_MACHINE,
  261. .class_init = lm32_uclinux_class_init,
  262. };
  263. static void lm32_machine_init(void)
  264. {
  265. type_register_static(&lm32_evr_type);
  266. type_register_static(&lm32_uclinux_type);
  267. }
  268. type_init(lm32_machine_init)