2
0

lm32_boards.c 10 KB

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