mps2.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * ARM V2M MPS2 board emulation.
  3. *
  4. * Copyright (c) 2017 Linaro Limited
  5. * Written by Peter Maydell
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 or
  9. * (at your option) any later version.
  10. */
  11. /* The MPS2 and MPS2+ dev boards are FPGA based (the 2+ has a bigger
  12. * FPGA but is otherwise the same as the 2). Since the CPU itself
  13. * and most of the devices are in the FPGA, the details of the board
  14. * as seen by the guest depend significantly on the FPGA image.
  15. * We model the following FPGA images:
  16. * "mps2-an385" -- Cortex-M3 as documented in ARM Application Note AN385
  17. * "mps2-an511" -- Cortex-M3 'DesignStart' as documented in AN511
  18. *
  19. * Links to the TRM for the board itself and to the various Application
  20. * Notes which document the FPGA images can be found here:
  21. * https://developer.arm.com/products/system-design/development-boards/cortex-m-prototyping-system
  22. */
  23. #include "qemu/osdep.h"
  24. #include "qemu/units.h"
  25. #include "qapi/error.h"
  26. #include "qemu/error-report.h"
  27. #include "hw/arm/boot.h"
  28. #include "hw/arm/armv7m.h"
  29. #include "hw/or-irq.h"
  30. #include "hw/boards.h"
  31. #include "exec/address-spaces.h"
  32. #include "sysemu/sysemu.h"
  33. #include "hw/misc/unimp.h"
  34. #include "hw/char/cmsdk-apb-uart.h"
  35. #include "hw/timer/cmsdk-apb-timer.h"
  36. #include "hw/timer/cmsdk-apb-dualtimer.h"
  37. #include "hw/misc/mps2-scc.h"
  38. #include "hw/net/lan9118.h"
  39. #include "net/net.h"
  40. typedef enum MPS2FPGAType {
  41. FPGA_AN385,
  42. FPGA_AN511,
  43. } MPS2FPGAType;
  44. typedef struct {
  45. MachineClass parent;
  46. MPS2FPGAType fpga_type;
  47. uint32_t scc_id;
  48. } MPS2MachineClass;
  49. typedef struct {
  50. MachineState parent;
  51. ARMv7MState armv7m;
  52. MemoryRegion psram;
  53. MemoryRegion ssram1;
  54. MemoryRegion ssram1_m;
  55. MemoryRegion ssram23;
  56. MemoryRegion ssram23_m;
  57. MemoryRegion blockram;
  58. MemoryRegion blockram_m1;
  59. MemoryRegion blockram_m2;
  60. MemoryRegion blockram_m3;
  61. MemoryRegion sram;
  62. MPS2SCC scc;
  63. CMSDKAPBDualTimer dualtimer;
  64. } MPS2MachineState;
  65. #define TYPE_MPS2_MACHINE "mps2"
  66. #define TYPE_MPS2_AN385_MACHINE MACHINE_TYPE_NAME("mps2-an385")
  67. #define TYPE_MPS2_AN511_MACHINE MACHINE_TYPE_NAME("mps2-an511")
  68. #define MPS2_MACHINE(obj) \
  69. OBJECT_CHECK(MPS2MachineState, obj, TYPE_MPS2_MACHINE)
  70. #define MPS2_MACHINE_GET_CLASS(obj) \
  71. OBJECT_GET_CLASS(MPS2MachineClass, obj, TYPE_MPS2_MACHINE)
  72. #define MPS2_MACHINE_CLASS(klass) \
  73. OBJECT_CLASS_CHECK(MPS2MachineClass, klass, TYPE_MPS2_MACHINE)
  74. /* Main SYSCLK frequency in Hz */
  75. #define SYSCLK_FRQ 25000000
  76. /* Initialize the auxiliary RAM region @mr and map it into
  77. * the memory map at @base.
  78. */
  79. static void make_ram(MemoryRegion *mr, const char *name,
  80. hwaddr base, hwaddr size)
  81. {
  82. memory_region_init_ram(mr, NULL, name, size, &error_fatal);
  83. memory_region_add_subregion(get_system_memory(), base, mr);
  84. }
  85. /* Create an alias of an entire original MemoryRegion @orig
  86. * located at @base in the memory map.
  87. */
  88. static void make_ram_alias(MemoryRegion *mr, const char *name,
  89. MemoryRegion *orig, hwaddr base)
  90. {
  91. memory_region_init_alias(mr, NULL, name, orig, 0,
  92. memory_region_size(orig));
  93. memory_region_add_subregion(get_system_memory(), base, mr);
  94. }
  95. static void mps2_common_init(MachineState *machine)
  96. {
  97. MPS2MachineState *mms = MPS2_MACHINE(machine);
  98. MPS2MachineClass *mmc = MPS2_MACHINE_GET_CLASS(machine);
  99. MemoryRegion *system_memory = get_system_memory();
  100. MachineClass *mc = MACHINE_GET_CLASS(machine);
  101. DeviceState *armv7m, *sccdev;
  102. if (strcmp(machine->cpu_type, mc->default_cpu_type) != 0) {
  103. error_report("This board can only be used with CPU %s",
  104. mc->default_cpu_type);
  105. exit(1);
  106. }
  107. /* The FPGA images have an odd combination of different RAMs,
  108. * because in hardware they are different implementations and
  109. * connected to different buses, giving varying performance/size
  110. * tradeoffs. For QEMU they're all just RAM, though. We arbitrarily
  111. * call the 16MB our "system memory", as it's the largest lump.
  112. *
  113. * Common to both boards:
  114. * 0x21000000..0x21ffffff : PSRAM (16MB)
  115. * AN385 only:
  116. * 0x00000000 .. 0x003fffff : ZBT SSRAM1
  117. * 0x00400000 .. 0x007fffff : mirror of ZBT SSRAM1
  118. * 0x20000000 .. 0x203fffff : ZBT SSRAM 2&3
  119. * 0x20400000 .. 0x207fffff : mirror of ZBT SSRAM 2&3
  120. * 0x01000000 .. 0x01003fff : block RAM (16K)
  121. * 0x01004000 .. 0x01007fff : mirror of above
  122. * 0x01008000 .. 0x0100bfff : mirror of above
  123. * 0x0100c000 .. 0x0100ffff : mirror of above
  124. * AN511 only:
  125. * 0x00000000 .. 0x0003ffff : FPGA block RAM
  126. * 0x00400000 .. 0x007fffff : ZBT SSRAM1
  127. * 0x20000000 .. 0x2001ffff : SRAM
  128. * 0x20400000 .. 0x207fffff : ZBT SSRAM 2&3
  129. *
  130. * The AN385 has a feature where the lowest 16K can be mapped
  131. * either to the bottom of the ZBT SSRAM1 or to the block RAM.
  132. * This is of no use for QEMU so we don't implement it (as if
  133. * zbt_boot_ctrl is always zero).
  134. */
  135. memory_region_allocate_system_memory(&mms->psram,
  136. NULL, "mps.ram", 16 * MiB);
  137. memory_region_add_subregion(system_memory, 0x21000000, &mms->psram);
  138. switch (mmc->fpga_type) {
  139. case FPGA_AN385:
  140. make_ram(&mms->ssram1, "mps.ssram1", 0x0, 0x400000);
  141. make_ram_alias(&mms->ssram1_m, "mps.ssram1_m", &mms->ssram1, 0x400000);
  142. make_ram(&mms->ssram23, "mps.ssram23", 0x20000000, 0x400000);
  143. make_ram_alias(&mms->ssram23_m, "mps.ssram23_m",
  144. &mms->ssram23, 0x20400000);
  145. make_ram(&mms->blockram, "mps.blockram", 0x01000000, 0x4000);
  146. make_ram_alias(&mms->blockram_m1, "mps.blockram_m1",
  147. &mms->blockram, 0x01004000);
  148. make_ram_alias(&mms->blockram_m2, "mps.blockram_m2",
  149. &mms->blockram, 0x01008000);
  150. make_ram_alias(&mms->blockram_m3, "mps.blockram_m3",
  151. &mms->blockram, 0x0100c000);
  152. break;
  153. case FPGA_AN511:
  154. make_ram(&mms->blockram, "mps.blockram", 0x0, 0x40000);
  155. make_ram(&mms->ssram1, "mps.ssram1", 0x00400000, 0x00800000);
  156. make_ram(&mms->sram, "mps.sram", 0x20000000, 0x20000);
  157. make_ram(&mms->ssram23, "mps.ssram23", 0x20400000, 0x400000);
  158. break;
  159. default:
  160. g_assert_not_reached();
  161. }
  162. sysbus_init_child_obj(OBJECT(mms), "armv7m", &mms->armv7m,
  163. sizeof(mms->armv7m), TYPE_ARMV7M);
  164. armv7m = DEVICE(&mms->armv7m);
  165. switch (mmc->fpga_type) {
  166. case FPGA_AN385:
  167. qdev_prop_set_uint32(armv7m, "num-irq", 32);
  168. break;
  169. case FPGA_AN511:
  170. qdev_prop_set_uint32(armv7m, "num-irq", 64);
  171. break;
  172. default:
  173. g_assert_not_reached();
  174. }
  175. qdev_prop_set_string(armv7m, "cpu-type", machine->cpu_type);
  176. qdev_prop_set_bit(armv7m, "enable-bitband", true);
  177. object_property_set_link(OBJECT(&mms->armv7m), OBJECT(system_memory),
  178. "memory", &error_abort);
  179. object_property_set_bool(OBJECT(&mms->armv7m), true, "realized",
  180. &error_fatal);
  181. create_unimplemented_device("zbtsmram mirror", 0x00400000, 0x00400000);
  182. create_unimplemented_device("RESERVED 1", 0x00800000, 0x00800000);
  183. create_unimplemented_device("Block RAM", 0x01000000, 0x00010000);
  184. create_unimplemented_device("RESERVED 2", 0x01010000, 0x1EFF0000);
  185. create_unimplemented_device("RESERVED 3", 0x20800000, 0x00800000);
  186. create_unimplemented_device("PSRAM", 0x21000000, 0x01000000);
  187. /* These three ranges all cover multiple devices; we may implement
  188. * some of them below (in which case the real device takes precedence
  189. * over the unimplemented-region mapping).
  190. */
  191. create_unimplemented_device("CMSDK APB peripheral region @0x40000000",
  192. 0x40000000, 0x00010000);
  193. create_unimplemented_device("CMSDK peripheral region @0x40010000",
  194. 0x40010000, 0x00010000);
  195. create_unimplemented_device("Extra peripheral region @0x40020000",
  196. 0x40020000, 0x00010000);
  197. create_unimplemented_device("RESERVED 4", 0x40030000, 0x001D0000);
  198. create_unimplemented_device("VGA", 0x41000000, 0x0200000);
  199. switch (mmc->fpga_type) {
  200. case FPGA_AN385:
  201. {
  202. /* The overflow IRQs for UARTs 0, 1 and 2 are ORed together.
  203. * Overflow for UARTs 4 and 5 doesn't trigger any interrupt.
  204. */
  205. Object *orgate;
  206. DeviceState *orgate_dev;
  207. int i;
  208. orgate = object_new(TYPE_OR_IRQ);
  209. object_property_set_int(orgate, 6, "num-lines", &error_fatal);
  210. object_property_set_bool(orgate, true, "realized", &error_fatal);
  211. orgate_dev = DEVICE(orgate);
  212. qdev_connect_gpio_out(orgate_dev, 0, qdev_get_gpio_in(armv7m, 12));
  213. for (i = 0; i < 5; i++) {
  214. static const hwaddr uartbase[] = {0x40004000, 0x40005000,
  215. 0x40006000, 0x40007000,
  216. 0x40009000};
  217. /* RX irq number; TX irq is always one greater */
  218. static const int uartirq[] = {0, 2, 4, 18, 20};
  219. qemu_irq txovrint = NULL, rxovrint = NULL;
  220. if (i < 3) {
  221. txovrint = qdev_get_gpio_in(orgate_dev, i * 2);
  222. rxovrint = qdev_get_gpio_in(orgate_dev, i * 2 + 1);
  223. }
  224. cmsdk_apb_uart_create(uartbase[i],
  225. qdev_get_gpio_in(armv7m, uartirq[i] + 1),
  226. qdev_get_gpio_in(armv7m, uartirq[i]),
  227. txovrint, rxovrint,
  228. NULL,
  229. serial_hd(i), SYSCLK_FRQ);
  230. }
  231. break;
  232. }
  233. case FPGA_AN511:
  234. {
  235. /* The overflow IRQs for all UARTs are ORed together.
  236. * Tx and Rx IRQs for each UART are ORed together.
  237. */
  238. Object *orgate;
  239. DeviceState *orgate_dev;
  240. int i;
  241. orgate = object_new(TYPE_OR_IRQ);
  242. object_property_set_int(orgate, 10, "num-lines", &error_fatal);
  243. object_property_set_bool(orgate, true, "realized", &error_fatal);
  244. orgate_dev = DEVICE(orgate);
  245. qdev_connect_gpio_out(orgate_dev, 0, qdev_get_gpio_in(armv7m, 12));
  246. for (i = 0; i < 5; i++) {
  247. /* system irq numbers for the combined tx/rx for each UART */
  248. static const int uart_txrx_irqno[] = {0, 2, 45, 46, 56};
  249. static const hwaddr uartbase[] = {0x40004000, 0x40005000,
  250. 0x4002c000, 0x4002d000,
  251. 0x4002e000};
  252. Object *txrx_orgate;
  253. DeviceState *txrx_orgate_dev;
  254. txrx_orgate = object_new(TYPE_OR_IRQ);
  255. object_property_set_int(txrx_orgate, 2, "num-lines", &error_fatal);
  256. object_property_set_bool(txrx_orgate, true, "realized",
  257. &error_fatal);
  258. txrx_orgate_dev = DEVICE(txrx_orgate);
  259. qdev_connect_gpio_out(txrx_orgate_dev, 0,
  260. qdev_get_gpio_in(armv7m, uart_txrx_irqno[i]));
  261. cmsdk_apb_uart_create(uartbase[i],
  262. qdev_get_gpio_in(txrx_orgate_dev, 0),
  263. qdev_get_gpio_in(txrx_orgate_dev, 1),
  264. qdev_get_gpio_in(orgate_dev, i * 2),
  265. qdev_get_gpio_in(orgate_dev, i * 2 + 1),
  266. NULL,
  267. serial_hd(i), SYSCLK_FRQ);
  268. }
  269. break;
  270. }
  271. default:
  272. g_assert_not_reached();
  273. }
  274. cmsdk_apb_timer_create(0x40000000, qdev_get_gpio_in(armv7m, 8), SYSCLK_FRQ);
  275. cmsdk_apb_timer_create(0x40001000, qdev_get_gpio_in(armv7m, 9), SYSCLK_FRQ);
  276. sysbus_init_child_obj(OBJECT(mms), "dualtimer", &mms->dualtimer,
  277. sizeof(mms->dualtimer), TYPE_CMSDK_APB_DUALTIMER);
  278. qdev_prop_set_uint32(DEVICE(&mms->dualtimer), "pclk-frq", SYSCLK_FRQ);
  279. object_property_set_bool(OBJECT(&mms->dualtimer), true, "realized",
  280. &error_fatal);
  281. sysbus_connect_irq(SYS_BUS_DEVICE(&mms->dualtimer), 0,
  282. qdev_get_gpio_in(armv7m, 10));
  283. sysbus_mmio_map(SYS_BUS_DEVICE(&mms->dualtimer), 0, 0x40002000);
  284. sysbus_init_child_obj(OBJECT(mms), "scc", &mms->scc,
  285. sizeof(mms->scc), TYPE_MPS2_SCC);
  286. sccdev = DEVICE(&mms->scc);
  287. qdev_prop_set_uint32(sccdev, "scc-cfg4", 0x2);
  288. qdev_prop_set_uint32(sccdev, "scc-aid", 0x00200008);
  289. qdev_prop_set_uint32(sccdev, "scc-id", mmc->scc_id);
  290. object_property_set_bool(OBJECT(&mms->scc), true, "realized",
  291. &error_fatal);
  292. sysbus_mmio_map(SYS_BUS_DEVICE(sccdev), 0, 0x4002f000);
  293. /* In hardware this is a LAN9220; the LAN9118 is software compatible
  294. * except that it doesn't support the checksum-offload feature.
  295. */
  296. lan9118_init(&nd_table[0], 0x40200000,
  297. qdev_get_gpio_in(armv7m,
  298. mmc->fpga_type == FPGA_AN385 ? 13 : 47));
  299. system_clock_scale = NANOSECONDS_PER_SECOND / SYSCLK_FRQ;
  300. armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
  301. 0x400000);
  302. }
  303. static void mps2_class_init(ObjectClass *oc, void *data)
  304. {
  305. MachineClass *mc = MACHINE_CLASS(oc);
  306. mc->init = mps2_common_init;
  307. mc->max_cpus = 1;
  308. }
  309. static void mps2_an385_class_init(ObjectClass *oc, void *data)
  310. {
  311. MachineClass *mc = MACHINE_CLASS(oc);
  312. MPS2MachineClass *mmc = MPS2_MACHINE_CLASS(oc);
  313. mc->desc = "ARM MPS2 with AN385 FPGA image for Cortex-M3";
  314. mmc->fpga_type = FPGA_AN385;
  315. mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-m3");
  316. mmc->scc_id = 0x41043850;
  317. }
  318. static void mps2_an511_class_init(ObjectClass *oc, void *data)
  319. {
  320. MachineClass *mc = MACHINE_CLASS(oc);
  321. MPS2MachineClass *mmc = MPS2_MACHINE_CLASS(oc);
  322. mc->desc = "ARM MPS2 with AN511 DesignStart FPGA image for Cortex-M3";
  323. mmc->fpga_type = FPGA_AN511;
  324. mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-m3");
  325. mmc->scc_id = 0x41045110;
  326. }
  327. static const TypeInfo mps2_info = {
  328. .name = TYPE_MPS2_MACHINE,
  329. .parent = TYPE_MACHINE,
  330. .abstract = true,
  331. .instance_size = sizeof(MPS2MachineState),
  332. .class_size = sizeof(MPS2MachineClass),
  333. .class_init = mps2_class_init,
  334. };
  335. static const TypeInfo mps2_an385_info = {
  336. .name = TYPE_MPS2_AN385_MACHINE,
  337. .parent = TYPE_MPS2_MACHINE,
  338. .class_init = mps2_an385_class_init,
  339. };
  340. static const TypeInfo mps2_an511_info = {
  341. .name = TYPE_MPS2_AN511_MACHINE,
  342. .parent = TYPE_MPS2_MACHINE,
  343. .class_init = mps2_an511_class_init,
  344. };
  345. static void mps2_machine_init(void)
  346. {
  347. type_register_static(&mps2_info);
  348. type_register_static(&mps2_an385_info);
  349. type_register_static(&mps2_an511_info);
  350. }
  351. type_init(mps2_machine_init);