2
0

mps2.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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 "qemu/cutils.h"
  26. #include "qapi/error.h"
  27. #include "qemu/error-report.h"
  28. #include "hw/arm/boot.h"
  29. #include "hw/arm/armv7m.h"
  30. #include "hw/or-irq.h"
  31. #include "hw/boards.h"
  32. #include "exec/address-spaces.h"
  33. #include "sysemu/sysemu.h"
  34. #include "hw/misc/unimp.h"
  35. #include "hw/char/cmsdk-apb-uart.h"
  36. #include "hw/timer/cmsdk-apb-timer.h"
  37. #include "hw/timer/cmsdk-apb-dualtimer.h"
  38. #include "hw/misc/mps2-scc.h"
  39. #include "hw/misc/mps2-fpgaio.h"
  40. #include "hw/ssi/pl022.h"
  41. #include "hw/i2c/arm_sbcon_i2c.h"
  42. #include "hw/net/lan9118.h"
  43. #include "net/net.h"
  44. #include "hw/watchdog/cmsdk-apb-watchdog.h"
  45. typedef enum MPS2FPGAType {
  46. FPGA_AN385,
  47. FPGA_AN511,
  48. } MPS2FPGAType;
  49. typedef struct {
  50. MachineClass parent;
  51. MPS2FPGAType fpga_type;
  52. uint32_t scc_id;
  53. } MPS2MachineClass;
  54. typedef struct {
  55. MachineState parent;
  56. ARMv7MState armv7m;
  57. MemoryRegion ssram1;
  58. MemoryRegion ssram1_m;
  59. MemoryRegion ssram23;
  60. MemoryRegion ssram23_m;
  61. MemoryRegion blockram;
  62. MemoryRegion blockram_m1;
  63. MemoryRegion blockram_m2;
  64. MemoryRegion blockram_m3;
  65. MemoryRegion sram;
  66. /* FPGA APB subsystem */
  67. MPS2SCC scc;
  68. MPS2FPGAIO fpgaio;
  69. /* CMSDK APB subsystem */
  70. CMSDKAPBDualTimer dualtimer;
  71. CMSDKAPBWatchdog watchdog;
  72. } MPS2MachineState;
  73. #define TYPE_MPS2_MACHINE "mps2"
  74. #define TYPE_MPS2_AN385_MACHINE MACHINE_TYPE_NAME("mps2-an385")
  75. #define TYPE_MPS2_AN511_MACHINE MACHINE_TYPE_NAME("mps2-an511")
  76. #define MPS2_MACHINE(obj) \
  77. OBJECT_CHECK(MPS2MachineState, obj, TYPE_MPS2_MACHINE)
  78. #define MPS2_MACHINE_GET_CLASS(obj) \
  79. OBJECT_GET_CLASS(MPS2MachineClass, obj, TYPE_MPS2_MACHINE)
  80. #define MPS2_MACHINE_CLASS(klass) \
  81. OBJECT_CLASS_CHECK(MPS2MachineClass, klass, TYPE_MPS2_MACHINE)
  82. /* Main SYSCLK frequency in Hz */
  83. #define SYSCLK_FRQ 25000000
  84. /* Initialize the auxiliary RAM region @mr and map it into
  85. * the memory map at @base.
  86. */
  87. static void make_ram(MemoryRegion *mr, const char *name,
  88. hwaddr base, hwaddr size)
  89. {
  90. memory_region_init_ram(mr, NULL, name, size, &error_fatal);
  91. memory_region_add_subregion(get_system_memory(), base, mr);
  92. }
  93. /* Create an alias of an entire original MemoryRegion @orig
  94. * located at @base in the memory map.
  95. */
  96. static void make_ram_alias(MemoryRegion *mr, const char *name,
  97. MemoryRegion *orig, hwaddr base)
  98. {
  99. memory_region_init_alias(mr, NULL, name, orig, 0,
  100. memory_region_size(orig));
  101. memory_region_add_subregion(get_system_memory(), base, mr);
  102. }
  103. static void mps2_common_init(MachineState *machine)
  104. {
  105. MPS2MachineState *mms = MPS2_MACHINE(machine);
  106. MPS2MachineClass *mmc = MPS2_MACHINE_GET_CLASS(machine);
  107. MemoryRegion *system_memory = get_system_memory();
  108. MachineClass *mc = MACHINE_GET_CLASS(machine);
  109. DeviceState *armv7m, *sccdev;
  110. int i;
  111. if (strcmp(machine->cpu_type, mc->default_cpu_type) != 0) {
  112. error_report("This board can only be used with CPU %s",
  113. mc->default_cpu_type);
  114. exit(1);
  115. }
  116. if (machine->ram_size != mc->default_ram_size) {
  117. char *sz = size_to_str(mc->default_ram_size);
  118. error_report("Invalid RAM size, should be %s", sz);
  119. g_free(sz);
  120. exit(EXIT_FAILURE);
  121. }
  122. /* The FPGA images have an odd combination of different RAMs,
  123. * because in hardware they are different implementations and
  124. * connected to different buses, giving varying performance/size
  125. * tradeoffs. For QEMU they're all just RAM, though. We arbitrarily
  126. * call the 16MB our "system memory", as it's the largest lump.
  127. *
  128. * Common to both boards:
  129. * 0x21000000..0x21ffffff : PSRAM (16MB)
  130. * AN385 only:
  131. * 0x00000000 .. 0x003fffff : ZBT SSRAM1
  132. * 0x00400000 .. 0x007fffff : mirror of ZBT SSRAM1
  133. * 0x20000000 .. 0x203fffff : ZBT SSRAM 2&3
  134. * 0x20400000 .. 0x207fffff : mirror of ZBT SSRAM 2&3
  135. * 0x01000000 .. 0x01003fff : block RAM (16K)
  136. * 0x01004000 .. 0x01007fff : mirror of above
  137. * 0x01008000 .. 0x0100bfff : mirror of above
  138. * 0x0100c000 .. 0x0100ffff : mirror of above
  139. * AN511 only:
  140. * 0x00000000 .. 0x0003ffff : FPGA block RAM
  141. * 0x00400000 .. 0x007fffff : ZBT SSRAM1
  142. * 0x20000000 .. 0x2001ffff : SRAM
  143. * 0x20400000 .. 0x207fffff : ZBT SSRAM 2&3
  144. *
  145. * The AN385 has a feature where the lowest 16K can be mapped
  146. * either to the bottom of the ZBT SSRAM1 or to the block RAM.
  147. * This is of no use for QEMU so we don't implement it (as if
  148. * zbt_boot_ctrl is always zero).
  149. */
  150. memory_region_add_subregion(system_memory, 0x21000000, machine->ram);
  151. switch (mmc->fpga_type) {
  152. case FPGA_AN385:
  153. make_ram(&mms->ssram1, "mps.ssram1", 0x0, 0x400000);
  154. make_ram_alias(&mms->ssram1_m, "mps.ssram1_m", &mms->ssram1, 0x400000);
  155. make_ram(&mms->ssram23, "mps.ssram23", 0x20000000, 0x400000);
  156. make_ram_alias(&mms->ssram23_m, "mps.ssram23_m",
  157. &mms->ssram23, 0x20400000);
  158. make_ram(&mms->blockram, "mps.blockram", 0x01000000, 0x4000);
  159. make_ram_alias(&mms->blockram_m1, "mps.blockram_m1",
  160. &mms->blockram, 0x01004000);
  161. make_ram_alias(&mms->blockram_m2, "mps.blockram_m2",
  162. &mms->blockram, 0x01008000);
  163. make_ram_alias(&mms->blockram_m3, "mps.blockram_m3",
  164. &mms->blockram, 0x0100c000);
  165. break;
  166. case FPGA_AN511:
  167. make_ram(&mms->blockram, "mps.blockram", 0x0, 0x40000);
  168. make_ram(&mms->ssram1, "mps.ssram1", 0x00400000, 0x00800000);
  169. make_ram(&mms->sram, "mps.sram", 0x20000000, 0x20000);
  170. make_ram(&mms->ssram23, "mps.ssram23", 0x20400000, 0x400000);
  171. break;
  172. default:
  173. g_assert_not_reached();
  174. }
  175. object_initialize_child(OBJECT(mms), "armv7m", &mms->armv7m, TYPE_ARMV7M);
  176. armv7m = DEVICE(&mms->armv7m);
  177. switch (mmc->fpga_type) {
  178. case FPGA_AN385:
  179. qdev_prop_set_uint32(armv7m, "num-irq", 32);
  180. break;
  181. case FPGA_AN511:
  182. qdev_prop_set_uint32(armv7m, "num-irq", 64);
  183. break;
  184. default:
  185. g_assert_not_reached();
  186. }
  187. qdev_prop_set_string(armv7m, "cpu-type", machine->cpu_type);
  188. qdev_prop_set_bit(armv7m, "enable-bitband", true);
  189. object_property_set_link(OBJECT(&mms->armv7m), "memory",
  190. OBJECT(system_memory), &error_abort);
  191. sysbus_realize(SYS_BUS_DEVICE(&mms->armv7m), &error_fatal);
  192. create_unimplemented_device("zbtsmram mirror", 0x00400000, 0x00400000);
  193. create_unimplemented_device("RESERVED 1", 0x00800000, 0x00800000);
  194. create_unimplemented_device("Block RAM", 0x01000000, 0x00010000);
  195. create_unimplemented_device("RESERVED 2", 0x01010000, 0x1EFF0000);
  196. create_unimplemented_device("RESERVED 3", 0x20800000, 0x00800000);
  197. create_unimplemented_device("PSRAM", 0x21000000, 0x01000000);
  198. /* These three ranges all cover multiple devices; we may implement
  199. * some of them below (in which case the real device takes precedence
  200. * over the unimplemented-region mapping).
  201. */
  202. create_unimplemented_device("CMSDK APB peripheral region @0x40000000",
  203. 0x40000000, 0x00010000);
  204. create_unimplemented_device("CMSDK AHB peripheral region @0x40010000",
  205. 0x40010000, 0x00010000);
  206. create_unimplemented_device("Extra peripheral region @0x40020000",
  207. 0x40020000, 0x00010000);
  208. create_unimplemented_device("RESERVED 4", 0x40030000, 0x001D0000);
  209. create_unimplemented_device("VGA", 0x41000000, 0x0200000);
  210. switch (mmc->fpga_type) {
  211. case FPGA_AN385:
  212. {
  213. /* The overflow IRQs for UARTs 0, 1 and 2 are ORed together.
  214. * Overflow for UARTs 4 and 5 doesn't trigger any interrupt.
  215. */
  216. Object *orgate;
  217. DeviceState *orgate_dev;
  218. orgate = object_new(TYPE_OR_IRQ);
  219. object_property_set_int(orgate, "num-lines", 6, &error_fatal);
  220. qdev_realize(DEVICE(orgate), NULL, &error_fatal);
  221. orgate_dev = DEVICE(orgate);
  222. qdev_connect_gpio_out(orgate_dev, 0, qdev_get_gpio_in(armv7m, 12));
  223. for (i = 0; i < 5; i++) {
  224. static const hwaddr uartbase[] = {0x40004000, 0x40005000,
  225. 0x40006000, 0x40007000,
  226. 0x40009000};
  227. /* RX irq number; TX irq is always one greater */
  228. static const int uartirq[] = {0, 2, 4, 18, 20};
  229. qemu_irq txovrint = NULL, rxovrint = NULL;
  230. if (i < 3) {
  231. txovrint = qdev_get_gpio_in(orgate_dev, i * 2);
  232. rxovrint = qdev_get_gpio_in(orgate_dev, i * 2 + 1);
  233. }
  234. cmsdk_apb_uart_create(uartbase[i],
  235. qdev_get_gpio_in(armv7m, uartirq[i] + 1),
  236. qdev_get_gpio_in(armv7m, uartirq[i]),
  237. txovrint, rxovrint,
  238. NULL,
  239. serial_hd(i), SYSCLK_FRQ);
  240. }
  241. break;
  242. }
  243. case FPGA_AN511:
  244. {
  245. /* The overflow IRQs for all UARTs are ORed together.
  246. * Tx and Rx IRQs for each UART are ORed together.
  247. */
  248. Object *orgate;
  249. DeviceState *orgate_dev;
  250. orgate = object_new(TYPE_OR_IRQ);
  251. object_property_set_int(orgate, "num-lines", 10, &error_fatal);
  252. qdev_realize(DEVICE(orgate), NULL, &error_fatal);
  253. orgate_dev = DEVICE(orgate);
  254. qdev_connect_gpio_out(orgate_dev, 0, qdev_get_gpio_in(armv7m, 12));
  255. for (i = 0; i < 5; i++) {
  256. /* system irq numbers for the combined tx/rx for each UART */
  257. static const int uart_txrx_irqno[] = {0, 2, 45, 46, 56};
  258. static const hwaddr uartbase[] = {0x40004000, 0x40005000,
  259. 0x4002c000, 0x4002d000,
  260. 0x4002e000};
  261. Object *txrx_orgate;
  262. DeviceState *txrx_orgate_dev;
  263. txrx_orgate = object_new(TYPE_OR_IRQ);
  264. object_property_set_int(txrx_orgate, "num-lines", 2, &error_fatal);
  265. qdev_realize(DEVICE(txrx_orgate), NULL, &error_fatal);
  266. txrx_orgate_dev = DEVICE(txrx_orgate);
  267. qdev_connect_gpio_out(txrx_orgate_dev, 0,
  268. qdev_get_gpio_in(armv7m, uart_txrx_irqno[i]));
  269. cmsdk_apb_uart_create(uartbase[i],
  270. qdev_get_gpio_in(txrx_orgate_dev, 0),
  271. qdev_get_gpio_in(txrx_orgate_dev, 1),
  272. qdev_get_gpio_in(orgate_dev, i * 2),
  273. qdev_get_gpio_in(orgate_dev, i * 2 + 1),
  274. NULL,
  275. serial_hd(i), SYSCLK_FRQ);
  276. }
  277. break;
  278. }
  279. default:
  280. g_assert_not_reached();
  281. }
  282. for (i = 0; i < 4; i++) {
  283. static const hwaddr gpiobase[] = {0x40010000, 0x40011000,
  284. 0x40012000, 0x40013000};
  285. create_unimplemented_device("cmsdk-ahb-gpio", gpiobase[i], 0x1000);
  286. }
  287. /* CMSDK APB subsystem */
  288. cmsdk_apb_timer_create(0x40000000, qdev_get_gpio_in(armv7m, 8), SYSCLK_FRQ);
  289. cmsdk_apb_timer_create(0x40001000, qdev_get_gpio_in(armv7m, 9), SYSCLK_FRQ);
  290. object_initialize_child(OBJECT(mms), "dualtimer", &mms->dualtimer,
  291. TYPE_CMSDK_APB_DUALTIMER);
  292. qdev_prop_set_uint32(DEVICE(&mms->dualtimer), "pclk-frq", SYSCLK_FRQ);
  293. sysbus_realize(SYS_BUS_DEVICE(&mms->dualtimer), &error_fatal);
  294. sysbus_connect_irq(SYS_BUS_DEVICE(&mms->dualtimer), 0,
  295. qdev_get_gpio_in(armv7m, 10));
  296. sysbus_mmio_map(SYS_BUS_DEVICE(&mms->dualtimer), 0, 0x40002000);
  297. object_initialize_child(OBJECT(mms), "watchdog", &mms->watchdog,
  298. TYPE_CMSDK_APB_WATCHDOG);
  299. qdev_prop_set_uint32(DEVICE(&mms->watchdog), "wdogclk-frq", SYSCLK_FRQ);
  300. sysbus_realize(SYS_BUS_DEVICE(&mms->watchdog), &error_fatal);
  301. sysbus_connect_irq(SYS_BUS_DEVICE(&mms->watchdog), 0,
  302. qdev_get_gpio_in_named(armv7m, "NMI", 0));
  303. sysbus_mmio_map(SYS_BUS_DEVICE(&mms->watchdog), 0, 0x40008000);
  304. /* FPGA APB subsystem */
  305. object_initialize_child(OBJECT(mms), "scc", &mms->scc, TYPE_MPS2_SCC);
  306. sccdev = DEVICE(&mms->scc);
  307. qdev_prop_set_uint32(sccdev, "scc-cfg4", 0x2);
  308. qdev_prop_set_uint32(sccdev, "scc-aid", 0x00200008);
  309. qdev_prop_set_uint32(sccdev, "scc-id", mmc->scc_id);
  310. sysbus_realize(SYS_BUS_DEVICE(&mms->scc), &error_fatal);
  311. sysbus_mmio_map(SYS_BUS_DEVICE(sccdev), 0, 0x4002f000);
  312. object_initialize_child(OBJECT(mms), "fpgaio",
  313. &mms->fpgaio, TYPE_MPS2_FPGAIO);
  314. qdev_prop_set_uint32(DEVICE(&mms->fpgaio), "prescale-clk", 25000000);
  315. sysbus_realize(SYS_BUS_DEVICE(&mms->fpgaio), &error_fatal);
  316. sysbus_mmio_map(SYS_BUS_DEVICE(&mms->fpgaio), 0, 0x40028000);
  317. sysbus_create_simple(TYPE_PL022, 0x40025000, /* External ADC */
  318. qdev_get_gpio_in(armv7m, 22));
  319. for (i = 0; i < 2; i++) {
  320. static const int spi_irqno[] = {11, 24};
  321. static const hwaddr spibase[] = {0x40020000, /* APB */
  322. 0x40021000, /* LCD */
  323. 0x40026000, /* Shield0 */
  324. 0x40027000}; /* Shield1 */
  325. DeviceState *orgate_dev;
  326. Object *orgate;
  327. int j;
  328. orgate = object_new(TYPE_OR_IRQ);
  329. object_property_set_int(orgate, "num-lines", 2, &error_fatal);
  330. orgate_dev = DEVICE(orgate);
  331. qdev_realize(orgate_dev, NULL, &error_fatal);
  332. qdev_connect_gpio_out(orgate_dev, 0,
  333. qdev_get_gpio_in(armv7m, spi_irqno[i]));
  334. for (j = 0; j < 2; j++) {
  335. sysbus_create_simple(TYPE_PL022, spibase[2 * i + j],
  336. qdev_get_gpio_in(orgate_dev, j));
  337. }
  338. }
  339. for (i = 0; i < 4; i++) {
  340. static const hwaddr i2cbase[] = {0x40022000, /* Touch */
  341. 0x40023000, /* Audio */
  342. 0x40029000, /* Shield0 */
  343. 0x4002a000}; /* Shield1 */
  344. sysbus_create_simple(TYPE_ARM_SBCON_I2C, i2cbase[i], NULL);
  345. }
  346. create_unimplemented_device("i2s", 0x40024000, 0x400);
  347. /* In hardware this is a LAN9220; the LAN9118 is software compatible
  348. * except that it doesn't support the checksum-offload feature.
  349. */
  350. lan9118_init(&nd_table[0], 0x40200000,
  351. qdev_get_gpio_in(armv7m,
  352. mmc->fpga_type == FPGA_AN385 ? 13 : 47));
  353. system_clock_scale = NANOSECONDS_PER_SECOND / SYSCLK_FRQ;
  354. armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
  355. 0x400000);
  356. }
  357. static void mps2_class_init(ObjectClass *oc, void *data)
  358. {
  359. MachineClass *mc = MACHINE_CLASS(oc);
  360. mc->init = mps2_common_init;
  361. mc->max_cpus = 1;
  362. mc->default_ram_size = 16 * MiB;
  363. mc->default_ram_id = "mps.ram";
  364. }
  365. static void mps2_an385_class_init(ObjectClass *oc, void *data)
  366. {
  367. MachineClass *mc = MACHINE_CLASS(oc);
  368. MPS2MachineClass *mmc = MPS2_MACHINE_CLASS(oc);
  369. mc->desc = "ARM MPS2 with AN385 FPGA image for Cortex-M3";
  370. mmc->fpga_type = FPGA_AN385;
  371. mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-m3");
  372. mmc->scc_id = 0x41043850;
  373. }
  374. static void mps2_an511_class_init(ObjectClass *oc, void *data)
  375. {
  376. MachineClass *mc = MACHINE_CLASS(oc);
  377. MPS2MachineClass *mmc = MPS2_MACHINE_CLASS(oc);
  378. mc->desc = "ARM MPS2 with AN511 DesignStart FPGA image for Cortex-M3";
  379. mmc->fpga_type = FPGA_AN511;
  380. mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-m3");
  381. mmc->scc_id = 0x41045110;
  382. }
  383. static const TypeInfo mps2_info = {
  384. .name = TYPE_MPS2_MACHINE,
  385. .parent = TYPE_MACHINE,
  386. .abstract = true,
  387. .instance_size = sizeof(MPS2MachineState),
  388. .class_size = sizeof(MPS2MachineClass),
  389. .class_init = mps2_class_init,
  390. };
  391. static const TypeInfo mps2_an385_info = {
  392. .name = TYPE_MPS2_AN385_MACHINE,
  393. .parent = TYPE_MPS2_MACHINE,
  394. .class_init = mps2_an385_class_init,
  395. };
  396. static const TypeInfo mps2_an511_info = {
  397. .name = TYPE_MPS2_AN511_MACHINE,
  398. .parent = TYPE_MPS2_MACHINE,
  399. .class_init = mps2_an511_class_init,
  400. };
  401. static void mps2_machine_init(void)
  402. {
  403. type_register_static(&mps2_info);
  404. type_register_static(&mps2_an385_info);
  405. type_register_static(&mps2_an511_info);
  406. }
  407. type_init(mps2_machine_init);