lm32_boards.c 10 KB

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