mps3r.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /*
  2. * Arm MPS3 board emulation for Cortex-R-based FPGA images.
  3. * (For M-profile images see mps2.c and mps2tz.c.)
  4. *
  5. * Copyright (c) 2017 Linaro Limited
  6. * Written by Peter Maydell
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 or
  10. * (at your option) any later version.
  11. */
  12. /*
  13. * The MPS3 is an FPGA based dev board. This file handles FPGA images
  14. * which use the Cortex-R CPUs. We model these separately from the
  15. * M-profile images, because on M-profile the FPGA image is based on
  16. * a "Subsystem for Embedded" which is similar to an SoC, whereas
  17. * the R-profile FPGA images don't have that abstraction layer.
  18. *
  19. * We model the following FPGA images here:
  20. * "mps3-an536" -- dual Cortex-R52 as documented in Arm Application Note AN536
  21. *
  22. * Application Note AN536:
  23. * https://developer.arm.com/documentation/dai0536/latest/
  24. */
  25. #include "qemu/osdep.h"
  26. #include "qemu/units.h"
  27. #include "qapi/error.h"
  28. #include "qobject/qlist.h"
  29. #include "exec/address-spaces.h"
  30. #include "cpu.h"
  31. #include "system/system.h"
  32. #include "hw/boards.h"
  33. #include "hw/or-irq.h"
  34. #include "hw/qdev-clock.h"
  35. #include "hw/qdev-properties.h"
  36. #include "hw/arm/boot.h"
  37. #include "hw/arm/bsa.h"
  38. #include "hw/char/cmsdk-apb-uart.h"
  39. #include "hw/i2c/arm_sbcon_i2c.h"
  40. #include "hw/intc/arm_gicv3.h"
  41. #include "hw/misc/mps2-scc.h"
  42. #include "hw/misc/mps2-fpgaio.h"
  43. #include "hw/misc/unimp.h"
  44. #include "hw/net/lan9118.h"
  45. #include "hw/rtc/pl031.h"
  46. #include "hw/ssi/pl022.h"
  47. #include "hw/timer/cmsdk-apb-dualtimer.h"
  48. #include "hw/watchdog/cmsdk-apb-watchdog.h"
  49. /* Define the layout of RAM and ROM in a board */
  50. typedef struct RAMInfo {
  51. const char *name;
  52. hwaddr base;
  53. hwaddr size;
  54. int mrindex; /* index into rams[]; -1 for the system RAM block */
  55. int flags;
  56. } RAMInfo;
  57. /*
  58. * The MPS3 DDR is 3GiB, but on a 32-bit host QEMU doesn't permit
  59. * emulation of that much guest RAM, so artificially make it smaller.
  60. */
  61. #if HOST_LONG_BITS == 32
  62. #define MPS3_DDR_SIZE (1 * GiB)
  63. #else
  64. #define MPS3_DDR_SIZE (3 * GiB)
  65. #endif
  66. /*
  67. * Flag values:
  68. * IS_MAIN: this is the main machine RAM
  69. * IS_ROM: this area is read-only
  70. */
  71. #define IS_MAIN 1
  72. #define IS_ROM 2
  73. #define MPS3R_RAM_MAX 9
  74. #define MPS3R_CPU_MAX 2
  75. #define MPS3R_UART_MAX 4 /* shared UART count */
  76. #define PERIPHBASE 0xf0000000
  77. #define NUM_SPIS 96
  78. typedef enum MPS3RFPGAType {
  79. FPGA_AN536,
  80. } MPS3RFPGAType;
  81. struct MPS3RMachineClass {
  82. MachineClass parent;
  83. MPS3RFPGAType fpga_type;
  84. const RAMInfo *raminfo;
  85. hwaddr loader_start;
  86. };
  87. struct MPS3RMachineState {
  88. MachineState parent;
  89. struct arm_boot_info bootinfo;
  90. MemoryRegion ram[MPS3R_RAM_MAX];
  91. Object *cpu[MPS3R_CPU_MAX];
  92. MemoryRegion cpu_sysmem[MPS3R_CPU_MAX];
  93. MemoryRegion sysmem_alias[MPS3R_CPU_MAX];
  94. MemoryRegion cpu_ram[MPS3R_CPU_MAX];
  95. GICv3State gic;
  96. /* per-CPU UARTs followed by the shared UARTs */
  97. CMSDKAPBUART uart[MPS3R_CPU_MAX + MPS3R_UART_MAX];
  98. OrIRQState cpu_uart_oflow[MPS3R_CPU_MAX];
  99. OrIRQState uart_oflow;
  100. CMSDKAPBWatchdog watchdog;
  101. CMSDKAPBDualTimer dualtimer;
  102. ArmSbconI2CState i2c[5];
  103. PL022State spi[3];
  104. MPS2SCC scc;
  105. MPS2FPGAIO fpgaio;
  106. UnimplementedDeviceState i2s_audio;
  107. PL031State rtc;
  108. Clock *clk;
  109. };
  110. #define TYPE_MPS3R_MACHINE "mps3r"
  111. #define TYPE_MPS3R_AN536_MACHINE MACHINE_TYPE_NAME("mps3-an536")
  112. OBJECT_DECLARE_TYPE(MPS3RMachineState, MPS3RMachineClass, MPS3R_MACHINE)
  113. /*
  114. * Main clock frequency CLK in Hz (50MHz). In the image there are also
  115. * ACLK, MCLK, GPUCLK and PERIPHCLK at the same frequency; for our
  116. * model we just roll them all into one.
  117. */
  118. #define CLK_FRQ 50000000
  119. static const RAMInfo an536_raminfo[] = {
  120. {
  121. .name = "ATCM",
  122. .base = 0x00000000,
  123. .size = 0x00008000,
  124. .mrindex = 0,
  125. }, {
  126. /* We model the QSPI flash as simple ROM for now */
  127. .name = "QSPI",
  128. .base = 0x08000000,
  129. .size = 0x00800000,
  130. .flags = IS_ROM,
  131. .mrindex = 1,
  132. }, {
  133. .name = "BRAM",
  134. .base = 0x10000000,
  135. .size = 0x00080000,
  136. .mrindex = 2,
  137. }, {
  138. .name = "DDR",
  139. .base = 0x20000000,
  140. .size = MPS3_DDR_SIZE,
  141. .mrindex = -1,
  142. }, {
  143. .name = "ATCM0",
  144. .base = 0xee000000,
  145. .size = 0x00008000,
  146. .mrindex = 3,
  147. }, {
  148. .name = "BTCM0",
  149. .base = 0xee100000,
  150. .size = 0x00008000,
  151. .mrindex = 4,
  152. }, {
  153. .name = "CTCM0",
  154. .base = 0xee200000,
  155. .size = 0x00008000,
  156. .mrindex = 5,
  157. }, {
  158. .name = "ATCM1",
  159. .base = 0xee400000,
  160. .size = 0x00008000,
  161. .mrindex = 6,
  162. }, {
  163. .name = "BTCM1",
  164. .base = 0xee500000,
  165. .size = 0x00008000,
  166. .mrindex = 7,
  167. }, {
  168. .name = "CTCM1",
  169. .base = 0xee600000,
  170. .size = 0x00008000,
  171. .mrindex = 8,
  172. }, {
  173. .name = NULL,
  174. }
  175. };
  176. static const int an536_oscclk[] = {
  177. 24000000, /* 24MHz reference for RTC and timers */
  178. 50000000, /* 50MHz ACLK */
  179. 50000000, /* 50MHz MCLK */
  180. 50000000, /* 50MHz GPUCLK */
  181. 24576000, /* 24.576MHz AUDCLK */
  182. 23750000, /* 23.75MHz HDLCDCLK */
  183. 100000000, /* 100MHz DDR4_REF_CLK */
  184. };
  185. static MemoryRegion *mr_for_raminfo(MPS3RMachineState *mms,
  186. const RAMInfo *raminfo)
  187. {
  188. /* Return an initialized MemoryRegion for the RAMInfo. */
  189. MemoryRegion *ram;
  190. if (raminfo->mrindex < 0) {
  191. /* Means this RAMInfo is for QEMU's "system memory" */
  192. MachineState *machine = MACHINE(mms);
  193. assert(!(raminfo->flags & IS_ROM));
  194. return machine->ram;
  195. }
  196. assert(raminfo->mrindex < MPS3R_RAM_MAX);
  197. ram = &mms->ram[raminfo->mrindex];
  198. memory_region_init_ram(ram, NULL, raminfo->name,
  199. raminfo->size, &error_fatal);
  200. if (raminfo->flags & IS_ROM) {
  201. memory_region_set_readonly(ram, true);
  202. }
  203. return ram;
  204. }
  205. /*
  206. * There is no defined secondary boot protocol for Linux for the AN536,
  207. * because real hardware has a restriction that atomic operations between
  208. * the two CPUs do not function correctly, and so true SMP is not
  209. * possible. Therefore for cases where the user is directly booting
  210. * a kernel, we treat the system as essentially uniprocessor, and
  211. * put the secondary CPU into power-off state (as if the user on the
  212. * real hardware had configured the secondary to be halted via the
  213. * SCC config registers).
  214. *
  215. * Note that the default secondary boot code would not work here anyway
  216. * as it assumes a GICv2, and we have a GICv3.
  217. */
  218. static void mps3r_write_secondary_boot(ARMCPU *cpu,
  219. const struct arm_boot_info *info)
  220. {
  221. /*
  222. * Power the secondary CPU off. This means we don't need to write any
  223. * boot code into guest memory. Note that the 'cpu' argument to this
  224. * function is the primary CPU we passed to arm_load_kernel(), not
  225. * the secondary. Loop around all the other CPUs, as the boot.c
  226. * code does for the "disable secondaries if PSCI is enabled" case.
  227. */
  228. for (CPUState *cs = first_cpu; cs; cs = CPU_NEXT(cs)) {
  229. if (cs != first_cpu) {
  230. object_property_set_bool(OBJECT(cs), "start-powered-off", true,
  231. &error_abort);
  232. }
  233. }
  234. }
  235. static void mps3r_secondary_cpu_reset(ARMCPU *cpu,
  236. const struct arm_boot_info *info)
  237. {
  238. /* We don't need to do anything here because the CPU will be off */
  239. }
  240. static void create_gic(MPS3RMachineState *mms, MemoryRegion *sysmem)
  241. {
  242. MachineState *machine = MACHINE(mms);
  243. DeviceState *gicdev;
  244. QList *redist_region_count;
  245. object_initialize_child(OBJECT(mms), "gic", &mms->gic, TYPE_ARM_GICV3);
  246. gicdev = DEVICE(&mms->gic);
  247. qdev_prop_set_uint32(gicdev, "num-cpu", machine->smp.cpus);
  248. qdev_prop_set_uint32(gicdev, "num-irq", NUM_SPIS + GIC_INTERNAL);
  249. redist_region_count = qlist_new();
  250. qlist_append_int(redist_region_count, machine->smp.cpus);
  251. qdev_prop_set_array(gicdev, "redist-region-count", redist_region_count);
  252. object_property_set_link(OBJECT(&mms->gic), "sysmem",
  253. OBJECT(sysmem), &error_fatal);
  254. sysbus_realize(SYS_BUS_DEVICE(&mms->gic), &error_fatal);
  255. sysbus_mmio_map(SYS_BUS_DEVICE(&mms->gic), 0, PERIPHBASE);
  256. sysbus_mmio_map(SYS_BUS_DEVICE(&mms->gic), 1, PERIPHBASE + 0x100000);
  257. /*
  258. * Wire the outputs from each CPU's generic timer and the GICv3
  259. * maintenance interrupt signal to the appropriate GIC PPI inputs,
  260. * and the GIC's IRQ/FIQ/VIRQ/VFIQ interrupt outputs to the CPU's inputs.
  261. */
  262. for (int i = 0; i < machine->smp.cpus; i++) {
  263. DeviceState *cpudev = DEVICE(mms->cpu[i]);
  264. SysBusDevice *gicsbd = SYS_BUS_DEVICE(&mms->gic);
  265. int intidbase = NUM_SPIS + i * GIC_INTERNAL;
  266. int irq;
  267. /*
  268. * Mapping from the output timer irq lines from the CPU to the
  269. * GIC PPI inputs used for this board. This isn't a BSA board,
  270. * but it uses the standard convention for the PPI numbers.
  271. */
  272. const int timer_irq[] = {
  273. [GTIMER_PHYS] = ARCH_TIMER_NS_EL1_IRQ,
  274. [GTIMER_VIRT] = ARCH_TIMER_VIRT_IRQ,
  275. [GTIMER_HYP] = ARCH_TIMER_NS_EL2_IRQ,
  276. };
  277. for (irq = 0; irq < ARRAY_SIZE(timer_irq); irq++) {
  278. qdev_connect_gpio_out(cpudev, irq,
  279. qdev_get_gpio_in(gicdev,
  280. intidbase + timer_irq[irq]));
  281. }
  282. qdev_connect_gpio_out_named(cpudev, "gicv3-maintenance-interrupt", 0,
  283. qdev_get_gpio_in(gicdev,
  284. intidbase + ARCH_GIC_MAINT_IRQ));
  285. qdev_connect_gpio_out_named(cpudev, "pmu-interrupt", 0,
  286. qdev_get_gpio_in(gicdev,
  287. intidbase + VIRTUAL_PMU_IRQ));
  288. sysbus_connect_irq(gicsbd, i,
  289. qdev_get_gpio_in(cpudev, ARM_CPU_IRQ));
  290. sysbus_connect_irq(gicsbd, i + machine->smp.cpus,
  291. qdev_get_gpio_in(cpudev, ARM_CPU_FIQ));
  292. sysbus_connect_irq(gicsbd, i + 2 * machine->smp.cpus,
  293. qdev_get_gpio_in(cpudev, ARM_CPU_VIRQ));
  294. sysbus_connect_irq(gicsbd, i + 3 * machine->smp.cpus,
  295. qdev_get_gpio_in(cpudev, ARM_CPU_VFIQ));
  296. }
  297. }
  298. /*
  299. * Create UART uartno, and map it into the MemoryRegion mem at address baseaddr.
  300. * The qemu_irq arguments are where we connect the various IRQs from the UART.
  301. */
  302. static void create_uart(MPS3RMachineState *mms, int uartno, MemoryRegion *mem,
  303. hwaddr baseaddr, qemu_irq txirq, qemu_irq rxirq,
  304. qemu_irq txoverirq, qemu_irq rxoverirq,
  305. qemu_irq combirq)
  306. {
  307. g_autofree char *s = g_strdup_printf("uart%d", uartno);
  308. SysBusDevice *sbd;
  309. assert(uartno < ARRAY_SIZE(mms->uart));
  310. object_initialize_child(OBJECT(mms), s, &mms->uart[uartno],
  311. TYPE_CMSDK_APB_UART);
  312. qdev_prop_set_uint32(DEVICE(&mms->uart[uartno]), "pclk-frq", CLK_FRQ);
  313. qdev_prop_set_chr(DEVICE(&mms->uart[uartno]), "chardev", serial_hd(uartno));
  314. sbd = SYS_BUS_DEVICE(&mms->uart[uartno]);
  315. sysbus_realize(sbd, &error_fatal);
  316. memory_region_add_subregion(mem, baseaddr,
  317. sysbus_mmio_get_region(sbd, 0));
  318. sysbus_connect_irq(sbd, 0, txirq);
  319. sysbus_connect_irq(sbd, 1, rxirq);
  320. sysbus_connect_irq(sbd, 2, txoverirq);
  321. sysbus_connect_irq(sbd, 3, rxoverirq);
  322. sysbus_connect_irq(sbd, 4, combirq);
  323. }
  324. static void mps3r_common_init(MachineState *machine)
  325. {
  326. MPS3RMachineState *mms = MPS3R_MACHINE(machine);
  327. MPS3RMachineClass *mmc = MPS3R_MACHINE_GET_CLASS(mms);
  328. MemoryRegion *sysmem = get_system_memory();
  329. DeviceState *gicdev;
  330. QList *oscclk;
  331. mms->clk = clock_new(OBJECT(machine), "CLK");
  332. clock_set_hz(mms->clk, CLK_FRQ);
  333. for (const RAMInfo *ri = mmc->raminfo; ri->name; ri++) {
  334. MemoryRegion *mr = mr_for_raminfo(mms, ri);
  335. memory_region_add_subregion(sysmem, ri->base, mr);
  336. }
  337. assert(machine->smp.cpus <= MPS3R_CPU_MAX);
  338. for (int i = 0; i < machine->smp.cpus; i++) {
  339. g_autofree char *sysmem_name = g_strdup_printf("cpu-%d-memory", i);
  340. g_autofree char *ramname = g_strdup_printf("cpu-%d-memory", i);
  341. g_autofree char *alias_name = g_strdup_printf("sysmem-alias-%d", i);
  342. /*
  343. * Each CPU has some private RAM/peripherals, so create the container
  344. * which will house those, with the whole-machine system memory being
  345. * used where there's no CPU-specific device. Note that we need the
  346. * sysmem_alias aliases because we can't put one MR (the original
  347. * 'sysmem') into more than one other MR.
  348. */
  349. memory_region_init(&mms->cpu_sysmem[i], OBJECT(machine),
  350. sysmem_name, UINT64_MAX);
  351. memory_region_init_alias(&mms->sysmem_alias[i], OBJECT(machine),
  352. alias_name, sysmem, 0, UINT64_MAX);
  353. memory_region_add_subregion_overlap(&mms->cpu_sysmem[i], 0,
  354. &mms->sysmem_alias[i], -1);
  355. mms->cpu[i] = object_new(machine->cpu_type);
  356. object_property_set_link(mms->cpu[i], "memory",
  357. OBJECT(&mms->cpu_sysmem[i]), &error_abort);
  358. object_property_set_int(mms->cpu[i], "reset-cbar",
  359. PERIPHBASE, &error_abort);
  360. qdev_realize(DEVICE(mms->cpu[i]), NULL, &error_fatal);
  361. object_unref(mms->cpu[i]);
  362. /* Per-CPU RAM */
  363. memory_region_init_ram(&mms->cpu_ram[i], NULL, ramname,
  364. 0x1000, &error_fatal);
  365. memory_region_add_subregion(&mms->cpu_sysmem[i], 0xe7c01000,
  366. &mms->cpu_ram[i]);
  367. }
  368. create_gic(mms, sysmem);
  369. gicdev = DEVICE(&mms->gic);
  370. /*
  371. * UARTs 0 and 1 are per-CPU; their interrupts are wired to
  372. * the relevant CPU's PPI 0..3, aka INTID 16..19
  373. */
  374. for (int i = 0; i < machine->smp.cpus; i++) {
  375. int intidbase = NUM_SPIS + i * GIC_INTERNAL;
  376. g_autofree char *s = g_strdup_printf("cpu-uart-oflow-orgate%d", i);
  377. DeviceState *orgate;
  378. /* The two overflow IRQs from the UART are ORed together into PPI 3 */
  379. object_initialize_child(OBJECT(mms), s, &mms->cpu_uart_oflow[i],
  380. TYPE_OR_IRQ);
  381. orgate = DEVICE(&mms->cpu_uart_oflow[i]);
  382. qdev_prop_set_uint32(orgate, "num-lines", 2);
  383. qdev_realize(orgate, NULL, &error_fatal);
  384. qdev_connect_gpio_out(orgate, 0,
  385. qdev_get_gpio_in(gicdev, intidbase + 19));
  386. create_uart(mms, i, &mms->cpu_sysmem[i], 0xe7c00000,
  387. qdev_get_gpio_in(gicdev, intidbase + 17), /* tx */
  388. qdev_get_gpio_in(gicdev, intidbase + 16), /* rx */
  389. qdev_get_gpio_in(orgate, 0), /* txover */
  390. qdev_get_gpio_in(orgate, 1), /* rxover */
  391. qdev_get_gpio_in(gicdev, intidbase + 18) /* combined */);
  392. }
  393. /*
  394. * UARTs 2 to 5 are whole-system; all overflow IRQs are ORed
  395. * together into IRQ 17
  396. */
  397. object_initialize_child(OBJECT(mms), "uart-oflow-orgate",
  398. &mms->uart_oflow, TYPE_OR_IRQ);
  399. qdev_prop_set_uint32(DEVICE(&mms->uart_oflow), "num-lines",
  400. MPS3R_UART_MAX * 2);
  401. qdev_realize(DEVICE(&mms->uart_oflow), NULL, &error_fatal);
  402. qdev_connect_gpio_out(DEVICE(&mms->uart_oflow), 0,
  403. qdev_get_gpio_in(gicdev, 17));
  404. for (int i = 0; i < MPS3R_UART_MAX; i++) {
  405. hwaddr baseaddr = 0xe0205000 + i * 0x1000;
  406. int rxirq = 5 + i * 2, txirq = 6 + i * 2, combirq = 13 + i;
  407. create_uart(mms, i + MPS3R_CPU_MAX, sysmem, baseaddr,
  408. qdev_get_gpio_in(gicdev, txirq),
  409. qdev_get_gpio_in(gicdev, rxirq),
  410. qdev_get_gpio_in(DEVICE(&mms->uart_oflow), i * 2),
  411. qdev_get_gpio_in(DEVICE(&mms->uart_oflow), i * 2 + 1),
  412. qdev_get_gpio_in(gicdev, combirq));
  413. }
  414. for (int i = 0; i < 4; i++) {
  415. /* CMSDK GPIO controllers */
  416. g_autofree char *s = g_strdup_printf("gpio%d", i);
  417. create_unimplemented_device(s, 0xe0000000 + i * 0x1000, 0x1000);
  418. }
  419. object_initialize_child(OBJECT(mms), "watchdog", &mms->watchdog,
  420. TYPE_CMSDK_APB_WATCHDOG);
  421. qdev_connect_clock_in(DEVICE(&mms->watchdog), "WDOGCLK", mms->clk);
  422. sysbus_realize(SYS_BUS_DEVICE(&mms->watchdog), &error_fatal);
  423. sysbus_connect_irq(SYS_BUS_DEVICE(&mms->watchdog), 0,
  424. qdev_get_gpio_in(gicdev, 0));
  425. sysbus_mmio_map(SYS_BUS_DEVICE(&mms->watchdog), 0, 0xe0100000);
  426. object_initialize_child(OBJECT(mms), "dualtimer", &mms->dualtimer,
  427. TYPE_CMSDK_APB_DUALTIMER);
  428. qdev_connect_clock_in(DEVICE(&mms->dualtimer), "TIMCLK", mms->clk);
  429. sysbus_realize(SYS_BUS_DEVICE(&mms->dualtimer), &error_fatal);
  430. sysbus_connect_irq(SYS_BUS_DEVICE(&mms->dualtimer), 0,
  431. qdev_get_gpio_in(gicdev, 3));
  432. sysbus_connect_irq(SYS_BUS_DEVICE(&mms->dualtimer), 1,
  433. qdev_get_gpio_in(gicdev, 1));
  434. sysbus_connect_irq(SYS_BUS_DEVICE(&mms->dualtimer), 2,
  435. qdev_get_gpio_in(gicdev, 2));
  436. sysbus_mmio_map(SYS_BUS_DEVICE(&mms->dualtimer), 0, 0xe0101000);
  437. for (int i = 0; i < ARRAY_SIZE(mms->i2c); i++) {
  438. static const hwaddr i2cbase[] = {0xe0102000, /* Touch */
  439. 0xe0103000, /* Audio */
  440. 0xe0107000, /* Shield0 */
  441. 0xe0108000, /* Shield1 */
  442. 0xe0109000}; /* DDR4 EEPROM */
  443. g_autofree char *s = g_strdup_printf("i2c%d", i);
  444. object_initialize_child(OBJECT(mms), s, &mms->i2c[i],
  445. TYPE_ARM_SBCON_I2C);
  446. sysbus_realize(SYS_BUS_DEVICE(&mms->i2c[i]), &error_fatal);
  447. sysbus_mmio_map(SYS_BUS_DEVICE(&mms->i2c[i]), 0, i2cbase[i]);
  448. if (i != 2 && i != 3) {
  449. /*
  450. * internal-only bus: mark it full to avoid user-created
  451. * i2c devices being plugged into it.
  452. */
  453. qbus_mark_full(qdev_get_child_bus(DEVICE(&mms->i2c[i]), "i2c"));
  454. }
  455. }
  456. for (int i = 0; i < ARRAY_SIZE(mms->spi); i++) {
  457. g_autofree char *s = g_strdup_printf("spi%d", i);
  458. hwaddr baseaddr = 0xe0104000 + i * 0x1000;
  459. object_initialize_child(OBJECT(mms), s, &mms->spi[i], TYPE_PL022);
  460. sysbus_realize(SYS_BUS_DEVICE(&mms->spi[i]), &error_fatal);
  461. sysbus_mmio_map(SYS_BUS_DEVICE(&mms->spi[i]), 0, baseaddr);
  462. sysbus_connect_irq(SYS_BUS_DEVICE(&mms->spi[i]), 0,
  463. qdev_get_gpio_in(gicdev, 22 + i));
  464. }
  465. object_initialize_child(OBJECT(mms), "scc", &mms->scc, TYPE_MPS2_SCC);
  466. qdev_prop_set_uint32(DEVICE(&mms->scc), "scc-cfg0", 0);
  467. qdev_prop_set_uint32(DEVICE(&mms->scc), "scc-cfg4", 0x2);
  468. qdev_prop_set_uint32(DEVICE(&mms->scc), "scc-aid", 0x00200008);
  469. qdev_prop_set_uint32(DEVICE(&mms->scc), "scc-id", 0x41055360);
  470. oscclk = qlist_new();
  471. for (int i = 0; i < ARRAY_SIZE(an536_oscclk); i++) {
  472. qlist_append_int(oscclk, an536_oscclk[i]);
  473. }
  474. qdev_prop_set_array(DEVICE(&mms->scc), "oscclk", oscclk);
  475. sysbus_realize(SYS_BUS_DEVICE(&mms->scc), &error_fatal);
  476. sysbus_mmio_map(SYS_BUS_DEVICE(&mms->scc), 0, 0xe0200000);
  477. create_unimplemented_device("i2s-audio", 0xe0201000, 0x1000);
  478. object_initialize_child(OBJECT(mms), "fpgaio", &mms->fpgaio,
  479. TYPE_MPS2_FPGAIO);
  480. qdev_prop_set_uint32(DEVICE(&mms->fpgaio), "prescale-clk", an536_oscclk[1]);
  481. qdev_prop_set_uint32(DEVICE(&mms->fpgaio), "num-leds", 10);
  482. qdev_prop_set_bit(DEVICE(&mms->fpgaio), "has-switches", true);
  483. qdev_prop_set_bit(DEVICE(&mms->fpgaio), "has-dbgctrl", false);
  484. sysbus_realize(SYS_BUS_DEVICE(&mms->fpgaio), &error_fatal);
  485. sysbus_mmio_map(SYS_BUS_DEVICE(&mms->fpgaio), 0, 0xe0202000);
  486. create_unimplemented_device("clcd", 0xe0209000, 0x1000);
  487. object_initialize_child(OBJECT(mms), "rtc", &mms->rtc, TYPE_PL031);
  488. sysbus_realize(SYS_BUS_DEVICE(&mms->rtc), &error_fatal);
  489. sysbus_mmio_map(SYS_BUS_DEVICE(&mms->rtc), 0, 0xe020a000);
  490. sysbus_connect_irq(SYS_BUS_DEVICE(&mms->rtc), 0,
  491. qdev_get_gpio_in(gicdev, 4));
  492. /*
  493. * In hardware this is a LAN9220; the LAN9118 is software compatible
  494. * except that it doesn't support the checksum-offload feature.
  495. */
  496. lan9118_init(0xe0300000,
  497. qdev_get_gpio_in(gicdev, 18));
  498. create_unimplemented_device("usb", 0xe0301000, 0x1000);
  499. create_unimplemented_device("qspi-write-config", 0xe0600000, 0x1000);
  500. mms->bootinfo.ram_size = machine->ram_size;
  501. mms->bootinfo.board_id = -1;
  502. mms->bootinfo.loader_start = mmc->loader_start;
  503. mms->bootinfo.write_secondary_boot = mps3r_write_secondary_boot;
  504. mms->bootinfo.secondary_cpu_reset_hook = mps3r_secondary_cpu_reset;
  505. arm_load_kernel(ARM_CPU(mms->cpu[0]), machine, &mms->bootinfo);
  506. }
  507. static void mps3r_set_default_ram_info(MPS3RMachineClass *mmc)
  508. {
  509. /*
  510. * Set mc->default_ram_size and default_ram_id from the
  511. * information in mmc->raminfo.
  512. */
  513. MachineClass *mc = MACHINE_CLASS(mmc);
  514. const RAMInfo *p;
  515. for (p = mmc->raminfo; p->name; p++) {
  516. if (p->mrindex < 0) {
  517. /* Found the entry for "system memory" */
  518. mc->default_ram_size = p->size;
  519. mc->default_ram_id = p->name;
  520. mmc->loader_start = p->base;
  521. return;
  522. }
  523. }
  524. g_assert_not_reached();
  525. }
  526. static void mps3r_class_init(ObjectClass *oc, void *data)
  527. {
  528. MachineClass *mc = MACHINE_CLASS(oc);
  529. mc->init = mps3r_common_init;
  530. }
  531. static void mps3r_an536_class_init(ObjectClass *oc, void *data)
  532. {
  533. MachineClass *mc = MACHINE_CLASS(oc);
  534. MPS3RMachineClass *mmc = MPS3R_MACHINE_CLASS(oc);
  535. static const char * const valid_cpu_types[] = {
  536. ARM_CPU_TYPE_NAME("cortex-r52"),
  537. NULL
  538. };
  539. mc->desc = "ARM MPS3 with AN536 FPGA image for Cortex-R52";
  540. /*
  541. * In the real FPGA image there are always two cores, but the standard
  542. * initial setting for the SCC SYSCON 0x000 register is 0x21, meaning
  543. * that the second core is held in reset and halted. Many images built for
  544. * the board do not expect the second core to run at startup (especially
  545. * since on the real FPGA image it is not possible to use LDREX/STREX
  546. * in RAM between the two cores, so a true SMP setup isn't supported).
  547. *
  548. * As QEMU's equivalent of this, we support both -smp 1 and -smp 2,
  549. * with the default being -smp 1. This seems a more intuitive UI for
  550. * QEMU users than, for instance, having a machine property to allow
  551. * the user to set the initial value of the SYSCON 0x000 register.
  552. */
  553. mc->default_cpus = 1;
  554. mc->min_cpus = 1;
  555. mc->max_cpus = 2;
  556. mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-r52");
  557. mc->valid_cpu_types = valid_cpu_types;
  558. mmc->raminfo = an536_raminfo;
  559. mps3r_set_default_ram_info(mmc);
  560. }
  561. static const TypeInfo mps3r_machine_types[] = {
  562. {
  563. .name = TYPE_MPS3R_MACHINE,
  564. .parent = TYPE_MACHINE,
  565. .abstract = true,
  566. .instance_size = sizeof(MPS3RMachineState),
  567. .class_size = sizeof(MPS3RMachineClass),
  568. .class_init = mps3r_class_init,
  569. }, {
  570. .name = TYPE_MPS3R_AN536_MACHINE,
  571. .parent = TYPE_MPS3R_MACHINE,
  572. .class_init = mps3r_an536_class_init,
  573. },
  574. };
  575. DEFINE_TYPES(mps3r_machine_types);